<?php
/**
 * Admin Payment Methods
 */
require_once 'includes/header.php';

// Handle form
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    if ($action === 'save') {
        $methods = ['upi' => $_POST['upi'] ?? '', 'bank' => $_POST['bank'] ?? '', 'crypto' => $_POST['crypto'] ?? ''];
        foreach ($methods as $key => $value) {
            $exists = dbFetch("SELECT id FROM settings WHERE setting_key = ?", ["payment_$key"]);
            if ($exists) {
                dbUpdate('settings', ['setting_value' => $value], 'setting_key = ?', ["payment_$key"]);
            } else {
                dbInsert('settings', ['setting_key' => "payment_$key", 'setting_value' => $value]);
            }
        }
        echo '<div class="alert alert-success"><i class="fas fa-check"></i> Payment methods updated!</div>';
    }
}

$upi = dbFetch("SELECT setting_value FROM settings WHERE setting_key = 'payment_upi'")['setting_value'] ?? '';
$bank = dbFetch("SELECT setting_value FROM settings WHERE setting_key = 'payment_bank'")['setting_value'] ?? '';
$crypto = dbFetch("SELECT setting_value FROM settings WHERE setting_key = 'payment_crypto'")['setting_value'] ?? '';
?>

<div class="page-header">
    <h1 class="page-title">Payment Methods</h1>
</div>

<form method="POST">
    <input type="hidden" name="action" value="save">
    
    <div class="card mb-4">
        <div class="card-header"><h4><i class="fas fa-mobile-alt text-green"></i> UPI Payment</h4></div>
        <div class="card-body">
            <div class="form-group">
                <label class="form-label">UPI ID</label>
                <input type="text" name="upi" class="form-control" value="<?= htmlspecialchars($upi) ?>" placeholder="example@upi">
            </div>
        </div>
    </div>
    
    <div class="card mb-4">
        <div class="card-header"><h4><i class="fas fa-university text-blue"></i> Bank Transfer</h4></div>
        <div class="card-body">
            <div class="form-group">
                <label class="form-label">Bank Details (JSON format)</label>
                <textarea name="bank" class="form-control" rows="4" placeholder='{"bank_name":"SBI","account_no":"123456","ifsc":"SBIN0001234","holder":"John Doe"}'><?= htmlspecialchars($bank) ?></textarea>
            </div>
        </div>
    </div>
    
    <div class="card mb-4">
        <div class="card-header"><h4><i class="fab fa-bitcoin text-yellow"></i> Cryptocurrency</h4></div>
        <div class="card-body">
            <div class="form-group">
                <label class="form-label">Crypto Addresses (JSON format)</label>
                <textarea name="crypto" class="form-control" rows="4" placeholder='{"btc":"bc1q...","eth":"0x...","usdt":"TRC20..."}'><?= htmlspecialchars($crypto) ?></textarea>
            </div>
        </div>
    </div>
    
    <button type="submit" class="btn btn-primary btn-lg">
        <i class="fas fa-save"></i> Save Payment Methods
    </button>
</form>

<?php require_once 'includes/footer.php'; ?>
