<?php $__template->layout('Layout.main'); ?>
<?php $isEdit = $contract !== null; ?>
<?php $__template->section('title'); ?><?= $isEdit ? 'تعديل العقد' : 'إنشاء عقد جديد' ?><?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/hr/contracts" class="btn btn-secondary">رجوع</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<form method="POST" action="<?= $isEdit ? '/hr/contracts/' . (int) $contract->id : '/hr/contracts' ?>">
    <?= csrf_field() ?>
    <div class="card" style="margin-bottom:20px;">
        <div style="padding:16px;border-bottom:1px solid #E5E7EB;"><h3 style="margin:0;font-size:16px;">بيانات العقد</h3></div>
        <div style="padding:16px;display:grid;grid-template-columns:repeat(auto-fit,minmax(250px,1fr));gap:16px;">
            <?php if (!$isEdit): ?>
            <div>
                <label style="display:block;margin-bottom:4px;font-size:13px;">الموظف <span style="color:#DC2626;">*</span></label>
                <input type="hidden" name="employee_profile_id" value="<?= (int) ($profile ? $profile->id : old('employee_profile_id')) ?>">
                <input type="text" class="form-control" value="<?= $profile ? e($profile->first_name_ar . ' ' . $profile->last_name_ar) : '' ?>" readonly>
            </div>
            <?php endif; ?>
            <div>
                <label style="display:block;margin-bottom:4px;font-size:13px;">نوع العقد <span style="color:#DC2626;">*</span></label>
                <select name="contract_type" class="form-control">
                    <option value="definite" <?= (old('contract_type') ?? ($isEdit ? $contract->contract_type : '')) === 'definite' ? 'selected' : '' ?>>محدد المدة</option>
                    <option value="indefinite" <?= (old('contract_type') ?? ($isEdit ? $contract->contract_type : '')) === 'indefinite' ? 'selected' : '' ?>>غير محدد المدة</option>
                </select>
            </div>
            <div>
                <label style="display:block;margin-bottom:4px;font-size:13px;">تاريخ البداية <span style="color:#DC2626;">*</span></label>
                <input type="date" name="start_date" value="<?= e(old('start_date') ?? ($isEdit ? $contract->start_date : '')) ?>" class="form-control" required>
            </div>
            <div>
                <label style="display:block;margin-bottom:4px;font-size:13px;">تاريخ النهاية</label>
                <input type="date" name="end_date" value="<?= e(old('end_date') ?? ($isEdit ? $contract->end_date : '')) ?>" class="form-control">
            </div>
            <div>
                <label style="display:block;margin-bottom:4px;font-size:13px;">فترة الاختبار (أشهر)</label>
                <input type="number" name="probation_months" value="<?= e(old('probation_months') ?? ($isEdit ? $contract->probation_months : '3')) ?>" class="form-control" min="0" max="3">
                <small style="color:#6B7280;">الحد الأقصى 3 أشهر (مادة 33)</small>
            </div>
            <div>
                <label style="display:block;margin-bottom:4px;font-size:13px;">ساعات العمل اليومية</label>
                <input type="text" name="working_hours_per_day" value="<?= e(old('working_hours_per_day') ?? ($isEdit ? $contract->working_hours_per_day : '8')) ?>" class="form-control">
            </div>
            <div>
                <label style="display:block;margin-bottom:4px;font-size:13px;">فترة الإنذار (أشهر)</label>
                <input type="number" name="notice_period_months" value="<?= e(old('notice_period_months') ?? ($isEdit ? $contract->notice_period_months : '2')) ?>" class="form-control" min="0">
            </div>
            <div>
                <label style="display:block;margin-bottom:4px;font-size:13px;">الراتب الأساسي</label>
                <input type="text" name="basic_salary" value="<?= e(old('basic_salary') ?? ($isEdit ? $contract->basic_salary : ($profile ? $profile->basic_salary : '0.00'))) ?>" class="form-control">
            </div>
            <div>
                <label style="display:block;margin-bottom:4px;font-size:13px;">إجمالي الحزمة</label>
                <input type="text" name="total_package" value="<?= e(old('total_package') ?? ($isEdit ? $contract->total_package : ($profile ? $profile->basic_salary : '0.00'))) ?>" class="form-control">
                <small style="color:#6B7280;">الراتب الأساسي + البدلات</small>
            </div>
            <div style="grid-column:1/-1;">
                <label style="display:block;margin-bottom:4px;font-size:13px;">شروط العقد</label>
                <textarea name="terms_ar" class="form-control" rows="4"><?= e(old('terms_ar') ?? ($isEdit ? json_decode($contract->terms_json ?? 'null', true) ?? '' : '')) ?></textarea>
            </div>
            <div style="grid-column:1/-1;">
                <label style="display:block;margin-bottom:4px;font-size:13px;">ملاحظات</label>
                <textarea name="notes" class="form-control" rows="2"><?= e(old('notes') ?? ($isEdit ? $contract->notes : '')) ?></textarea>
            </div>
        </div>
    </div>
    <div style="display:flex;gap:10px;justify-content:flex-end;">
        <a href="/hr/contracts" class="btn btn-secondary">إلغاء</a>
        <button type="submit" class="btn btn-primary"><?= $isEdit ? 'تحديث' : 'حفظ' ?></button>
    </div>
</form>
<?php $__template->endSection(); ?>
