<?php
 session_start();
 require_once('../connect.php');
 require_once('sql_required.php');

 $datetoday = date('Y-m-d'); 

if (!isset($_SESSION['unique_id'])) { ?>
    <script language="javascript">
      window.location.href = '../index.php';
    </script>
<?php } 

$active_tab = "order";

$success_messages = '';
if (isset($_POST['save'])) {
  $id = $_POST['id'];
  $status = $_POST['status'];

  $sqlMobileCustomer = "SELECT contactnumber FROM tbl_user_checkout WHERE id = '$id'";
  $resultMobileCustomer = $conn->query($sqlMobileCustomer);
  $rowMobileCustomer = mysqli_fetch_assoc($resultMobileCustomer);

  if ($status == '1') {
    $number = $rowMobileCustomer['contactnumber'];
    $message = "Your order has been accepted";
    include_once("sms_stockist.php");
  }elseif($status == '2'){
    $number = $rowMobileCustomer['contactnumber'];
    $message = "Your order is ready to ship";
    include_once("sms_stockist.php");
  }elseif($status == '5') {
    $number = $rowMobileCustomer['contactnumber'];
    $message = "Your order was cancelled";
    include_once("sms_stockist.php");
  }

  $sql_update = "UPDATE tbl_user_checkout SET order_status = '$status' WHERE id = '$id'";
  if (mysqli_query($conn, $sql_update)) {
    $success_messages = '<div class="alert alert-success text-center mb-0">Successfully change status</div>';
  }
}

