<?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,#198754,#34d399); display:flex; align-items:center; justify-content:center; color:#fff; font-size:1.05rem; box-shadow:0 4px 12px rgba(25,135,84,.32); flex-shrink:0; }
.ar-breadcrumb { display:flex; align-items:center; gap:6px; font-size:.77rem; color:#94a3b8; padding-left:51px; }
.ar-breadcrumb a { color:#198754; text-decoration:none; font-weight:600; }
.form-card { background:#fff; border-radius:14px; box-shadow:0 1px 8px rgba(0,0,0,.07); border:none; }
.form-label { font-size:.83rem; font-weight:700; color:#374151; margin-bottom:5px; }
.form-control, .form-select { border-radius:9px; border:1.5px solid #e5e7eb; font-size:.875rem; padding:.55rem .85rem; }
.form-control:focus, .form-select:focus { border-color:#198754; box-shadow:0 0 0 3px rgba(25,135,84,.12); }
.btn-simpan { padding:11px 36px; border-radius:50px; background:#198754; border:none; color:#fff; font-weight:700; font-size:.88rem; box-shadow:0 3px 12px rgba(25,135,84,.25); transition:all .2s; }
.btn-simpan:hover { background:#157347; transform:translateY(-1px); }
.info-row { display:flex; padding:8px 0; border-bottom:1px solid #f3f4f6; font-size:.85rem; }
.info-row:last-child { border-bottom:none; }
.info-label { color:#6b7280; font-weight:600; width:160px; flex-shrink:0; }
</style>

<div class="ar-page-header">
    <div class="ar-header-left">
        <h4>
            <span class="hi"><i class="bi bi-box-arrow-in-down"></i></span>
            Proses Pengembalian
        </h4>
        <div class="ar-breadcrumb">
            <a href="<?php echo base_url('peminjaman-inventaris'); ?>">Peminjaman</a>
            <span>/</span>
            <a href="<?php echo base_url('peminjaman-inventaris/detail/'.$pinjam->id); ?>"><?php echo $pinjam->kode_pinjam; ?></a>
            <span>/</span>
            <span>Kembalikan</span>
        </div>
    </div>
</div>

<?php
$terlambat = ($pinjam->tanggal_rencana_kembali < date('Y-m-d'));
if($terlambat):
?>
<div class="alert border-0 rounded-3 mb-4 d-flex align-items-center gap-3" style="background:#fff5f5;border-left:4px solid #dc3545!important;">
    <i class="bi bi-exclamation-triangle-fill text-danger fs-4"></i>
    <div>
        <strong class="text-danger">Peminjaman ini sudah terlambat!</strong>
        <div style="font-size:.83rem;color:#991b1b;">Rencana kembali: <?php echo date('d M Y', strtotime($pinjam->tanggal_rencana_kembali)); ?></div>
    </div>
</div>
<?php endif; ?>

<div class="row g-4">
    <!-- Form Kembali -->
    <div class="col-lg-7">
        <div class="form-card p-4">
            <h6 class="fw-bold mb-4" style="color:#374151;"><i class="bi bi-pencil-square me-2" style="color:#198754;"></i>Form Pengembalian</h6>

            <?php echo form_open('peminjaman-inventaris/kembali/'.$pinjam->id); ?>

            <div class="mb-3">
                <label class="form-label">Tanggal Kembali <span class="text-danger">*</span></label>
                <input type="date" name="tanggal_kembali" class="form-control"
                       value="<?php echo set_value('tanggal_kembali', date('Y-m-d')); ?>" required>
                <?php echo form_error('tanggal_kembali', '<small class="text-danger">', '</small>'); ?>
            </div>

            <div class="mb-3">
                <label class="form-label">Kondisi Barang Saat Dikembalikan <span class="text-danger">*</span></label>
                <select name="kondisi_kembali" class="form-select" required>
                    <option value="">— Pilih Kondisi —</option>
                    <option value="baik" <?php echo set_select('kondisi_kembali','baik'); ?>>✅ Baik</option>
                    <option value="rusak_ringan" <?php echo set_select('kondisi_kembali','rusak_ringan'); ?>>⚠️ Rusak Ringan</option>
                    <option value="rusak_berat" <?php echo set_select('kondisi_kembali','rusak_berat'); ?>>❌ Rusak Berat</option>
                </select>
                <?php echo form_error('kondisi_kembali', '<small class="text-danger">', '</small>'); ?>
            </div>

            <div class="mb-4">
                <label class="form-label">Catatan Pengembalian</label>
                <textarea name="catatan_kembali" class="form-control" rows="3"
                          placeholder="Catatan kondisi, kerusakan, atau hal lain..."><?php echo set_value('catatan_kembali'); ?></textarea>
            </div>

            <div class="d-flex gap-2">
                <a href="<?php echo base_url('peminjaman-inventaris/detail/'.$pinjam->id); ?>"
                   class="btn btn-outline-secondary rounded-pill px-4">Batal</a>
                <button type="submit" class="btn-simpan">
                    <i class="bi bi-check-lg me-1"></i>Konfirmasi Pengembalian
                </button>
            </div>

            <?php echo form_close(); ?>
        </div>
    </div>

    <!-- Ringkasan Peminjaman -->
    <div class="col-lg-5">
        <div class="form-card p-4">
            <h6 class="fw-bold mb-3" style="color:#374151;">📋 Ringkasan Peminjaman</h6>

            <div class="info-row"><span class="info-label">Kode</span><span class="fw-bold" style="color:#7c3aed;"><?php echo $pinjam->kode_pinjam; ?></span></div>
            <div class="info-row"><span class="info-label">Barang</span><span class="fw-semibold"><?php echo htmlspecialchars($pinjam->nama_barang ?? 'Barang Dihapus'); ?></span></div>
            <div class="info-row"><span class="info-label">Jumlah</span><span><?php echo $pinjam->jumlah_pinjam . ' ' . $pinjam->satuan; ?></span></div>
            <div class="info-row"><span class="info-label">Peminjam</span><span><?php echo htmlspecialchars($pinjam->nama_peminjam ?? 'Tidak Diketahui'); ?></span></div>
            <div class="info-row"><span class="info-label">Tgl Pinjam</span><span><?php echo date('d M Y', strtotime($pinjam->tanggal_pinjam)); ?></span></div>
            <div class="info-row">
                <span class="info-label">Rencana Kembali</span>
                <span class="<?php echo $terlambat ? 'text-danger fw-bold' : ''; ?>">
                    <?php echo date('d M Y', strtotime($pinjam->tanggal_rencana_kembali)); ?>
                </span>
            </div>
            <div class="info-row"><span class="info-label">Kondisi Awal</span>
                <span><?php
                    $k = ['baik'=>'✅ Baik','rusak_ringan'=>'⚠️ Rusak Ringan','rusak_berat'=>'❌ Rusak Berat'];
                    echo $k[$pinjam->kondisi_awal] ?? $pinjam->kondisi_awal;
                ?></span>
            </div>
        </div>
    </div>
</div>