<?= $this->extend('admin/layout/main') ?>
<?= $this->section('content') ?>
<h3 class="mb-3"><?= $quote ? lang('App.edit') : lang('App.add_new') ?> - <?= lang('App.quotes') ?></h3>

<div class="card p-4" style="max-width:650px;">
<form method="post" action="<?= $quote ? '/admin/quotes/' . $quote['id'] : '/admin/quotes' ?>">
    <?= csrf_field() ?>
    <div class="mb-3">
        <label class="form-label"><?= lang('App.client') ?></label>
        <select name="client_id" class="form-select" required>
            <?php foreach ($clients as $c): ?>
                <option value="<?= $c['id'] ?>" <?= ($quote['client_id'] ?? null) == $c['id'] ? 'selected' : '' ?>><?= esc($c['name']) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="mb-3">
        <label class="form-label">رقم العرض</label>
        <input type="text" name="quote_number" class="form-control" value="<?= esc($quote['quote_number'] ?? $suggestedNumber ?? '') ?>" required>
    </div>
    <div class="row">
        <div class="col-6 mb-3">
            <label class="form-label"><?= lang('App.amount') ?></label>
            <input type="number" step="0.01" name="amount" class="form-control" value="<?= esc($quote['amount'] ?? '') ?>" required>
        </div>
        <div class="col-6 mb-3">
            <label class="form-label"><?= lang('App.currency') ?></label>
            <select name="currency" class="form-select">
                <?php foreach ($currencies as $curr): $cur = $curr['code']; ?>
                    <option <?= ($quote['currency'] ?? $currencies[0]['code']) === $cur ? 'selected' : '' ?>><?= $cur ?><?= !empty($curr['name']) ? ' - ' . esc($curr['name']) : '' ?></option>
                <?php endforeach; ?>
            </select>
        </div>
    </div>
    <div class="row">
        <div class="col-6 mb-3">
            <label class="form-label"><?= lang('App.issue_date') ?></label>
            <input type="date" name="issue_date" class="form-control" value="<?= esc($quote['issue_date'] ?? date('Y-m-d')) ?>" required>
        </div>
        <div class="col-6 mb-3">
            <label class="form-label">ساري حتى</label>
            <input type="date" name="valid_until" class="form-control" value="<?= esc($quote['valid_until'] ?? date('Y-m-d', strtotime('+14 days'))) ?>" required>
        </div>
    </div>
    <div class="mb-3">
        <label class="form-label"><?= lang('App.status') ?></label>
        <select name="status" class="form-select">
            <?php foreach (['draft' => lang('App.quote_draft'), 'sent' => lang('App.quote_sent'), 'accepted' => lang('App.quote_accepted'), 'rejected' => lang('App.quote_rejected')] as $val => $label): ?>
                <option value="<?= $val ?>" <?= ($quote['status'] ?? 'draft') === $val ? 'selected' : '' ?>><?= $label ?></option>
            <?php endforeach; ?>
        </select>
    </div>

    <label class="form-label"><?= lang('App.items') ?></label>
    <div id="items-wrap">
        <?php $rows = $items ?? []; if (empty($rows)) $rows = [['description' => '', 'amount' => '', 'product_id' => '']]; ?>
        <?php foreach ($rows as $item): ?>
            <div class="row mb-2 g-2">
                <div class="col-3">
                    <select name="item_product_id[]" class="form-select form-select-sm product-select" onchange="fillFromProduct(this)">
                        <option value="">— منتج —</option>
                        <?php foreach ($products as $p): ?>
                            <option value="<?= $p['id'] ?>" data-name="<?= esc($p['name']) ?>" data-price="<?= $p['price'] ?>"
                                <?= ($item['product_id'] ?? '') == $p['id'] ? 'selected' : '' ?>>
                                <?= esc($p['name']) ?> (<?= number_format($p['price'], 2) ?>)
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-6"><input type="text" name="item_description[]" class="form-control form-control-sm" placeholder="الوصف" value="<?= esc($item['description'] ?? '') ?>"></div>
                <div class="col-3"><input type="number" step="0.01" name="item_amount[]" class="form-control form-control-sm" placeholder="<?= lang('App.amount') ?>" value="<?= esc($item['amount'] ?? '') ?>"></div>
            </div>
        <?php endforeach; ?>
    </div>
    <button type="button" class="btn btn-sm btn-outline-secondary mb-3" onclick="addItemRow()">+ إضافة بند</button>

    <div class="mb-3">
        <label class="form-label">ملاحظات</label>
        <textarea name="notes" class="form-control"><?= esc($quote['notes'] ?? '') ?></textarea>
    </div>
    <button class="btn btn-dark"><?= lang('App.save') ?></button>
    <a href="/admin/quotes" class="btn btn-light"><?= lang('App.cancel') ?></a>
</form>
</div>

<script>
function addItemRow() {
    const wrap = document.getElementById('items-wrap');
    const row = document.createElement('div');
    row.className = 'row mb-2 g-2';
    const productOptions = `<?php foreach ($products as $p): ?>
        <option value="<?= $p['id'] ?>" data-name="<?= esc($p['name']) ?>" data-price="<?= $p['price'] ?>"><?= esc($p['name']) ?> (<?= number_format($p['price'], 2) ?>)</option>
    <?php endforeach; ?>`;
    row.innerHTML = `<div class="col-3">
            <select name="item_product_id[]" class="form-select form-select-sm product-select" onchange="fillFromProduct(this)">
                <option value="">— منتج —</option>
                ${productOptions}
            </select>
        </div>
        <div class="col-6"><input type="text" name="item_description[]" class="form-control form-control-sm" placeholder="الوصف"></div>
        <div class="col-3"><input type="number" step="0.01" name="item_amount[]" class="form-control form-control-sm" placeholder="المبلغ"></div>`;
    wrap.appendChild(row);
}
function fillFromProduct(select) {
    const row = select.closest('.row');
    const option = select.options[select.selectedIndex];
    if (!option.value) return;
    row.querySelector('input[name="item_description[]"]').value = option.dataset.name;
    row.querySelector('input[name="item_amount[]"]').value = option.dataset.price;
}
</script>
<?= $this->endSection() ?>
