<?php
session_start();
include('../../includes/config.php');
check_user_session($tng_conn);
?>


<?php
checkSession();
?>

<!doctype html>
<html lang="en" class="color-sidebar sidebarcolor1 color-header headercolor1">


<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!--favicon-->
    <link rel="icon" href="<?php echo XyZ_val('logo'); ?>" type="image/png" />
    <!--plugins-->
    <link href="../template/tngweb/assets/plugins/vectormap/jquery-jvectormap-2.0.2.css" rel="stylesheet" />
    <link href="../template/tngweb/assets/plugins/simplebar/css/simplebar.css" rel="stylesheet" />
    <link href="../template/tngweb/assets/plugins/perfect-scrollbar/css/perfect-scrollbar.css" rel="stylesheet" />
    <link href="../template/tngweb/assets/plugins/metismenu/css/metisMenu.min.css" rel="stylesheet" />
    <!-- loader-->
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.4.14/dist/sweetalert2.all.min.js"></script>
<script disable-devtool-auto src='https://cdn.jsdelivr.net/npm/disable-devtool@latest'></script>
    <link href="../template/tngweb/assets/css/pace.min.css" rel="stylesheet" />
    <script src="../template/tngweb/assets/js/pace.min.js"></script>
    <!-- Bootstrap CSS -->
    <link href="../template/tngweb/assets/plugins/datatable/css/dataTables.bootstrap5.min.css" rel="stylesheet" />
    <link href="../template/tngweb/assets/css/bootstrap.min.css" rel="stylesheet">
    <link href="../template/tngweb/assets/css/bootstrap-extended.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&amp;display=swap" rel="stylesheet">
    <link href="../template/tngweb/assets/css/app.css" rel="stylesheet">
    <link href="../template/tngweb/assets/css/icons.css" rel="stylesheet">
    <!-- Theme Style CSS -->

    <link rel="stylesheet" href="../template/tngweb/assets/css/dark-theme.css" />
    <link rel="stylesheet" href="../template/tngweb/assets/css/semi-dark.css" />
    
    <title>Welcome TO - <?php echo XyZ_val('brand'); ?> | <?php echo XyZ_val('title'); ?></title>
  
</head>

<body>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/3.1.62/jquery.inputmask.bundle.js"></script>
    <!--wrapper-->
    <div class="wrapper">
        <!--sidebar wrapper -->
        <div class="sidebar-wrapper" data-simplebar="true">
            <div class="sidebar-header">
                <div>
                    <img src="<?php echo XyZ_val('logo'); ?>" class="logo-icon" alt="logo icon">
                </div>
                <div>
                    <h4 class="logo-text"><?php echo XyZ_val('brand'); ?></h4>
                </div>
                <div class="toggle-icon ms-auto"><i class='bx bx-arrow-to-left'></i>
                </div>
            </div>
             <!--navigation-->
           <ul class="metismenu" id="menu">
              <!-- Sidebar Menu -->


    <!-- Dashboard -->
    <li>
        <a href="index.php">
            <div class="parent-icon"><i class="bx bx-home-alt"></i></div>
            <div class="menu-title">Dashboard</div>
        </a>
    </li>

   <?php renderMemberManagementMenu($udata['type']); ?>

    <li class="menu-item has-sub">
    <a href="javascript:void(0);">
        <div class="parent-icon">
            <i class="bx bx-history"></i>
        </div>
        <div class="menu-title">All Reports</div>
        <i class="bx bx-chevron-down ms-auto"></i>
    </a>

    <ul class="submenu">
        <li>
            <a href="all_history.php">
                <i class="bx bx-file"></i>
                All PDF History
            </a>
        </li>

        <li>
            <a href="wallet_history.php">
                <i class="bx bx-rupee"></i>
                Transaction History
            </a>
        </li>
    </ul>
</li>

  <?php renderWalletMenu(); ?>
    <li>
        <a href="profile.php">
            <div class="parent-icon"><i class="bx bx-user"></i></div>
            <div class="menu-title">Profile</div>
        </a>
    </li>
    <?php renderSupportMenu(); ?>
    <!-- Complaint -->
