<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?><?= isset($bom) ? 'تعديل قائمة المواد' : 'قائمة مواد جديدة' ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/inventory/bom" class="btn btn-outline"><i data-lucide="arrow-right" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> العودة للقائمة</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>

<?php
$isEdit = isset($bom);
$action = $isEdit ? '/inventory/bom/' . (int) $bom['id'] . '/update' : '/inventory/bom';
?>

<form method="POST" action="<?= $action ?>" id="bomForm">
    <?= csrf_field() ?>

    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
            <i data-lucide="layers" style="width:18px;height:18px;color:#1e3a5f;"></i>
            <h3 style="margin:0;color:#1e3a5f;font-size:15px;">بيانات القائمة</h3>
        </div>
        <div style="padding:20px;">
            <div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:15px;">
                <div>
                    <label class="form-label">الصنف المنتج <span style="color:#DC2626;">*</span></label>
                    <select name="item_id" class="form-select" required>
                        <option value="">-- اختر الصنف --</option>
                        <?php foreach ($items as $item): ?>
                        <option value="<?= (int) $item['id'] ?>" <?= ($bom['item_id'] ?? '') == $item['id'] ? 'selected' : '' ?>><?= e($item['name_ar']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="form-label">اسم القائمة <span style="color:#DC2626;">*</span></label>
                    <input type="text" name="name" class="form-input" value="<?= e($bom['name'] ?? '') ?>" required placeholder="اسم قائمة المواد">
                </div>
                <div>
                    <label class="form-label">كمية الإنتاج <span style="color:#DC2626;">*</span></label>
                    <input type="number" name="output_quantity" class="form-input" step="0.001" min="0.001" value="<?= e($bom['output_quantity'] ?? '1') ?>" required>
                </div>
            </div>
        </div>
    </div>

    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;justify-content:space-between;">
            <div style="display:flex;align-items:center;gap:8px;">
                <i data-lucide="package" style="width:18px;height:18px;color:#D97706;"></i>
                <h3 style="margin:0;color:#D97706;font-size:15px;">المكونات</h3>
            </div>
            <button type="button" class="btn btn-sm btn-primary" onclick="addComponentRow()">
                <i data-lucide="plus" style="width:14px;height:14px;vertical-align:middle;margin-left:4px;"></i> إضافة مكون
            </button>
        </div>
        <div class="table-responsive">
            <table class="data-table" id="componentsTable">
                <thead>
                    <tr>
                        <th>الصنف</th>
                        <th style="width:100px;">الكمية</th>
                        <th style="width:100px;">الوحدة</th>
                        <th style="width:100px;">نسبة الهالك %</th>
                        <th style="width:50px;"></th>
                    </tr>
                </thead>
                <tbody id="componentsBody"></tbody>
            </table>
        </div>
    </div>

    <div style="text-align:left;">
        <button type="submit" class="btn btn-primary" style="padding:10px 30px;">
            <i data-lucide="save" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> حفظ القائمة
        </button>
    </div>
</form>

<script>
var compRowIdx = 0;
var itemsData = <?= json_encode($items) ?>;

function addComponentRow(data) {
    data = data || {};
    var idx = compRowIdx++;
    var tr = document.createElement('tr');

    var itemOptions = '<option value="">-- اختر المكون --</option>';
    itemsData.forEach(function(item) {
        var selected = (data.item_id && data.item_id == item.id) ? 'selected' : '';
        itemOptions += '<option value="' + item.id + '" ' + selected + '>' + item.name_ar + '</option>';
    });

    tr.innerHTML = ''
        + '<td><select name="components['+idx+'][item_id]" class="form-select" required>' + itemOptions + '</select></td>'
        + '<td><input type="number" name="components['+idx+'][quantity]" class="form-input" step="0.001" min="0.001" value="'+(data.quantity||'')+'" required></td>'
        + '<td><input type="text" name="components['+idx+'][unit]" class="form-input" value="'+(data.unit||'')+'" placeholder="وحدة"></td>'
        + '<td><input type="number" name="components['+idx+'][waste_percent]" class="form-input" step="0.01" min="0" max="100" value="'+(data.waste_percent||'0')+'"></td>'
        + '<td><button type="button" onclick="this.closest(\'tr\').remove();" style="background:none;border:none;cursor:pointer;color:#DC2626;"><i data-lucide="trash-2" style="width:16px;height:16px;"></i></button></td>';

    document.getElementById('componentsBody').appendChild(tr);
    if (typeof lucide !== 'undefined') lucide.createIcons();
}

document.addEventListener('DOMContentLoaded', function() {
    if (typeof lucide !== 'undefined') lucide.createIcons();
    <?php if ($isEdit && !empty($components)): ?>
    <?= json_encode($components) ?>.forEach(function(comp) { addComponentRow(comp); });
    <?php else: ?>
    addComponentRow();
    <?php endif; ?>
});
</script>
<?php $__template->endSection(); ?>
