<?php
/* =========================================================
   MANAGE SALARY PRO - FULL LOGIC + ULTRA UI
   ========================================================= */
session_start();
if($_SESSION['unohs'] == null){
    header("location:index.php?msg=unauthorized");
    exit();
}
include ("conn.php");

// --- DATE LOGIC (DO NOT TOUCH) ---
$tem = date("Y-m-d H:i:s");
$timestamp = strtotime($tem);
$newTimestamp = $timestamp - 60 * 60 * 24;
$startDate = date("Y-m-d 00:00:00", $newTimestamp);
$endDate = date("Y-m-d H:i:s", $timestamp);
$newDate = date("Y-m-d H:i:s", $newTimestamp);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=1200, initial-scale=0.1">
    <title>Salary Manager Pro</title>
    
    <link href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700;800;900&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    
    <link rel="stylesheet" href="vendors/mdi/css/materialdesignicons.min.css">
    <link rel="stylesheet" href="vendors/base/vendor.bundle.base.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css">
    <link rel="stylesheet" href="css/style.css">

    <style>
        /* --- ULTRA MODERN THEME --- */
        :root {
            --bg-color: #eef2f5;
            --primary: #4e54c8;
            --secondary: #8f94fb;
            --accent: #ff4b1f;
            --success: #00b09b;
            --text-dark: #2d3436;
            --white: #ffffff;
            --glow-primary: 0 0 15px rgba(78, 84, 200, 0.5);
            --glow-success: 0 0 15px rgba(0, 176, 155, 0.4);
        }

        body { 
            background-color: var(--bg-color); 
            font-family: 'Nunito', sans-serif; 
            color: var(--text-dark);
        }
        .content-wrapper { padding: 30px; }

        /* --- TABLE CARD STYLE --- */
        .table-card { 
            background: var(--white); border-radius: 30px; padding: 30px; 
            box-shadow: 0 10px 40px rgba(0,0,0,0.05); margin-bottom: 30px;
            border: 1px solid #fff;
        }
        .card-header-custom {
            display: flex; justify-content: space-between; align-items: center; margin-bottom: 25px;
            border-bottom: 2px dashed #eee; padding-bottom: 15px;
        }
        .table-title { 
            font-size: 1.8rem; font-weight: 900; color: var(--primary); 
            display: flex; align-items: center; gap: 15px; 
            text-transform: uppercase; letter-spacing: -0.5px;
        }

        /* --- ADVANCED DATATABLE --- */
        .table-responsive { overflow-x: auto !important; padding-bottom: 10px; }
        
        table.dataTable { width: 100% !important; border-collapse: separate; border-spacing: 0 10px; }
        
        table.dataTable thead th { 
            background: transparent; color: #666; padding: 15px; 
            font-weight: 800; font-size: 0.85rem;
            text-transform: uppercase; border-bottom: none !important;
            white-space: nowrap;
        }
        
        table.dataTable tbody tr {
            background: #fff; box-shadow: 0 5px 15px rgba(0,0,0,0.03);
            border-radius: 15px; transition: transform 0.2s;
        }
        table.dataTable tbody tr:hover { transform: scale(1.005); background: #fcfcfc; z-index: 10; position: relative; }

        table.dataTable tbody td { 
            padding: 20px 15px; font-weight: 700; color: #444; 
            border: none; vertical-align: middle; white-space: nowrap; font-size: 0.9rem;
        }
        table.dataTable tbody td:first-child { border-radius: 15px 0 0 15px; }
        table.dataTable tbody td:last-child { border-radius: 0 15px 15px 0; }

        /* --- BADGES & ICONS --- */
        .badge-id { background: #e0e7ff; color: var(--primary); padding: 5px 10px; border-radius: 8px; font-weight: 900; }
        .text-green { color: #00b09b; font-weight: 800; }
        .text-red { color: #ff4b1f; font-weight: 800; }
        .text-blue { color: #4e54c8; font-weight: 800; }
        
        /* Edit & Pay Buttons */
        .btn-edit-salary {
            width: 35px; height: 35px; border-radius: 50%; background: #f1f2f6; 
            display: flex; align-items: center; justify-content: center; color: #666; transition: 0.3s;
            text-decoration: none;
        }
        .btn-edit-salary:hover { background: var(--primary); color: #fff; transform: rotate(15deg); box-shadow: var(--glow-primary); }

        .btn-pay {
            background: var(--success); color: white; padding: 8px 20px; border-radius: 30px;
            font-weight: 800; text-decoration: none; box-shadow: 0 4px 10px rgba(0, 176, 155, 0.2);
            transition: 0.3s; display: inline-block;
        }
        .btn-pay:hover { transform: translateY(-3px); box-shadow: var(--glow-success); color: white; }

        /* --- MODAL STYLES --- */
        .modal-content { border-radius: 20px; border: none; box-shadow: 0 25px 50px rgba(0,0,0,0.2); }
        .modal-header { background: linear-gradient(135deg, #4e54c8, #8f94fb); color: white; border-radius: 20px 20px 0 0; }
        .form-control { border-radius: 10px; height: 45px; font-weight: 700; border: 2px solid #eee; }
        .form-control:focus { border-color: var(--primary); box-shadow: none; }
        .btn-danger { background: var(--accent); border: none; border-radius: 10px; font-weight: 800; width: 100%; padding: 12px; }

        /* Navbar & Sidebar */
        .navbar { background: #fff !important; box-shadow: 0 4px 20px rgba(0,0,0,0.03) !important; }
        .dots-menu { font-size: 1.5rem; color: var(--text-dark); cursor: pointer; padding: 0 15px; }
    </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">
             <h3 style="color:var(--primary); font-weight:900; margin:0; letter-spacing:-1px;">
                <i class="fa-solid fa-sack-dollar"></i> SALARY<span style="color:var(--text-dark);">PRO</span>
            </h3>
        </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-staggered" style="font-size:24px;"></i>
            </button>
            <ul class="navbar-nav navbar-nav-right">
                <li class="nav-item d-flex align-items-center">
                    <a class="nav-link" href="logout.php" style="font-weight:800; color:var(--text-dark); margin-right:10px; display:flex; align-items:center; gap:8px;">
                        <i class="fa-solid fa-power-off" style="color:var(--accent); background:#fff0f0; padding:8px; border-radius:50%;"></i> Logout
                    </a>
                </li>
            </ul>
        </div>
    </nav>

    <div class="container-fluid page-body-wrapper">
        <nav class="sidebar sidebar-offcanvas" id="sidebar" style="background:#fff;">
            <div class="user-profile" style="text-align:center; padding:30px 15px;">
                <div class="user-image">
                    <img src="images/faces/face28.png" style="width:80px; height:80px; border-radius:50%; border:4px solid #e0e7ff; padding:3px;">
                </div>
                <div class="user-name" style="font-weight:900; font-size:1.2rem; margin-top:15px;">AB Coders</div>
                <div class="user-designation" style="color:#888; font-weight:700; font-size:0.9rem;">Admin</div>
            </div>
            <?php include 'compass.php'; ?>
        </nav>

        <div class="main-panel">
            <div class="content-wrapper">
                
                <div class="table-card">
                    <div class="card-header-custom">
                        <span class="table-title">
                            <span style="background: var(--bg-color); padding: 10px; border-radius: 12px; color: var(--primary);">
                                <i class="fa-solid fa-file-invoice-dollar"></i>
                            </span> 
                            Daily Salary Report
                        </span>
                        <span style="font-weight:700; color:#888;">
                            <?php if(isset($_GET['msg']) && $_GET['msg']=="updt") { echo '<span style="color:#00b09b;">✨ Updated Successfully!</span>'; } ?>
                        </span>
                    </div>

                    <form id="formID" name="formID" method="post" action="#" enctype="multipart/form-data">
                        <div class="table-responsive">
                            <table id="example1" class="table">
                                <thead>
                                    <tr>
                                        <th><i class="fa-solid fa-id-badge"></i> Cust ID</th>
                                        <th><i class="fa-solid fa-money-bill-wave"></i> Dir. Dep.</th>
                                        <th><i class="fa-solid fa-users-line"></i> Team Dep.</th>
                                        <th><i class="fa-solid fa-dice"></i> Dir. Bet</th>
                                        <th><i class="fa-solid fa-dice-d20"></i> Team Bet</th>          
                                        <th><i class="fa-solid fa-user-group"></i> Betters</th>
                                        <th><i class="fa-solid fa-hand-holding-dollar"></i> Dir. W/D</th>
                                        <th><i class="fa-solid fa-money-bill-transfer"></i> Team W/D</th>
                                        <th><i class="fa-solid fa-chart-line"></i> P/L Rpt</th>
                                        <th><i class="fa-solid fa-sack-dollar"></i> Salary</th>
                                        <th><i class="fa-solid fa-check-double"></i> Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php 
                                        // --- BACKEND QUERY START ---
                                        $query = mysqli_query($conn, "SELECT userid, salary 
                                                                         FROM `dailysalary` 
                                                                         WHERE salary > 0 
                                                                         AND `createdate` BETWEEN '$startDate' AND '$endDate' AND status = 0 
                                                                         ORDER BY macau DESC");
                                        $i=0; 
                                        while($row = mysqli_fetch_array($query)){
                                            $i++;
                                    ?>
                                    <tr>
                                        <td>
                                            <?php 
                                                $userid = $row['userid'];
                                                $mob=mysqli_query($conn,"SELECT id, owncode FROM `shonu_subjects` WHERE id = $userid");
                                                $mobrow = mysqli_fetch_array($mob);
                                                $owncode = $mobrow['owncode'];
                                                $mobile = $mobrow['id'];
                                                echo '<span class="badge-id">#'.$mobile.'</span>';
                                            ?>
                                        </td>
                                        
                                        <td>
                                            <span class="text-green">
                                            <?php
                                                $mob=mysqli_query($conn,"SELECT SUM(motta) as tomo FROM thevani WHERE balakedara = $userid and `sthiti`='1' && DATE(`dinankavannuracisi`)=DATE('".$newDate."')");
                                                $mobrow = mysqli_fetch_array($mob);
                                                echo ($mobrow['tomo'] == null) ? 0 : $mobrow['tomo'];
                                            ?>
                                            </span>
                                        </td>

                                        <td>
                                            <span class="text-green">
                                            <?php
                                                $mob=mysqli_query($conn,"SELECT SUM(motta) as tomo FROM thevani WHERE balakedara IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') and `sthiti`='1' && DATE(`dinankavannuracisi`)=DATE('".$newDate."')");
                                                $mobrow = mysqli_fetch_array($mob);
                                                echo ($mobrow['tomo'] == null) ? 0 : $mobrow['tomo'];
                                            ?>
                                            </span>
                                        </td>

                                        <td>
                                            <span class="text-blue">
                                            <?php
                                                $selectbetting_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet from `bajikattuttate` where `byabaharkarta`='".$userid."' && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_o = mysqli_fetch_array($selectbetting_o);
                                                $tbet_wg_1 = $selectbettingar_o['tbet'];
                                                
                                                $selectbetting_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet from `bajikattuttate_drei` where `byabaharkarta`='".$userid."' && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_o = mysqli_fetch_array($selectbetting_o);
                                                $tbet_wg_3 = $selectbettingar_o['tbet'];
                                                
                                                $selectbetting_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet from `bajikattuttate_funf` where `byabaharkarta`='".$userid."' && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_o = mysqli_fetch_array($selectbetting_o);
                                                $tbet_wg_5 = $selectbettingar_o['tbet'];
                                                
                                                $selectbetting_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet from `bajikattuttate_zehn` where `byabaharkarta`='".$userid."' && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_o = mysqli_fetch_array($selectbetting_o);
                                                $tbet_wg_10 = $selectbettingar_o['tbet'];

                                                $selectbetting_one_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet_one from `bajikattuttate_aidudi` where `byabaharkarta`='".$userid."' && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_one_o = mysqli_fetch_array($selectbetting_one_o);
                                                $tbet_5d_1 = $selectbettingar_one_o['tbet_one'];
                                                
                                                $selectbetting_one_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet_one from `bajikattuttate_aidudi_drei` where `byabaharkarta`='".$userid."' && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_one_o = mysqli_fetch_array($selectbetting_one_o);
                                                $tbet_5d_3 = $selectbettingar_one_o['tbet_one'];
                                                
                                                $selectbetting_one_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet_one from `bajikattuttate_aidudi_funf` where `byabaharkarta`='".$userid."' && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_one_o = mysqli_fetch_array($selectbetting_one_o);
                                                $tbet_5d_5 = $selectbettingar_one_o['tbet_one'];
                                                
                                                $selectbetting_one_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet_one from `bajikattuttate_aidudi_zehn` where `byabaharkarta`='".$userid."' && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_one_o = mysqli_fetch_array($selectbetting_one_o);
                                                $tbet_5d_10 = $selectbettingar_one_o['tbet_one'];

                                                $selectbetting_ten_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet_ten from `bajikattuttate_kemuru` where `byabaharkarta`='".$userid."' && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_ten_o = mysqli_fetch_array($selectbetting_ten_o);
                                                $tbet_k3_1 = $selectbettingar_ten_o['tbet_ten'];
                                                
                                                $selectbetting_ten_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet_ten from `bajikattuttate_kemuru_drei` where `byabaharkarta`='".$userid."' && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_ten_o = mysqli_fetch_array($selectbetting_ten_o);
                                                $tbet_k3_3 = $selectbettingar_ten_o['tbet_ten'];
                                                
                                                $selectbetting_ten_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet_ten from `bajikattuttate_kemuru_funf` where `byabaharkarta`='".$userid."' && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_ten_o = mysqli_fetch_array($selectbetting_ten_o);
                                                $tbet_k3_5 = $selectbettingar_ten_o['tbet_ten'];
                                                
                                                $selectbetting_ten_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet_ten from `bajikattuttate_kemuru_zehn` where `byabaharkarta`='".$userid."' && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_ten_o = mysqli_fetch_array($selectbetting_ten_o);
                                                $tbet_k3_10 = $selectbettingar_ten_o['tbet_ten'];
                                                
                                                $selectbetting_av = mysqli_query($conn,"select SUM(`amount`) as tbet_av from `crashbetrecord` where `username`='".$mobile."' && DATE(`time`)=DATE('".$newDate."')");
                                                $selectbettingar_av = mysqli_fetch_array($selectbetting_av);
                                                $tbet_av = $selectbettingar_av['tbet_av'];
                                                
                                                $total_tbet_o = $tbet_wg_1 + $tbet_wg_3 + $tbet_wg_5 + $tbet_wg_10 + $tbet_5d_1 + $tbet_5d_3 + $tbet_5d_5 + $tbet_5d_10 + $tbet_k3_1 + $tbet_k3_3 + $tbet_k3_5 + $tbet_k3_10 + $tbet_av;
                                                echo $total_tbet_o;
                                            ?>
                                            </span>
                                        </td>

                                        <td>
                                            <span class="text-blue">
                                            <?php
                                                $selectbetting_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet from `bajikattuttate` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_o = mysqli_fetch_array($selectbetting_o);
                                                $tbet_wg_1 = $selectbettingar_o['tbet'];
                                                
                                                $selectbetting_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet from `bajikattuttate_drei` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_o = mysqli_fetch_array($selectbetting_o);
                                                $tbet_wg_3 = $selectbettingar_o['tbet'];
                                                
                                                $selectbetting_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet from `bajikattuttate_funf` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_o = mysqli_fetch_array($selectbetting_o);
                                                $tbet_wg_5 = $selectbettingar_o['tbet'];
                                                
                                                $selectbetting_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet from `bajikattuttate_zehn` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_o = mysqli_fetch_array($selectbetting_o);
                                                $tbet_wg_10 = $selectbettingar_o['tbet'];

                                                $selectbetting_one_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet_one from `bajikattuttate_aidudi` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_one_o = mysqli_fetch_array($selectbetting_one_o);
                                                $tbet_5d_1 = $selectbettingar_one_o['tbet_one'];
                                                
                                                $selectbetting_one_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet_one from `bajikattuttate_aidudi_drei` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_one_o = mysqli_fetch_array($selectbetting_one_o);
                                                $tbet_5d_3 = $selectbettingar_one_o['tbet_one'];
                                                
                                                $selectbetting_one_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet_one from `bajikattuttate_aidudi_funf` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_one_o = mysqli_fetch_array($selectbetting_one_o);
                                                $tbet_5d_5 = $selectbettingar_one_o['tbet_one'];
                                                
                                                $selectbetting_one_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet_one from `bajikattuttate_aidudi_zehn` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_one_o = mysqli_fetch_array($selectbetting_one_o);
                                                $tbet_5d_10 = $selectbettingar_one_o['tbet_one'];

                                                $selectbetting_ten_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet_ten from `bajikattuttate_kemuru` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_ten_o = mysqli_fetch_array($selectbetting_ten_o);
                                                $tbet_k3_1 = $selectbettingar_ten_o['tbet_ten'];
                                                
                                                $selectbetting_ten_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet_ten from `bajikattuttate_kemuru_drei` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_ten_o = mysqli_fetch_array($selectbetting_ten_o);
                                                $tbet_k3_3 = $selectbettingar_ten_o['tbet_ten'];
                                                
                                                $selectbetting_ten_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet_ten from `bajikattuttate_kemuru_funf` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_ten_o = mysqli_fetch_array($selectbetting_ten_o);
                                                $tbet_k3_5 = $selectbettingar_ten_o['tbet_ten'];
                                                
                                                $selectbetting_ten_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet_ten from `bajikattuttate_kemuru_zehn` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_ten_o = mysqli_fetch_array($selectbetting_ten_o);
                                                $tbet_k3_10 = $selectbettingar_ten_o['tbet_ten'];
                                                
                                                $selectbetting_av = mysqli_query($conn,"select SUM(`amount`) as tbet_av from `crashbetrecord` where `username` IN (SELECT mobile FROM shonu_subjects WHERE code = '$owncode') && DATE(`time`)=DATE('".$newDate."')");
                                                $selectbettingar_av = mysqli_fetch_array($selectbetting_av);
                                                $tbet_av = $selectbettingar_av['tbet_av'];
                                                
                                                $total_tbet_o_og = $tbet_wg_1 + $tbet_wg_3 + $tbet_wg_5 + $tbet_wg_10 + $tbet_5d_1 + $tbet_5d_3 + $tbet_5d_5 + $tbet_5d_10 + $tbet_k3_1 + $tbet_k3_3 + $tbet_k3_5 + $tbet_k3_10 + $tbet_av;
                                                echo $total_tbet_o_og;
                                            ?>
                                            </span>
                                        </td>

                                        <td>
                                            <span style="font-weight:bold; color:#666;">
                                            <?php
                                                $selectbetting_o = mysqli_query($conn,"select COUNT(DISTINCT `byabaharkarta`) as tbet from `bajikattuttate` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_o = mysqli_fetch_array($selectbetting_o);
                                                $tbet_wg_1 = $selectbettingar_o['tbet'];
                                                
                                                $selectbetting_o = mysqli_query($conn,"select COUNT(DISTINCT `byabaharkarta`) as tbet from `bajikattuttate_drei` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_o = mysqli_fetch_array($selectbetting_o);
                                                $tbet_wg_3 = $selectbettingar_o['tbet'];
                                                
                                                $selectbetting_o = mysqli_query($conn,"select COUNT(DISTINCT `byabaharkarta`) as tbet from `bajikattuttate_funf` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_o = mysqli_fetch_array($selectbetting_o);
                                                $tbet_wg_5 = $selectbettingar_o['tbet'];
                                                
                                                $selectbetting_o = mysqli_query($conn,"select COUNT(DISTINCT `byabaharkarta`) as tbet from `bajikattuttate_zehn` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_o = mysqli_fetch_array($selectbetting_o);
                                                $tbet_wg_10 = $selectbettingar_o['tbet'];

                                                $selectbetting_one_o = mysqli_query($conn,"select COUNT(DISTINCT `byabaharkarta`) as tbet_one from `bajikattuttate_aidudi` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_one_o = mysqli_fetch_array($selectbetting_one_o);
                                                $tbet_5d_1 = $selectbettingar_one_o['tbet_one'];
                                                
                                                $selectbetting_one_o = mysqli_query($conn,"select COUNT(DISTINCT `byabaharkarta`) as tbet_one from `bajikattuttate_aidudi_drei` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_one_o = mysqli_fetch_array($selectbetting_one_o);
                                                $tbet_5d_3 = $selectbettingar_one_o['tbet_one'];
                                                
                                                $selectbetting_one_o = mysqli_query($conn,"select COUNT(DISTINCT `byabaharkarta`) as tbet_one from `bajikattuttate_aidudi_funf` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_one_o = mysqli_fetch_array($selectbetting_one_o);
                                                $tbet_5d_5 = $selectbettingar_one_o['tbet_one'];
                                                
                                                $selectbetting_one_o = mysqli_query($conn,"select COUNT(DISTINCT `byabaharkarta`) as tbet_one from `bajikattuttate_aidudi_zehn` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_one_o = mysqli_fetch_array($selectbetting_one_o);
                                                $tbet_5d_10 = $selectbettingar_one_o['tbet_one'];

                                                $selectbetting_ten_o = mysqli_query($conn,"select COUNT(DISTINCT `byabaharkarta`) as tbet_ten from `bajikattuttate_kemuru` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_ten_o = mysqli_fetch_array($selectbetting_ten_o);
                                                $tbet_k3_1 = $selectbettingar_ten_o['tbet_ten'];
                                                
                                                $selectbetting_ten_o = mysqli_query($conn,"select COUNT(DISTINCT `byabaharkarta`) as tbet_ten from `bajikattuttate_kemuru_drei` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_ten_o = mysqli_fetch_array($selectbetting_ten_o);
                                                $tbet_k3_3 = $selectbettingar_ten_o['tbet_ten'];
                                                
                                                $selectbetting_ten_o = mysqli_query($conn,"select COUNT(DISTINCT `byabaharkarta`) as tbet_ten from `bajikattuttate_kemuru_funf` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_ten_o = mysqli_fetch_array($selectbetting_ten_o);
                                                $tbet_k3_5 = $selectbettingar_ten_o['tbet_ten'];
                                                
                                                $selectbetting_ten_o = mysqli_query($conn,"select COUNT(DISTINCT `byabaharkarta`) as tbet_ten from `bajikattuttate_kemuru_zehn` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."')");
                                                $selectbettingar_ten_o = mysqli_fetch_array($selectbetting_ten_o);
                                                $tbet_k3_10 = $selectbettingar_ten_o['tbet_ten'];
                                                
                                                $selectbetting_av = mysqli_query($conn,"select COUNT(DISTINCT `username`) as tbet_av from `crashbetrecord` where `username` IN (SELECT mobile FROM shonu_subjects WHERE code = '$owncode') && DATE(`time`)=DATE('".$newDate."')");
                                                $selectbettingar_av = mysqli_fetch_array($selectbetting_av);
                                                $tbet_av = $selectbettingar_av['tbet_av'];
                                                
                                                $total_tbet_o = $tbet_wg_1 + $tbet_wg_3 + $tbet_wg_5 + $tbet_wg_10 + $tbet_5d_1 + $tbet_5d_3 + $tbet_5d_5 + $tbet_5d_10 + $tbet_k3_1 + $tbet_k3_3 + $tbet_k3_5 + $tbet_k3_10 + $tbet_av;
                                                echo $total_tbet_o;
                                            ?>
                                            </span>
                                        </td>

                                        <td>
                                            <span class="text-red">
                                            <?php
                                                $mob=mysqli_query($conn,"SELECT SUM(motta) as tomo FROM hintegedukolli WHERE balakedara = $userid and `sthiti`='1' && DATE(`dinankavannuracisi`)=DATE('".$newDate."')");
                                                $mobrow = mysqli_fetch_array($mob);
                                                echo ($mobrow['tomo'] == null) ? 0 : $mobrow['tomo'];
                                            ?>
                                            </span>
                                        </td>

                                        <td>
                                            <span class="text-red">
                                            <?php
                                                $mob=mysqli_query($conn,"SELECT SUM(motta) as tomo FROM hintegedukolli WHERE balakedara IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') and `sthiti`='1' && DATE(`dinankavannuracisi`)=DATE('".$newDate."')");
                                                $mobrow = mysqli_fetch_array($mob);
                                                echo ($mobrow['tomo'] == null) ? 0 : $mobrow['tomo'];
                                            ?>
                                            </span>
                                        </td>

                                        <td>
                                            <span style="font-weight:900; color:#333;">
                                            <?php
                                                $selectbetting_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet from `bajikattuttate` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."') && `phalaphala`='gagner'");
                                                $selectbettingar_o = mysqli_fetch_array($selectbetting_o);
                                                $tbet_wg_1 = $selectbettingar_o['tbet'];
                                                
                                                $selectbetting_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet from `bajikattuttate_drei` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."') && `phalaphala`='gagner'");
                                                $selectbettingar_o = mysqli_fetch_array($selectbetting_o);
                                                $tbet_wg_3 = $selectbettingar_o['tbet'];
                                                
                                                $selectbetting_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet from `bajikattuttate_funf` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."') && `phalaphala`='gagner'");
                                                $selectbettingar_o = mysqli_fetch_array($selectbetting_o);
                                                $tbet_wg_5 = $selectbettingar_o['tbet'];
                                                
                                                $selectbetting_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet from `bajikattuttate_zehn` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."') && `phalaphala`='gagner'");
                                                $selectbettingar_o = mysqli_fetch_array($selectbetting_o);
                                                $tbet_wg_10 = $selectbettingar_o['tbet'];

                                                $selectbetting_one_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet_one from `bajikattuttate_aidudi` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."') && `phalaphala`='gagner'");
                                                $selectbettingar_one_o = mysqli_fetch_array($selectbetting_one_o);
                                                $tbet_5d_1 = $selectbettingar_one_o['tbet_one'];
                                                
                                                $selectbetting_one_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet_one from `bajikattuttate_aidudi_drei` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."') && `phalaphala`='gagner'");
                                                $selectbettingar_one_o = mysqli_fetch_array($selectbetting_one_o);
                                                $tbet_5d_3 = $selectbettingar_one_o['tbet_one'];
                                                
                                                $selectbetting_one_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet_one from `bajikattuttate_aidudi_funf` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."') && `phalaphala`='gagner'");
                                                $selectbettingar_one_o = mysqli_fetch_array($selectbetting_one_o);
                                                $tbet_5d_5 = $selectbettingar_one_o['tbet_one'];
                                                
                                                $selectbetting_one_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet_one from `bajikattuttate_aidudi_zehn` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."') && `phalaphala`='gagner'");
                                                $selectbettingar_one_o = mysqli_fetch_array($selectbetting_one_o);
                                                $tbet_5d_10 = $selectbettingar_one_o['tbet_one'];

                                                $selectbetting_ten_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet_ten from `bajikattuttate_kemuru` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."') && `phalaphala`='gagner'");
                                                $selectbettingar_ten_o = mysqli_fetch_array($selectbetting_ten_o);
                                                $tbet_k3_1 = $selectbettingar_ten_o['tbet_ten'];
                                                
                                                $selectbetting_ten_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet_ten from `bajikattuttate_kemuru_drei` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."') && `phalaphala`='gagner'");
                                                $selectbettingar_ten_o = mysqli_fetch_array($selectbetting_ten_o);
                                                $tbet_k3_3 = $selectbettingar_ten_o['tbet_ten'];
                                                
                                                $selectbetting_ten_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet_ten from `bajikattuttate_kemuru_funf` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."') && `phalaphala`='gagner'");
                                                $selectbettingar_ten_o = mysqli_fetch_array($selectbetting_ten_o);
                                                $tbet_k3_5 = $selectbettingar_ten_o['tbet_ten'];
                                                
                                                $selectbetting_ten_o = mysqli_query($conn,"select SUM(`ketebida`) as tbet_ten from `bajikattuttate_kemuru_zehn` where `byabaharkarta` IN (SELECT id FROM shonu_subjects WHERE code = '$owncode') && DATE(`tiarikala`)=DATE('".$newDate."') && `phalaphala`='gagner'");
                                                $selectbettingar_ten_o = mysqli_fetch_array($selectbetting_ten_o);
                                                $tbet_k3_10 = $selectbettingar_ten_o['tbet_ten'];
                                                
                                                $selectbetting_av = mysqli_query($conn,"select SUM(`amount`) as tbet_av from `crashbetrecord` where `username` IN (SELECT mobile FROM shonu_subjects WHERE code = '$owncode') && DATE(`time`)=DATE('".$newDate."') && `status`='success'");
                                                $selectbettingar_av = mysqli_fetch_array($selectbetting_av);
                                                $tbet_av = $selectbettingar_av['tbet_av'];
                                                
                                                $total_tbet_o_1 = $tbet_wg_1 + $tbet_wg_3 + $tbet_wg_5 + $tbet_wg_10 + $tbet_5d_1 + $tbet_5d_3 + $tbet_5d_5 + $tbet_5d_10 + $tbet_k3_1 + $tbet_k3_3 + $tbet_k3_5 + $tbet_k3_10 + $tbet_av;
                                                echo $total_tbet_o_og - $total_tbet_o_1;
                                            ?>
                                            </span>
                                        </td>

                                        <td>
                                            <div style="display:flex; align-items:center; gap:10px;">
                                                <span class="text-blue"><?php echo $row['salary']; ?></span>
                                                <?php 
                                                    $aid = $userid;
                                                    $motta = $row['salary'];
                                                    echo '<a href="javascript:void(0);" onClick="edita('.$aid.','.$mobile.','.$motta.')" class="btn-edit-salary" title="Edit Salary"><i class="fa-solid fa-pen"></i></a>';
                                                ?>
                                            </div>
                                        </td>

                                        <td>
                                            <?php 
                                                $aid = $userid;
                                                $motta = $row['salary'];
                                                echo '<a href="javascript:void(0);" onClick="edit('.$aid.','.$mobile.','.$motta.')" class="btn-pay" title="Approve Payment"><i class="fa-solid fa-check"></i> Pay</a>';
                                            ?>
                                        </td>

                                    </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                        </div>
                    </form>           
                </div>          
            </div>      
        </div>
        
        <footer class="footer" style="background:transparent; border:none; margin-top:20px;">
            <div class="d-sm-flex justify-content-center">
                <span class="text-muted" style="font-weight:700;">Copyright © 2026 AB Coders</span>
            </div>
        </footer>
    </div>      
</div>  

<div id="excel" class="modal fade" role="dialog">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">                      
                <h5 class="modal-title" style="color:white; font-weight:900;"><i class="fa-solid fa-circle-check"></i> Approve Payment</h5>
                <button type="button" class="close" data-dismiss="modal" style="color:white; opacity:1;">&times;</button>
            </div>
            <form name="type" id="type" enctype="multipart/form-data" action="#" method="post">
                <div class="modal-body" style="padding:25px;">
                    <p id="mob" style="font-weight:700; color:#555; margin-bottom:15px;"></p>
                    <div class="form-group">
                        <label style="font-weight:bold;">Enter Amount</label>  
                        <input class="form-control" id="amount" name="amount" type="text" onkeypress="return isNumber(event)" required>
                        <input class="form-control" id="editid" name="editid" type="hidden">
                    </div>               
                </div>
                <div class="modal-footer" style="border:none; padding:20px;">              
                    <button type="submit" class="btn-danger"><i class="fa-solid fa-paper-plane"></i> Submit</button>
                </div>
            </form>
        </div>
    </div>
</div>

<div id="excela" class="modal fade" role="dialog">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">                      
                <h5 class="modal-title" style="color:white; font-weight:900;"><i class="fa-solid fa-pen-to-square"></i> Edit Salary</h5>
                <button type="button" class="close" data-dismiss="modal" style="color:white; opacity:1;">&times;</button>
            </div>
            <form name="type" id="typea" enctype="multipart/form-data" action="#" method="post">
                <div class="modal-body" style="padding:25px;">
                    <p id="moba" style="font-weight:700; color:#555; margin-bottom:15px;"></p>
                    <div class="form-group">
                        <label style="font-weight:bold;">Enter New Salary</label>  
                        <input class="form-control" id="amounta" name="amount" type="text" onkeypress="return isNumber(event)" required>
                        <input class="form-control" id="editida" name="editid" type="hidden">
                    </div>               
                </div>
                <div class="modal-footer" style="border:none; padding:20px;">              
                    <button type="submit" class="btn-danger"><i class="fa-solid fa-floppy-disk"></i> Update</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="vendors/base/vendor.bundle.base.js"></script>
<script src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.min.js"></script>

<script>
    $(document).ready(function() {
        // Init DataTable with modern settings
        var table = $('#example1').DataTable({          
            "paging": true,
            "lengthChange": false,
            "searching": true,
            "ordering": false,
            "info": true,
            "autoWidth": true,
            "pageLength": 50,
            "language": {
                "search": "",
                "searchPlaceholder": "🔍 Search ID..."
            }
        });
    });

    // JS Logic for Modals (Same as Original)
    function edita(id,mob,balance) {
        $('#excela').modal({backdrop: 'static', keyboard: false})   
        $('#excela').modal('show');
        document.getElementById('moba').innerHTML = 'Mobile: '+mob;
        document.getElementById('amounta').value = balance;
        document.getElementById('editida').value = id;
    }
    
    $(document).ready(function () {
        $("#typea").on('submit',(function(e) {
            e.preventDefault();
            var quantity = $('input#quantitya').val();
            // Basic validation
            $.ajax({
                type: "POST", 
                url: "updatesalaryNow.php",              
                data: new FormData(this), 
                contentType: false,        
                cache: false,             
                processData:false,        
                success: function(html)   
                {
                    if (html == 1) {
                        alert("✅ Amount update successfully!");            
                        $("#typea")[0].reset();
                        $('#excela').modal('hide');
                        window.location ='';
                    }           
                    else if(html==0) { 
                        alert("❌ Some Technical Error....");                       
                    }           
                }
            }); 
        }));            
    });
    
    function edit(id,mob,balance) {
        $('#excel').modal({backdrop: 'static', keyboard: false})   
        $('#excel').modal('show');
        document.getElementById('mob').innerHTML = 'Mobile: '+mob;
        document.getElementById('amount').value = balance;
        document.getElementById('editid').value = id;
    }
    
    $(document).ready(function () {
        $("#type").on('submit',(function(e) {
            e.preventDefault();
            $.ajax({
                type: "POST", 
                url: "updatewalbalNow.php",              
                data: new FormData(this), 
                contentType: false,        
                cache: false,             
                processData:false,        
                success: function(html)   
                {
                    if (html == 1) {
                        alert("✅ Payment Approved successfully!");            
                        $("#type")[0].reset();
                        $('#excel').modal('hide');
                        window.location ='';
                    }           
                    else if(html==0) { 
                        alert("❌ Some Technical Error....");                       
                    }           
                }
            }); 
        }));            
    });
</script>
</body>
</html>