<?php
$__template->layout('Layout.main');
$isEdit = $entity !== null;
?>
<?php $__template->section('title'); ?><?= $isEdit ? 'تعديل: ' . e($entity->name_ar) : 'إضافة جهة مستأجرة' ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if ($isEdit): ?>
<a href="/rentals/entities/<?= (int) $entity->id ?>" class="btn btn-outline"><i data-lucide="arrow-right" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> العودة للجهة</a>
<?php else: ?>
<a href="/rentals/entities" class="btn btn-outline"><i data-lucide="arrow-right" style="width:16px;height:16px;vertical-align:middle;margin-left:4px;"></i> العودة للقائمة</a>
<?php endif; ?>
<?php $__template->endSection(); ?>

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

<?php
// Pre-populate: old input takes priority, then entity values for edit, then empty
$val = function(string $field, $default = '') use ($entity, $isEdit) {
    $oldInput = $_SESSION['_old_input'] ?? [];
    if (isset($oldInput[$field])) {
        return $oldInput[$field];
    }
    if ($isEdit && $entity !== null) {
        return $entity->$field ?? $default;
    }
    return $default;
};
?>

<form method="POST" action="<?= $isEdit ? '/rentals/entities/' . (int) $entity->id : '/rentals/entities' ?>">
    <?= \App\Core\CSRF::field() ?>

    <div class="card" style="margin-bottom:20px;">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;color:#0D7377;">بيانات الجهة المستأجرة</h3>
        </div>
        <div style="padding:20px;display:grid;grid-template-columns:1fr 1fr;gap:15px;">

            <div>
                <label class="form-label">نوع الجهة <span style="color:#DC2626;">*</span></label>
                <select name="entity_type" class="form-input" required>
                    <option value="">-- اختر --</option>
                    <?php foreach ($entityTypes as $tKey => $tLabel): ?>
                    <option value="<?= e($tKey) ?>" <?= (string) $val('entity_type') === $tKey ? 'selected' : '' ?>><?= e($tLabel) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div>
                <label class="form-label">الحالة <span style="color:#DC2626;">*</span></label>
                <select name="status" class="form-input" required>
                    <?php foreach ($statuses as $sKey => $sLabel): ?>
                    <option value="<?= e($sKey) ?>" <?= (string) $val('status', 'pending') === $sKey ? 'selected' : '' ?>><?= e($sLabel) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div>
                <label class="form-label">الاسم بالعربي <span style="color:#DC2626;">*</span></label>
                <input type="text" name="name_ar" value="<?= e($val('name_ar')) ?>" class="form-input" required>
            </div>

            <div>
                <label class="form-label">الاسم بالإنجليزي</label>
                <input type="text" name="name_en" value="<?= e($val('name_en')) ?>" class="form-input">
            </div>

            <div>
                <label class="form-label">جهة الاتصال</label>
                <input type="text" name="contact_person" value="<?= e($val('contact_person')) ?>" class="form-input">
            </div>

            <div>
                <label class="form-label">هاتف الاتصال</label>
                <input type="text" name="contact_phone" value="<?= e($val('contact_phone')) ?>" class="form-input">
            </div>

            <div>
                <label class="form-label">البريد الإلكتروني</label>
                <input type="email" name="contact_email" value="<?= e($val('contact_email')) ?>" class="form-input">
            </div>

            <div>
                <label class="form-label">الرقم الضريبي</label>
                <input type="text" name="tax_number" value="<?= e($val('tax_number')) ?>" class="form-input" style="direction:ltr;text-align:left;">
            </div>

            <div style="grid-column:1/-1;">
                <label class="form-label">العنوان</label>
                <textarea name="address" class="form-input" rows="2" style="resize:vertical;"><?= e($val('address')) ?></textarea>
            </div>

            <div>
                <label class="form-label">تاريخ الاعتماد</label>
                <input type="date" name="approval_date" value="<?= e($val('approval_date')) ?>" class="form-input">
            </div>

            <div style="display:flex;align-items:end;gap:20px;">
                <label style="display:flex;align-items:center;gap:8px;cursor:pointer;padding-bottom:8px;">
                    <input type="checkbox" name="official_letter_received" value="1" <?= $val('official_letter_received') ? 'checked' : '' ?>>
                    <span style="font-size:14px;">تم استلام الخطاب الرسمي</span>
                </label>
            </div>

            <div style="display:flex;align-items:end;gap:20px;">
                <label style="display:flex;align-items:center;gap:8px;cursor:pointer;padding-bottom:8px;">
                    <input type="checkbox" name="board_approved" value="1" <?= $val('board_approved') ? 'checked' : '' ?>>
                    <span style="font-size:14px;">معتمد من مجلس الإدارة</span>
                </label>
            </div>

            <div style="grid-column:1/-1;">
                <label class="form-label">ملاحظات</label>
                <textarea name="notes" class="form-input" rows="3" style="resize:vertical;"><?= e($val('notes')) ?></textarea>
            </div>

        </div>
    </div>

    <div style="display:flex;gap:10px;">
        <button type="submit" class="btn btn-primary">حفظ</button>
        <a href="<?= $isEdit ? '/rentals/entities/' . (int) $entity->id : '/rentals/entities' ?>" class="btn btn-outline">إلغاء</a>
    </div>
</form>

<script>
document.addEventListener('DOMContentLoaded', function() {
    if (typeof lucide !== 'undefined') lucide.createIcons();
});
</script>
<?php $__template->endSection(); ?>