<li class="menu-item menu-admin">
    <a href="javascript:void(0)" class="menu-link menu-link-complaint">
        <div class="parent-icon"><i class="bx bx-message-square-dots"></i></div>
        <div class="menu-title">Complaint</div>
    </a>
    <ul class="submenu">
        <li><a href="contact.php"><i class="bx bx-right-arrow-alt"></i> Any Query</a></li>
        <li><a href="CheckStatus.php"><i class="bx bx-right-arrow-alt"></i> Check Status</a></li>
    </ul>
</li>

    <?php if($udata['type'] == "admin"){ ?>
    <!-- Website Setting -->
    <li class="menu-item menu-admin">
        <a href="javascript:void(0)" class="menu-link menu-link-website">
            <div class="parent-icon"><i class="bx bx-cog"></i></div>
            <div class="menu-title">Website Setting</div>
        </a>
        <ul class="submenu">
            <li><a href="Services_Manage.php"><i class="bx bx-right-arrow-alt"></i> Services Manage</a></li>
            <li><a href="settings.php"><i class="bx bx-right-arrow-alt"></i> Website Details</a></li>
            <li><a href="admin_contact.php"><i class="bx bx-right-arrow-alt"></i> Query List</a></li>
            <li><a href="importantlinks.php"><i class="bx bx-right-arrow-alt"></i> Important Links</a></li>
            <li><a href="popupalert.php"><i class="bx bx-right-arrow-alt"></i> Popup Alert</a></li>
            <li><a href="commission.php"><i class="bx bx-right-arrow-alt"></i> Recharge Commission</a></li>
        </ul>
    </li>

    <!-- API Setting -->
    <li class="menu-item menu-admin">
        <a href="javascript:void(0)" class="menu-link menu-link-api">
            <div class="parent-icon"><i class="bx bx-code-alt"></i></div>
            <div class="menu-title">API Setting</div>
        </a>
        <ul class="submenu">
            <li><a href="apihistory.php"><i class="bx bx-right-arrow-alt"></i> API History Details</a></li>
            <li><a href="api_manage.php"><i class="bx bx-right-arrow-alt"></i> API Setting Manage</a></li>
        </ul>
    </li>
<?php } ?>


    <!-- Logout -->
   <li>
    <a href="javascript:void(0);" id="logoutBtn">
        <div class="parent-icon"><i class="bx bx-log-out"></i></div>
        <div class="menu-title">Logout</div>
    </a>
</li>

</ul>


                <!-- Settings -->
                
                
                <li>
                    <a
                    </a>
                   
                </li>
                
                <!-- Refer and Earn -->
               <!-- Content Shifted to sidebarextra.php -->
        </div>
        <!--end sidebar wrapper -->
        <!--start header -->
        <header>
            <div class="topbar d-flex align-items-center">
                <nav class="navbar navbar-expand">
                    <div class="mobile-toggle-menu"><i class='bx bx-menu'></i>
                    </div>
                   <?php
                   if(isset($_GET['gettoAdmin']) && $_GET['session'] == 'true'){
                    loginAsAdmin();
                   }
                   
                        if(isset($_SESSION['adminasuser'])== true && $_SESSION['adminusername']!=NULL){
                                ?>
                                <!-- <form action="" method="POST">
                                    <div class="search-bar flex-grow-1">
                                        <div class="position-relative search-bar-box">
                                            <a href="?gettoAdmin=1&session=true" type="submit" class="btn btn-success">Go Back to Admin Panel</a>
                                        </div>
                                    </div>
                                </form> -->
                               
                           
                                <?php 
                            }else{
                                ?>
                           
                                <?php
                            }
                   ?>
                   
                    <a class="btn btn-light" href="wallet.php">Wallet: 
    <?php 
    if (empty($udata['balance'])) {
        echo "0";
    } else {
        echo number_format($udata['balance'], 2); 
    }
    ?>
</a>

                    
                    
                    <div class="top-menu ms-auto">
                        
                        <ul class="navbar-nav align-items-center">
                            <?php if($udata['type'] == 'retailer'): ?>
<button id="upgradeDistributor" class="btn btn-success">
    Upgrade to Distributor
</button>
<?php endif; ?> 
                            <?php if(checkAdmin($udata['type']) == true){ ?> 
                             <?php } ?>
							 <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
