<?php
session_start();
include '../koneksi.php';
$is_input_pengeluaran = (isset($_SESSION['status']) && $_SESSION['status'] === 'input_pengeluaran_logedin');

$id = isset($_GET['id']) ? intval($_GET['id']) : 0;

if ($id > 0) {
    $query = "SELECT t.*, k.kategori FROM transaksi t 
              INNER JOIN kategori k ON k.kategori_id = t.transaksi_kategori 
              WHERE t.transaksi_id = $id";
    $result = mysqli_query($koneksi, $query);
    $d = mysqli_fetch_assoc($result);
    
    if ($d) {
        if ($is_input_pengeluaran && $d['transaksi_jenis'] !== 'Pengeluaran') {
            echo '<div class="modal-body"><p class="text-danger">Anda hanya dapat mengedit transaksi pengeluaran.</p></div>';
            return;
        }
        ?>
        <div class="modal-header">
            <h4 class="modal-title" id="exampleModalLabel">Edit transaksi</h4>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
        <form id="formEditTransaksi" action="transaksi_update.php" method="post" enctype="multipart/form-data">
            <div class="modal-body">
                <input type="hidden" name="id" value="<?php echo $d['transaksi_id'] ?>">
                
                <div class="form-group">
                    <label>Tanggal</label>
                    <input type="text" name="tanggal" required="required" class="form-control datepicker2" placeholder="dd/mm/yyyy" value="<?php 
                    $tgl = $d['transaksi_tanggal'];
                    if (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $tgl, $m)) { echo $m[3].'/'.$m[2].'/'.$m[1]; } else { echo htmlspecialchars($tgl); }
                    ?>">
                </div>

                <div class="form-group">
                    <label>Jenis</label>
                    <?php if ($is_input_pengeluaran) { ?>
                    <input type="hidden" name="jenis" value="Pengeluaran">
                    <input type="text" class="form-control" value="Pengeluaran" readonly disabled>
                    <?php } else { ?>
                    <select name="jenis" class="form-control" required="required">
                        <option value="">- Pilih -</option>
                        <option <?php if($d['transaksi_jenis'] == "Pemasukan"){echo "selected='selected'";} ?> value="Pemasukan">Pemasukan</option>
                        <option <?php if($d['transaksi_jenis'] == "Pengeluaran"){echo "selected='selected'";} ?> value="Pengeluaran">Pengeluaran</option>
                    </select>
                    <?php } ?>
                </div>

                <div class="form-group">
                    <label>Kategori</label>
                    <select name="kategori" class="form-control select2-form" required="required" data-placeholder="Cari atau pilih kategori...">
                        <option value="">- Pilih Kategori -</option>
                        <?php 
                        $kategori = mysqli_query($koneksi,"SELECT * FROM kategori ORDER BY kategori ASC");
                        while($k = mysqli_fetch_array($kategori)){
                            ?>
                            <option <?php if($d['transaksi_kategori'] == $k['kategori_id']){echo "selected='selected'";} ?> value="<?php echo $k['kategori_id']; ?>"><?php echo htmlspecialchars($k['kategori']); ?></option>
                            <?php 
                        }
                        ?>
                    </select>
                </div>

                <div class="form-group">
                    <label>Nominal</label>
                    <input type="text" name="nominal" required="required" class="form-control input-rupiah" placeholder="0" value="<?php 
                    $n = (float)($d['transaksi_nominal'] ?? 0); 
                    echo $n > 0 ? 'Rp. ' . number_format($n, 0, '', '.') : ''; 
                    ?>" autocomplete="off">
                </div>

                <div class="form-group">
                    <label>Keterangan</label>
                    <textarea name="keterangan" class="form-control" rows="3"><?php echo $d['transaksi_keterangan'] ?></textarea>
                </div>

                <div class="form-group">
                    <label>Bukti Upload <small class="text-muted">(Opsional)</small></label>
                    <input type="file" name="trnfoto" class="form-control">
                    <?php if(!empty($d['transaksi_foto'])){ ?>
                        <p class="help-block">Bila file <a class="btn btn-xs btn-primary" target="_blank" href="../gambar/bukti/<?php echo htmlspecialchars($d['transaksi_foto']); ?>"><?php echo htmlspecialchars($d['transaksi_foto']); ?></a> tidak diubah, kosongkan saja.</p>
                    <?php } ?>
                </div>

                <div class="form-group">
                    <label>Rekening Bank</label>
                    <select name="bank" class="form-control select2-form" required="required" data-placeholder="Cari atau pilih rekening...">
                        <option value="">- Pilih Rekening Bank -</option>
                        <?php 
                        $bank = mysqli_query($koneksi,"SELECT * FROM bank ORDER BY bank_nama ASC");
                        while($b = mysqli_fetch_array($bank)){
                            $label = $b['bank_nama'];
                            if (!empty($b['bank_nomor'])) $label .= ' - ' . $b['bank_nomor'];
                            ?>
                            <option <?php if($d['transaksi_bank'] == $b['bank_id']){echo "selected='selected'";} ?> value="<?php echo $b['bank_id']; ?>"><?php echo htmlspecialchars($label); ?></option>
                            <?php 
                        }
                        ?>
                    </select>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Tutup</button>
                <button type="submit" class="btn btn-primary">Simpan</button>
            </div>
        </form>
        <?php
    }
}
?>
