<?php
/**
 * @var object $transfers
 * @var array $branches
 * @var array $products
 * @var string $search
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-exchange-alt me-2 text-gold"></i>Stock Transfers</h5>
</div>

<div class="row g-4">
    <div class="col-lg-5">
        <div class="glass-card p-0">
            <div class="card-header">
                <div class="card-header-left">
                    <i class="fas fa-plus-circle card-icon"></i>
                    <h6 class="card-title">New Transfer</h6>
                </div>
            </div>
            <div class="card-body">
                <form method="POST" action="<?= url('stock/transfers/store') ?>" id="transferForm">
                    <?= csrf_field() ?>

                    <div class="row g-3 mb-3">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">From Branch <span class="text-danger">*</span></label>
                                <select name="from_branch_id" class="form-select bg-transparent <?= errorClass('from_branch_id') ?>" required>
                                    <option value="">Select Source</option>
                                    <?php foreach ($branches as $b): ?>
                                        <option value="<?= $b->id ?>"><?= e($b->name) ?></option>
                                    <?php endforeach; ?>
                                </select>
                                <?= errorMsg('from_branch_id') ?>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label class="form-label">To Branch <span class="text-danger">*</span></label>
                                <select name="to_branch_id" class="form-select bg-transparent <?= errorClass('to_branch_id') ?>" required>
                                    <option value="">Select Destination</option>
                                    <?php foreach ($branches as $b): ?>
                                        <option value="<?= $b->id ?>"><?= e($b->name) ?></option>
                                    <?php endforeach; ?>
                                </select>
                                <?= errorMsg('to_branch_id') ?>
                            </div>
                        </div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Notes</label>
                        <textarea name="notes" class="form-control bg-transparent" rows="2" maxlength="2000"></textarea>
                    </div>

                    <h6 class="mb-2"><i class="fas fa-box me-1 text-gold"></i>Items to Transfer</h6>
                    <div id="transferItems">
                        <div class="row g-2 mb-2 item-row">
                            <div class="col-md-6">
                                <select name="items[0][product_id]" class="form-select form-select-sm bg-transparent" required>
                                    <option value="">Select Product</option>
                                    <?php foreach ($products as $p): ?>
                                        <option value="<?= $p->id ?>"><?= e($p->name) ?> (<?= e($p->sku) ?>)</option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="col-md-4">
                                <input type="number" name="items[0][quantity]" class="form-control form-control-sm bg-transparent"
                                       placeholder="Qty" min="1" required>
                            </div>
                            <div class="col-md-2">
                                <button type="button" class="btn btn-sm btn-outline-success w-100" onclick="addTransferItem()">
                                    <i class="fas fa-plus"></i>
                                </button>
                            </div>
                        </div>
                    </div>

                    <div class="mt-3">
                        <button type="submit" class="btn btn-gold">
                            <i class="fas fa-exchange-alt me-1"></i>Transfer Stock
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="col-lg-7">
        <div class="glass-card p-0">
            <div class="card-header">
                <div class="card-header-left">
                    <i class="fas fa-history card-icon"></i>
                    <h6 class="card-title">Transfer History</h6>
                </div>
            </div>
            <div class="card-body">
                <div class="mb-3">
                    <form method="GET" action="<?= url('stock/transfers') ?>" class="d-flex gap-2">
                        <div class="input-group">
                            <input type="text" name="search" class="form-control form-control-sm bg-transparent"
                                   placeholder="Search by reference..." value="<?= e($search) ?>">
                            <button type="submit" class="btn btn-sm btn-gold">Search</button>
                        </div>
                    </form>
                </div>

                <div class="table-responsive">
                    <table class="table table-dark table-hover mb-0">
                        <thead>
                            <tr>
                                <th>Reference</th>
                                <th>From</th>
                                <th>To</th>
                                <th>Date</th>
                                <th>Status</th>
                                <th>By</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (empty($transfers->data)): ?>
                                <tr>
                                    <td colspan="6" class="text-center text-muted py-3">No transfers found</td>
                                </tr>
                            <?php else: ?>
                                <?php foreach ($transfers->data as $t): ?>
                                    <tr>
                                        <td><code><?= e($t->reference) ?></code></td>
                                        <td><?= e($t->from_branch_name ?? '-') ?></td>
                                        <td><?= e($t->to_branch_name ?? '-') ?></td>
                                        <td><?= formatDateTime($t->created_at) ?></td>
                                        <td>
                                            <span class="badge bg-<?= statusBadge($t->status) ?>">
                                                <?= e(ucfirst($t->status)) ?>
                                            </span>
                                        </td>
                                        <td><small><?= e($t->user_first_name ?? '') ?> <?= e($t->user_last_name ?? '') ?></small></td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>

                <?php if ($transfers->totalPages > 1): ?>
                    <div class="mt-3">
                        <?= paginateLinks($transfers, url('stock/transfers')) ?>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<?php $inlineScripts = <<<'EOS'
var transferItemCount = 1;
function addTransferItem() {
    var html = '<div class="row g-2 mb-2 item-row">';
    html += '<div class="col-md-6">';
    html += '<select name="items[' + transferItemCount + '][product_id]" class="form-select form-select-sm bg-transparent" required>';
    html += '<option value="">Select Product</option>';
    html += $('#transferItems .item-row:first select').html();
    html += '</select></div>';
    html += '<div class="col-md-4">';
    html += '<input type="number" name="items[' + transferItemCount + '][quantity]" class="form-control form-control-sm bg-transparent" placeholder="Qty" min="1" required>';
    html += '</div>';
    html += '<div class="col-md-2">';
    html += '<button type="button" class="btn btn-sm btn-outline-danger w-100" onclick="$(this).closest(\'.item-row\').remove()"><i class="fas fa-times"></i></button>';
    html += '</div></div>';
    $('#transferItems').append(html);
    transferItemCount++;
}
EOS;
?>