if (isset($_POST['mark_complete'])) {
  $orderNumber = $_POST['order_number'];
  $orderunique_customer = $_POST['customeruniqueID'];
  $totalSaleIncome = 0;
  $totalOrder = 0;
  $sqlTblOrderProduct = "SELECT product_id,quantity,price FROM tbl_user_checkout_details WHERE order_number = '$orderNumber'";
  $resultTblOrderProduct = $conn->query($sqlTblOrderProduct);
  while($rowTblOrderProduct = mysqli_fetch_array($resultTblOrderProduct)) {
    $productID = $rowTblOrderProduct['product_id'];
    $productQty = $rowTblOrderProduct['quantity'];

    if ($productID == 1) {
      $saleIncome = 300 * $productQty;
      $totalProduct = $rowTblOrderProduct['price'] * $productQty;
    }elseif($productID == 2){
      $saleIncome = 50 * $productQty;
      $totalProduct = $rowTblOrderProduct['price'] * $productQty;
    }elseif($productID == 3){
      $saleIncome = 50 * $productQty;
      $totalProduct = $rowTblOrderProduct['price'] * $productQty;
    }elseif($productID == 4){
      $saleIncome = 60 * $productQty;
      $totalProduct = $rowTblOrderProduct['price'] * $productQty;
    }elseif($productID == 5){
      $saleIncome = 50 * $productQty;
      $totalProduct = $rowTblOrderProduct['price'] * $productQty;
    }

    $totalOrder += ($totalProduct);
    $totalSaleIncome += ($saleIncome);

    if ($_POST['stockistID'] != '') {
      $stockistID = $_POST['stockistID'];
      $sqlUpdateInventory = "SELECT number_stocks,product_id FROM tbl_stockist_inventory WHERE stockist_id = '$stockistID' AND product_id = '$productID'";
      $resultUpdateInventory = $conn->query($sqlUpdateInventory);
      $rowUpdateInventory = mysqli_fetch_assoc($resultUpdateInventory);

      $updateQty = $rowUpdateInventory['number_stocks'] - $productQty;

      $sqlUpdateInventoryTriggered = "UPDATE tbl_stockist_inventory SET number_stocks = '$updateQty' WHERE product_id = '$productID' AND stockist_id = '$stockistID'";
      mysqli_query($conn, $sqlUpdateInventoryTriggered);
    }
  }

  $sql_update = "UPDATE tbl_user_checkout SET order_status = '4' WHERE order_number = '$orderNumber'";
  if (mysqli_query($conn, $sql_update)) {
    $success_messages = '<div class="alert alert-success text-center mb-0">Successfully Released</div>';

    include("getunilevels.php");
    $youRI = $totalOrder * 0.06;
    $lvl1ri = $totalOrder * 0.04;
    $lvl2ri = $totalOrder * 0.03;
    $lvl3ri = $totalOrder * 0.03;
    $lvl4ri = $totalOrder * 0.02;
    $lvl5ri = $totalOrder * 0.02;

    $sql_insertbonus ="INSERT INTO tbl_hybrid_bonus(unique_id, debit, credit, bonus_type, transaction_type, date_accumulated, verification_code, level) VALUES
    ('$orderunique_customer', '$youRI', '0', 'RI', 'PP', '$datetoday', '$orderNumber', '0'),
    ('$indrlevel1', '$lvl1ri', '0', 'RI', 'PP', '$datetoday', '$orderNumber', '1'),
    ('$indrlevel2', '$lvl2ri', '0', 'RI', 'PP', '$datetoday', '$orderNumber', '2'),
    ('$indrlevel3', '$lvl3ri', '0', 'RI', 'PP', '$datetoday', '$orderNumber', '3'),
    ('$indrlevel4', '$lvl4ri', '0', 'RI', 'PP', '$datetoday', '$orderNumber', '4'),
    ('$indrlevel5', '$lvl5ri', '0', 'RI', 'PP', '$datetoday', '$orderNumber', '5')";
    mysqli_query($conn, $sql_insertbonus);

    if ($_POST['stockistID'] != '') {
      $stockistID = $_POST['stockistID'];
      $sqlStockistNumber = "SELECT mobilenumber FROM tbl_user_account WHERE unique_id = '$stockistID'";
      $resultStockistNumber = $conn->query($sqlStockistNumber);
      $rowStockistDetails = mysqli_fetch_assoc($resultStockistNumber);

      $number = $rowStockistDetails['mobilenumber'];
      $message = "You have earned P".$totalSaleIncome." from transaction #".$orderNumber.".";
      include_once("sms_stockist.php");

      $sqlSaleIncome = "INSERT INTO tbl_sale_income(stockist_id, transaction_number, sale_income, date_inserted) VALUES ('$stockistID', '$orderNumber', '$totalSaleIncome', '$datetoday')";
      mysqli_query($conn, $sqlSaleIncome);
    }

  }
}
?>  
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Order</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
  <link rel="icon" href="images/logo.png" type="image/png" sizes="16x16">
  <link rel="stylesheet" href="plugins/fontawesome-free/css/all.min.css">
  <link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
  <link rel="stylesheet" href="dist/css/adminlte.min.css">
  <link rel="stylesheet" href="plugins/overlayScrollbars/css/OverlayScrollbars.min.css">
  <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700" rel="stylesheet">
  <link rel="stylesheet" href="plugins/datatables-bs4/css/dataTables.bootstrap4.min.css">
  <link rel="stylesheet" href="plugins/datatables-responsive/css/responsive.bootstrap4.min.css">
  <link rel="stylesheet" type="text/css" href="css/style1.css">
  <link rel="stylesheet" type="text/css" href="css/skeleton_loading.css">
<style type="text/css">
  .content-wrapper {
    background: #151e27!important;
  }
  @media(max-width:800px) {
    .modal-content {
      position: fixed; bottom: 40%; width: 96%; left: 7;
    }
    .modal-dialog {
      margin: 0!important; padding: 0!important;
    }
  }
  input[type='radio'] {
    opacity: 0.01;
    display: none;
    z-index: 100;
  }
  input[type="radio"]:checked+label,
  .Checked+label {
    background: #1d2532!important;
    color: #1d2532!important;
  }
  input[type="radio"]:checked+label,
  .Checked+label:hover {
    background: #1d2532!important;
    color: #fff!important;
  }
  .option-service{
    font-size: 35px;
    padding-bottom: 10px;
    padding-top: 10px;
  }