document.getElementById('upgradeDistributor')?.addEventListener('click', function() {

    // Step 1: Show initial info
    Swal.fire({
        title: 'Become a Distributor?',
        html: 'Upgrade fee: ₹<?php echo XyZ_val('dist_reg_fee'); ?>. You will earn profit from your users\' transactions.',
        icon: 'info',
        showCancelButton: true,
        confirmButtonText: 'Yes, Upgrade',
        cancelButtonText: 'Cancel'
    }).then((result) => {
        if (result.isConfirmed) {

            
            Swal.fire({
                title: 'Are you sure?',
                text: "Once upgraded, you cannot revert easily!",
                icon: 'warning',
                showCancelButton: true,
                confirmButtonText: 'Yes, Upgrade me',
                cancelButtonText: 'Cancel'
            }).then((confirm) => {
                if(confirm.isConfirmed) {

                    
                    fetch('upgrade_distributor.php', {
                        method: 'POST',
                        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                        body: 'userid=<?php echo $udata["id"]; ?>'
                    })
                    .then(response => response.json())
                    .then(data => {
                        if(data.success){
                            Swal.fire(
                                'Upgraded!',
                                data.msg,
                                'success'
                            ).then(() => location.reload());
                        } else {
                            Swal.fire(
                                'Error!',
                                data.msg,
                                'error'
                            );
                        }
                    })
                    .catch(err => {
                        Swal.fire('Error!', 'Something went wrong!', 'error');
                    });

                }
            });

        }
    });

});
</script>

                           
                                <div class="dropdown-menu dropdown-menu-end">
                                    <a href="javascript:;">
                                        <div class="msg-header">
                                           
     <p class="msg-header-title">Notifications</p>
                                            <p class="msg-header-clear ms-auto">Marks all as read</p>
                                        </div>
                                    </a>
                                    <div class="header-notifications-list">
                                        <a class="dropdown-item" href="javascript:;">
                                            <div class="d-flex align-items-center">
                                                <div class="notify bg-light-primary text-primary"><i
                                                        class="bx bx-group"></i>
                                                </div>
                                                <div class="flex-grow-1">
                                                    <h6 class="msg-name">New Customers<span
                                                            class="msg-time float-end">14 Sec
                                                            ago</span></h6>
                                                    <p class="msg-info">5 new user registered</p>
                                                </div>
                                            </div>
                                        </a>
                                        <a class="dropdown-item" href="javascript:;">
                                            <div class="d-flex align-items-center">
                                                <div class="notify bg-light-danger text-danger"><i
                                                        class="bx bx-cart-alt"></i>
                                                </div>
                                                <div class="flex-grow-1">
                                                    <h6 class="msg-name">New Orders <span class="msg-time float-end">2
                                                            min
                                                            ago</span></h6>
                                                    <p class="msg-info">You have recived new orders</p>
                                                </div>
                                            </div>
                                        </a>
                                        <a class="dropdown-item" href="javascript:;">
                                            <div class="d-flex align-items-center">
                                                <div class="notify bg-light-success text-success"><i
                                                        class="bx bx-file"></i>
                                                </div>
                                                <div class="flex-grow-1">
                                                    <h6 class="msg-name">24 PDF File<span class="msg-time float-end">19
                                                            min
                                                            ago</span></h6>
                                                    <p class="msg-info">The pdf files generated</p>
                                                </div>
                                            </div>
                                        </a>
                                        <a class="dropdown-item" href="javascript:;">
                                            <div class="d-flex align-items-center">
                                                <div class="notify bg-light-warning text-warning"><i
                                                        class="bx bx-send"></i>
                                                </div>
                                                <div class="flex-grow-1">
                                                    <h6 class="msg-name">Time Response <span
                                                            class="msg-time float-end">28 min
                                                            ago</span></h6>
                                                    <p class="msg-info">5.1 min avarage time response</p>
                                                </div>
                                            </div>
                                        </a>
                                        <a class="dropdown-item" href="javascript:;">
                                            <div class="d-flex align-items-center">
                                                <div class="notify bg-light-info text-info"><i
                                                        class="bx bx-home-circle"></i>
                                                </div>
                                                <div class="flex-grow-1">
                                                    <h6 class="msg-name">New Product Approved <span
                                                            class="msg-time float-end">2 hrs ago</span></h6>
                                                    <p class="msg-info">Your new product has approved</p>
                                                </div>
                                            </div>
                                        </a>
                                        <a class="dropdown-item" href="javascript:;">
                                            <div class="d-flex align-items-center">
                                                <div class="notify bg-light-danger text-danger"><i
                                                        class="bx bx-message-detail"></i>
                                                </div>
                                                <div class="flex-grow-1">
                                                    <h6 class="msg-name">New Comments <span class="msg-time float-end">4
                                                            hrs
                                                            ago</span></h6>
                                                    <p class="msg-info">New customer comments recived</p>
                                                </div>
                                            </div>
                                        </a>
                                        <a class="dropdown-item" href="javascript:;">
                                            <div class="d-flex align-items-center">
                                                <div class="notify bg-light-success text-success"><i
                                                        class='bx bx-check-square'></i>
                                                </div>
                                                <div class="flex-grow-1">
                                                    <h6 class="msg-name">Your item is shipped <span
                                                            class="msg-time float-end">5 hrs
                                                            ago</span></h6>
                                                    <p class="msg-info">Successfully shipped your item</p>
                                                </div>
                                            </div>
                                        </a>
                                        <a class="dropdown-item" href="javascript:;">
                                            <div class="d-flex align-items-center">
                                                <div class="notify bg-light-primary text-primary"><i
                                                        class='bx bx-user-pin'></i>
                                                </div>
                                                <div class="flex-grow-1">
                                                    <h6 class="msg-name">New 24 authors<span
                                                            class="msg-time float-end">1 day
                                                            ago</span></h6>
                                                    <p class="msg-info">24 new authors joined last week</p>
                                                </div>
                                            </div>
                                        </a>
                                        <a class="dropdown-item" href="javascript:;">
                                            <div class="d-flex align-items-center">
                                                <div class="notify bg-light-warning text-warning"><i
                                                        class='bx bx-door-open'></i>
                                                </div>
                                                <div class="flex-grow-1">
                                                    <h6 class="msg-name">Defense Alerts <span
                                                            class="msg-time float-end">2 weeks
                                                            ago</span></h6>
                                                    <p class="msg-info">45% less alerts last 4 weeks</p>
                                                </div>
                                            </div>
                                        </a>
                                    </div>
                                    <a href="javascript:;">
                                        <div class="text-center msg-footer">View All Notifications</div>
                                    </a>
                                </div>
                            </li> 
                            
                            <li class="nav-item dropdown dropdown-large">
                                  <?php
                if(checkAdmin($udata['type']) == true){
                    ?>
                                <a class="nav-link dropdown-toggle dropdown-toggle-nocaret position-relative" href="#"
                                    role="button" data-bs-toggle="dropdown" aria-expanded="false"> <span
                                        class="alert-count"><?=  $query_noforadmin;?></span>
                                    <i class='bx bx-comment'></i>
                                </a>
                           
                <?php
                }else{
                    ?>
                        <a class="nav-link dropdown-toggle dropdown-toggle-nocaret position-relative" href="#"
                                    role="button" data-bs-toggle="dropdown" aria-expanded="false"> <span
                                        class="alert-count"><?=  $query_foruser;?></span>
                                    <i class='bx bx-comment'></i>
                                </a>
                                <?php
                }
                ?>
                                
                                  
                                        <div class="msg-header">
                                           
                                        
                                        </div>
                                    
                                    <div class="header-message-list">
                                       
                                    
                                        
                                   
                                </div>
                            </li> 
                        </ul>
                        
                    </div>
                    

                     <div style="margin-right:12px;" class="text-success">
                        <style>
                        #time {
                            color: white;
                            font-size: 18px;
                            font-family: "Times New Roman", Times, serif;
                        }
                        </style>
                        <a id="time"></a>
                        <script>
                        var timeDisplay = document.getElementById("time");

                        function refreshTime() {
                            var dateString = new Date().toLocaleString("en-IN", {
                                timeZone: "Asia/Kolkata"
                            });
                            var formattedString = dateString.replace(", ", " - ");
                            timeDisplay.innerHTML = formattedString;
                        }

                        setInterval(refreshTime, 1000);
                        </script>
                    </div>
                    <div class="user-box dropdown">
                        <a class="d-flex align-items-center nav-link dropdown-toggle dropdown-toggle-nocaret" href="#"
                            role="button" data-bs-toggle="dropdown" aria-expanded="false">
                            <img src="<?php echo XyZ_val('logo'); ?>" class="user-img"
                                alt="user avatar">
                            <div class="user-info ps-3">
                                <p class="user-name mb-0"><?php  echo $udata['name'];  ?></p>
                                <p class="designattion mb-1"><?php  echo $udata['type'];  ?> </p>
                            </div>
                        </a>
                        <ul class="dropdown-menu dropdown-menu-end">
                            <li><a class="dropdown-item" href="profile.php"><i
                                        class="bx bx-user"></i><span>Profile</span></a>
                            </li>
                            <li><a
                            </li>
                            <li><a 
                            </li>
                            <li><a 
                            </li>
                            <li><a
                            </li>
                            <li>
                                 <div class="dropdown-divider mb-0"></div>
                            </li>
                            <li> <a class="dropdown-item" href="javascript:void(0);" id="changePasswordBtn">
    <i class='bx bx-key'></i><span>Change Password</span>
