<?php
// Ambil data web
$web = mysqli_fetch_assoc(mysqli_query($koneksi, "SELECT * FROM tb_web LIMIT 1"));

// FIX PATH KHUSUS STRUKTUR FOLDER DASH/TEMPLATE/ (Naik 2 Tingkat)
$qris_image = '';

if (!empty($web['img_qris_manual'])) {
    // Naik 2 tingkat ke root public_html/ lalu masuk ke uploads/qris/
    $qris_image = '../../uploads/qris/' . $web['img_qris_manual'];
}

// Jika total kosong, kasih placeholder gambar dummy biar gak kotak hitam kosong
if (empty($qris_image)) {
    $qris_image = 'https://placehold.co/180x180/1a1a2e/ffffff?text=Belum+Ada+QRIS';
}
?>

<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-body" style="padding: 30px;">
                
                <h4 class="card-title" style="margin-bottom: 25px; border-bottom: 1px solid #333; padding-bottom: 15px;">Pengaturan Metode Deposit</h4>
                
                <form action="?page=update_payment" method="POST" enctype="multipart/form-data">
                    
                    <div class="form-group row" style="margin-bottom: 25px;">
                        <label class="col-md-3 col-form-label" style="font-weight: 500;">Status Bank Transfer</label>
                        <div class="col-md-9">
                            <select name="status_bank" class="form-control custom-select">
                                <option value="on" <?= ($web['status_bank'] == 'on') ? 'selected' : '' ?>>✅ ON (Aktif)</option>
                                <option value="off" <?= ($web['status_bank'] == 'off') ? 'selected' : '' ?>>❌ OFF (Maintenance)</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-group row" style="margin-bottom: 25px;">
                        <label class="col-md-3 col-form-label" style="font-weight: 500;">Status QRIS Manual</label>
                        <div class="col-md-9">
                            <select name="status_qris_manual" class="form-control custom-select">
                                <option value="on" <?= ($web['status_qris_manual'] == 'on') ? 'selected' : '' ?>>✅ ON (Aktif)</option>
                                <option value="off" <?= ($web['status_qris_manual'] == 'off') ? 'selected' : '' ?>>❌ OFF (Maintenance)</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-group row" style="margin-bottom: 25px;">
                        <label class="col-md-3 col-form-label" style="font-weight: 500;">Gambar QRIS Manual</label>
                        <div class="col-md-9">
                            
                            <div class="row align-items-center">
                                <div class="col-sm-4 text-center" style="margin-bottom: 15px;">
                                    <div style="background: #1a1a2e; padding: 15px; border-radius: 10px; border: 2px dashed #333; display: inline-block; width: 100%;">
                                        <img id="previewQRIS" src="<?= htmlspecialchars($qris_image) ?>" 
                                             style="max-width: 100%; max-height: 180px; border-radius: 8px; display: block; margin: 0 auto;">
                                    </div>
                                </div>

                                <div class="col-sm-8">
                                    <div style="position: relative; background: #1a1a2e; border: 2px dashed #333; padding: 35px 20px; border-radius: 10px; text-align: center; cursor: pointer;" 
                                         onclick="document.getElementById('fileQRIS').click();">
                                        
                                        <input type="file" name="img_qris_manual" id="fileQRIS" accept="image/*" style="display: none;">
                                        
                                        <div style="font-size: 28px; margin-bottom: 8px;">📸</div>
                                        <span id="labelFile" style="color: #fff; font-weight: 600; display: block;">Upload Gambar QRIS Baru</span>
                                        <small style="color: #888; display: block; margin-top: 5px;">Klik di area ini untuk memilih file gambar (PNG/JPG)</small>
                                    </div>
                                </div>
                            </div>

                        </div>
                    </div>

                    <hr style="border-color: #333; margin: 35px 0;">

                    <h4 class="card-title" style="margin-bottom: 25px;">Pengaturan Auto Deposit (SiTransfer)</h4>

                    <div class="form-group row" style="margin-bottom: 25px;">
                        <label class="col-md-3 col-form-label" style="font-weight: 500;">Status QRIS Auto</label>
                        <div class="col-md-9">
                            <select name="status_qris_auto" class="form-control custom-select">
                                <option value="on" <?= ($web['status_qris_auto'] == 'on') ? 'selected' : '' ?>>✅ ON (Aktif)</option>
                                <option value="off" <?= ($web['status_qris_auto'] == 'off') ? 'selected' : '' ?>>❌ OFF (Maintenance)</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-group row" style="margin-bottom: 35px;">
                        <label class="col-md-3 col-form-label" style="font-weight: 500;">SiTransfer Store Key</label>
                        <div class="col-md-9">
                            <input type="text" name="sitransfer_key" class="form-control" value="<?= htmlspecialchars($web['sitransfer_key']) ?>" required style="font-family: monospace; letter-spacing: 1px;">
                        </div>
                    </div>

                    <div style="text-align: right; padding-top: 15px; border-top: 1px solid #222;">
                        <a href="?page=dashboard" class="btn btn-secondary" style="padding: 10px 25px; margin-right: 10px;">Cancel</a>
                        <button type="submit" class="btn btn-success" style="padding: 10px 35px; font-weight: 600;">Submit</button>
                    </div>

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

<script>
document.getElementById('fileQRIS').addEventListener('change', function(e) {
    if (e.target.files && e.target.files[0]) {
        var file = e.target.files[0];
        
        // Ganti teks info nama file pas dipilih
        document.getElementById('labelFile').innerText = file.name;
        
        // Live preview gambarnya
        var reader = new FileReader();
        reader.onload = function(ev) {
            var img = document.getElementById('previewQRIS');
            img.src = ev.target.result;
        };
        reader.readAsDataURL(file);
    }
});
</script>
