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

// Fetch Last Period (5D 1 Min: gelluonduhogu_aidudi)
$samasye = "SELECT atadaaidi FROM `gelluonduhogu_aidudi` ORDER BY kramasankhye DESC LIMIT 1";
$samasyephalitansa = $conn->query($samasye);
$samasyephalitansa_dhadi = mysqli_fetch_assoc($samasyephalitansa);

// Fetch Next Prediction (5D 1 Min: hastacalita_phalitansa_aidudi)
$munde = mysqli_query($conn, "SELECT sankhye FROM `hastacalita_phalitansa_aidudi` WHERE `sthiti`='1'");
if (mysqli_num_rows($munde) > 0) {
    $uhisi = mysqli_fetch_array($munde);
    $uhisisankhye = $uhisi['sankhye'];
} else {
    $uhisisankhye = "NOT SET";
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
   <meta name="viewport" content="width=1200, initial-scale=0.1">
    <title>5D 1 Min | Admin</title>
    
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&family=Rajdhani:wght@600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="vendors/mdi/css/materialdesignicons.min.css">
    <link rel="stylesheet" href="vendors/feather/feather.css">
    <link rel="stylesheet" href="vendors/base/vendor.bundle.base.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>
        /* =========================================
           5D PROFESSIONAL DARK THEME (Updated)
           ========================================= */
        :root {
            --primary: #7367f0;
            --5d-blue: #00cfe8;
            --5d-shadow: rgba(0, 0, 0, 0.2); /* Stronger shadow */
            --neon-green: #28c76f;
            --neon-red: #ea5455;
            --bg-body: #f4f7f6;
            --bg-card: #ffffff;
            --text-dark: #1a1a1a; /* Darker Black */
            --text-grey: #4a4a4a; /* Dark Grey */
            --input-bg: #e9ecef; /* Darker Input Background */
            --input-border: #ced4da;
        }

        body {
            background-color: var(--bg-body) !important;
            color: var(--text-dark);
            font-family: 'Poppins', sans-serif;
        }

        /* --- COMPACT GAME CARD (Professional Look) --- */
        .wingo-pro-card {
            background: var(--bg-card);
            border-radius: 12px;
            padding: 20px;
            border: 1px solid #e0e0e0;
            box-shadow: 0 8px 30px var(--5d-shadow); /* Deep Shadow */
            margin-bottom: 20px;
            position: relative;
            overflow: hidden;
        }
        
        /* Stylish Top Border */
        .wingo-pro-card::before {
            content: ''; position: absolute; top: 0; left: 0; width: 100%; height: 5px;
            background: linear-gradient(90deg, #7367f0, var(--5d-blue), #28c76f);
        }

        /* --- TIMER SECTION (Dark & Bold) --- */
        .timer-section {
            text-align: center;
            background: #f8f9fa;
            border-radius: 10px;
            padding: 15px;
            border: 1px solid #dee2e6;
            height: 100%; display: flex; flex-direction: column; justify-content: center;
            box-shadow: inset 0 2px 5px rgba(0,0,0,0.05);
        }
        
        .timer-label {
            font-size: 0.8rem; color: var(--text-grey); text-transform: uppercase; font-weight: 700; letter-spacing: 1px;
        }

        .timer-big {
            font-family: 'Rajdhani', sans-serif;
            font-size: 3rem;
            font-weight: 800; /* Extra Bold */
            color: var(--text-dark); /* Dark Black Text */
            text-shadow: 2px 2px 0px rgba(0,0,0,0.1); /* Subtle Text Shadow */
            line-height: 1;
            margin: 8px 0;
        }
        
        .period-pill {
            background: #e3f2fd; /* Light Blue Bg */
            color: #0d47a1; /* Dark Blue Text */
            padding: 4px 15px;
            border-radius: 20px;
            font-size: 0.85rem;
            font-weight: 700;
            display: inline-block;
            border: 1px solid #bbdefb;
        }

        /* --- PREDICTION SECTION --- */
        .pred-section {
            padding: 10px;
            text-align: center;
        }
        .next-res {
            font-size: 1rem; color: var(--text-dark); margin-bottom: 12px; font-weight: 600;
        }
        .next-res span {
            color: var(--neon-green); font-weight: 900; font-size: 1.3rem; text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
        }

        /* Darker Input Field */
        .input-glass {
            width: 100%; padding: 12px;
            background: var(--input-bg);
            border: 2px solid var(--input-border);
            color: #000; /* Black Text */
            border-radius: 8px;
            text-align: center; font-size: 1.1rem; font-weight: 700;
            outline: none; margin-bottom: 12px;
            transition: 0.3s;
            box-shadow: inset 0 2px 4px rgba(0,0,0,0.06);
        }
        .input-glass::placeholder { color: #6c757d; font-weight: 500; }
        .input-glass:focus {
            border-color: var(--5d-blue);
            background: #fff;
            box-shadow: 0 0 0 3px rgba(0, 207, 232, 0.25);
        }

        /* Shadow Buttons */
        .btn-pro {
            width: 100%; padding: 12px; border: none; border-radius: 8px;
            font-weight: 800; text-transform: uppercase; font-size: 0.85rem;
            cursor: pointer; color: #fff; margin-bottom: 5px;
            transition: 0.2s; letter-spacing: 0.5px;
        }
        .btn-green {
            background: linear-gradient(135deg, #28c76f, #1f9d57);
            box-shadow: 0 4px 15px rgba(40, 199, 111, 0.4);
        }
        .btn-red {
            background: linear-gradient(135deg, #ea5455, #c0392b);
            box-shadow: 0 4px 15px rgba(234, 84, 85, 0.4);
        }
        .btn-pro:active { transform: translateY(2px); box-shadow: none; }

        /* Tables (Professional & Contrast) */
        .pro-table-container {
            background: #ffffff;
            border-radius: 12px;
            overflow: hidden;
            margin-top: 20px;
            box-shadow: 0 5px 25px rgba(0,0,0,0.08); /* Better Shadow */
            border: 1px solid #e0e0e0;
        }
        .pro-table-header {
            padding: 15px 20px;
            background: #f1f3f5;
            font-weight: 800; color: var(--text-dark); font-size: 1rem;
            border-bottom: 2px solid #dee2e6;
            text-transform: uppercase; letter-spacing: 0.5px;
        }
        
        /* DataTables Customization */
        table.dataTable { width: 100% !important; border-collapse: collapse; }
        table.dataTable tbody tr { background-color: #fff !important; color: #212529; font-size: 0.9rem; font-weight: 500; }
        table.dataTable tbody tr:nth-child(even) { background-color: #f8f9fa !important; }
        table.dataTable tbody td { border-bottom: 1px solid #e9ecef !important; padding: 12px 10px !important; vertical-align: middle; }
        table.dataTable thead th {
            background-color: #343a40 !important; /* Dark Header */
            color: #fff; padding: 12px 10px !important; font-size: 0.85rem;
            border-bottom: none !important; font-weight: 600; text-transform: uppercase;
        }
        
        /* Stats Box */
        .stats-box {
            padding: 15px; background: linear-gradient(135deg, #343a40, #212529); /* Dark Stats bg */
            color: #fff; display: flex; justify-content: space-between; align-items: center;
            border-radius: 8px; margin-bottom: 5px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.2);
        }

        #copied {
            visibility: hidden; position: fixed; bottom: 20px; left: 50%; 
            transform: translateX(-50%); background: #212529; color: #fff; 
            padding: 10px 25px; border-radius: 30px; z-index: 9999; font-size: 0.9rem;
            box-shadow: 0 5px 20px rgba(0,0,0,0.3); font-weight: 600;
        }

        /* --- MOBILE LAYOUT --- */
        @media (max-width: 768px) {
            .content-wrapper { padding: 15px !important; }
            .wingo-pro-card { padding: 15px !important; margin-bottom: 15px !important; }
            .timer-big { font-size: 2.5rem; }
            .row-compact { display: flex; flex-wrap: nowrap; gap: 15px; }
            .col-compact { width: 50%; flex: 0 0 50%; max-width: 50%; }
            table.dataTable { width: 100% !important; margin: 0 !important; }
            .input-glass { font-size: 1rem; }
        }
    </style>
</head>

<body>
    <div class="container-scroller">
        
        <nav class="navbar col-lg-12 col-12 p-0 fixed-top d-flex flex-row" style="background: #fff; box-shadow: 0 2px 15px rgba(0,0,0,0.08);">
            <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(--5d-blue);font-weight:900;margin:0; text-transform:uppercase;">5D<span style="color:#212529;">1M</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">
                    <span class="icon-menu" style="color:#212529;"></span>
                </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="icon-cog" style="color:#212529;"></i>
                        </a>
                        <div class="dropdown-menu dropdown-menu-right navbar-dropdown preview-list" aria-labelledby="notificationDropdown">
                            <p class="mb-0 font-weight-normal float-left dropdown-header">Settings</p>              
                            <a class="dropdown-item preview-item" href="logout.php">
                                <i class="icon-inbox"></i> Logout
                            </a>
                        </div>
                    </li>
                </ul>
                <button class="navbar-toggler navbar-toggler-right d-lg-none align-self-center" type="button" data-toggle="offcanvas">
                    <span class="icon-menu" style="color:#212529;"></span>
                </button>
            </div>
        </nav>

        <div class="container-fluid page-body-wrapper">
            
            <nav class="sidebar sidebar-offcanvas" id="sidebar" style="background: #fff; border-right:1px solid #eee;">
                <div class="user-profile" style="background: #f8f9fa; border-bottom:1px solid #eee;">
                    <div class="user-image">
                        <img src="images/faces/face28.png" style="border:2px solid #ccc;">
                    </div>
                    <div class="user-name" style="color:#212529; font-weight:700;">AB Coders</div>
                    <div class="user-designation" style="color:#666;">Admin</div>
                </div>
                <?php include 'compass.php'; ?>
            </nav>

            <div class="main-panel">
                <div class="content-wrapper">
                    
                    <div class="wingo-pro-card">
                        <div class="row row-compact">
                            
                            <div class="col-6 col-compact text-center">
                                <div class="timer-section">
                                    <div class="timer-label">Time Remaining</div>
                                    <div id="demo" class="timer-big">00:00</div>
                                    <span class="period-pill"><?php echo $samasyephalitansa_dhadi['atadaaidi'];?></span>
                                    <input type="hidden" name="periodid" id="periodid" value="<?php echo $samasyephalitansa_dhadi['atadaaidi'];?>">
                                </div>
                            </div>

                            <div class="col-6 col-compact">
                                <div class="pred-section">
                                    <div class="next-res">Next: <span><?php echo $uhisisankhye; ?></span></div>
                                    
                                    <form action="itticina_geluvu_aidudi.php" id="pre" method="POST">
                                        <input type="number" name="username" id="next" class="input-glass" placeholder="00000-99999" autocomplete="off">
                                        <div style="display:flex; gap:10px;">
                                            <button type="button" onclick="sub()" class="btn-pro btn-green">Set</button>
                                            <button type="button" onclick="unsetman()" class="btn-pro btn-red">Unset</button>
                                        </div>
                                    </form>
                                </div>
                            </div>

                        </div>
                    </div>

                    <div class="wingo-pro-card" style="padding: 0; border:none; box-shadow:none; background:transparent;">
                        <div class="stats-box">
                            <span style="font-size:0.9rem; font-weight:700; letter-spacing:1px;">TOTAL BETS</span>
                            <span style="font-size:1.4rem; font-weight:800; color:#ffc107;">₹ <span id="tobet" style="color:#ffc107;">0</span></span>
                        </div>
                    </div>

                    <div class="pro-table-container">
                        <div class="pro-table-header">
                            <span><i class="fa-solid fa-list" style="color:var(--primary); margin-right:5px;"></i> Manual History</span>
                        </div>
                        <div style="padding:0;">
                            <table id="example1" class="display" style="width:100%">
                                <thead>
                                    <tr>
                                        <th>Res</th>
                                        <th>Num</th>
                                        <th>Stat</th>
                                        <th>Users</th>
                                        <th>Pay</th>
                                    </tr>
                                </thead>
                                <tbody id="betdetail">
                                    <?php 
                                        $samasye = mysqli_query($conn,"select * from `hastacalita_phalitansa_aidudi`");
                                        while($dhadi = mysqli_fetch_array($samasye)){
                                    ?>
                                        <tr>
                                            <td style="color:var(--5d-blue); font-weight:800;"><?php echo $dhadi["banna"]; ?></td>
                                            <td style="font-weight:700; color:#212529;"><?php echo $dhadi["sankhye"]; ?></td>
                                            <td style="color:#ff9f43; font-weight:600;">Wait</td>
                                            <td style="color:#6c757d;">Wait</td>
                                            <td style="color:#6c757d;">Wait</td>
                                        </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>

                    <div class="pro-table-container">
                        <div class="pro-table-header" style="border-bottom:2px solid var(--neon-green);">
                            <span style="color:var(--neon-green);"><i class="fa-solid fa-dice" style="margin-right:5px;"></i> Live Bets</span>
                        </div>
                        <div style="padding:0;">
                            <table id="example2" class="display" style="width:100%">
                                <thead>
                                    <tr>
                                        <th>User</th>
                                        <th>Val</th>
                                        <th>Amt</th>
                                        <th>Mob</th>
                                        <th>Bal</th>
                                    </tr>
                                </thead>
                                <tbody></tbody>
                            </table>
                        </div>
                    </div>

                </div>
                
                <footer class="footer" style="padding:20px; text-align:center; color:#6c757d; font-size:0.85rem; background:#f4f7f6; font-weight:500;">
                    <span>© 2026 AB Coders</span>
                </footer>
                
                <div id="copied">Enter Number (00000-99999)</div>

            </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="vendors/jquery-bar-rating/jquery.barrating.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>
    

    // --- 2. DATA & TIMER LOGIC (5D 1 MINUTE) ---
    $(function () {
        $('#example1').DataTable({ "paging": false, "info": false, "searching": false, "lengthChange": false });

        setInterval(function() { 
            wingoonetotal();
        }, 2000);

        setInterval(function() {
            $('#example2').DataTable({
                "processing": true, "serverSide": true,
                "ajax": "detavannunirvahisi_aidudi.php", // 5D 1M File
                "paging": true, "searching": false, "info": false, "lengthChange": false,
                "bDestroy": true, "pageLength": 10
            });
        }, 2000);
    });

    function wingoonetotal() {
        $.ajax({ type: "Post", url: "ottu_gellaluhogiondu_aidudi.php", success: function (html) { document.getElementById("tobet").innerHTML = html; } });
    }

    function sub() {
        var p = document.getElementById("next").value;
        var p_l = p.length;
        if (p === '' || p < 0 || p > 99999 || p_l !== 5 || isNaN(p)) {
            var x = document.getElementById("copied");
            x.style.visibility = "visible";
            setTimeout(function(){ x.style.visibility = "hidden"; }, 3000);
        } else {
            document.getElementById("pre").submit();
        }
    }

    function resetman() {
        $.ajax({ type: "Post", data: "stat=1", url: "maruhondisi_gellalu_aidudi.php" });
    }
    function unsetman() { resetman(); location.reload(); }

    function fetchServerTime() {
        return fetch('servertime').then(res => res.json().then(data => data.serverTime));
    }

    // --- 1 MINUTE TIMER LOGIC ---
    function startTimer(serverTime) {
        var distance = 60 - serverTime % 60; // 60 Seconds
        var interval = setInterval(function () {
            var min = Math.floor(distance / 60);
            var sec = ('0' + Math.floor(distance % 60)).slice(-2);

            document.getElementById("demo").innerHTML = min + " : " + sec;

            if (distance === 54) { resetman(); }
            if (distance === 52) { location.reload(); }
            
            distance--;
            if (distance < 0) {
                clearInterval(interval);
                fetchServerTime().then(st => startTimer(st));
            }
        }, 1000);
    }

    fetchServerTime().then(st => startTimer(st));
    </script>
</body>
</html>