</a>
          </li>
                                <div class="dropdown-divider mb-0"></div>
                            </li>
                            <li><a class="dropdown-item" href="javascript:void(0);" id="logoutDropdownBtn">
    <i class='bx bx-log-out-circle'></i><span>Logout</span>
</a>

                            </li>
                        </ul>
                    </div>
                </nav>
            </div>
           <script>
$(document).ready(function(){

    $('#changePasswordBtn').click(function(){
        Swal.fire({
            title: 'Change Password',
            html:
                '<form id="swalChangePassForm">' +
                '<input name="curpass" type="password" class="swal2-input" placeholder="Current Password" required>' +
                '<input name="npass" type="password" class="swal2-input" placeholder="New Password" required>' +
                '<input name="cpass" type="password" class="swal2-input" placeholder="Confirm Password" required>' +
                '</form>',
            showCancelButton: true,
            confirmButtonText: 'Change',
            preConfirm: () => {
                const form = Swal.getPopup().querySelector('#swalChangePassForm');
                const data = {
                    curpass: form.curpass.value,
                    npass: form.npass.value,
                    cpass: form.cpass.value
                };
                return data;
            }
        }).then((result) => {
            if(result.isConfirmed){
                // Submit via POST by creating a hidden form
                const form = document.createElement('form');
                form.method = 'POST';
                form.style.display = 'none';
                form.innerHTML = 
                    '<input name="curpass" value="'+result.value.curpass+'">' +
                    '<input name="npass" value="'+result.value.npass+'">' +
                    '<input name="cpass" value="'+result.value.cpass+'">';
                document.body.appendChild(form);
                form.submit();
            }
        });
    });

});
</script>
<?php
if(isset($_POST['curpass'], $_POST['npass'], $_POST['cpass'])){
    session_start();

    $result = change_password($tng_conn, $_SESSION['phone'], $_POST['curpass'], $_POST['npass'], $_POST['cpass']);

    echo "<script>
        Swal.fire('{$result['title']}', '{$result['text']}', '".($result['status'] ? 'success' : 'error')."');
    </script>";
}
?>
<script>
$(document).ready(function(){

    function confirmLogout(url){
        Swal.fire({
            title: 'Are you sure?',
            text: "You will be logged out!",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes, Logout!'
        }).then((result) => {
            if(result.isConfirmed){
                window.location.href = url;
            }
        });
    }

    // Top menu logout
    $('#logoutBtn').click(function(){
        confirmLogout('../includes/logout.php');
    });

    // Dropdown logout
    $('#logoutDropdownBtn').click(function(){
        confirmLogout('../includes/logout.php');
    });

});
</script>
<!-- ADDED: SimpleBar JS and app.js for proper sidebar scrolling -->
        <script src="../template/tngweb/assets/plugins/simplebar/js/simplebar.min.js"></script>
        <script src="../template/tngweb/assets/js/app.js"></script>
        <script>
            // Manually initialize SimpleBar if app.js doesn't do it
            document.addEventListener("DOMContentLoaded", function() {
                if (typeof SimpleBar !== 'undefined') {
                    new SimpleBar(document.querySelector('.sidebar-wrapper'));
                }
            });
        </script>

        </header>
        <!--end header --> 