@media(max-width:526px) {
  .option-service{
    font-size: 20px;
    padding-bottom: 10px;
    padding-top: 10px;
  }
  .radio-div {
    padding-left: 2px;
    padding-right: 2px;
  }
  .title-radio {
    display: none;
  }
}
@media(max-width:340px) {
  .option-service{
    font-size: 16px;
    padding-bottom: 8px;
    padding-top: 8px;
  }
  .radio-div {
    padding-left: 1px;
    padding-right: 1px;
  }
}
@media(max-width:320px) {
  .option-service{
    font-size: 15px;
    padding-bottom: 6px;
    padding-top: 6px;
  }
  .radio-div {
    padding-left: 1px;
    padding-right: 1px;
  }
}
@media(max-width:305px) {
  .option-service{
    font-size: 14px;
    padding-bottom: 6px;
    padding-top: 6px;
  }
  .radio-div {
    padding-left: 1px;
    padding-right: 1px;
  }
}
  .table td {
    border-top: 1px solid grey!important;
    color: #fff;
  }
  .table thead th {
    border-bottom: 0px;
    color: #fff;
  }
  .txn-history {
    text-align: left;
  }
  .txn-list {
    background-color: #1d2532;
    padding: 12px 10px; 
    color: #777;
    font-size: 14px;
    margin: 7px 0;
    box-shadow: 0 2px 5px 0 rgba(0,0,0,.16), 0 2px 10px 0 rgba(0,0,0,.12)!important;
    cursor: pointer;
  }
  .transaction-amount {
    float: right;
    color: #fff;
  }
  .transaction-number {
   float: right;
  }
  .form-control {
    border-radius: 10px;
  }
  .form-control:focus {
    border: 1px solid #ffd700!important;
  }
  .alert-success {
    color: #3c763d;
    background-color: #dff0d8;
    border-color: #d6e9c6;
    border-radius: 0;
  }

  .alert-danger {
    color: #a94442;
    background-color: #f2dede;
    border-color: #ebccd1;
    border-radius: 0;
  }

@media(max-width:991px) {
  #order_full_screen {
    display: none!important;
  }
  #order_small_screen {
    display: block!important;
  }
}
  .page-item.active .page-link {
    background-color: #27313d!important;
    border-color: #27313d!important;
    color: #000;
  }
  .page-item .page-link {
    background-color: #151e27!important;
    border-color: grey!important;
    color: grey!important;
  }
</style>
</head>
<body class="hold-transition sidebar-mini layout-footer-fixed layout-fixed" style="background: #151e27;">
<div id="preloader" class="loading" style="display: none"></div>
<div class="wrapper">
  <nav class="main-header navbar navbar-expand navbar-white" style="background: #151e27; border-bottom: 0; box-shadow: none!important;">
    <ul class="navbar-nav">
      <li class="nav-item">
        <a href="index.php?<?php echo generate_string($permitted_chars, 100); ?>" class="withanimation back_arrow"><i class="fas fa-angle-left pr-2" style="font-size: 20px; color: #ffd700;"></i></a><span class="brand-text" style="font-size: 20px;"><span style="font-size: 20px; font-weight: bolder; line-height: 20px; color: #fff;">Order</span></a>
      </li>
    </ul>
    <ul class="navbar-nav ml-auto">
      <a class="nav-link" data-widget="pushmenu" href="#" role="button"><i class="fas fa-bars"></i></a>
    </ul>
  </nav>

