<?php
// --- ERROR REPORTING OFF (Clean Page) ---
error_reporting(0);

session_start();
if ($_SESSION['unohs'] == null) {
    header("location:index.php?msg=unauthorized");
}
include("conn.php");

// --- GET & DECRYPT ID ---
if (isset($_GET['id'])) {
    
    // conn.php wala encryptor function
    $id = encryptor('decrypt', $_GET['id']);
    
    if(!$id || $id == "") {
        echo "<h3 style='color:red; text-align:center; margin-top:50px;'>Error: Invalid Link/ID.</h3>";
        exit;
    }

    // --- FETCH DATA (LEFT JOIN for Safety) ---
    // User Data (shonu_subjects) aur Bank Data (khate) fetch kar rahe hain
    $sql = "SELECT 
                h.shonu, h.motta, h.sthiti, h.dinankavannuracisi, h.khateshonu, h.balakedara,
                s.mobile, s.email,
                k.khatehesaru, k.khatesankhye, k.kod, k.phalanubhavi
            FROM hintegedukolli h
            LEFT JOIN shonu_subjects s ON s.id = h.balakedara
            LEFT JOIN khate k ON k.shonu = h.khateshonu
            WHERE h.shonu = '$id'";

    $query = mysqli_query($conn, $sql);
    $Result = mysqli_fetch_array($query);

    if (!$Result) {
        echo "<h3 style='color:red; text-align:center; margin-top:50px;'>Error: No Data Found.</h3>";
        exit;
    }
} else {
    echo "ID Missing"; 
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=1200, initial-scale=0.1">
    <title>Process Withdrawal</title>
    
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@500;600;700;800;900&family=Rajdhani:wght@700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="vendors/mdi/css/materialdesignicons.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="css/style.css">
    <link rel="shortcut icon" href="images/favicon.png" />

    <style>
        /* =========================================
           ULTRA MODERN 3D UI
           ========================================= */
        :root {
            --primary: #4834d4;
            --success: #20bf6b;
            --danger: #eb3b5a;
            --dark: #2d3436;
            --light: #f4f7f6;
            --white: #ffffff;
        }

        body {
            background-color: var(--light);
            font-family: 'Poppins', sans-serif;
            color: var(--dark);
            font-weight: 600;
        }

        /* 3D Card */
        .detail-card {
            background: var(--white);
            border-radius: 20px;
            padding: 30px;
            border-bottom: 6px solid #d1ccc0;
            box-shadow: 0 10px 30px rgba(0,0,0,0.08);
            margin: 20px auto;
            max-width: 700px;
            position: relative;
        }

        .card-header-flex {
            text-align: center; margin-bottom: 30px; border-bottom: 2px dashed #eee; padding-bottom: 20px;
        }
        
        .amount-display {
            font-family: 'Rajdhani', sans-serif;
            font-size: 3rem; font-weight: 900; color: var(--success);
            margin: 10px 0;
            text-shadow: 2px 2px 0 rgba(32, 191, 107, 0.1);
        }

        /* Data Rows */
        .info-row {
            display: flex; justify-content: space-between; align-items: center;
            padding: 15px 0; border-bottom: 1px solid #f1f2f6;
        }
        .info-label { color: #636e72; font-size: 0.9rem; font-weight: 700; }
        .info-val { color: var(--dark); font-size: 1rem; font-weight: 800; text-align: right; }
        .info-val.bank { color: var(--primary); text-transform: uppercase; }

        /* 3D Buttons */
        .action-area { display: flex; gap: 15px; margin-top: 30px; }
        
        .btn-3d {
            flex: 1; border: none; border-radius: 12px; padding: 15px;
            font-weight: 800; text-transform: uppercase; font-size: 1rem;
            cursor: pointer; position: relative; top: 0; color: #fff;
            transition: all 0.1s; display: flex; align-items: center; justify-content: center; gap: 10px;
        }
        
        .btn-accept { background: var(--success); box-shadow: 0 5px 0 #169352; }
        .btn-reject { background: var(--danger); box-shadow: 0 5px 0 #b32a43; }
        .btn-back { background: var(--dark); box-shadow: 0 5px 0 #000; text-decoration: none; width: 100%; display: block; text-align: center; padding: 12px; margin-bottom: 20px; border-radius: 10px; color: #fff; font-weight: bold; }
        
        .btn-3d:active { top: 5px; box-shadow: 0 0 0; }

        @media (max-width: 768px) {
            .content-wrapper { padding: 10px !important; }
            .detail-card { padding: 20px; }
            .amount-display { font-size: 2.5rem; }
        }
    </style>
</head>

<body>
    <div class="container-scroller">
        
        <nav class="navbar col-lg-12 col-12 p-0 fixed-top d-flex flex-row">
            <div class="text-center navbar-brand-wrapper d-flex align-items-center justify-content-center" style="background:#fff;">
                <a class="navbar-brand brand-logo" href="dashboard.php"><h4 style="color:var(--primary); font-weight:900; margin:0;">PROCESS<span style="color:var(--dark);">PAY</span></h4></a>
                <a class="navbar-brand brand-logo-mini" href="dashboard.php"><img src="images/logo-mini.png" alt="logo"/></a>
            </div>
            <div class="navbar-menu-wrapper d-flex align-items-center justify-content-end">
                <button class="navbar-toggler navbar-toggler align-self-center" type="button" data-toggle="minimize"><i class="fa-solid fa-bars"></i></button>
                <ul class="navbar-nav navbar-nav-right">
                    <li class="nav-item dropdown d-flex mr-4">
                        <a class="nav-link count-indicator dropdown-toggle d-flex align-items-center justify-content-center" id="notificationDropdown" href="#" data-toggle="dropdown"><i class="fa-solid fa-gear" style="color:var(--dark); font-size:1.2rem;"></i></a>
                        <div class="dropdown-menu dropdown-menu-right navbar-dropdown preview-list">
                            <a class="dropdown-item preview-item" href="logout.php"><i class="fa-solid fa-power-off"></i> Logout</a>
                        </div>
                    </li>
                </ul>
                <button class="navbar-toggler navbar-toggler-right d-lg-none align-self-center" type="button" data-toggle="offcanvas"><i class="fa-solid fa-bars"></i></button>
            </div>
        </nav>

        <div class="container-fluid page-body-wrapper">
            <nav class="sidebar sidebar-offcanvas" id="sidebar" style="background:#fff;">
                <div class="user-profile" style="background:#fff; border-bottom:2px dashed #eee;">
                    <div class="user-image"><img src="images/faces/face28.png"></div>
                    <div class="user-name" style="color:var(--dark);">AB Coders</div>
                    <div class="user-designation">Admin</div>
                </div>
                <?php include 'compass.php'; ?>
            </nav>

            <div class="main-panel">
                <div class="content-wrapper">
                    
                    <a href="manage_withdraw.php" class="btn-back">
                        <i class="fa-solid fa-arrow-left"></i> Back to List
                    </a>

                    <div class="detail-card">
                        <div class="card-header-flex">
                            <div style="font-size:0.9rem; font-weight:700; color:#888; letter-spacing:1px;">WITHDRAWAL REQUEST</div>
                            <div class="amount-display">₹<?php echo number_format($Result['motta'], 2); ?></div>
                            <div style="font-size:0.85rem; color:#555;"><?php echo date('d M Y, h:i A', strtotime($Result['dinankavannuracisi'])); ?></div>
                        </div>

                        <div class="info-row">
                            <span class="info-label">User Mobile</span>
                            <span class="info-val"><?php echo $Result['mobile']; ?></span>
                        </div>
                        <div class="info-row">
                            <span class="info-label">User Email</span>
                            <span class="info-val"><?php echo !empty($Result['email']) ? $Result['email'] : 'N/A'; ?></span>
                        </div>

                        <div style="margin-top:20px; padding:15px; background:#f9f9f9; border-radius:10px; border:1px solid #eee;">
                            <div style="font-size:0.8rem; font-weight:800; color:var(--primary); margin-bottom:10px; text-transform:uppercase;">Banking Details</div>
                            
                            <div class="info-row">
                                <span class="info-label">Account Holder</span>
                                <span class="info-val"><?php echo !empty($Result['phalanubhavi']) ? $Result['phalanubhavi'] : '<span style="color:red;">Not Set</span>'; ?></span>
                            </div>
                            <div class="info-row">
                                <span class="info-label">Bank Name</span>
                                <span class="info-val bank"><?php echo !empty($Result['khatehesaru']) ? $Result['khatehesaru'] : 'N/A'; ?></span>
                            </div>
                            <div class="info-row">
                                <span class="info-label">Account Number</span>
                                <span class="info-val"><?php echo !empty($Result['khatesankhye']) ? $Result['khatesankhye'] : 'N/A'; ?></span>
                            </div>
                            <div class="info-row" style="border:none;">
                                <span class="info-label">IFSC Code</span>
                                <span class="info-val"><?php echo !empty($Result['kod']) ? $Result['kod'] : 'N/A'; ?></span>
                            </div>
                        </div>

                        <?php if($Result['sthiti'] == 0) { ?>
                        <div class="action-area">
                            <button class="btn-3d btn-accept" onclick="processAction(<?php echo $Result['shonu'];?>,'accept');">
                                <i class="fa-solid fa-check-circle"></i> Approve
                            </button>
                            <button class="btn-3d btn-reject" onclick="processAction(<?php echo $Result['shonu'];?>,'reject');">
                                <i class="fa-solid fa-circle-xmark"></i> Reject
                            </button>
                        </div>
                        <?php } else { 
                            $status_msg = ($Result['sthiti'] == 1) ? "✅ Approved" : "❌ Rejected";
                        ?>
                            <div style="text-align:center; margin-top:20px; padding:15px; background:#eee; border-radius:10px; font-weight:800; color:#555;">
                                <?php echo $status_msg; ?>
                            </div>
                        <?php } ?>

                    </div>

                </div>
                <footer class="footer" style="padding:20px; text-align:center; color:#999; font-size:0.8rem;"><span>© 2026 AB Coders</span></footer>
            </div>
        </div>
    </div>

    <script src="vendors/base/vendor.bundle.base.js"></script>
    <script src="js/off-canvas.js"></script>
    <script src="js/hoverable-collapse.js"></script>
    <script src="js/template.js"></script>
    <script src="vendors/chart.js/Chart.min.js"></script>
    <script src="js/dashboard.js"></script>

    <script>
    // CLEAN & FAST JS ACTION (With Fix for 'Error: 2')
    function processAction(id, type) {
        var msg = (type === 'accept') ? "✅ Confirm APPROVE?" : "⚠️ Confirm REJECT & REFUND?";
        
        if (confirm(msg)) {
            var remark = "";
            if (type === 'reject') {
                remark = prompt("Enter Rejection Remark (Optional):");
            }

            $.ajax({
                type: "POST",
                url: "manage_withdrawAction.php",
                data: { id: id, type: type, remark: remark },
                success: function(response) {
                    var res = response.trim(); // Clean whitespace

                    // Response 1 = Success Accept
                    // Response 2 = Success Reject
                    if(res == "1") {
                        window.location.href = 'manage_withdraw.php'; // Or 'withdraw_accept_list.php'
                    } 
                    else if(res == "2") {
                        window.location.href = 'manage_withdraw.php'; // Or 'withdraw_reject_list.php'
                    }
                    else {
                        alert("Error: " + response);
                    }
                },
                error: function() {
                    alert("Server Error! Check Internet or Code.");
                }
            });
        }
    }
    
    
    </script>
</body>
</html>