<?php
session_start();
$key = md5(date("Y-m-d").":".$_SERVER['REMOTE_ADDR']);
if (isset($_SESSION['aW1iZXN0cmEuY29tLm15']) <> $key) {
    header("location:".$_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME']); // cara baru !
} else {
   
    include "_header.php";
    include "_navtop.php";
    include "_navside.php";
    include "_functions.php";
    require "conn.php";

    // ---------------------- HANDLE PACKAGE CRUD ------------------------
    if (isset($_POST['add_package'])) {
        $name   = mysqli_real_escape_string($con, $_POST['name']);
        $price  = mysqli_real_escape_string($con, $_POST['price']);
        $qty    = mysqli_real_escape_string($con, $_POST['qty_set']);
        $direct = mysqli_real_escape_string($con, $_POST['direct_sponsor']);
        $level  = mysqli_real_escape_string($con, $_POST['commission_level']);

        mysqli_query($con, "INSERT INTO packages (name, price, qty_set, direct_sponsor, commission_level) 
                            VALUES ('$name','$price','$qty','$direct','$level')");
        $pkg_msg = '<div class="alert alert-success">Package added successfully!</div>';
    }

    if (isset($_POST['update_package'])) {
        $id     = intval($_POST['id']);
        $name   = mysqli_real_escape_string($con, $_POST['name']);
        $price  = mysqli_real_escape_string($con, $_POST['price']);
        $qty    = mysqli_real_escape_string($con, $_POST['qty_set']);
        $direct = mysqli_real_escape_string($con, $_POST['direct_sponsor']);
        $level  = mysqli_real_escape_string($con, $_POST['commission_level']);

        mysqli_query($con, "UPDATE packages SET 
                name='$name', price='$price', qty_set='$qty', 
                direct_sponsor='$direct', commission_level='$level' 
                WHERE id=$id");
        $pkg_msg = '<div class="alert alert-info">Package updated successfully!</div>';
    }

    if (isset($_GET['delete_pkg'])) {
        $id = intval($_GET['delete_pkg']);
        mysqli_query($con, "DELETE FROM packages WHERE id=$id");
        $pkg_msg = '<div class="alert alert-danger">Package deleted successfully!</div>';
    }
    // -------------------------------------------------------------------



    if (isset($_POST['komisen'])) {

    /// CASH
        $k1     = mysqli_real_escape_string($con, $_POST['k1']);
        $k2     = mysqli_real_escape_string($con, $_POST['k2']);
        $k3     = mysqli_real_escape_string($con, $_POST['k3']);
        $k4     = mysqli_real_escape_string($con, $_POST['k4']);
        $k5     = mysqli_real_escape_string($con, $_POST['k5']);
        $k6     = mysqli_real_escape_string($con, $_POST['k6']);
        $k7     = mysqli_real_escape_string($con, $_POST['k7']);
        $k8     = mysqli_real_escape_string($con, $_POST['k8']);
        $k9     = mysqli_real_escape_string($con, $_POST['k9']);
        $k10    = mysqli_real_escape_string($con, $_POST['k10']);
        $mp     = mysqli_real_escape_string($con, $_POST['mp']);
        $pv     = mysqli_real_escape_string($con, $_POST['pv']);

        $upd = mysqli_query($con, "UPDATE komisen SET lvl1='".$k1."', lvl2='".$k2."', lvl3='".$k3."', lvl4='".$k4."', lvl5='".$k5."', lvl6='".$k6."', lvl7='".$k7."', lvl8='".$k8."', lvl9='".$k9."', lvl10='".$k10."', maksima='".$mp."', nilaipin='".$pv."'");
    
    /// CARWASH    
    //    $k1_cw = mysqli_real_escape_string($con, $_POST['k1_carwash']);
    //    $k2_cw = mysqli_real_escape_string($con, $_POST['k2_carwash']);
    //    $k3_cw = mysqli_real_escape_string($con, $_POST['k3_carwash']);
    //    $k4_cw = mysqli_real_escape_string($con, $_POST['k4_carwash']);
    //    $k5_cw = mysqli_real_escape_string($con, $_POST['k5_carwash']);
    //    $mp_cw = mysqli_real_escape_string($con, $_POST['mp_carwash']);
    //    $pv_cw = mysqli_real_escape_string($con, $_POST['pv_carwash']);
     
    //    $upd_cw = mysqli_query($con, "UPDATE komisen_carwash SET lvl1_cw='".$k1_cw."', lvl2_cw='".$k2_cw."', lvl3_cw='".$k3_cw."', lvl4_cw='".$k4_cw."', lvl5_cw='".$k5_cw."', maksima_cw='".$mp_cw."', nilaipin_cw='".$pv_cw."'");
        
    /// POLISH    
    //    $k1_t = mysqli_real_escape_string($con, $_POST['k1_travel']);
    //    $k2_t = mysqli_real_escape_string($con, $_POST['k2_travel']);
    //    $k3_t = mysqli_real_escape_string($con, $_POST['k3_travel']);
    //    $k4_t = mysqli_real_escape_string($con, $_POST['k4_travel']);
    //    $k5_t = mysqli_real_escape_string($con, $_POST['k5_travel']);
    //    $mp_t = mysqli_real_escape_string($con, $_POST['mp_travel']);
    //    $pv_t = mysqli_real_escape_string($con, $_POST['pv_travel']);
        
    //    $upd_t = mysqli_query($con, "UPDATE komisen_travel SET lvl1_t='".$k1_t."', lvl2_t='".$k2_t."', lvl3_t='".$k3_t."', lvl4_t='".$k4_t."', lvl5_t='".$k5_t."', maksima_t='".$mp_t."', nilaipin_t='".$pv_t."'");

    /// REPAIRING
    //    $k1_c = mysqli_real_escape_string($con, $_POST['k1_cash']);
    //    $k2_c = mysqli_real_escape_string($con, $_POST['k2_cash']);
    //    $k3_c = mysqli_real_escape_string($con, $_POST['k3_cash']);
    //    $k4_c = mysqli_real_escape_string($con, $_POST['k4_cash']);
    //    $k5_c = mysqli_real_escape_string($con, $_POST['k5_cash']);
    //    $mp_c = mysqli_real_escape_string($con, $_POST['mp_cash']);
    //    $pv_c = mysqli_real_escape_string($con, $_POST['pv_cash']);
        
    //    $upd_c = mysqli_query($con, "UPDATE komisen_cash SET lvl1_c='".$k1_c."', lvl2_c='".$k2_c."', lvl3_c='".$k3_c."', lvl4_c='".$k4_c."', lvl5_c='".$k5_c."', maksima_c='".$mp_c."', nilaipin_c='".$pv_c."'");
    
    /// ACCESSORIES
    //    $k1_acc = mysqli_real_escape_string($con, $_POST['k1_accessories']);
    //    $k2_acc = mysqli_real_escape_string($con, $_POST['k2_accessories']);
    //    $k3_acc = mysqli_real_escape_string($con, $_POST['k3_accessories']);
    //    $k4_acc = mysqli_real_escape_string($con, $_POST['k4_accessories']);
    //    $k5_acc = mysqli_real_escape_string($con, $_POST['k5_accessories']);
    //    $mp_acc = mysqli_real_escape_string($con, $_POST['mp_accessories']);
    //    $pv_acc = mysqli_real_escape_string($con, $_POST['pv_accessories']);

    //    $upd_acc = mysqli_query($con, "UPDATE komisen_accessories SET lvl1_acc='".$k1_acc."', lvl2_acc='".$k2_acc."', lvl3_acc='".$k3_acc."', lvl4_acc='".$k4_acc."', lvl5_acc='".$k5_acc."', maksima_acc='".$mp_acc."', nilaipin_acc='".$pv_acc."'");

/// INSURANCE
    //    $k1_ins = mysqli_real_escape_string($con, $_POST['k1_insurance']);
    //    $k2_ins = mysqli_real_escape_string($con, $_POST['k2_insurance']);
    //    $k3_ins = mysqli_real_escape_string($con, $_POST['k3_insurance']);
    //    $k4_ins = mysqli_real_escape_string($con, $_POST['k4_insurance']);
    //    $k5_ins = mysqli_real_escape_string($con, $_POST['k5_insurance']);
    //    $mp_ins = mysqli_real_escape_string($con, $_POST['mp_insurance']);
    //    $pv_ins = mysqli_real_escape_string($con, $_POST['pv_insurance']);

    //    $upd_ins = mysqli_query($con, "UPDATE komisen_insurance SET lvl1_ins='".$k1_ins."', lvl2_ins='".$k2_ins."', lvl3_ins='".$k3_ins."', lvl4_ins='".$k4_ins."', lvl5_ins='".$k5_ins."', maksima_ins='".$mp_ins."', nilaipin_ins='".$pv_ins."'");


        
        $sl         = mysqli_real_escape_string($con, $_POST['sl']);
        $set_limit  = mysqli_query($con, "UPDATE setting SET sponsor_limit='".$sl."'");    
    
        
    }

    $kms        = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM komisen"));
    // $kms_cw     = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM komisen_carwash"));
    // $kms_t      = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM komisen_travel"));
    // $kms_c      = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM komisen_cash"));
    // $kms_acc    = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM komisen_accessories"));
    // $kms_ins    = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM komisen_insurance"));
    $spl        = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM setting"));
?>
        <div id="page-wrapper" >
            <div id="page-inner">
                <div class="row">
                    <div class="col-md-12 ">
                     <h2><i class="fa fa-cogs"></i> Setting</h2>                          
                    </div>
                </div>
<? if ($upd) { ?>
    <script>
    window.setTimeout(function() {
        $(".alert").fadeTo(500, 0).slideUp(500, function(){
            $(this).remove();
        });
    }, 2000);
    </script>

    <div class="alert alert-info">
        Commission settings saved.
    </div>


<? } ?>
                 <!-- /. ROW  -->
                 <hr />
               <div class="row">
                <div class="col-md-12">
                    <!-- Form Elements -->
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            Level Commission
                        </div>
                        <div class="panel-body">
                            <div class="row">
                                <div class="col-md-12">
                                    <form role="form" method="post">
                                        
                                            <div class="col-md-3 col-sm-12" style="background-color:#FAFAFA; padding:20px; margin:10px; border: 2px solid #EEEEEE;">
                                                <h4 class="text-center"><i class="fa fa-usd fa-2x text-primary"></i> </h4>
                                                <hr>
                                                <div class="form-group">
                                                    <label>Level 1 Commission</label>
                                                    <input class="form-control" name="k1" value="<?= $kms[1] ?>" step=".01" type="number" placeholder="Level 1" />
                                                </div>
                                                <div class="form-group">
                                                    <label>Level 2 Commission</label>
                                                    <input class="form-control" name="k2" value="<?= $kms[2] ?>" step=".01" type="number" placeholder="Level 2" />
                                                </div>
                                                <div class="form-group">
                                                    <label>Level 3 Commission</label>
                                                    <input class="form-control" name="k3" value="<?= $kms[3] ?>" step=".01" type="number" placeholder="Level 3" />
                                                </div>
                                                <div class="form-group">
                                                    <label>Level 4 Commission</label>
                                                    <input class="form-control" name="k4" value="<?= $kms[4] ?>" step=".01" type="number" placeholder="Level 4" />
                                                </div>
                                                <div class="form-group">
                                                    <label>Level 5 Commission</label>
                                                    <input class="form-control" name="k5" value="<?= $kms[5] ?>" step=".01" type="number" placeholder="Level 5" />
                                                </div>
                                                <div class="form-group">
                                                    <label>Level 6 Commission</label>
                                                    <input class="form-control" name="k6" value="<?= $kms[6] ?>" step=".01" type="number" placeholder="Level 6" />
                                                </div>
                                                <div class="form-group">
                                                    <label>Level 7 Commission</label>
                                                    <input class="form-control" name="k7" value="<?= $kms[7] ?>" step=".01" type="number" placeholder="Level 7" />
                                                </div>
                                                <div class="form-group">
                                                    <label>Level 8 Commission</label>
                                                    <input class="form-control" name="k8" value="<?= $kms[8] ?>" step=".01" type="number" placeholder="Level 8" />
                                                </div>
                                                <div class="form-group">
                                                    <label>Level 9 Commission</label>
                                                    <input class="form-control" name="k9" value="<?= $kms[9] ?>" step=".01" type="number" placeholder="Level 9" />
                                                </div>
                                                <div class="form-group " style="display:none;">
                                                    <label>Level 10 Commission</label>
                                                    <input class="form-control" name="k10" value="<?= $kms[10] ?>" step=".01" type="hidden" placeholder="Level 10" />
                                                </div>
                                                <div class="form-group">
                                                    <label>Maximum Payout</label>
                                                    <input class="form-control" name="mp" value="<?= $kms[11] ?>" type="text" placeholder="Max Payout" />
                                                </div>
                                            </div>
                                            
                                        <!-- Packages CRUD -->
                                        <div class="col-md-8 col-sm-12" style="background:#FAFAFA; padding:20px; margin:10px; border:2px solid #EEE;">
                                            <h4 class="text-center"><i class="fa fa-box fa-2x text-primary"></i></h4>
                                            <hr>
                                            <div class="form-group">
                                                <label><strong>Packages</strong></label>
                                        
                                                <!-- Add Package Form -->
                                                <form method="post" class="mb-3">
                                                    <div class="row g-2">
                                                        <div class="col-md-2"><input type="text" name="name" class="form-control" placeholder="Name" required></div>
                                                        <div class="col-md-2"><input type="number" step="0.01" name="price" class="form-control" placeholder="Price" required></div>
                                                        <div class="col-md-2"><input type="number" name="qty_set" class="form-control" placeholder="Qty" required></div>
                                                        <div class="col-md-2"><input type="number" step="0.01" name="direct_sponsor" class="form-control" placeholder="Sponsor %" required></div>
                                                        <div class="col-md-2"><input type="number" name="commission_level" class="form-control" placeholder="Level" required></div>
                                                        <div class="col-md-2">
                                                            <button type="submit" name="add_package" class="btn btn-success w-100" style="height:40px;">
                                                                <i class="fa fa-plus"></i> Add
                                                            </button>
                                                        </div>
                                                    </div>
                                                </form>
                                                <br>
                                                <!-- Packages List -->
                                                <div class="table-responsive">
                                                    <table class="table table-bordered table-sm">
                                                        <thead class="thead-light">
                                                            <tr>
                                                                <th>No</th>
                                                                <th>Name</th>
                                                                <th>Price (RM)</th>
                                                                <th>Qty</th>
                                                                <th>Sponsor RM</th>
                                                                <th>Commission Level</th>
                                                                <th width="100">Action</th>
                                                            </tr>
                                                        </thead>
                                                        <tbody>
                                                            <?php
                                                            $result = mysqli_query($con, "SELECT * FROM packages ORDER BY id ASC");
                                                            while ($row = mysqli_fetch_assoc($result)) {
                                                                echo '<tr>
                                                                    <form method="post">
                                                                        <td>'.$row['id'].'<input type="hidden" name="id" value="'.$row['id'].'"></td>
                                                                        <td><input type="text" name="name" value="'.$row['name'].'" class="form-control"></td>
                                                                        <td><input type="number" step="0.01" name="price" value="'.$row['price'].'" class="form-control"></td>
                                                                        <td><input type="number" name="qty_set" value="'.$row['qty_set'].'" class="form-control"></td>
                                                                        <td><input type="number" step=".01" name="direct_sponsor" value="'.$row['direct_sponsor'].'" class="form-control"></td>
                                                                        <td><input type="number" name="commission_level" value="'.$row['commission_level'].'" class="form-control"></td>
                                                                        <td class="text-left">
                                                                            <button type="submit" name="update_package" class="btn btn-sm" style="background:transparent; border:none; color:#007bff; padding:0;">
                                                                                <i class="fa fa-edit fa-2x text-primary"></i> 
                                                                            </button>
                                                                            &nbsp;
                                                                            <a href="?delete_pkg='.$row['id'].'" class="btn btn-sm" style="padding:4px 8px;" onclick="return confirm(\'Delete this package?\')">
                                                                                <i class="fa fa-trash fa-2x text-danger"></i> 
                                                                            </a>
                                                                        </td>
                                                                    </form>
                                                                </tr>';
                                                            }
                                                            ?>
                                                        </tbody>
                                                    </table>
                                                </div>
                                            </div>
                                        </div>



                                        <div class="col-md-3 col-sm-12" style="background-color:#FAFAFA; padding:20px; margin:10px; border: 2px solid #EEEEEE;">
                                                <h4 class="text-center"><i class="fa fa-sitemap fa-2x text-primary"></i></h4>
                                                <hr>
                                                <div class="form-group">
                                                    <label>Sponsor Limit</label>
                                                    <input class="form-control" name="sl" value="<?= $spl[1] ?>" type="text" placeholder="max sponsor" />
                                                </div>
                                        </div>
                                        <div class="col-md-3 col-sm-12" style="background-color:#FAFAFA; padding:20px; margin:10px; border: 2px solid #EEEEEE;">
                                                <h4 class="text-center"><i class="fa fa-key fa-2x text-primary"></i></h4>
                                                <hr>
                                                <div class="form-group">
                                                    <label>Basic Package (RM)</label>
                                                    <input class="form-control" name="pv" value="<?= $kms[12] ?>" type="text" placeholder="MYR" />
                                                </div>
                                        </div>
                                        <div class="col-md-12 col-sm-12" style="padding:20px; margin:10px; ">
                                        <button type="submit" name="komisen" class="btn btn-primary">Save</button>
                                        </div>
                                    </form>
                                    <br />
                                </div>
                            </div>
                        </div>
                    </div>
                     <!-- End Form Elements -->
                </div>

    </div>
             <!-- /. PAGE INNER  -->
            </div>
         <!-- /. PAGE WRAPPER  -->
        </div>
<?
    include "_footer.php";
}
?>
