<?php $isEdit = !empty($channel); ?>
<div class="row justify-content-center">
    <div class="col-lg-8">
        <div class="card">
            <div class="card-header"><h5 class="mb-0"><?= $isEdit ? lang('comm.edit_channel') : lang('comm.create_channel') ?></h5></div>
            <div class="card-body">
                <form method="POST" action="<?= $isEdit ? '/communications/channels/' . $channel['id'] : '/communications/channels' ?>" data-ajax="true">
                    <?= csrf_field() ?>
                    <div class="row g-3">
                        <div class="col-md-4">
                            <label class="form-label">Channel Code <span class="text-danger">*</span></label>
                            <input type="text" name="channel_code" class="form-control" value="<?= e($channel['channel_code'] ?? '') ?>" required>
                            <div class="invalid-feedback" data-field="channel_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($channel['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($channel['name_ar'] ?? '') ?>" required dir="rtl">
                            <div class="invalid-feedback" data-field="name_ar"></div>
                        </div>
                    </div>
                    <div class="row g-3 mt-1">
                        <div class="col-md-4">
                            <label class="form-label">Channel Type <span class="text-danger">*</span></label>
                            <select name="channel_type" class="form-select" required>
                                <?php foreach (['email','sms','push','whatsapp','in_app','webhook'] as $t): ?>
                                <option value="<?= $t ?>" <?= selected($t, $channel['channel_type'] ?? '') ?>><?= ucfirst($t) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Provider</label>
                            <input type="text" name="provider" class="form-control" value="<?= e($channel['provider'] ?? '') ?>">
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">&nbsp;</label>
                            <div class="form-check mt-2">
                                <input class="form-check-input" type="checkbox" name="is_active" value="1" id="ch_active" <?= checked(1, $channel['is_active'] ?? 1) ?>>
                                <label class="form-check-label" for="ch_active">Active</label>
                            </div>
                        </div>
                    </div>
                    <div class="mt-3">
                        <label class="form-label">Configuration (JSON)</label>
                        <textarea name="config" class="form-control font-monospace" rows="5" placeholder='{"api_key": "...", "sender": "..."}'><?= e($channel['config'] ?? '') ?></textarea>
                    </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="/communications/channels" class="btn btn-secondary"><?= lang('common.cancel') ?></a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>