<div class="row justify-content-center">
<div class="col-lg-8">
<div class="card">
    <div class="card-header"><h5 class="mb-0"><?= $template ? lang('common.edit') : lang('common.create') ?> <?= lang('adv_finance.template') ?></h5></div>
    <div class="card-body">
        <form method="POST"
              action="<?= $template ? '/advanced-finance/installment-templates/' . $template['id'] : '/advanced-finance/installment-templates/store' ?>"
              data-ajax="true">
            <?= csrf_field() ?>
            <div class="row g-3">
                <div class="col-md-4">
                    <label class="form-label">Code <span class="text-danger">*</span></label>
                    <input type="text" name="template_code" class="form-control" value="<?= e($template['template_code'] ?? '') ?>" required>
                    <div class="invalid-feedback" data-field="template_code"></div>
                </div>
                <div class="col-md-4">
                    <label class="form-label">Name (EN) <span class="text-danger">*</span></label>
                    <input type="text" name="name_en" class="form-control" value="<?= e($template['name_en'] ?? '') ?>" required>
                    <div class="invalid-feedback" data-field="name_en"></div>
                </div>
                <div class="col-md-4">
                    <label class="form-label">Name (AR) <span class="text-danger">*</span></label>
                    <input type="text" name="name_ar" class="form-control" value="<?= e($template['name_ar'] ?? '') ?>" required>
                    <div class="invalid-feedback" data-field="name_ar"></div>
                </div>
                <div class="col-md-3">
                    <label class="form-label"># Installments <span class="text-danger">*</span></label>
                    <input type="number" name="number_of_installments" class="form-control" value="<?= e($template['number_of_installments'] ?? '12') ?>" min="1" max="120" required>
                    <div class="invalid-feedback" data-field="number_of_installments"></div>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Frequency <span class="text-danger">*</span></label>
                    <select name="frequency" class="form-select" required>
                        <?php foreach (['weekly','biweekly','monthly','bimonthly','quarterly','semi_annual','annual'] as $f): ?>
                        <option value="<?= $f ?>" <?= selected($f, $template['frequency'] ?? 'monthly') ?>><?= ucfirst(str_replace('_', ' ', $f)) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Down Payment % <span class="text-danger">*</span></label>
                    <input type="number" name="down_payment_percentage" class="form-control" step="0.01" value="<?= e($template['down_payment_percentage'] ?? '0') ?>" required>
                    <div class="invalid-feedback" data-field="down_payment_percentage"></div>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Interest Rate % <span class="text-danger">*</span></label>
                    <input type="number" name="interest_rate" class="form-control" step="0.01" value="<?= e($template['interest_rate'] ?? '0') ?>" required>
                </div>
                <div class="col-md-4">
                    <label class="form-label">Late Payment Fee <span class="text-danger">*</span></label>
                    <input type="number" name="late_payment_fee" class="form-control" step="0.01" value="<?= e($template['late_payment_fee'] ?? '0') ?>" required>
                </div>
                <div class="col-md-4">
                    <label class="form-label">Grace Days <span class="text-danger">*</span></label>
                    <input type="number" name="grace_days" class="form-control" value="<?= e($template['grace_days'] ?? '7') ?>" required>
                </div>
                <div class="col-md-4 d-flex align-items-end">
                    <div class="form-check">
                        <input class="form-check-input" type="checkbox" name="is_active" value="1" <?= checked(1, $template['is_active'] ?? 1) ?>>
                        <label class="form-check-label">Active</label>
                    </div>
                </div>
            </div>
            <div class="mt-4 d-flex gap-2">
                <button type="submit" class="btn btn-primary"><i class="fas fa-save me-1"></i><?= lang('common.save') ?></button>
                <a href="/advanced-finance/installment-templates" class="btn btn-secondary"><?= lang('common.cancel') ?></a>
            </div>
        </form>
    </div>
</div>
</div>
</div>