<?php $__env->startSection('content'); ?>
<div class="row g-4">
    <div class="col-lg-4">
        <div class="card">
            <div class="card-body">
                <h1 class="h4 mb-3">Record Agent Balance</h1>
                <form method="POST" action="<?php echo e(route('agent-balances.store')); ?>">
                    <?php echo csrf_field(); ?>
                    
                    <div class="mb-3">
                        <label class="form-label" for="agent_id">Agent</label>
                        <select class="form-select" id="agent_id" name="agent_id" required>
                            <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="mb-3">
                        <label class="form-label" for="account_id">Target Account (To receive currency)</label>
                        <select class="form-select" id="account_id" name="account_id" required>
                            <option value="">Select 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('account_id') == $account->id): echo 'selected'; endif; ?>>
                                    <?php echo e($account->name); ?> (<?php echo e($account->currency->code); ?> - Balance: <?php echo e(number_format((float) $account->balance, 4)); ?>)
                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>

                    <div class="mb-3">
                        <label class="form-label" for="quantity">Total Quantity / Currency Amount</label>
                        <input type="number" step="0.0001" min="0.0001" class="form-control" id="quantity" name="quantity" value="<?php echo e(old('quantity')); ?>" placeholder="0.0000" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label" for="rate">Buying Rate</label>
                        <input type="number" step="0.0001" min="0.0001" class="form-control" id="rate" name="rate" value="<?php echo e(old('rate')); ?>" placeholder="0.0000" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label" for="date">Transaction Date</label>
                        <input type="date" class="form-control" id="date" name="date" value="<?php echo e(old('date', now()->format('Y-m-d'))); ?>" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label" for="description">Description / Reason</label>
                        <textarea class="form-control" id="description" name="description" rows="3" placeholder="e.g. Balance from manual transactions before system usage"><?php echo e(old('description')); ?></textarea>
                    </div>

                    <button class="btn btn-primary w-100" type="submit">Save Agent Balance</button>
                </form>
            </div>
        </div>
    </div>

    <div class="col-lg-8">
        <div class="card">
            <div class="card-body">
                <h2 class="h4 mb-3">Agent Balance Records</h2>
                
                <form class="d-flex flex-wrap flex-md-nowrap gap-2 mb-4 align-items-center" method="GET" action="<?php echo e(route('agent-balances.index')); ?>">
                    <div class="flex-grow-1">
                        <input type="text" class="form-control" name="search" value="<?php echo e($filters['search'] ?? ''); ?>" placeholder="Search description, agent...">
                    </div>
                    <div class="flex-grow-1">
                        <select class="form-select" name="agent_id">
                            <option value="">All Agents</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(($filters['agent_id'] ?? '') == $agent->id): echo 'selected'; endif; ?>>
                                    <?php echo e($agent->name); ?>

                                </option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                    </div>
                    <div>
                        <input type="date" class="form-control" name="date_from" value="<?php echo e($filters['date_from'] ?? ''); ?>" title="From Date">
                    </div>
                    <div>
                        <input type="date" class="form-control" name="date_to" value="<?php echo e($filters['date_to'] ?? ''); ?>" title="To Date">
                    </div>
                    <div class="d-flex gap-1">
                        <button type="submit" class="btn btn-primary px-3">Filter</button>
                        <a href="<?php echo e(route('agent-balances.index')); ?>" class="btn btn-outline-secondary px-3">Reset</a>
                    </div>
                </form>
                <div class="table-responsive">
                    <table class="table table-striped align-middle">
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>Agent</th>
                                <th>Account</th>
                                <th>Currency</th>
                                <th class="text-end">Quantity</th>
                                <th class="text-end">Buying Rate</th>
                                <th class="text-end">Total LKR</th>
                                <th>Status</th>
                                <th class="text-end">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $__empty_1 = true; $__currentLoopData = $balances; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $balance): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                <tr>
                                    <td><?php echo e($balance->date->format('Y-m-d')); ?></td>
                                    <td class="fw-semibold"><?php echo e($balance->agent->name); ?></td>
                                    <td><?php echo e($balance->account->name); ?></td>
                                    <td><?php echo e($balance->account->currency->code); ?></td>
                                    <td class="text-end fw-semibold"><?php echo e(number_format((float) $balance->quantity, 4)); ?></td>
                                    <td class="text-end"><?php echo e(number_format((float) $balance->rate, 4)); ?></td>
                                    <td class="text-end fw-semibold"><?php echo e(number_format((float) $balance->quantity * (float) $balance->rate, 4)); ?></td>
                                    <td>
                                        <?php if($balance->is_received): ?>
                                            <span style="padding: 2px 8px; border-radius: 4px; font-size: 12px; font-weight: 600; background-color: #d1fae5; color: #065f46;">
                                                Received
                                            </span>
                                        <?php else: ?>
                                            <span style="padding: 2px 8px; border-radius: 4px; font-size: 12px; font-weight: 600; background-color: #fee2e2; color: #991b1b;">
                                                Pending
                                            </span>
                                        <?php endif; ?>
                                    </td>
                                    <td class="text-end">
                                        <div class="btn-group btn-group-sm d-flex gap-1 justify-content-end">
                                            <?php if(!$balance->is_received): ?>
                                                <form action="<?php echo e(route('agent-balances.received', $balance)); ?>" method="POST" onsubmit="return confirm('Confirm receipt of this agent balance? This will credit the target account.')" style="display:inline-block;">
                                                    <?php echo csrf_field(); ?>
                                                    <button type="submit" class="btn btn-sm btn-outline-success" style="padding: 2px 6px; font-size: 12px;">Received</button>
                                                </form>
                                            <?php endif; ?>
                                            
                                            <a href="<?php echo e(route('agent-balances.edit', $balance)); ?>" class="btn btn-sm btn-outline-primary" style="padding: 2px 6px; font-size: 12px;">Edit</a>
                                            
                                            <form action="<?php echo e(route('agent-balances.destroy', $balance)); ?>" method="POST" onsubmit="return confirm('Are you sure you want to delete this agent balance record?<?php echo e($balance->is_received ? " This will reverse the account balance adjustment." : ""); ?>')" style="display:inline-block;">
                                                <?php echo csrf_field(); ?>
                                                <?php echo method_field('DELETE'); ?>
                                                <button type="submit" class="btn btn-sm btn-outline-danger" style="padding: 2px 6px; font-size: 12px;">Delete</button>
                                            </form>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                <tr>
                                    <td class="text-muted text-center" colspan="9">No agent balances recorded yet.</td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

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