<?php
// --- ERROR REPORTING OFF ---
error_reporting(0);
session_start();
if ($_SESSION['unohs'] == null) {
    header("location:index.php?msg=unauthorized");
}
include("conn.php");

// --- 1. CLEAR HISTORY LOGIC ---
if (isset($_POST['clear_history'])) {
    // Sirf 'sthiti = 2' (Rejected) walo ko delete karega
    $del_sql = "DELETE FROM hintegedukolli WHERE sthiti = '2'";
    if (mysqli_query($conn, $del_sql)) {
        echo '<script>alert("✅ Rejected History Cleared Successfully!"); window.location.href="withdraw_reject_list.php";</script>';
    }
}

// --- 2. FILTER & SEARCH LOGIC ---
// Query: Users aur Bank table ko JOIN kar rahe hain taaki Account No se bhi search ho sake
$filter_query = "SELECT h.*, 
                 s.mobile AS user_mobile, s.id AS user_id,
                 k.khatesankhye AS acc_no
                 FROM hintegedukolli h
                 LEFT JOIN shonu_subjects s ON s.id = h.balakedara
                 LEFT JOIN khate k ON k.shonu = h.khateshonu
                 WHERE h.sthiti = '2'";

$total_amount = 0;

if (isset($_POST['apply_filter'])) {
    $start = $_POST['start_date'];
    $end = $_POST['end_date'];
    $search = trim($_POST['search_term']);

    // Date Filter
    if (!empty($start) && !empty($end)) {
        $filter_query .= " AND h.dinankavannuracisi BETWEEN '$start' AND '$end'";
    }

    // Deep Search (UID, Mobile, Account No, Order ID)
    if (!empty($search)) {
        $filter_query .= " AND (s.mobile LIKE '%$search%' OR s.id LIKE '%$search%' OR h.dharavahi LIKE '%$search%' OR k.khatesankhye LIKE '%$search%')";
    }
}

$filter_query .= " ORDER BY h.shonu DESC";
$run_query = mysqli_query($conn, $filter_query);

