<?php
session_start();
error_reporting(0);
include('includes/config.php');
include('includes/db.php');

if (strlen($_SESSION['signAdminlogin']) == 0) {
    header('location:index.php');
} else {

    //---------------------------

    if (isset($_POST['cc_sign_on'])) {
        $sql2 = 'UPDATE control SET cc_sign_control=1 WHERE id = 1';
        if ($result = mysqli_query($link, $sql2)) {
            $msg1 = 'Turned ON';
        }
    } elseif (isset($_POST['cc_sign_off'])) {
        $sql2 = 'UPDATE control SET cc_sign_control=0 WHERE id = 1';
        if ($result = mysqli_query($link, $sql2)) {
            $msg1 = 'Turned OFF';
        }
    }

    if (isset($_POST['cc_server_on'])) {
        $sql2 = 'UPDATE control SET cc_server_control=1 WHERE id = 1';
        if ($result = mysqli_query($link, $sql2)) {
            $msg1 = 'Turned ON';
        }
    } elseif (isset($_POST['cc_server_off'])) {
        $sql2 = 'UPDATE control SET cc_server_control=0 WHERE id = 1';
        if ($result = mysqli_query($link, $sql2)) {
            $msg1 = 'Turned OFF';
        }
    }

    if (isset($_POST['cc_card_on'])) {
        $sql2 = 'UPDATE control SET cc_card_control=1 WHERE id = 1';
        if ($result = mysqli_query($link, $sql2)) {
            $msg1 = 'Turned ON';
        }
    } elseif (isset($_POST['cc_card_off'])) {
        $sql2 = 'UPDATE control SET cc_card_control=0 WHERE id = 1';
        if ($result = mysqli_query($link, $sql2)) {
            $msg1 = 'Turned OFF';
        }
    }

    // -----------------------------------------------

    if (isset($_GET['edit'])) {
        $editid = $_GET['edit'];
    }
    
    if (isset($_GET['upstatus'])) {
        $upstatus = $_GET['upstatus'];
        mysqli_query($link, "UPDATE sign_control SET sign_status=$upstatus WHERE id=1");
        $msg = "Sign Copy Status Updated";
    }
    

    if (isset($_POST['submit'])) {
        $card_fee = $_POST['fee'];
        $whatsapp_number = $_POST['whatsapp'];
    try{ 
        mysqli_query($link, "UPDATE settings SET value=$card_fee WHERE name='card_fee'");
        mysqli_query($link, "UPDATE settings SET value=$whatsapp_number WHERE name='whatsapp'");
    }catch(\Exception $e) {
        die($e->getMessage());
    }
        $msg = "Setting updated";
    } 
    
    
    if (isset($_POST['notice'])) {
        $notice_msg = $_POST['notice'];
    try{ 
        mysqli_query($link, "UPDATE `setting` SET `notice` = '$notice_msg' WHERE `setting`.`id` = 1");
    }catch(\Exception $e) {
        die($e->getMessage());
    }
        $msg = "Notice updated";
    }
    
    if (isset($_POST['min_rcg'])) {
        $min_rcg = $_POST['min_rcg'];
    try{ 
        mysqli_query($link, "UPDATE `setting` SET `min_rcg` = '$min_rcg' WHERE `setting`.`id` = 1");
    }catch(\Exception $e) {
        die($e->getMessage());
    }
        $msg = "Recharge updated";
    }
    

// delete button is clicked
if (isset($_POST['delete'])) {
    
    $dbd = $_POST['db'];
    
    if ($dbd == 1) {
         $querydd = "DELETE FROM notification";
    } else if ($dbd == 2) {
         $querydd = "DELETE FROM log";
    } else if ($dbd == 3) {
         $querydd = "DELETE FROM deleteduser";
    }
    else if ($dbd == 10001){
         $querydd = "DELETE FROM signcopy_sub";
    }
    
   
   
    $resultdd = mysqli_query($link, $querydd); 

    if ($resultdd) {
        $msg = "All Data deleted";
    } else {
        $msg = "Error deleting user data: " . mysqli_error($link);

    }
}

?>

    <!doctype html>
    <html lang="en" class="no-js">

    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
        <meta name="description" content="">
        <meta name="author" content="">
        <meta name="theme-color" content="#3e454c">

        <title>Settings</title>

        <!-- Font awesome -->
        <link rel="stylesheet" href="css/font-awesome.min.css">
        <!-- Sandstone Bootstrap CSS -->
        <link rel="stylesheet" href="css/bootstrap.min.css">
        <!-- Admin Stye -->
        <link rel="stylesheet" href="css/style.css">

        <script type="text/javascript" src="../vendor/countries.js"></script>
        <style>
            .errorWrap {
                padding: 10px;
                margin: 0 0 20px 0;
                background: #dd3d36;
                color: #fff;
                -webkit-box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1);
                box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1);
            }

            .succWrap {
                padding: 10px;
                margin: 0 0 20px 0;
                background: #5cb85c;
                color: #fff;
                -webkit-box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1);
                box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1);
            }
        </style>
    </head>

    <body>
        <?php 
             
    		$cardFeeSql = "select * from settings where name='card_fee' limit 1";
    		$card_result = mysqli_query($link, $cardFeeSql);
    		if(mysqli_num_rows($card_result) > 0) {
    		    $card_row = mysqli_fetch_array($card_result);
    		}else {
    		    $card_row = [];
    		}
    		
    		$signConSql = "select * from sign_control where id=1 limit 1";
    		$signCon_result = mysqli_query($link, $signConSql);
    		if(mysqli_num_rows($signCon_result) > 0) {
    		    $signCon_row = mysqli_fetch_array($signCon_result);
    		}else {
    		    $signCon_row = [];
    		}
    		
    		
    		$whatsappSql = "select * from settings where name='whatsapp' limit 1";
    		$whatsapp_result = mysqli_query($link, $whatsappSql);
    		if(mysqli_num_rows($whatsapp_result) > 0) {
    		    $whatsapp_row = mysqli_fetch_array($whatsapp_result);
    		}else {
    		    $whatsapp_row = [];
    		}
    		
    		$noticeSql = "select * from setting where id=1 limit 1";
    		$notice_result = mysqli_query($link, $noticeSql);
    		if(mysqli_num_rows($notice_result) > 0) {
    		    $notice_row = mysqli_fetch_array($notice_result);
    		}else {
    		    $notice_row = [];
    		}

		?>
        <?php include('includes/header.php'); ?>
        <div class="ts-main-content">
            <?php include('includes/leftbar.php'); ?>
            <div class="content-wrapper">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-md-12">
                            <h3 class="page-title">Manage Settings</h3>
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="panel panel-default">
                                        
                                       <div class="panel-body">

                                        <!-- SIGN ON/OFF (Start) -->
                                        <?php 
                                            $sql = "SELECT * FROM control WHERE id='1'";
                                            if ($result = mysqli_query($link, $sql)) {
                                                if (mysqli_num_rows($result) > 0) {
                                                    while ($row = mysqli_fetch_array($result)) {
                                                        $apr = $row['cc_sign_control'];
                                                        if ($apr == 1) {
                                                            $st = "Sign ON";
                                                        } else {
                                                            $st = "Sign OFF";
                                                        }
                                                    }
                                                }
                                            } 
                                        ?>
                                        
                                        <form method="post" class="form-horizontal" enctype="multipart/form-data">

                                            <div class="form-group">
                                                <label class="col-sm-2 control-label">Sign</label>
                                                <div class="col-sm-6">
                                                    <input type="text" name="sign_on_off" class="form-control" disabled style=" background: none;" value="<?php echo $st; ?>">
                                                </div>
                                                <div class="col-sm-8 col-lg-2  ">
                                                    <?php $sql = "SELECT * FROM control WHERE id='1'";
                                                    if ($result = mysqli_query($link, $sql)) {
                                                        if (mysqli_num_rows($result) > 0) {
                                                            while ($row = mysqli_fetch_array($result)) {
                                                                $apr = $row['cc_sign_control'];
                                                                if ($apr == 0) {
                                                                    echo "<button class='btn btn-success'  name='cc_sign_on' type='submit'>Turn ON</button>";
                                                                } else {
                                                                    echo "<button class='btn btn-danger'  name='cc_sign_off' type='submit'>Turn OFF</button>";
                                                                }
                                                            }
                                                        }
                                                    }  ?>
                                                </div>
                                            </div>

                                            <input type="hidden" name="editid" class="form-control" required value="<?php echo htmlentities($result->id); ?>">
                                        </form>
                                        
                                        <!-- SIGN ON/OFF (END) -->

                                        <!-- Server ON/OFF (Start) -->
                                        <?php 
                                            $sql = "SELECT * FROM control WHERE id='1'";
                                            if ($result = mysqli_query($link, $sql)) {
                                                if (mysqli_num_rows($result) > 0) {
                                                    while ($row = mysqli_fetch_array($result)) {
                                                        $apr = $row['cc_sign_control'];
                                                        if ($apr == 1) {
                                                            $st = "server ON";
                                                        } else {
                                                            $st = "server OFF";
                                                        }
                                                    }
                                                }
                                            } 
                                        ?>
                                        
                                        <form method="post" class="form-horizontal" enctype="multipart/form-data">

                                            <div class="form-group">
                                                <label class="col-sm-2 control-label">Server</label>
                                                <div class="col-sm-6">
                                                    <input type="text" name="server_on_off" class="form-control" disabled style=" background: none;" value="<?php echo $st; ?>">
                                                </div>
                                                <div class="col-sm-8 col-lg-2  ">
                                                    <?php $sql = "SELECT * FROM control WHERE id='1'";
                                                    if ($result = mysqli_query($link, $sql)) {
                                                        if (mysqli_num_rows($result) > 0) {
                                                            while ($row = mysqli_fetch_array($result)) {
                                                                $apr = $row['cc_server_control'];
                                                                if ($apr == 0) {
                                                                    echo "<button class='btn btn-success'  name='cc_server_on' type='submit'>Turn ON</button>";
                                                                } else {
                                                                    echo "<button class='btn btn-danger'  name='cc_server_off' type='submit'>Turn OFF</button>";
                                                                }
                                                            }
                                                        }
                                                    }  ?>
                                                </div>
                                            </div>

                                            <input type="hidden" name="editid" class="form-control" required value="<?php echo htmlentities($result->id); ?>">
                                        </form>
                                        
                                        <!-- server ON/OFF (END) -->

                                        <!-- card ON/OFF (Start) -->
                                        <?php 
                                            $sql = "SELECT * FROM control WHERE id='1'";
                                            if ($result = mysqli_query($link, $sql)) {
                                                if (mysqli_num_rows($result) > 0) {
                                                    while ($row = mysqli_fetch_array($result)) {
                                                        $apr = $row['cc_sign_control'];
                                                        if ($apr == 1) {
                                                            $st = "card ON";
                                                        } else {
                                                            $st = "card OFF";
                                                        }
                                                    }
                                                }
                                            } 
                                        ?>
                                        
                                        <form method="post" class="form-horizontal" enctype="multipart/form-data">

                                            <div class="form-group">
                                                <label class="col-sm-2 control-label">Card</label>
                                                <div class="col-sm-6">
                                                    <input type="text" name="card_on_off" class="form-control" disabled style=" background: none;" value="<?php echo $st; ?>">
                                                </div>
                                                <div class="col-sm-8 col-lg-2  ">
                                                    <?php $sql = "SELECT * FROM control WHERE id='1'";
                                                    if ($result = mysqli_query($link, $sql)) {
                                                        if (mysqli_num_rows($result) > 0) {
                                                            while ($row = mysqli_fetch_array($result)) {
                                                                $apr = $row['cc_card_control'];
                                                                if ($apr == 0) {
                                                                    echo "<button class='btn btn-success'  name='cc_card_on' type='submit'>Turn ON</button>";
                                                                } else {
                                                                    echo "<button class='btn btn-danger'  name='cc_card_off' type='submit'>Turn OFF</button>";
                                                                }
                                                            }
                                                        }
                                                    }  ?>
                                                </div>
                                            </div>

                                            <input type="hidden" name="editid" class="form-control" required value="<?php echo htmlentities($result->id); ?>">
                                        </form>
                                        
                                        <!-- card ON/OFF (END) -->

                                    </div>
                                            
                                            
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Loading Scripts -->
        <script src="js/jquery.min.js"></script>
        <script src="js/bootstrap.min.js"></script>
        <script src="js/main.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                setTimeout(function() {
                    $('.succWrap').slideUp("slow");
                }, 3000);
            });
        </script>

    </body>

    </html>
<?php } ?>