<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?><?= $unit ? 'تعديل الوحدة: ' . e($unit->name_ar) : 'إضافة وحدة جديدة' ?><?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<a href="/sa/facilities/<?= (int) $facility->id ?>/units" class="btn btn-outline">رجوع للوحدات</a>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>

<div class="card" style="padding:10px 15px;margin-bottom:15px;background:#F9FAFB;">
    <span style="font-size:13px;color:#6B7280;">المنشأة:</span>
    <a href="/sa/facilities/<?= (int) $facility->id ?>" style="color:#0D7377;font-weight:600;margin-right:5px;"><?= e($facility->name_ar) ?></a>
    <span style="font-size:12px;color:#9CA3AF;">(<?= e($facility->code) ?>)</span>
</div>

<div class="card" style="max-width:600px;padding:25px;">
    <h3 style="margin:0 0 20px;color:#1F2937;"><?= $unit ? 'تعديل الوحدة' : 'إضافة وحدة جديدة' ?></h3>

    <form method="POST" action="<?= $unit ? "/sa/facilities/{$facility->id}/units/{$unit->id}" : "/sa/facilities/{$facility->id}/units" ?>">
        <?= csrf_field() ?>

        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:15px;">
            <div>
                <label class="form-label">الكود <span style="color:#DC2626;">*</span></label>
                <input type="text" name="code" value="<?= e(old('code', $unit ? $unit->code : '')) ?>" class="form-input" required maxlength="30" placeholder="مثال: LANE-01" style="direction:ltr;text-align:left;text-transform:uppercase;">
            </div>
            <div>
                <label class="form-label">نوع الوحدة <span style="color:#DC2626;">*</span></label>
                <select name="unit_type" class="form-select" required>
                    <option value="">اختر النوع</option>
                    <?php foreach ($unitTypeOptions as $key => $label): ?>
                    <option value="<?= e($key) ?>" <?= old('unit_type', $unit ? $unit->unit_type : '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>

        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:15px;">
            <div>
                <label class="form-label">الاسم بالعربية <span style="color:#DC2626;">*</span></label>
                <input type="text" name="name_ar" value="<?= e(old('name_ar', $unit ? $unit->name_ar : '')) ?>" class="form-input" required maxlength="200">
            </div>
            <div>
                <label class="form-label">الاسم بالإنجليزية</label>
                <input type="text" name="name_en" value="<?= e(old('name_en', $unit ? ($unit->name_en ?? '') : '')) ?>" class="form-input" maxlength="200" dir="ltr">
            </div>
        </div>

        <div style="display:grid;grid-template-columns:1fr 1fr;gap:15px;margin-bottom:15px;">
            <div>
                <label class="form-label">نمط الحجز <span style="color:#DC2626;">*</span></label>
                <select name="booking_mode" class="form-select" required>
                    <option value="">اختر النمط</option>
                    <?php foreach ($bookingModes as $key => $label): ?>
                    <option value="<?= e($key) ?>" <?= old('booking_mode', $unit ? $unit->booking_mode : '') === $key ? 'selected' : '' ?>><?= e($label) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div>
                <label class="form-label">السعة القصوى <span style="color:#DC2626;">*</span></label>
                <input type="number" name="max_capacity" value="<?= e(old('max_capacity', $unit ? (string) $unit->max_capacity : '1')) ?>" class="form-input" required min="1" step="1">
            </div>
        </div>

        <div style="margin-bottom:20px;">
            <label class="form-label">ترتيب العرض</label>
            <input type="number" name="sort_order" value="<?= e(old('sort_order', $unit ? (string) $unit->sort_order : '0')) ?>" class="form-input" style="max-width:150px;" min="0" step="1">
            <small style="color:#6B7280;display:block;margin-top:4px;">رقم أصغر = يظهر أولاً</small>
        </div>

        <div style="display:flex;gap:10px;">
            <button type="submit" class="btn btn-primary"><?= $unit ? 'حفظ التعديلات' : 'إضافة الوحدة' ?></button>
            <a href="/sa/facilities/<?= (int) $facility->id ?>/units" class="btn btn-outline">إلغاء</a>
        </div>
    </form>
</div>

<?php $__template->endSection(); ?>
