<?= $this->extend('admin/layout/main') ?>
<?= $this->section('content') ?>
<h3 class="mb-3"><?= $service ? lang('App.edit') : lang('App.add_new') ?> - <?= lang('App.services') ?></h3>
<div class="card p-4" style="max-width:600px;">
<form method="post" action="<?= $service ? '/admin/services/' . $service['id'] : '/admin/services' ?>">
    <?= csrf_field() ?>
    <div class="mb-3">
        <label class="form-label"><?= lang('App.client') ?></label>
        <select name="client_id" class="form-select" required>
            <?php foreach ($clients as $c): ?>
                <option value="<?= $c['id'] ?>" <?= ($service['client_id'] ?? null) == $c['id'] ? 'selected' : '' ?>><?= esc($c['name']) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="mb-3">
        <label class="form-label">النوع</label>
        <select name="type" class="form-select">
            <?php foreach (['hosting' => lang('App.hosting'), 'domain' => lang('App.domain'), 'other' => lang('App.other')] as $val => $label): ?>
                <option value="<?= $val ?>" <?= ($service['type'] ?? '') === $val ? 'selected' : '' ?>><?= $label ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="mb-3">
        <label class="form-label"><?= lang('App.name') ?></label>
        <input type="text" name="name" class="form-control" value="<?= esc($service['name'] ?? '') ?>" required>
    </div>
    <div class="row">
        <div class="col-6 mb-3">
            <label class="form-label"><?= lang('App.amount') ?></label>
            <input type="number" step="0.01" name="price" class="form-control" value="<?= esc($service['price'] ?? '') ?>" required>
        </div>
        <div class="col-6 mb-3">
            <label class="form-label"><?= lang('App.currency') ?></label>
            <select name="currency" class="form-select">
                <?php foreach ($currencies as $curr): $cur = $curr['code']; ?>
                    <option <?= ($service['currency'] ?? $currencies[0]['code']) === $cur ? 'selected' : '' ?>><?= $cur ?><?= !empty($curr['name']) ? ' - ' . esc($curr['name']) : '' ?></option>
                <?php endforeach; ?>
            </select>
        </div>
    </div>
    <div class="mb-3">
        <label class="form-label">دورة الفوترة</label>
        <select name="billing_cycle" class="form-select">
            <?php foreach (['monthly' => 'شهري', 'quarterly' => 'ربع سنوي', 'semi_annual' => 'نصف سنوي', 'annual' => 'سنوي'] as $val => $label): ?>
                <option value="<?= $val ?>" <?= ($service['billing_cycle'] ?? 'annual') === $val ? 'selected' : '' ?>><?= $label ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="row">
        <div class="col-6 mb-3">
            <label class="form-label">تاريخ البداية</label>
            <input type="date" name="start_date" class="form-control" value="<?= esc($service['start_date'] ?? '') ?>" required>
        </div>
        <div class="col-6 mb-3">
            <label class="form-label"><?= lang('App.renewal_date') ?></label>
            <input type="date" name="renewal_date" class="form-control" value="<?= esc($service['renewal_date'] ?? '') ?>" required>
        </div>
    </div>
    <div class="mb-3">
        <label class="form-label"><?= lang('App.status') ?></label>
        <select name="status" class="form-select">
            <?php foreach (['active' => lang('App.active'), 'expired' => lang('App.expired'), 'cancelled' => lang('App.cancelled'), 'suspended' => lang('App.suspended')] as $val => $label): ?>
                <option value="<?= $val ?>" <?= ($service['status'] ?? 'active') === $val ? 'selected' : '' ?>><?= $label ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div class="mb-3">
        <label class="form-label">ملاحظات</label>
        <textarea name="notes" class="form-control"><?= esc($service['notes'] ?? '') ?></textarea>
    </div>
    <button class="btn btn-dark"><?= lang('App.save') ?></button>
    <a href="/admin/services" class="btn btn-light"><?= lang('App.cancel') ?></a>
</form>
</div>
<?= $this->endSection() ?>
