<?php
$teknisi = $teknisi ?? [];
$kodeTeknisiValue = $teknisi['kode_teknisi'] ?? ($kodeTeknisi ?? '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" enctype="multipart/form-data">
            <?= csrf_field() ?>

            <?php if (! empty($teknisi['foto'])) : ?>
                <div class="mb-4">
                    <div class="fw-semibold mb-2">Foto Saat Ini</div>
                    <img src="<?= base_url('uploads/teknisi/' . $teknisi['foto']) ?>" alt="Foto <?= esc($teknisi['nama'] ?? '-') ?>" class="preview-photo">
                </div>
            <?php endif; ?>

            <div class="row g-4">
                <div class="col-12 col-lg-4">
                    <label for="kode_teknisi" class="form-label fw-semibold">Kode Teknisi</label>
                    <input type="text" class="form-control" id="kode_teknisi" value="<?= esc($kodeTeknisiValue, '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', $teknisi['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', $teknisi['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', $teknisi['alamat'] ?? '')) ?></textarea>
                </div>

                <div class="col-12">
                    <label for="foto" class="form-label fw-semibold"><?= empty($teknisi['foto']) ? 'Foto Teknisi' : 'Ganti Foto Teknisi' ?></label>
                    <input type="file" class="filepond-upload" id="foto" name="foto_upload" accept=".jpg,.jpeg,.png,.webp,image/*" data-temp-field="foto_temp" data-upload-url="<?= site_url('admin/uploads/temp/teknisi-photo') ?>">
                    <input type="hidden" name="foto_temp" value="<?= esc((string) old('foto_temp'), 'attr') ?>">
                    <div class="form-text">Format jpg, jpeg, png, atau webp. Maksimal 2 MB. Tombol submit otomatis nonaktif saat upload berjalan.</div>
                </div>
            </div>

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