<?php include ("navbar.php")?>
<div class="content-wrapper">
  <div class="content-header p-0">
    <div class="container-fluid p-0">
      <span id="message">
        <?php echo $success_messages; ?>
      </span>
    </div>
  </div>
  <section class="content">
    <div class="container-fluid pt-3">
      <div class="row">
        <div class="col-lg-2 col-2 radio-div">
          <input type="radio" class="btn-check" name="deliver" id="pending" value="pending" autocomplete="off" checked>
          <label class="btn btn-outline-dark btn-block btn-radio btn-lg" for="pending">
            <i class="fas fa-clock option-service"></i>
          </label>
          <p align="center" class="text-white title-radio" style="margin-bottom: 0px;"><small>Pending</small></p>
        </div>

        <div class="col-lg-2 col-2 radio-div">
          <input type="radio" class="btn-check" name="deliver" id="review" value="review" autocomplete="off">
          <label class="btn btn-outline-dark btn-block btn-radio btn-lg" for="review">
            <i class="fas fa-check option-service"></i>
          </label>
          <p align="center" class="text-white title-radio" style="margin-bottom: 0px;"><small>Accept</small></p>
        </div>

        <div class="col-lg-2 col-2 radio-div">
          <input type="radio" class="btn-check" name="deliver" id="toship" value="toship" autocomplete="off">
          <label class="btn btn-outline-dark btn-block btn-radio btn-lg" for="toship">
            <i class="fas fa-box option-service"></i>
          </label>
          <p align="center" class="text-white title-radio" style="margin-bottom: 0px;"><small>To Ship</small></p>
        </div>

        <div class="col-lg-2 col-2 radio-div">
          <input type="radio" class="btn-check" name="deliver" id="complete" value="complete" autocomplete="off">
          <label class="btn btn-outline-dark btn-block btn-radio btn-lg" for="complete">
            <i class="fas fa-check-circle option-service"></i>
          </label>
          <p align="center" class="text-white title-radio" style="margin-bottom: 0px;"><small>Completed</small></p>
        </div>

        <div class="col-lg-2 col-2 radio-div">
          <input type="radio" class="btn-check" name="deliver" id="canceled" value="canceled" autocomplete="off">
          <label class="btn btn-outline-dark btn-block btn-radio btn-lg" for="canceled">
            <i class="fas fa-times-circle option-service"></i>
          </label>
          <p align="center" class="text-white title-radio" style="margin-bottom: 0px;"><small>Canceled</small></p>
        </div>

      </div> <!-- row -->

      <span id="pending_div">
        <?php include('order_pending.php'); ?>
      </span>

      <span id="review_div" style="display: none;">
        <?php include('order_review.php'); ?>
      </span>

      <span id="accept_div" style="display: none;">
        <?php include('order_toship.php'); ?>
      </span>

      <span id="complete_div" style="display: none;">
          <?php include('order_complete.php'); ?>
      </span>

      <span id="canceled_div" style="display: none;">
          <?php include('order_canceled.php'); ?>
      </span>
    </div>
  </section>
</div>
</div>