// Calculate Total
while ($t_row = mysqli_fetch_assoc($run_query)) {
    $total_amount += $t_row['motta'];
}
mysqli_data_seek($run_query, 0); // Reset Pointer
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta name="viewport" content="width=1200, initial-scale=0.9">
    <title>Rejected Withdrawals</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="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css">
    <link rel="stylesheet" href="css/style.css">
    <link rel="shortcut icon" href="images/favicon.png" />

    <style>
        /* =========================================
           3D RED THEME (Reject List)
           ========================================= */
        :root {
            --primary: #eb3b5a; /* Red for Reject */
            --dark: #2d3436;
            --light: #f4f7f6;
            --white: #ffffff;
            --shadow: #b32a43;
        }

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

        /* --- 3D CARD --- */
        .list-card {
            background: var(--white);
            border-radius: 15px;
            padding: 20px;
            border-bottom: 5px solid #d1ccc0;
            box-shadow: 0 8px 20px rgba(0,0,0,0.05);
            margin-bottom: 25px;
            transition: 0.3s;
        }
        .list-card:hover { transform: translateY(-2px); }

        .card-header-flex {
            display: flex; justify-content: space-between; align-items: center;
            margin-bottom: 20px; border-bottom: 2px dashed #eee; padding-bottom: 10px;
            flex-wrap: wrap; gap: 10px;
        }
        
        .card-title {
            font-size: 1.4rem; font-weight: 900; color: var(--primary);
            text-transform: uppercase; letter-spacing: 0.5px;
            display: flex; align-items: center; gap: 10px;
        }

        /* --- FILTER PANEL --- */
        .filter-panel {
            background: #fff0f0; padding: 15px; border-radius: 12px;
            border: 2px solid #ffccd2; display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end;
        }
        .form-grp { flex: 1; min-width: 150px; }
        .form-label { font-size: 0.75rem; font-weight: 800; color: #c0392b; margin-bottom: 5px; display: block; }
        
        .modern-input {
            width: 100%; padding: 10px; border: 2px solid #ccc; border-radius: 8px;
            font-weight: 700; outline: none; transition: 0.3s;
        }
        .modern-input:focus { border-color: var(--primary); background: #fff; }

        /* --- 3D BUTTONS --- */
        .btn-3d {
            border: none; border-radius: 8px; padding: 10px 20px;
            font-weight: 800; text-transform: uppercase; font-size: 0.85rem;
            cursor: pointer; position: relative; top: 0; color: #fff;
            transition: all 0.1s; display: inline-flex; align-items: center; gap: 5px;
        }
        .btn-search { background: var(--primary); box-shadow: 0 4px 0 var(--shadow); width: 100%; justify-content: center; }
        .btn-clear { background: #2d3436; box-shadow: 0 4px 0 #000; font-size: 0.7rem; padding: 8px 15px; }
        
        .btn-3d:active { top: 4px; box-shadow: 0 0 0; }

        /* --- TABLE --- */
        .table-responsive { overflow-x: auto; }
        table.dataTable thead th {
            background-color: var(--dark) !important; color: #fff !important; 
            padding: 12px !important; font-size: 0.85rem; border: none; white-space: nowrap;
        }
        table.dataTable tbody td {
            padding: 12px !important; font-weight: 600; font-size: 0.9rem; 
            border-bottom: 1px solid #eee; vertical-align: middle; white-space: nowrap;
        }
        
        .amt-text { font-family: 'Rajdhani', sans-serif; font-size: 1.2rem; font-weight: 800; color: var(--primary); }
        
        /* Stats Box */
        .stats-box {
            background: #fff; border: 2px solid var(--primary); border-radius: 10px;
            padding: 10px 20px; display: inline-block;
            box-shadow: 4px 4px 0 rgba(235, 59, 90, 0.2);
        }

        /* Navbar Fix */
        .navbar { background: #fff !important; box-shadow: 0 4px 15px rgba(0,0,0,0.05) !important; }
        .navbar-toggler { border:none !important; outline:none !important; }

        @media (max-width: 768px) {
            .content-wrapper { padding: 10px !important; }
            .filter-panel { flex-direction: column; }
            .form-grp { width: 100%; }
            .btn-3d { width: 100%; margin-bottom: 5px; }
        }
    </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;">REJECT<span style="color:var(--dark);">LIST</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" style="font-size:1.5rem; color:var(--dark);"></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" 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" style="font-size:1.5rem; color:var(--dark);"></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" style="border:3px solid var(--primary);"></div>
                    <div class="user-name" style="color:var(--dark);">AB Coders</div>
                    <div class="user-designation" style="color:#888;">Admin</div>
                </div>
                <?php include 'compass.php'; ?>
            </nav>

            <div class="main-panel">
                <div class="content-wrapper">
                    
                    <div class="list-card">
                        <div class="card-header-flex">
                            <div class="card-title">
                                <i class="fa-solid fa-circle-xmark"></i> Filter Rejects
                            </div>
                            <form method="post" onsubmit="return confirm('⚠️ Are you sure? This will delete ALL Rejected records permanently!');">
                                <button type="submit" name="clear_history" class="btn-3d btn-clear">
                                    <i class="fa-solid fa-trash"></i> CLEAR HISTORY
                                </button>
                            </form>
                        </div>

                        <form method="post" class="filter-panel">
                            <div class="form-grp">
                                <label class="form-label">Search (UID / Mobile / Account No)</label>
                                <input type="text" name="search_term" class="modern-input" placeholder="Enter ID, Phone or Acc No...">
                            </div>
                            <div class="form-grp">
                                <label class="form-label">Start Date</label>
                                <input type="date" name="start_date" class="modern-input">
                            </div>
                            <div class="form-grp">
                                <label class="form-label">End Date</label>
                                <input type="date" name="end_date" class="modern-input">
                            </div>
                            <div class="form-grp" style="flex:0 0 auto;">
                                <label class="form-label">&nbsp;</label>
                                <button type="submit" name="apply_filter" class="btn-3d btn-search">
                                    <i class="fa-solid fa-filter"></i> Search
                                </button>
                            </div>
                        </form>
                    </div>

                    <div class="list-card" style="border-bottom-color: var(--dark);">
                        <div class="card-header-flex">
                            <div class="card-title" style="color:var(--dark);">
                                <i class="fa-solid fa-list-ul"></i> Rejected List
                            </div>
                            <div class="stats-box">
                                <span style="font-weight:700; color:#555;">Rejected Amount:</span>
                                <span style="font-size:1.2rem; font-weight:900; color:var(--primary); margin-left:5px;">
                                    ₹<?php echo number_format($total_amount, 2); ?>
                                </span>
                            </div>
                        </div>

                        <div class="table-responsive">
                            <table id="example1" class="table table-bordered" style="width:100%">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>User ID</th>
                                        <th>Mobile</th>
                                        <th>Amount</th>
                                        <th>Order ID</th>
                                        <th>Req. Date</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php 
                                        $i=0;
                                        if(mysqli_num_rows($run_query) > 0){
                                            while($row = mysqli_fetch_array($run_query)){
                                                $i++;
                                    ?>
                                    <tr>
                                        <td><?php echo $i; ?></td>
                                        <td style="font-weight:800;"><?php echo $row['user_id']; ?></td>
                                        <td><?php echo $row['user_mobile']; ?></td>
                                        <td><span class="amt-text">₹<?php echo number_format($row['motta'], 2); ?></span></td>
                                        <td><small><?php echo $row['dharavahi']; ?></small></td>
                                        <td><?php echo date('d-m-Y', strtotime($row['dinankavannuracisi'])); ?></td>
                                    </tr>
                                    <?php } } else { echo "<tr><td colspan='6' class='text-center'>No Records Found</td></tr>"; } ?>
                                </tbody>
                            </table>
                        </div>
                    </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 src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.min.js"></script>

    <script>
    $(document).ready(function() {
        $('#example1').DataTable({
            "paging": true,
            "lengthChange": false,
            "searching": false, 
            "ordering": false,
            "info": true,
            "autoWidth": true,
            "pageLength": 50
        });
    });

    
    </script>
</body>
</html>