<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>

<style>
.ar-page-header { display:flex; justify-content:space-between; align-items:flex-start; flex-wrap:wrap; gap:14px; margin-bottom:24px; }
.ar-header-left h4 { font-size:1.25rem; font-weight:800; color:#0b5a52; margin:0 0 5px; display:flex; align-items:center; gap:11px; }
.ar-header-left h4 .hi { width:40px; height:40px; border-radius:11px; background:linear-gradient(135deg,#0d9488,#14b8a6); display:flex; align-items:center; justify-content:center; color:#fff; font-size:1.05rem; box-shadow:0 4px 12px rgba(13,148,136,0.32); flex-shrink:0; }
.ar-breadcrumb { display:flex; align-items:center; gap:6px; font-size:0.77rem; color:#94a3b8; padding-left:51px; }
.ar-breadcrumb a { color:#0d9488; text-decoration:none; font-weight:600; }
.form-label { font-size:.83rem; font-weight:700; color:#475569; margin-bottom:5px; }
.form-control, .form-select { border-radius:10px; border:1.5px solid #e2e8f0; font-size:.88rem; padding:9px 14px; transition:border-color .2s; }
.form-control:focus, .form-select:focus { border-color:#0d9488; box-shadow:0 0 0 3px rgba(13,148,136,.12); outline:none; }
.section-title { font-size:.8rem; font-weight:800; color:#0d9488; text-transform:uppercase; letter-spacing:.7px; margin-bottom:14px; padding-bottom:8px; border-bottom:2px solid #f0fdf9; }
.btn-simpan { padding:11px 32px; border-radius:50px; background:#0d9488; color:#fff; border:none; font-weight:700; font-size:.88rem; box-shadow:0 3px 12px rgba(13,148,136,0.28); transition:all .2s; }
.btn-simpan:hover { background:#0b7a6e; transform:translateY(-1px); }
</style>

<!-- Header -->
<div class="ar-page-header">
    <div class="ar-header-left">
        <h4>
            <span class="hi"><i class="bi bi-pencil-square"></i></span>
            Edit Rencana Pembelian
        </h4>
        <div class="ar-breadcrumb">
            <a href="<?php echo base_url('dashboard'); ?>">Dashboard</a>
            <span>/</span>
            <a href="<?php echo base_url('rencana-pembelian'); ?>">Rencana Pembelian</a>
            <span>/</span>
            <span>Edit</span>
        </div>
    </div>
</div>

<div class="card border-0 shadow-sm" style="border-radius:14px; max-width:780px;">
    <div class="card-body p-4">

        <!-- Kode rencana info -->
        <div class="alert border-0 mb-4 d-flex align-items-center gap-2" style="background:#f0fdf9; border-radius:10px;">
            <i class="bi bi-tag" style="color:#0d9488;"></i>
            <span style="font-size:.85rem;">Kode: <strong style="color:#0d9488;"><?php echo $item->kode_rencana; ?></strong></span>
        </div>

        <?php if (validation_errors()): ?>
        <div class="alert alert-danger border-0 rounded-3 mb-4">
            <?php echo validation_errors('<div class="d-flex align-items-center gap-2 mb-1"><i class="bi bi-x-circle"></i>', '</div>'); ?>
        </div>
        <?php endif; ?>

        <?php echo form_open('rencana-pembelian/edit/' . $item->id); ?>

        <!-- Informasi Barang -->
        <div class="section-title"><i class="bi bi-box me-2"></i>Informasi Barang</div>
        <div class="row g-3 mb-4">
            <div class="col-md-8">
                <label class="form-label">Nama Barang <span class="text-danger">*</span></label>
                <input type="text" name="nama_barang" class="form-control"
                       value="<?php echo set_value('nama_barang', $item->nama_barang); ?>" required>
            </div>
            <div class="col-md-4">
                <label class="form-label">Kategori</label>
                <select name="kategori" class="form-select">
                    <?php foreach (['elektronik'=>'💻 Elektronik','furniture'=>'🪑 Furniture','kendaraan'=>'🚗 Kendaraan','perlengkapan'=>'🔧 Perlengkapan','lainnya'=>'📦 Lainnya'] as $v => $l): ?>
                    <option value="<?php echo $v; ?>" <?php echo set_select('kategori', $v, $item->kategori == $v); ?>><?php echo $l; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-4">
                <label class="form-label">Jumlah <span class="text-danger">*</span></label>
                <input type="number" name="jumlah" class="form-control" min="1"
                       value="<?php echo set_value('jumlah', $item->jumlah); ?>" required>
            </div>
            <div class="col-md-4">
                <label class="form-label">Satuan</label>
                <input type="text" name="satuan" class="form-control"
                       value="<?php echo set_value('satuan', $item->satuan); ?>">
            </div>
            <div class="col-md-4">
                <label class="form-label">Prioritas</label>
                <select name="prioritas" class="form-select">
                    <?php foreach (['tinggi'=>'🔴 Tinggi','sedang'=>'🟡 Sedang','rendah'=>'🟢 Rendah'] as $v => $l): ?>
                    <option value="<?php echo $v; ?>" <?php echo set_select('prioritas', $v, $item->prioritas == $v); ?>><?php echo $l; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>

        <!-- Anggaran -->
        <div class="section-title"><i class="bi bi-cash-coin me-2"></i>Anggaran</div>
        <div class="row g-3 mb-4">
            <div class="col-md-6">
                <label class="form-label">Estimasi Harga Satuan</label>
                <div class="input-group">
                    <span class="input-group-text" style="border-radius:10px 0 0 10px; border:1.5px solid #e2e8f0; background:#f8fafc; font-size:.85rem;">Rp</span>
                    <input type="text" name="estimasi_harga" id="estimasi_harga" class="form-control"
                           value="<?php echo set_value('estimasi_harga', number_format($item->estimasi_harga, 0, ',', '.')); ?>"
                           style="border-radius:0 10px 10px 0;" oninput="formatRibuan(this)">
                </div>
            </div>
            <div class="col-md-6">
                <label class="form-label">Sumber Dana Rencana</label>
                <input type="text" name="sumber_dana" class="form-control"
                       value="<?php echo set_value('sumber_dana', $item->sumber_dana); ?>">
            </div>
        </div>

        <!-- Status (boleh ubah) -->
        <div class="section-title"><i class="bi bi-flag me-2"></i>Status</div>
        <div class="row g-3 mb-4">
            <div class="col-md-5">
                <label class="form-label">Status Rencana</label>
                <select name="status" class="form-select">
                    <?php foreach (['direncanakan'=>'Direncanakan','disetujui'=>'Disetujui','dibatalkan'=>'Dibatalkan'] as $v => $l): ?>
                    <option value="<?php echo $v; ?>" <?php echo set_select('status', $v, $item->status == $v); ?>><?php echo $l; ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-5">
                <label class="form-label">Tanggal Rencana</label>
                <input type="date" name="tanggal_rencana" class="form-control"
                       value="<?php echo set_value('tanggal_rencana', $item->tanggal_rencana); ?>">
            </div>
        </div>

        <!-- Keterangan -->
        <div class="mb-4">
            <label class="form-label">Keterangan</label>
            <textarea name="keterangan" class="form-control" rows="3"><?php echo set_value('keterangan', $item->keterangan); ?></textarea>
        </div>

        <div class="d-flex gap-3 align-items-center">
            <button type="submit" class="btn-simpan">
                <i class="bi bi-check-lg me-2"></i>Simpan Perubahan
            </button>
            <a href="<?php echo base_url('rencana-pembelian'); ?>" class="text-muted fw-semibold text-decoration-none" style="font-size:.88rem;">
                <i class="bi bi-arrow-left me-1"></i>Kembali
            </a>
        </div>

        </form>
    </div>
</div>

<script>
function formatRibuan(el) {
    let v = el.value.replace(/\D/g, '');
    el.value = v ? parseInt(v).toLocaleString('id-ID') : '';
}
</script>