<?php
    $quote = $quotation ?? null;
    $isEdit = (bool) $quote;
    $formAction = $isEdit ? route('admin.quotations.update', $quote) : route('admin.quotations.store');
    $labels = $docLabels;
    $lineItems = $lineItems ?? [['item_name' => '', 'description' => '', 'unit' => 'NOS', 'quantity' => 1, 'rate' => 0, 'discount_percent' => 0, 'tax_rate' => 0]];
?>

<form method="POST" action="<?php echo e($formAction); ?>" id="quotation-form" class="billing-invoice-form<?php echo e(($gstEnabled ?? false) ? ' billing-invoice-form--gst' : ''); ?>"
    data-quote-type="<?php echo e($quoteType); ?>"
    data-gst-enabled="<?php echo e(($gstEnabled ?? false) ? '1' : '0'); ?>"
    data-company-state="<?php echo e($company->state ?? ''); ?>"
    data-clients='<?php echo json_encode($clientsJson ?? [], 15, 512) ?>'
    data-items='<?php echo json_encode($itemsJson, 15, 512) ?>'
    data-tax-rates='<?php echo json_encode($taxRatesJson, 15, 512) ?>'
    data-units='<?php echo json_encode($unitsJson, 15, 512) ?>'>
    <?php echo csrf_field(); ?>
    <?php if($isEdit): ?> <?php echo method_field('PUT'); ?> <?php endif; ?>
    <?php echo $__env->make('partials.admin.billing-catalog-items-json', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
    <input type="hidden" name="type" value="<?php echo e($quoteType); ?>">

    <div class="billing-form-card card border-0 mb-3">
        <div class="card-body">
            <h5 class="fw-semibold mb-1"><?php echo e($isEdit ? 'Edit' : 'Add New'); ?> <?php echo e($typeLabel); ?></h5>
            <p class="text-muted small mb-4">Fill in client details, line items, and totals.</p>

            <div class="row g-3 billing-invoice-header-grid">
                <div class="col-md-4">
                    <label class="form-label">Client name <span class="text-danger">*</span></label>
                    <?php echo $__env->make('partials.admin.client-picker', [
                        'selectedCustomerId' => old('customer_id', $quote?->customer_id),
                        'customers' => $customers,
                    ], array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
                </div>
                <div class="col-md-4">
                    <label class="form-label"><?php echo e($labels['number']); ?> <span class="text-danger">*</span></label>
                    <div class="input-group">
                        <input type="text" name="quote_prefix" class="form-control" style="max-width:45%" value="<?php echo e(old('quote_prefix', $quote?->quote_prefix ?? $defaultPrefix)); ?>" required>
                        <input type="number" name="quote_seq" class="form-control" min="1" value="<?php echo e(old('quote_seq', $quote?->quote_seq ?? $defaultSeq)); ?>" required>
                    </div>
                </div>
                <div class="col-md-4">
                    <label class="form-label"><?php echo e($labels['date']); ?> <span class="text-danger">*</span></label>
                    <input type="date" name="quote_date" class="form-control" value="<?php echo e(old('quote_date', $quote?->quote_date?->format('Y-m-d') ?? date('Y-m-d'))); ?>" required>
                </div>
                <div class="col-md-4">
                    <label class="form-label">PO reference</label>
                    <div class="input-group">
                        <select name="po_label" class="form-select" style="max-width:42%">
                            <?php $__currentLoopData = $poLabels; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($key); ?>" <?php if(old('po_label', $quote?->po_label ?? 'po_number') === $key): echo 'selected'; endif; ?>><?php echo e($label); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </select>
                        <input type="text" name="po_no" class="form-control" value="<?php echo e(old('po_no', $quote?->po_no)); ?>" placeholder="Number">
                    </div>
                </div>
                <div class="col-md-4">
                    <label class="form-label">PO date</label>
                    <input type="date" name="po_date" class="form-control" value="<?php echo e(old('po_date', $quote?->po_date?->format('Y-m-d'))); ?>">
                </div>
                <div class="col-md-4">
                    <label class="form-label">Valid until</label>
                    <input type="date" name="valid_until" class="form-control" value="<?php echo e(old('valid_until', $quote?->valid_until?->format('Y-m-d') ?? $defaultValidUntil)); ?>">
                </div>
            </div>

            <?php echo $__env->make('partials.admin.gst-form-fields', ['quote' => $quote, 'gstEnabled' => $gstEnabled ?? false, 'indianStates' => $indianStates ?? []], array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
        </div>
    </div>

    <div class="billing-form-card card border-0 mb-3">
        <div class="card-body p-0">
            <div class="px-3 pt-3 pb-2">
                <h6 class="fw-semibold mb-0"><?php echo e($labels['title']); ?></h6>
            </div>
            <div class="table-responsive">
                <table class="table table-bordered billing-line-items-table mb-0<?php echo e(($gstEnabled ?? false) ? ' billing-line-items-table--gst' : ''); ?>">
                    <thead class="table-light">
                        <tr>
                            <th class="col-no">No</th>
                            <th class="col-item">Item Name</th>
                            <th class="col-desc">Description</th>
                            <th class="col-unit">Unit</th>
                            <th class="col-qty">QTY</th>
                            <th class="col-price">Price</th>
                            <th class="col-disc">Disc. %</th>
                            <?php echo $__env->make('partials.admin.gst-line-item-columns', ['gstEnabled' => $gstEnabled ?? false], array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
                            <?php if(!($gstEnabled ?? false)): ?>
                            <th class="col-tax">Tax</th>
                            <?php endif; ?>
                            <th class="col-total text-end">Total</th>
                            <th class="col-act"></th>
                        </tr>
                    </thead>
                    <tbody id="line-items-body">
                        <?php $__currentLoopData = $lineItems; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $idx => $line): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <?php
                            $unitCode = $line['unit'] ?? 'NOS';
                            $unitMeta = collect($unitsCatalog)->firstWhere('code', $unitCode);
                            $unitLabel = $unitMeta['label'] ?? $unitCode;
                        ?>
                        <tr class="line-item-row">
                            <td class="text-center line-no"><?php echo e($idx + 1); ?></td>
                            <td class="col-item">
                                <select name="line_items[<?php echo e($idx); ?>][item_id]" data-field="item_id" class="form-select form-select-sm mb-1">
                                    <option value="">Select Item</option>
                                    <?php echo $__env->make('partials.admin.catalog-item-options', [
                                        'catalogItems' => $catalogItems,
                                        'selectedId' => $line['item_id'] ?? null,
                                        'rateColumn' => 'sale_rate',
                                    ], array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
                                </select>
                                <input type="text" name="line_items[<?php echo e($idx); ?>][item_name]" data-field="item_name" class="form-control form-control-sm" value="<?php echo e($line['item_name'] ?? ''); ?>" placeholder="Item name" required>
                            </td>
                            <td class="col-desc">
                                <textarea name="line_items[<?php echo e($idx); ?>][description]" data-field="description" class="form-control form-control-sm billing-desc-field" rows="2" maxlength="1000" placeholder="Description"><?php echo e($line['description'] ?? ''); ?></textarea>
                                <small class="text-muted billing-desc-hint">1000 characters left</small>
                            </td>
                            <td class="col-unit">
                                <?php echo $__env->make('partials.admin.unit-combobox', [
                                    'name' => "line_items[{$idx}][unit]",
                                    'value' => $unitCode,
                                    'label' => $unitLabel,
                                ], array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
                            </td>
                            <td class="col-qty"><input type="number" step="0.001" min="0.001" name="line_items[<?php echo e($idx); ?>][quantity]" data-field="quantity" class="form-control form-control-sm text-center" value="<?php echo e($line['quantity'] ?? 1); ?>"></td>
                            <td class="col-price"><input type="number" step="0.01" min="0" name="line_items[<?php echo e($idx); ?>][rate]" data-field="rate" class="form-control form-control-sm" value="<?php echo e($line['rate'] ?? 0); ?>" placeholder="0"></td>
                            <td class="col-disc"><input type="number" step="0.01" min="0" max="100" name="line_items[<?php echo e($idx); ?>][discount_percent]" data-field="discount_percent" class="form-control form-control-sm" value="<?php echo e($line['discount_percent'] ?? 0); ?>" placeholder="0"></td>
                            <?php echo $__env->make('partials.admin.gst-line-item-cells', ['gstEnabled' => $gstEnabled ?? false, 'idx' => $idx, 'line' => $line, 'taxRates' => $taxRates], array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
                            <?php if(!($gstEnabled ?? false)): ?>
                            <td>
                                <select name="line_items[<?php echo e($idx); ?>][tax_rate]" data-field="tax_rate" class="form-select form-select-sm">
                                    <option value="0">Select Tax</option>
                                    <?php $__currentLoopData = $taxRates; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $tax): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($tax->rate); ?>" data-tax-name="<?php echo e($tax->name); ?>" <?php if(($line['tax_rate'] ?? 0) == $tax->rate): echo 'selected'; endif; ?>><?php echo e($tax->name); ?> (<?php echo e($tax->rate); ?>%)</option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                            </td>
                            <?php endif; ?>
                            <td class="text-end fw-medium" data-line-total>₹ 0.00</td>
                            <td class="text-center">
                                <button type="button" class="btn btn-sm btn-soft-danger btn-remove-line"><i class="ri-delete-bin-line"></i></button>
                            </td>
                        </tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </tbody>
                </table>
            </div>
            <div class="p-3 border-top text-end">
                <button type="button" class="btn btn-primary btn-sm" id="btn-add-line">
                    <i class="ri-add-line me-1"></i> Add line
                </button>
            </div>
        </div>
    </div>

    <?php
        $discAllType = old('discount_all_type', $quote?->discount_all_type ?? 'percent');
        $discAllVal = old('discount_all_value', $quote?->discount_all_value ?? $quote?->discount_all_percent ?? 0);
    ?>
    <div class="row g-3 billing-invoice-options-row align-items-start">
        <div class="col-lg-6">
            <div class="billing-form-card card border-0 h-100">
                <div class="card-body billing-invoice-options">
                    <h6 class="billing-options-heading"><i class="ri-settings-4-line me-1"></i> Additional options</h6>

                    <div class="billing-option-block" data-option="shipping">
                        <div class="billing-option-head">
                            <input class="form-check-input" type="checkbox" name="has_shipping" id="has_shipping" value="1" <?php if(old('has_shipping', $quote?->has_shipping)): echo 'checked'; endif; ?>>
                            <label class="form-check-label fw-medium" for="has_shipping">Add shipping charges</label>
                        </div>
                        <div class="billing-option-body <?php echo e(old('has_shipping', $quote?->has_shipping) ? '' : 'd-none'); ?>" id="has_shipping_wrap">
                            <div class="row g-2">
                                <div class="col-sm-5">
                                    <select name="shipping_mode" id="shipping_mode" class="form-select form-select-sm">
                                        <option value="">Select mode</option>
                                        <?php $__currentLoopData = $shippingModes; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <option value="<?php echo e($key); ?>" <?php if(old('shipping_mode', $quote?->shipping_mode) === $key): echo 'selected'; endif; ?>><?php echo e($label); ?></option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                </div>
                                <div class="col-sm-7">
                                    <input type="number" step="0.01" min="0" name="shipping_charges" id="shipping_charges" class="form-control form-control-sm" value="<?php echo e(old('shipping_charges', $quote?->shipping_charges ?? 0)); ?>" placeholder="Shipping Amount">
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="billing-option-block" data-option="discount_total">
                        <div class="billing-option-head">
                            <input class="form-check-input" type="checkbox" name="has_discount_total" id="has_discount_total" value="1" <?php if(old('has_discount_total', $quote?->has_discount_total)): echo 'checked'; endif; ?>>
                            <label class="form-check-label fw-medium" for="has_discount_total">Add Discount On Total</label>
                        </div>
                        <div class="billing-option-body <?php echo e(old('has_discount_total', $quote?->has_discount_total) ? '' : 'd-none'); ?>" id="has_discount_total_wrap">
                            <input type="number" step="0.01" min="0" name="discount_on_total" id="discount_on_total" class="form-control form-control-sm" value="<?php echo e(old('discount_on_total', $quote?->discount_on_total ?? 0)); ?>" placeholder="0">
                        </div>
                    </div>

                    <div class="billing-option-block" data-option="discount_all">
                        <div class="billing-option-head">
                            <input class="form-check-input" type="checkbox" name="has_discount_all" id="has_discount_all" value="1" <?php if(old('has_discount_all', $quote?->has_discount_all)): echo 'checked'; endif; ?>>
                            <label class="form-check-label fw-medium" for="has_discount_all">Add discount to all</label>
                        </div>
                        <div class="billing-option-body <?php echo e(old('has_discount_all', $quote?->has_discount_all) ? '' : 'd-none'); ?>" id="has_discount_all_wrap">
                            <div class="row g-2">
                                <div class="col-sm-5">
                                    <input type="number" step="0.01" min="0" name="discount_all_value" id="discount_all_value" class="form-control form-control-sm" value="<?php echo e($discAllVal); ?>" placeholder="0">
                                </div>
                                <div class="col-sm-7">
                                    <select name="discount_all_type" id="discount_all_type" class="form-select form-select-sm">
                                        <?php $__currentLoopData = $discountAllTypes; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <option value="<?php echo e($key); ?>" <?php if($discAllType === $key): echo 'selected'; endif; ?>><?php echo e($label); ?></option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="billing-option-block" data-option="custom">
                        <div class="billing-option-head">
                            <input class="form-check-input" type="checkbox" name="has_custom_amount" id="has_custom_amount" value="1" <?php if(old('has_custom_amount', $quote?->has_custom_amount)): echo 'checked'; endif; ?>>
                            <label class="form-check-label fw-medium" for="has_custom_amount">Add Custom Amount</label>
                        </div>
                        <div class="billing-option-body <?php echo e(old('has_custom_amount', $quote?->has_custom_amount) ? '' : 'd-none'); ?>" id="has_custom_amount_wrap">
                            <div class="row g-2">
                                <div class="col-sm-6">
                                    <input type="text" name="custom_amount_label" id="custom_amount_label" class="form-control form-control-sm" value="<?php echo e(old('custom_amount_label', $quote?->custom_amount_label)); ?>" placeholder="Custom Amount Label">
                                </div>
                                <div class="col-sm-6">
                                    <input type="number" step="0.01" min="0" name="custom_amount" id="custom_amount" class="form-control form-control-sm" value="<?php echo e(old('custom_amount', $quote?->custom_amount ?? 0)); ?>" placeholder="Custom Amount">
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="billing-option-block mb-0" data-option="advance">
                        <div class="billing-option-head">
                            <input class="form-check-input" type="checkbox" name="has_advance_payment" id="has_advance_payment" value="1" <?php if(old('has_advance_payment', $quote?->has_advance_payment)): echo 'checked'; endif; ?>>
                            <label class="form-check-label fw-medium" for="has_advance_payment">Add/Use Advance Payment</label>
                            <span class="billing-option-inline-val text-muted small" id="advance-inline-display"><?php echo e(old('has_advance_payment', $quote?->has_advance_payment) ? '₹ '.number_format(old('advance_payment', $quote?->advance_payment ?? 0), 2) : ''); ?></span>
                        </div>
                        <div class="billing-option-body <?php echo e(old('has_advance_payment', $quote?->has_advance_payment) ? '' : 'd-none'); ?>" id="has_advance_payment_wrap">
                            <input type="number" step="0.01" min="0" name="advance_payment" id="advance_payment" class="form-control form-control-sm mb-1" value="<?php echo e(old('advance_payment', $quote?->advance_payment ?? 0)); ?>">
                            <a href="#" class="small text-primary" id="link-add-advance"><i class="ri-add-line"></i> Add advance payment</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-lg-6">
            <div class="billing-invoice-summary card border-0 h-100 sticky-lg-top" style="top:80px">
                <div class="card-body p-0">
                    <div class="billing-summary-header px-3 py-2">Amount summary</div>
                    <div class="billing-summary-lines px-3 py-2">
                        <div class="billing-summary-line">
                            <span>Subtotal</span>
                            <span id="summary-subtotal" class="fw-medium">₹ 0.00</span>
                        </div>
                        <?php echo $__env->make('partials.admin.gst-summary-rows', ['gstEnabled' => $gstEnabled ?? false], array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
                        <div id="summary-tax-breakdown" class="billing-summary-tax-breakdown"></div>
                        <div class="billing-summary-line billing-summary-line--deduct d-none" id="row-summary-advance" data-summary="advance">
                            <span>Used Advanced Payment</span>
                            <span class="text-danger">(-) <span id="summary-advance">₹ 0.00</span></span>
                        </div>
                        <div class="billing-summary-line d-none" id="row-summary-shipping" data-summary="shipping">
                            <span>Shipping &amp; Packaging charges</span>
                            <span id="summary-shipping" class="text-primary">₹ 0.00</span>
                        </div>
                        <div class="billing-summary-line billing-summary-line--deduct d-none" id="row-summary-discount-total" data-summary="discount_total">
                            <span>Discount On Total</span>
                            <span class="text-danger">(-) <span id="summary-discount-total">₹ 0.00</span></span>
                        </div>
                        <div class="billing-summary-line billing-summary-line--deduct d-none" id="row-summary-discount-all" data-summary="discount_all">
                            <span id="summary-discount-all-label">Discount on all</span>
                            <span class="text-danger">(-) <span id="summary-discount-all">₹ 0.00</span></span>
                        </div>
                        <div class="billing-summary-line d-none" id="row-summary-custom" data-summary="custom">
                            <span id="summary-custom-label">Custom Amount</span>
                            <span id="summary-custom">₹ 0.00</span>
                        </div>
                    </div>
                    <div class="billing-summary-total px-3 py-3">
                        <span class="fw-semibold">Total</span>
                        <span class="billing-summary-total__amount" id="summary-total">₹ 0</span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row g-3 mt-1">
        <div class="col-md-6">
            <label class="form-label">Terms &amp; Conditions</label>
            <textarea name="terms" class="form-control" rows="4" placeholder="Payment terms visible on document"><?php echo e(old('terms', $quote?->terms)); ?></textarea>
        </div>
        <div class="col-md-6">
            <label class="form-label">Private notes <span class="text-muted">(not shown to client)</span></label>
            <textarea name="private_notes" class="form-control" rows="4" placeholder="Internal notes"><?php echo e(old('private_notes', $quote?->private_notes)); ?></textarea>
        </div>
    </div>

    <div class="billing-form-footer mt-4">
        <div class="d-flex flex-wrap gap-2 ms-md-auto">
            <button type="submit" name="save_action" value="preview" class="btn btn-success">
                <i class="ri-eye-line me-1"></i> Save &amp; Preview
            </button>
            <button type="submit" name="save_action" value="save" class="btn btn-outline-success">
                <i class="ri-check-line me-1"></i> Save
            </button>
            <button type="submit" name="save_action" value="draft" class="btn btn-light border">
                Save as draft
            </button>
            <a href="<?php echo e(route('admin.quotations.index')); ?>" class="btn btn-light">Cancel</a>
        </div>
    </div>
</form>

<div class="modal fade" id="modal-add-unit" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-sm modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header py-2">
                <h6 class="modal-title">Add new unit</h6>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form id="form-add-unit">
                <div class="modal-body">
                    <div class="mb-2">
                        <label class="form-label small">Unit name</label>
                        <input type="text" name="name" class="form-control form-control-sm" required placeholder="e.g. Carton">
                    </div>
                    <div class="mb-2">
                        <label class="form-label small">Unit code</label>
                        <input type="text" name="code" class="form-control form-control-sm" required placeholder="e.g. CTN" maxlength="20">
                    </div>
                    <div class="form-check">
                        <input class="form-check-input" type="checkbox" name="is_gst" value="1" id="new_unit_gst">
                        <label class="form-check-label small" for="new_unit_gst">GST unit</label>
                    </div>
                </div>
                <div class="modal-footer py-2">
                    <button type="button" class="btn btn-light btn-sm" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-success btn-sm">Add unit</button>
                </div>
            </form>
        </div>
    </div>
</div>

<?php echo $__env->make('partials.admin.modal-quick-client', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>

<?php $__env->startPush('scripts'); ?>
<script>
    window.__billingUnits = <?php echo json_encode($unitsJson, 15, 512) ?>;
    window.__billingCsrf = '<?php echo e(csrf_token()); ?>';
    window.__billingUnitsStore = '<?php echo e(route('admin.units.store')); ?>';
</script>
<?php echo $__env->make('partials.admin.document-form-client-scripts', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
<script src="<?php echo e(asset('js/unit-picker.js')); ?>"></script>
<?php echo $__env->make('partials.admin.invoice-form-script', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
<script>
    document.addEventListener('DOMContentLoaded', function () {
        if (window.BillingClientPicker) {
            BillingClientPicker.configure({ clients: window.__billingClients || [] });
            BillingClientPicker.initAll(document.getElementById('quotation-form'));
        }
        document.dispatchEvent(new CustomEvent('billing-invoice-form-ready'));
    });
</script>
<?php $__env->stopPush(); ?>
<?php /**PATH /Applications/XAMPP/xamppfiles/htdocs/cms/account/billing-system/resources/views/admin/quotations/_form.blade.php ENDPATH**/ ?>