<?php
require_once '../includes/config.php';
requireAdmin();
$db = getDB();
$currencies = $db->query("SELECT * FROM currencies ORDER BY is_default DESC, code")->fetchAll();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Currency Edit Test</title>
<style>
*{margin:0;padding:0;box-sizing:border-box}
body{font-family:sans-serif;padding:20px;background:#f5f7fa}
.btn{padding:8px 14px;background:#1a3c8f;color:white;border:none;border-radius:8px;cursor:pointer;font-size:13px}
table{width:100%;border-collapse:collapse;background:white;border-radius:10px;overflow:hidden;box-shadow:0 2px 8px rgba(0,0,0,0.1)}
th,td{padding:10px 14px;text-align:left;border-bottom:1px solid #f0f0f0;font-size:13px}
th{background:#f8fafc;font-weight:700;color:#555}
.modal-bg{display:none;position:fixed;inset:0;background:rgba(0,0,0,0.5);z-index:9999;align-items:center;justify-content:center}
.modal-bg.on{display:flex}
.modal{background:white;border-radius:14px;padding:24px;width:90%;max-width:440px}
.modal h3{font-size:16px;font-weight:700;margin-bottom:16px}
.fg{display:flex;flex-direction:column;gap:5px;margin-bottom:12px}
label{font-size:12px;font-weight:600;color:#555}
input,select{padding:9px 12px;border:2px solid #e8ecf4;border-radius:9px;font-size:14px;outline:none}
input:focus{border-color:#1a3c8f}
.flex-gap{display:flex;gap:10px;margin-top:4px}
.btn-gray{background:#f5f5f5;color:#333;padding:9px 16px;border:none;border-radius:9px;cursor:pointer;font-size:13px;font-weight:600}
.alert{padding:10px 14px;border-radius:8px;margin-bottom:14px;font-size:13px}
.alert-ok{background:#f0fff4;color:#276749}
.alert-err{background:#fff5f5;color:#c53030}
</style>
</head>
<body>
<h2 style="margin-bottom:16px">Currency Edit Test</h2>

<?php if($_POST['action']??''==='edit'): 
    $id=$_POST['id']; $name=$_POST['name']; $sym=$_POST['symbol']; $rate=$_POST['rate']; $act=$_POST['is_active'];
    $db->prepare("UPDATE currencies SET name=?,symbol=?,rate=?,is_active=? WHERE id=?")->execute([$name,$sym,$rate,$act,$id]);
    echo '<div class="alert alert-ok">✅ Updated!</div>';
    $currencies = $db->query("SELECT * FROM currencies ORDER BY is_default DESC, code")->fetchAll();
endif; ?>

<table>
<tr><th>Code</th><th>Symbol</th><th>Name</th><th>Rate</th><th>Edit</th></tr>
<?php foreach($currencies as $c): ?>
<tr>
    <td><?= $c['code'] ?></td>
    <td><?= htmlspecialchars($c['symbol']) ?></td>
    <td><?= htmlspecialchars($c['name']) ?></td>
    <td><?= $c['rate'] ?></td>
    <td>
        <button class="btn" 
            data-id="<?= $c['id'] ?>"
            data-code="<?= htmlspecialchars($c['code'],ENT_QUOTES) ?>"
            data-sym="<?= htmlspecialchars($c['symbol'],ENT_QUOTES) ?>"
            data-name="<?= htmlspecialchars($c['name'],ENT_QUOTES) ?>"
            data-rate="<?= $c['rate'] ?>"
            data-active="<?= $c['is_active'] ?>"
            onclick="doEdit(this)">Edit</button>
    </td>
</tr>
<?php endforeach; ?>
</table>

<!-- Modal -->
<div class="modal-bg" id="modal">
<div class="modal">
    <h3>Edit Currency</h3>
    <form method="POST">
        <input type="hidden" name="action" value="edit">
        <input type="hidden" name="id" id="mId">
        <div class="fg"><label>Code</label><input id="mCode" readonly style="background:#f5f5f5"></div>
        <div class="fg"><label>Symbol</label><input name="symbol" id="mSym" required></div>
        <div class="fg"><label>Name</label><input name="name" id="mName" required></div>
        <div class="fg"><label>Rate (1 BDT = ?)</label><input type="number" name="rate" id="mRate" step="0.000001" min="0.000001" required></div>
        <div class="fg"><label>Status</label>
            <select name="is_active" id="mActive">
                <option value="1">Active</option>
                <option value="0">Inactive</option>
            </select>
        </div>
        <div class="flex-gap">
            <button type="submit" class="btn">Save</button>
            <button type="button" class="btn-gray" onclick="closeModal()">Cancel</button>
        </div>
    </form>
</div>
</div>

<script>
function doEdit(btn) {
    document.getElementById('mId').value     = btn.dataset.id;
    document.getElementById('mCode').value   = btn.dataset.code;
    document.getElementById('mSym').value    = btn.dataset.sym;
    document.getElementById('mName').value   = btn.dataset.name;
    document.getElementById('mRate').value   = btn.dataset.rate;
    document.getElementById('mActive').value = btn.dataset.active;
    document.getElementById('modal').classList.add('on');
}
function closeModal() {
    document.getElementById('modal').classList.remove('on');
}
document.getElementById('modal').addEventListener('click', function(e){
    if(e.target === this) closeModal();
});
</script>
</body>
</html>
