<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();

// এডমিন লগইন চেক
if (!isset($_SESSION['admin_id'])) {
    header("Location: login.php");
    exit();
}

require_once __DIR__ . '/../config.php';

$message = "";

// ==================== পেমেন্ট সেটিংস আপডেট লজিক ====================
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['update_payment_settings'])) {
    $ssl_status = $_POST['ssl_status'];
    $ssl_store_id = trim($_POST['ssl_store_id']);
    $ssl_store_password = trim($_POST['ssl_store_password']);
    
    $manual_status = $_POST['manual_status'];
    $bkash_manual_number = trim($_POST['bkash_manual_number']);
    $nagad_manual_number = trim($_POST['nagad_manual_number']);

    try {
        $sql = "UPDATE payment_settings SET 
                ssl_status = :ssl_status, 
                ssl_store_id = :ssl_store_id, 
                ssl_store_password = :ssl_store_password, 
                manual_status = :manual_status, 
                bkash_manual_number = :bkash_manual_number, 
                nagad_manual_number = :nagad_manual_number 
                WHERE id = 1";
        
        $stmt = $pdo->prepare($sql);
        $stmt->execute([
            'ssl_status' => $ssl_status,
            'ssl_store_id' => $ssl_store_id,
            'ssl_store_password' => $ssl_store_password,
            'manual_status' => $manual_status,
            'bkash_manual_number' => $bkash_manual_number,
            'nagad_manual_number' => $nagad_manual_number
        ]);

        $message = "<div class='alert alert-success'><i class='fa-solid fa-circle-check'></i> পেমেন্ট মেথড কনফিগারেশন সফলভাবে আপডেট করা হয়েছে!</div>";
    } catch (PDOException $e) {
        $message = "<div class='alert alert-danger'>ত্রুটি: " . htmlspecialchars($e->getMessage()) . "</div>";
    }
}

// ==================== বর্তমান ডাটা তুলে আনা ====================
try {
    $stmt = $pdo->query("SELECT * FROM payment_settings WHERE id = 1");
    $pay_settings = $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    die("ডাটা লোড ব্যর্থ: " . $e->getMessage());
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Payment Gateway Settings</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="css/sidebar.css?v=3.0">
    <link rel="stylesheet" href="css/dashboard.css?v=3.0">
    <link rel="stylesheet" href="css/products.css?v=3.0">
    <link rel="stylesheet" href="css/payment_settings.css?v=1.1">
</head>
<body>

    <!-- সাইডবার যুক্ত করা হলো -->
    <?php include 'sidebar.php'; ?>

    <div class="main-content">
        <div class="top-bar">
            <h2 class="page-title"><i class="fa-solid fa-credit-card"></i> পেমেন্ট মেথড কনফিগারেশন</h2>
            <div class="admin-badge"><i class="fa-regular fa-user-circle"></i> এডমিন</div>
        </div>

        <?php echo $message; ?>

        <div class="payment-card">
            <form action="" method="POST">
                
                <!-- অপশন ১: SSLCOMMERZ গেটওয়ে -->
                <div class="payment-section-title">
                    <span><i class="fa-solid fa-bolt" style="color: #ff2a54;"></i> ১. SSLCOMMERZ অনলাইন গেটওয়ে</span>
                    <select name="ssl_status" class="status-select <?php echo ($pay_settings['ssl_status'] == 'active') ? 'status-active' : 'status-inactive'; ?>" onchange="this.className='status-select ' + (this.value=='active'?'status-active':'status-inactive')">
                        <option value="active" <?php echo ($pay_settings['ssl_status'] == 'active') ? 'selected' : ''; ?>>সক্রিয় (Active)</option>
                        <option value="inactive" <?php echo ($pay_settings['ssl_status'] == 'inactive') ? 'selected' : ''; ?>>নিষ্ক্রিয় (Inactive)</option>
                    </select>
                </div>
                
                <div class="payment-grid">
                    <div class="form-group">
                        <label><span class="method-badge badge-ssl">STORE ID</span> SSLCommerz Store ID</label>
                        <input type="text" name="ssl_store_id" class="form-control" value="<?php echo htmlspecialchars($pay_settings['ssl_store_id'] ?? ''); ?>" placeholder="Store ID দিন">
                    </div>
                    <div class="form-group">
                        <label><span class="method-badge badge-ssl">PASSWORD</span> SSLCommerz Store Password</label>
                        <input type="password" name="ssl_store_password" class="form-control" value="<?php echo htmlspecialchars($pay_settings['ssl_store_password'] ?? ''); ?>" placeholder="••••••••">
                    </div>
                </div>


                <!-- অপশন ২: ম্যানুয়াল পেমেন্ট (বিকাশ ও নগদ পার্সোনাল) -->
                <div class="payment-section-title">
                    <span><i class="fa-solid fa-mobile-screen-button" style="color: #4e73df;"></i> ২. ম্যানুয়াল পেমেন্ট (বিকাশ / নগদ পার্সোনাল)</span>
                    <select name="manual_status" class="status-select <?php echo ($pay_settings['manual_status'] == 'active') ? 'status-active' : 'status-inactive'; ?>" onchange="this.className='status-select ' + (this.value=='active'?'status-active':'status-inactive')">
                        <option value="active" <?php echo ($pay_settings['manual_status'] == 'active') ? 'selected' : ''; ?>>সক্রিয় (Active)</option>
                        <option value="inactive" <?php echo ($pay_settings['manual_status'] == 'inactive') ? 'selected' : ''; ?>>নিষ্ক্রিয় (Inactive)</option>
                    </select>
                </div>

                <div class="payment-grid">
                    <div class="form-group">
                        <label><span class="method-badge badge-bkash">bKash</span> বিকাশ পার্সোনাল নাম্বার</label>
                        <input type="text" name="bkash_manual_number" class="form-control" value="<?php echo htmlspecialchars($pay_settings['bkash_manual_number'] ?? ''); ?>" placeholder="017XXXXXXXX">
                    </div>
                    <div class="form-group">
                        <label><span class="method-badge badge-nagad">Nagad</span> নগদ পার্সোনাল নাম্বার</label>
                        <input type="text" name="nagad_manual_number" class="form-control" value="<?php echo htmlspecialchars($pay_settings['nagad_manual_number'] ?? ''); ?>" placeholder="017XXXXXXXX">
                    </div>
                </div>

                <!-- সেটিংস সংরক্ষণ বাটন -->
                <button type="submit" name="update_payment_settings" class="btn-submit" style="width: auto; padding: 14px 45px; background: #4e73df; color: white; border: none; border-radius: 5px; cursor: pointer; font-weight: bold; font-size: 1rem; margin-top: 30px;">
                    <i class="fa-solid fa-save"></i> পেমেন্ট মেথড সেটিংস সেভ করুন
                </button>
            </form>
        </div>
    </div>

</body>
</html>