<?= view('admin/layouts/header', ['title' => 'Setting Aplikasi']) ?>
<?php $canManageSetting = ($userRole ?? '') === 'administrator'; ?>
<?= view('admin/layouts/sidebar', [
    'activeMenu' => 'setting',
    'userName'   => $userName,
    'userEmail'  => $userEmail,
]) ?>
<?= view('admin/layouts/topbar', [
    'pageTitle'    => 'Setting Aplikasi',
    'pageSubtitle' => 'Perbarui identitas tampilan dan judul aplikasi.',
    'userName'     => $userName,
    'userRole'     => $userRole,
]) ?>

<section class="content-area">
    <div class="row justify-content-center">
        <div class="col-12 col-xl-12">
            <?php if (session()->getFlashdata('success')) : ?>
                <div class="alert alert-success"><?= esc(session()->getFlashdata('success')) ?></div>
            <?php endif; ?>

            <?php if (session()->getFlashdata('error')) : ?>
                <div class="alert alert-danger"><?= esc(session()->getFlashdata('error')) ?></div>
            <?php endif; ?>

            <?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="<?= site_url('admin/setting/update') ?>" method="post" enctype="multipart/form-data">
                    <?= csrf_field() ?>

                    <div class="row g-4">
                        <div class="col-12 col-lg-6">
                            <div class="fw-semibold mb-2">Logo Saat Ini</div>
                            <?php if (! empty($setting['logo'])) : ?>
                                <img src="<?= base_url('uploads/settings/' . $setting['logo']) ?>" alt="Logo <?= esc($setting['nama_aplikasi']) ?>" class="preview-photo mb-3">
                            <?php else : ?>
                                <div class="photo-placeholder preview-placeholder mb-3">
                                    <?= esc(strtoupper(substr((string) $setting['nama_aplikasi'], 0, 1))) ?>
                                </div>
                            <?php endif; ?>

                            <!-- <label for="logo" class="form-label fw-semibold">Upload Logo</label> -->
                            <input type="file" class="filepond-upload" id="logo" name="logo_upload" accept=".jpg,.jpeg,.png,.webp,image/*" data-temp-field="logo_temp" data-upload-url="<?= site_url('admin/uploads/temp/settings-logo') ?>" <?= $canManageSetting ? '' : 'disabled' ?>>
                            <input type="hidden" name="logo_temp" value="<?= esc((string) old('logo_temp'), 'attr') ?>">
                            <div class="form-text">Kosongkan jika logo tidak ingin diganti. Format jpg, jpeg, png, atau webp. Maksimal 2 MB.</div>
                        </div>

                        <div class="col-12 col-lg-6">
                            <div class="fw-semibold mb-2">Favicon Saat Ini</div>
                            <?php if (! empty($setting['favicon'])) : ?>
                                <img src="<?= base_url('uploads/settings/' . $setting['favicon']) ?>" alt="Favicon <?= esc($setting['nama_aplikasi']) ?>" class="preview-photo mb-3">
                            <?php else : ?>
                                <img src="<?= base_url('favicon.ico') ?>" alt="Favicon bawaan" class="preview-photo mb-3">
                            <?php endif; ?>

                            <!-- <label for="favicon" class="form-label fw-semibold">Upload Favicon</label> -->
                            <input type="file" class="filepond-upload" id="favicon" name="favicon_upload" accept=".ico,.jpg,.jpeg,.png,.webp,image/*" data-temp-field="favicon_temp" data-upload-url="<?= site_url('admin/uploads/temp/settings-favicon') ?>" <?= $canManageSetting ? '' : 'disabled' ?>>
                            <input type="hidden" name="favicon_temp" value="<?= esc((string) old('favicon_temp'), 'attr') ?>">
                            <div class="form-text">Format ico, jpg, jpeg, png, atau webp. Maksimal 2 MB.</div>
                        </div>

                        <div class="col-12 col-lg-6">
                            <label for="nama_aplikasi" class="form-label fw-semibold">Nama Aplikasi</label>
                            <input type="text" class="form-control" id="nama_aplikasi" name="nama_aplikasi" value="<?= old('nama_aplikasi', $setting['nama_aplikasi']) ?>" <?= $canManageSetting ? 'required' : 'readonly' ?>>
                        </div>

                        <div class="col-12 col-lg-6">
                            <label for="judul_aplikasi" class="form-label fw-semibold">Judul Aplikasi</label>
                            <input type="text" class="form-control" id="judul_aplikasi" name="judul_aplikasi" value="<?= old('judul_aplikasi', $setting['judul_aplikasi'] ?? $setting['nama_aplikasi']) ?>" <?= $canManageSetting ? 'required' : 'readonly' ?>>
                            <div class="form-text">Digunakan sebagai judul pada tab browser.</div>
                        </div>

                        <div class="col-12 col-lg-6">
                            <label for="nama_perusahaan" class="form-label fw-semibold">Nama Toko / Perusahaan</label>
                            <input type="text" class="form-control" id="nama_perusahaan" name="nama_perusahaan" value="<?= old('nama_perusahaan', $setting['nama_perusahaan']) ?>" <?= $canManageSetting ? '' : 'readonly' ?>>
                        </div>


                        <div class="col-12 col-lg-6">
                            <label for="tagline" class="form-label fw-semibold">Tagline</label>
                            <input type="text" class="form-control" id="tagline" name="tagline" value="<?= old('tagline', $setting['tagline']) ?>" placeholder="Contoh: Sistem administrasi perusahaan" <?= $canManageSetting ? '' : 'readonly' ?>>
                        </div>

                        <div class="col-12 col-lg-4">
                            <label for="pajak_persen" class="form-label fw-semibold">Pajak (%)</label>
                            <input type="number" class="form-control" id="pajak_persen" name="pajak_persen" value="<?= esc((string) old('pajak_persen', $setting['pajak_persen'] ?? 11), 'attr') ?>" min="0" max="100" step="1" <?= $canManageSetting ? 'required' : 'readonly' ?>>
                            <div class="form-text">Isi 0 jika tidak memakai pajak. Nilai ini menjadi default pajak transaksi baru.</div>
                        </div>

                        <div class="col-12 col-lg-4">
                            <label for="nota_penjualan_ukuran" class="form-label fw-semibold">Ukuran Nota Penjualan</label>
                            <?php $notaPenjualanUkuran = old('nota_penjualan_ukuran', $setting['nota_penjualan_ukuran'] ?? 'thermal_58'); ?>
                            <select class="form-select" id="nota_penjualan_ukuran" name="nota_penjualan_ukuran" <?= $canManageSetting ? 'required' : 'disabled' ?>>
                                <option value="thermal_58" <?= $notaPenjualanUkuran === 'thermal_58' ? 'selected' : '' ?>>Printer Thermal 58mm</option>
                                <option value="thermal_80" <?= $notaPenjualanUkuran === 'thermal_80' ? 'selected' : '' ?>>Printer Thermal 80mm</option>
                                <option value="a4" <?= $notaPenjualanUkuran === 'a4' ? 'selected' : '' ?>>A4 PDF</option>
                                <option value="continuous_form" <?= $notaPenjualanUkuran === 'continuous_form' ? 'selected' : '' ?>>Continuous Form Paper PDF</option>
                            </select>
                            <?php if (! $canManageSetting) : ?>
                                <input type="hidden" name="nota_penjualan_ukuran" value="<?= esc((string) $notaPenjualanUkuran, 'attr') ?>">
                            <?php endif; ?>
                            <div class="form-text">A4 dan continuous form akan langsung membuka nota dalam format PDF.</div>
                        </div>
                    </div>

                    <div class="d-flex justify-content-end gap-2 mt-4">
                        <a href="<?= site_url('admin/dashboard') ?>" class="btn btn-light">Batal</a>
                        <?php if ($canManageSetting) : ?>
                            <button type="submit" class="btn btn-dark">
                                <i class="bi bi-check2-circle me-1"></i>Simpan Setting
                            </button>
                        <?php endif; ?>
                    </div>
                </form>
            </div>
        </div>
    </div>
</section>

<?= view('admin/layouts/footer') ?>
