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

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

// Fetch Next Prediction (K3 10 Min: hastacalita_phalitansa_kemeru_zehn)
$munde = mysqli_query($conn, "SELECT sankhye FROM `hastacalita_phalitansa_kemeru_zehn` 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=device-width, initial-scale=1, shrink-to-fit=no, user-scalable=no">
    <title>K3 10 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>
        /* =========================================
           LIGHT THEME CSS (White & Colorful)
           ========================================= */
        :root {
            --primary: #7367f0;
            --wingo-purple: #8B5CF6;
            --neon-green: #28c76f;
            --neon-red: #ea5455;
            --bg-body: #f4f7f6;
            --bg-card: #ffffff;
            --text-dark: #333333;
            --text-grey: #666666;
        }

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

        /* --- COMPACT GAME CARD --- */
        .wingo-pro-card {
            background: var(--bg-card);
            border-radius: 12px;
            padding: 15px;
            border: 1px solid #edf2f9;
            box-shadow: 0 5px 25px rgba(0,0,0,0.05);
            margin-bottom: 15px;
            position: relative;
            overflow: hidden;
        }
        
        .wingo-pro-card::before {
            content: ''; position: absolute; top: 0; left: 0; width: 100%; height: 4px;
            background: linear-gradient(90deg, #7367f0, var(--wingo-purple), #28c76f);
        }

        /* --- TIMER SECTION --- */
        .timer-section {
            text-align: center;
            background: #f8f9fa;
            border-radius: 10px;
            padding: 10px;
            border: 1px solid #e9ecef;
            height: 100%; display: flex; flex-direction: column; justify-content: center;
        }
        
        .timer-label {
            font-size: 0.75rem; color: var(--text-grey); text-transform: uppercase; font-weight: 600;
        }

        .timer-big {
            font-family: 'Rajdhani', sans-serif;
            font-size: 2.8rem;
            font-weight: 700;
            color: var(--wingo-purple);
            text-shadow: 0 2px 10px rgba(139, 92, 246, 0.2);
            line-height: 1;
            margin: 5px 0;
        }
        
        .period-pill {
            background: rgba(139, 92, 246, 0.1);
            color: var(--wingo-purple);
            padding: 3px 12px;
            border-radius: 15px;
            font-size: 0.8rem;
            font-weight: 600;
            display: inline-block;
        }

        /* --- PREDICTION SECTION --- */
        .pred-section {
            padding: 5px;
            text-align: center;
        }
        .next-res {
            font-size: 0.95rem; color: var(--text-dark); margin-bottom: 8px; font-weight: 500;
        }
        .next-res span {
            color: var(--neon-green); font-weight: 800; font-size: 1.2rem;
        }

        .input-glass {
            width: 100%; padding: 10px;
            background: #f1f3f5;
            border: 2px solid #e9ecef;
            color: #333; border-radius: 8px;
            text-align: center; font-size: 1rem; font-weight: 600;
            outline: none; margin-bottom: 8px;
            transition: 0.3s;
        }
        .input-glass:focus { border-color: var(--wingo-purple); background: #fff; box-shadow: 0 0 10px rgba(139, 92, 246, 0.2); }

        /* Buttons */
        .btn-pro {
            width: 100%; padding: 10px; border: none; border-radius: 8px;
            font-weight: 700; text-transform: uppercase; font-size: 0.8rem;
            cursor: pointer; color: #fff; margin-bottom: 5px;
            transition: 0.2s;
        }
        .btn-green { background: linear-gradient(135deg, #28c76f, #1f9d57); box-shadow: 0 4px 10px rgba(40, 199, 111, 0.3); }
        .btn-red { background: linear-gradient(135deg, #ea5455, #c0392b); box-shadow: 0 4px 10px rgba(234, 84, 85, 0.3); }
        .btn-pro:active { transform: scale(0.95); }

        /* Tables */
        .pro-table-container {
            background: #ffffff;
            border-radius: 12px;
            overflow: hidden;
            margin-top: 15px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.05);
            border: 1px solid #f1f1f1;
        }
        .pro-table-header {
            padding: 12px 15px;
            background: #f8f9fa;
            font-weight: 700; color: var(--text-dark); font-size: 0.95rem;
            border-bottom: 1px solid #eee;
        }
        
        table.dataTable tbody tr { background-color: #fff !important; color: #333; font-size: 0.85rem; }
        table.dataTable tbody tr:nth-child(even) { background-color: #f9f9f9 !important; }
        table.dataTable tbody td { border-bottom: 1px solid #eee !important; padding: 10px 8px !important; }
        table.dataTable thead th { background-color: #fff !important; color: #555; padding: 10px 8px !important; font-size: 0.85rem; border-bottom: 2px solid #eee !important; }
        
        #copied {
            visibility: hidden; position: fixed; bottom: 20px; left: 50%; 
            transform: translateX(-50%); background: #333; color: #fff; 
            padding: 8px 20px; border-radius: 30px; z-index: 9999; font-size: 0.9rem;
            box-shadow: 0 5px 15px rgba(0,0,0,0.2);
        }

        /* --- MOBILE LAYOUT --- */
        @media (max-width: 768px) {
            .content-wrapper { padding: 15px !important; }
            .wingo-pro-card { padding: 12px !important; margin-bottom: 15px !important; }
            .timer-big { font-size: 2.5rem; }
            .row-compact { display: flex; flex-wrap: nowrap; gap: 10px; }
            .col-compact { width: 50%; flex: 0 0 50%; max-width: 50%; }
            table.dataTable { width: 100% !important; margin: 0 !important; }
        }
    </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">
                <a class="navbar-brand brand-logo" href="dashboard.php"><h4 style="color:#ff9f43;font-weight:800;margin:0;">K3<span style="color:#fff;">10M</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"></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"></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"></span>
                </button>
            </div>
        </nav>

        <div class="container-fluid page-body-wrapper">
            
            <nav class="sidebar sidebar-offcanvas" id="sidebar">
                <div class="user-profile">
                    <div class="user-image">
                        <img src="images/faces/face28.png">
                    </div>
                    <div class="user-name">AB Coders</div>
                    <div class="user-designation">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 style="font-size:0.7rem; color:#aaa; text-transform:uppercase;">Time Left</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_kemuru_zehn.php" id="pre" method="POST">
                                        <input type="number" name="username" id="next" class="input-glass" placeholder="111-666" autocomplete="off">
                                        <div style="display:flex; gap:5px;">
                                            <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: 10px; background: linear-gradient(90deg, #ff9f43, #ffc107); color:#fff; display:flex; justify-content:space-between; align-items:center;">
                        <span style="font-size:0.9rem; font-weight:600;">TOTAL BETS</span>
                        <span style="font-size:1.2rem; font-weight:800;">₹ <span id="tobet">0</span></span>
                    </div>

                    <div class="pro-table-container">
                        <div class="pro-table-header">
                            <span><i class="fa-solid fa-list"></i> Manual History</span>
                        </div>
                        <div style="padding:5px;">
                            <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_kemeru_zehn`");
                                        while($dhadi = mysqli_fetch_array($samasye)){
                                    ?>
                                        <tr>
                                            <td style="color:#7367f0; font-weight:bold;"><?php echo $dhadi["banna"]; ?></td>
                                            <td style="font-weight:bold; color:#fff;"><?php echo $dhadi["sankhye"]; ?></td>
                                            <td style="color:#ff9f43;">Wait</td>
                                            <td style="color:#aaa;">Wait</td>
                                            <td style="color:#aaa;">Wait</td>
                                        </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>

                    <div class="pro-table-container">
                        <div class="pro-table-header" style="border-bottom-color:#ff9f43;">
                            <span style="color:#ff9f43;"><i class="fa-solid fa-dice"></i> Live Bets</span>
                        </div>
                        <div style="padding:5px;">
                            <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:15px; text-align:center; color:#777; font-size:0.8rem;">
                    <span>© 2026 AB Coders</span>
                </footer>
                
                <div id="copied">Enter Number (111-666)</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 (K3 10 MINUTE) ---
    $(function () {
        setInterval(function() { 
            wingoonetotal();
        }, 2000);

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

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

    function sub() {
        var p = document.getElementById("next").value;
        if (p === '' || p < 111 || p > 666 || 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_kemuru_zehn.php" });
    }
    function unsetman() { resetman(); location.reload(); }

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

    // --- 10 MINUTE TIMER LOGIC ---
    function startTimer(serverTime) {
        var distance = 600 - serverTime % 600; // 600 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 === 590) { resetman(); }
            if (distance === 588) { location.reload(); }
            
            distance--;
            if (distance < 0) {
                clearInterval(interval);
                fetchServerTime().then(st => startTimer(st));
            }
        }, 1000);
    }

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