<?php
$pelanggan = $pelanggan ?? [];
$kodePelangganValue = $pelanggan['kode_pelanggan'] ?? ($kodePelanggan ?? 'Otomatis');
?>

<section class="content-area">
    <?php $errors = session()->getFlashdata('errors'); ?>
    <?php if ($errors) : ?>
        <div class="alert alert-danger">
            <?php foreach ($errors as $error) : ?>
                <div><?= esc($error) ?></div>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>

    <div class="panel p-4">
        <form action="<?= esc($action, 'attr') ?>" method="post">
            <?= csrf_field() ?>

            <div class="row g-4">
                <div class="col-12 col-lg-4">
                    <label for="kode_pelanggan" class="form-label fw-semibold">Kode Pelanggan</label>
                    <input type="text" class="form-control" id="kode_pelanggan" value="<?= esc($kodePelangganValue, 'attr') ?>" readonly>
                    <div class="form-text">Kode dibuat otomatis oleh sistem.</div>
                </div>

                <div class="col-12 col-lg-6">
                    <label for="nama" class="form-label fw-semibold">Nama</label>
                    <input type="text" class="form-control" id="nama" name="nama" value="<?= esc(old('nama', $pelanggan['nama'] ?? ''), 'attr') ?>" required>
                </div>

                <div class="col-12 col-lg-2">
                    <label for="no_hp" class="form-label fw-semibold">No. HP</label>
                    <input type="text" class="form-control" id="no_hp" name="no_hp" value="<?= esc(old('no_hp', $pelanggan['no_hp'] ?? ''), 'attr') ?>" required>
                </div>

                <div class="col-12">
                    <label for="alamat" class="form-label fw-semibold">Alamat</label>
                    <textarea class="form-control" id="alamat" name="alamat" rows="4" required><?= esc(old('alamat', $pelanggan['alamat'] ?? '')) ?></textarea>
                </div>
            </div>

            <div class="d-flex justify-content-end gap-2 mt-4">
                <a href="<?= site_url('admin/pelanggan') ?>" class="btn btn-light">Batal</a>
                <button type="submit" class="btn btn-dark"><?= esc($submitLabel ?? 'Simpan') ?></button>
            </div>
        </form>
    </div>
</section>