<script src="dist/js/adminlte.min.js"></script>
<script src="plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="plugins/jquery-validation/jquery.validate.min.js"></script>
<script src="plugins/jquery-validation/additional-methods.min.js"></script>
<script src="dist/js/pages/dashboard.js"></script>
<script src="plugins/overlayScrollbars/js/jquery.overlayScrollbars.min.js"></script>
<script src="plugins/datatables/jquery.dataTables.min.js"></script>
<script src="plugins/datatables-bs4/js/dataTables.bootstrap4.min.js"></script>
<script src="plugins/datatables-responsive/js/dataTables.responsive.min.js"></script>
<script src="plugins/datatables-responsive/js/responsive.bootstrap4.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {

  setTimeout(function() {
    var value = localStorage.getItem("deliver_tab");
    if (value == "pending") {
      localStorage.setItem("deliver_tab", "pending");
      $("#pending").attr("checked", true);
      $("#pending_div").show();
      $("#review_div").hide();
      $("#accept_div").hide();
      $("#delivering_div").hide();
      $("#complete_div").hide();
      $("#canceled_div").hide();
    }else if(value == "review") {
      localStorage.setItem("deliver_tab", "review");
      $("#review").attr("checked", true);
      $("#pending_div").hide();
      $("#review_div").show();
      $("#accept_div").hide();
      $("#delivering_div").hide();
      $("#complete_div").hide();
      $("#canceled_div").hide();
    }else if(value == "toship") {
      localStorage.setItem("deliver_tab", "toship");
      $("#toship").attr("checked", true);
      $("#pending_div").hide();
      $("#review_div").hide();
      $("#accept_div").show();
      $("#delivering_div").hide();
      $("#complete_div").hide();
      $("#canceled_div").hide();
    }else if(value == "todeliver") {
      localStorage.setItem("deliver_tab", "todeliver");
      $("#todeliver").attr("checked", true);
      $("#pending_div").hide();
      $("#review_div").hide();
      $("#accept_div").hide();
      $("#delivering_div").show();
      $("#complete_div").hide();
      $("#canceled_div").hide();
    }else if(value == "complete") {
      localStorage.setItem("deliver_tab", "complete");
      $("#complete").attr("checked", true);
      $("#pending_div").hide();
      $("#review_div").hide();
      $("#accept_div").hide();
      $("#delivering_div").hide();
      $("#complete_div").show();
      $("#canceled_div").hide();     
    }else if(value == "canceled") {
      localStorage.setItem("deliver_tab", "canceled");
      $("#canceled").attr("checked", true);
      $("#pending_div").hide();
      $("#review_div").hide();
      $("#accept_div").hide();
      $("#delivering_div").hide();
      $("#complete_div").hide();
      $("#canceled_div").show();           
    }
  },0);

  $('.btn-check').change(function() {
    var val = $(this).val();
    if (val == "pending") {
      localStorage.setItem("deliver_tab", "pending");
      $("#pending_div").show();
      $("#review_div").hide();
      $("#accept_div").hide();
      $("#delivering_div").hide();
      $("#complete_div").hide();
      $("#canceled_div").hide();
    }else if(val == "review") {
      localStorage.setItem("deliver_tab", "review");
      $("#pending_div").hide();
      $("#review_div").show();
      $("#accept_div").hide();
      $("#delivering_div").hide();
      $("#complete_div").hide();
      $("#canceled_div").hide();
    }else if(val == "toship") {
      localStorage.setItem("deliver_tab", "toship");
      $("#pending_div").hide();
      $("#review_div").hide();
      $("#accept_div").show();
      $("#delivering_div").hide();
      $("#complete_div").hide();
      $("#canceled_div").hide();
    }else if(val == "todeliver") {
      localStorage.setItem("deliver_tab", "todeliver");
      $("#pending_div").hide();
      $("#review_div").hide();
      $("#accept_div").hide();
      $("#delivering_div").show();
      $("#complete_div").hide();
      $("#canceled_div").hide();
    }else if(val == "complete") {
      localStorage.setItem("deliver_tab", "complete");
      $("#pending_div").hide();
      $("#review_div").hide();
      $("#accept_div").hide();
      $("#delivering_div").hide();
      $("#complete_div").show();
      $("#canceled_div").hide();     
    }else if(val == "canceled") {
      localStorage.setItem("deliver_tab", "canceled");
      $("#pending_div").hide();
      $("#review_div").hide();
      $("#accept_div").hide();
      $("#delivering_div").hide();
      $("#complete_div").hide();
      $("#canceled_div").show();           
    }
  });
});

$(function () {
  $(".withanimation_submit").click(function() {
      $(".loading").show();
  });
  $(".withanimation").click(function(e) {
      e.preventDefault();
      $(".loading").show();
      var url=$(this).attr("href");
      setTimeout(function() {
          window.location=url;
      }, 500);
  });
});

setTimeout(function(){
  $('#message').html('');
}, 5000);

if ( window.history.replaceState ) {
  window.history.replaceState( null, null, window.location.href );
}
</script>
</body>
</html>
