<?php $__env->startSection('content'); ?>
<div class="breadcrumbbar">
    <div class="row align-items-center">
        <div class="col-md-8 col-lg-8">
            <h4 class="page-title">Sell Currency</h4>
            <div class="breadcrumb-list">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="<?php echo e(route('dashboard')); ?>">Home</a></li>
                    <li class="breadcrumb-item"><a href="#">Transactions</a></li>
                    <li class="breadcrumb-item active" aria-current="page">Sell Currency</li>
                </ol>
            </div>
        </div>
    </div>          
</div>
<div class="row justify-content-center">
    <div class="col-lg-7">
        <div class="card m-b-30">
            <div class="card-header">
                <h5 class="card-title mb-0">Sell Currency Form</h5>
            </div>
            <div class="card-body">
                <form method="POST" action="<?php echo e(route('transactions.sell.store')); ?>">
                    <?php echo csrf_field(); ?>
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label class="form-label" for="agent_id">Agent (Optional)</label>
                            <select class="form-select" id="agent_id" name="agent_id">
                                <option value="">Select agent</option>
                                <?php $__currentLoopData = $agents; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $agent): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($agent->id); ?>" <?php if(old('agent_id') == $agent->id): echo 'selected'; endif; ?>>
                                        <?php echo e($agent->name); ?>

                                    </option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label" for="currency_id">Currency to Sell</label>
                            <select class="form-select" id="currency_id" name="currency_id" required>
                                <option value="">Select currency</option>
                                <?php $__currentLoopData = $currencies; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $currency): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($currency->id); ?>" <?php if(old('currency_id') == $currency->id): echo 'selected'; endif; ?>>
                                        <?php echo e($currency->code); ?> - <?php echo e($currency->name); ?>

                                        (Stock: <?php echo e(number_format($currency->stock, 4)); ?>)
                                    </option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label" for="source_account_id">Source Account (To deduct sold currency)</label>
                            <select class="form-select" id="source_account_id" name="source_account_id" required>
                                <option value="">Select source account</option>
                                <?php $__currentLoopData = $accounts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $account): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($account->id); ?>" <?php if(old('source_account_id') == $account->id): echo 'selected'; endif; ?>>
                                        <?php echo e($account->name); ?> - <?php echo e(number_format((float) $account->balance, 2)); ?> <?php echo e($account->currency->code); ?>

                                    </option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label" for="target_account_id">Target Account (To receive funds)</label>
                            <select class="form-select" id="target_account_id" name="target_account_id" required>
                                <option value="">Select target account</option>
                                <?php $__currentLoopData = $accounts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $account): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($account->id); ?>" <?php if(old('target_account_id') == $account->id): echo 'selected'; endif; ?>>
                                        <?php echo e($account->name); ?> - <?php echo e(number_format((float) $account->balance, 2)); ?> <?php echo e($account->currency->code); ?>

                                    </option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label" for="quantity">Quantity Sold</label>
                            <input class="form-control text-end atm-decimal" id="quantity" name="quantity" inputmode="numeric" placeholder="0.00" value="<?php echo e(old('quantity')); ?>" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label" for="payment_amount">Total Amount Received</label>
                            <input class="form-control text-end atm-decimal" id="payment_amount" name="payment_amount" inputmode="numeric" placeholder="0.00" value="<?php echo e(old('payment_amount')); ?>" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label" for="handling_fee">Handling Fee <small class="text-muted">(Charged in receiving account's currency)</small></label>
                            <input class="form-control text-end atm-decimal" id="handling_fee" name="handling_fee" inputmode="numeric" placeholder="0.00" value="<?php echo e(old('handling_fee', 0)); ?>" required>
                        </div>
                        <div class="col-md-12">
                            <div class="alert alert-info d-flex align-items-center justify-content-between mb-0" id="rate_calculator_container" style="display: none !important;">
                                <div class="d-flex align-items-center gap-2">
                                    <i class="feather icon-info"></i>
                                    <strong>Calculated Selling Rate:</strong>
                                </div>
                                <div class="h4 mb-0" id="live_calculated_rate">0.00</div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-check mt-2">
                                <input type="hidden" name="is_pending" value="0">
                                <input class="form-check-input" type="checkbox" id="is_pending" name="is_pending" value="1" <?php if(old('is_pending') == '1'): echo 'checked'; endif; ?>>
                                <label class="form-check-label" for="is_pending">
                                    <strong>Mark as Pending</strong> (Deduct source account immediately, receive target currency later)
                                </label>
                            </div>
                        </div>
                    </div>
                    <div class="d-flex justify-content-end mt-4">
                        <button class="btn btn-primary" type="submit">Save Sell Transaction</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script>
document.addEventListener('DOMContentLoaded', function() {
    const quantityInput = document.getElementById('quantity');
    const paymentAmountInput = document.getElementById('payment_amount');
    const rateContainer = document.getElementById('rate_calculator_container');
    const liveRateDisplay = document.getElementById('live_calculated_rate');

    function calculateRate() {
        const qtyStr = quantityInput.value.replace(/,/g, '');
        const amtStr = paymentAmountInput.value.replace(/,/g, '');
        const quantity = parseFloat(qtyStr);
        const amount = parseFloat(amtStr);

        if (!isNaN(quantity) && quantity > 0 && !isNaN(amount) && amount >= 0) {
            const rate = amount / quantity;
            liveRateDisplay.textContent = rate.toFixed(4);
            rateContainer.style.setProperty('display', 'flex', 'important');
        } else {
            rateContainer.style.setProperty('display', 'none', 'important');
        }
    }

    quantityInput.addEventListener('input', calculateRate);
    paymentAmountInput.addEventListener('input', calculateRate);
    quantityInput.addEventListener('keyup', calculateRate);
    paymentAmountInput.addEventListener('keyup', calculateRate);
    quantityInput.addEventListener('change', calculateRate);
    paymentAmountInput.addEventListener('change', calculateRate);
    
    // Initial calculation in case of old values
    calculateRate();
});
</script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH F:\christopher\currencychangerui\resources\views/transactions/sell.blade.php ENDPATH**/ ?>