<?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";
$page = $_GET['page'];
$success_messages = '';
if (isset($_POST['save'])) {
  $id = $_POST['id'];
  $status = $_POST['status'];

  $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>';
  }
}
?>  
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title><?php if($_GET['status'] == 0){ echo 'Pending'; }else{ echo 'Complete'; } ?> Orders</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 rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.theme.default.css">
  <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700" rel="stylesheet">
  <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;
    }
  }
  .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;
  }
  .transaction-amount {
    float: right;
    color: #fff;
  }
  .transaction-number {
   float: right;
  }
  .btn {
    border-radius: 10px!important;
  }
  .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;
  }
}


</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="<?php echo $page; ?>.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;"><?php if($_GET['status'] == 0){ echo 'Pending'; }else{ echo 'Complete'; } ?> Orders</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="card" style="border-radius: 10px; background-color: #1d2532;" id="order_full_screen">
              <div class="card-body table-responsive p-0">
                  <table class="table m-0">
                    <thead>
                    <tr>
                      <th>Order ID</th>
                      <th>Customer</th>
                      <th>Customer Details</th>
                      <th>Date</th>
                      <th>Action</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php 
                      $order_status = $_GET['status'];
                      $sql_order = "SELECT * FROM tbl_user_checkout WHERE order_status = '$order_status'";
                      $result_order = $conn->query($sql_order);
                      if ($result_order->num_rows > 0) {
                        while($row_order = mysqli_fetch_array($result_order)) {
                          $ordernumber = $row_order['order_number'];
                    ?>
                    <tr>
                      <td><?php echo $row_order['order_number']; ?></td>
                      <td><?php echo $row_order['completename']; ?></td>
                      <td>
                        <?php echo $row_order['contactnumber']; ?><br>
                        <?php echo $row_order['complete_address']; ?>
                      </td>
                      <td><?php echo date('F d Y', strtotime($row_order['date_checkout'])); ?></td>
                      <td>
                        <?php if($row_order['order_status'] == 0) { ?>
                        <a data-toggle="modal" data-target="#status<?php echo $row_order['id']; ?>" class="btn btn-sm btn-primary">Status</a>
                        <?php } ?>
                        <a data-toggle="modal" data-target="#order<?php echo $row_order['id']; ?>" class="btn btn-sm btn-info">View Order</a>
                      </td>
                    </tr>

                    <div class="modal" id="order<?php echo $row_order['id']; ?>">
                      <div class="modal-dialog">
                        <div class="modal-content" style="background-color: #151e27!important;">
                          <div class="modal-header pb-0" style="border: 0px;">
                            <h5 class="modal-title text-white" style="font-weight: bold;">Order Details</h5>
                          </div>
                          <div class="modal-body pt-2 pb-0">
                            <div style="display: flex; justify-content: space-between;">
                              <span class="text-white">Total:</span>
                              <span class="text-white" style="font-size: 20px; font-weight: bolder;">&#8369; <?php echo number_format(($row_order['order_total'])) ?>.00</span>
                            </div>
                            <?php 
                                $sql_cart = "SELECT * FROM tbl_user_checkout_details WHERE order_number = '$ordernumber'";
                                $result_cart = $conn->query($sql_cart);
                                  while($row_cart = mysqli_fetch_array($result_cart)) {
                                  $product_id = $row_cart['product_id'];
                                    $sql_product_details = "SELECT * FROM tbl_product WHERE product_id = '$product_id'";
                                    $result_product_details = mysqli_query($conn, $sql_product_details);
                                    $row_product_details = mysqli_fetch_assoc($result_product_details);
                            ?>
                            <div class="txn-history">
                              <p class="txn-list">
                                <span class="text-white">QTY: <b><?php echo $row_cart['quantity']; ?>x</b></span>
                                <span class="transaction-amount"><span class="text-white">Item: <b><?php echo $row_product_details['product_name']; ?></span>
                              </p>
                            </div>
                            <?php } ?>
                          </div>
                          <div class="modal-footer" style="border: 0px">
                            <button type="button" class="btn btn-default btn-lg btn-block" style="background: transparent; color: #fff;" data-dismiss="modal">Close</button>
                          </div>
                        </div>
                      </div>
                    </div>

                    <div class="modal" id="status<?php echo $row_order['id']; ?>">
                      <div class="modal-dialog">
                        <div class="modal-content" style="background-color: #151e27!important;">
                          <div class="modal-header pb-0" style="border: 0px;">
                            <h5 class="modal-title text-white" style="font-weight: bold;">Change Status</h5>
                          </div>
                          <form style="margin: 0px;" method="POST" id="myform">
                          <input type="hidden" name="id" value="<?php echo $row_order['id']; ?>">
                          <div class="modal-body pt-2 pb-0">
                            <div class="form-group">
                              <select class="form-control" name="status" id="status" required>
                                <option disabled selected>Select Status</option>
                                <option <?php if($row_order['order_status'] == 0) { echo 'selected'; } ?> value="0">Pending</option>
                                <option value="1">Review</option>
                                <option value="2">Accept</option>
                              </select>
                            </div>
                          </div>
                          <div class="modal-footer pr-0 pl-0 pb-2 pt-0" style="display: block; border: 0px">
                            <div class="row">
                              <div class="col-6">
                                <button type="button" class="btn btn-default btn-lg btn-block" style="background: transparent; color: #fff;" data-dismiss="modal">Cancel</button>
                              </div>
                              <div class="col-6">
                                <button type="submit" name="save" class="btn btn-warning btn-lg btn-block withanimation_submit" style="background: #FFD700; border-color: #ffd700;">Yes, Submit</button>
                              </div>
                            </div>
                          </div>
                          </form>
                        </div>
                      </div>
                    </div>

                    <?php } }else{ ?>
                    <tr>
                      <td colspan="4">
                        <center>No Data Entry</center>
                      </td>
                    </tr>
                    <?php } ?>
                    </tbody>
                  </table>
                </div>
            </div>


            <div id="order_small_screen" style="display: none;">
            <?php 
              $order_status = $_GET['status'];
              $sql_order = "SELECT * FROM tbl_user_checkout WHERE order_status = '$order_status'";
              $result_order = $conn->query($sql_order);
              if ($result_order->num_rows > 0) {
                while($row_order = mysqli_fetch_array($result_order)) {
                  $ordernumber = $row_order['order_number'];
            ?>
            <?php if ($row_order['order_status'] == 0) { ?>
              <a data-toggle="modal" data-target="#modalnew<?php echo $row_order['id']; ?>">
            <?php }else{ ?>
              <a data-toggle="modal" data-target="#order_small<?php echo $row_order['id']; ?>">
            <?php } ?>
                <div class="txn-history">
                  <p class="txn-list">
                    <span class="text-white">Order ID: </span><span class="transaction-amount" style="color: #ffd700;"><?php echo $row_order['order_number']; ?></span><br>
                    <span class="text-white">Customer: </span><span class="transaction-amount"><?php echo $row_order['completename']; ?></span><br>
                    <span class="text-white">Mobile Number: </span><span class="transaction-amount"><?php echo $row_order['contactnumber']; ?></span><br>
                    <span class="text-white">Address: </span><span class="transaction-amount"><?php echo $row_order['complete_address']; ?></span><br>
                  </p>
                </div>
              </a>
                    <div class="modal" id="modalnew<?php echo $row_order['id']; ?>">
                      <div class="modal-dialog">
                        <div class="modal-content" style="background-color: #151e27!important;">
                          <div class="modal-header pb-0" style="border: 0px;">
                            <h5 class="modal-title text-white" style="font-weight: bold;">Choose Action</h5>
                          </div>
                          <div class="modal-body pt-2 pb-0"></div>
                          <div class="modal-footer pr-0 pl-0 pb-2 pt-0" style="display: block; border: 0px">
                            <div class="row">
                              <div class="col-6">
                                <button type="button" id="status_btn<?php echo $row_order['id']; ?>" data-toggle="modal" data-target="#status_small<?php echo $row_order['id']; ?>" class="btn btn-primary btn-lg btn-block">Status</button>
                              </div>
                              <div class="col-6">
                                <button type="button" id="order_btn<?php echo $row_order['id']; ?>" data-toggle="modal" data-target="#order_small<?php echo $row_order['id']; ?>" id="view_order_btn<?php echo $row_order['id']; ?>" class="btn btn-info btn-lg btn-block">View Order</button>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>     

                    <div class="modal" id="status_small<?php echo $row_order['id']; ?>">
                      <div class="modal-dialog">
                        <div class="modal-content" style="background-color: #151e27!important;">
                          <div class="modal-header pb-0" style="border: 0px;">
                            <h5 class="modal-title text-white" style="font-weight: bold;">Change Status</h5>
                          </div>
                          <form style="margin: 0px;" method="POST" id="myform">
                          <input type="hidden" name="id" value="<?php echo $row_order['id']; ?>">
                          <div class="modal-body pt-2 pb-0">
                            <div class="form-group">
                              <select class="form-control" name="status" id="status" required>
                                <option disabled selected>Select Status</option>
                                <option <?php if($row_order['order_status'] == 0) { echo 'selected'; } ?> value="0">Pending</option>
                                <option value="1">Review</option>
                                <option value="2">Accept</option>
                              </select>
                            </div>
                          </div>
                          <div class="modal-footer pr-0 pl-0 pb-2 pt-0" style="display: block; border: 0px">
                            <div class="row">
                              <div class="col-6">
                                <button type="button" id="close<?php echo $row_order['id'] ?>" class="btn btn-default btn-lg btn-block" style="background: transparent; color: #fff;" data-dismiss="modal">Cancel</button>
                              </div>
                              <div class="col-6">
                                <button type="submit" name="save" class="btn btn-warning btn-lg btn-block withanimation_submit" style="background: #FFD700; border-color: #ffd700;">Yes, Submit</button>
                              </div>
                            </div>
                          </div>
                          </form>
                        </div>
                      </div>
                    </div>   

                    <div class="modal" id="order_small<?php echo $row_order['id']; ?>">
                      <div class="modal-dialog">
                        <div class="modal-content" style="background-color: #151e27!important;">
                          <div class="modal-header pb-0" style="border: 0px;">
                            <h5 class="modal-title text-white" style="font-weight: bold;">Order Details</h5>
                          </div>
                          <div class="modal-body pt-2 pb-0">
                            <div style="display: flex; justify-content: space-between;">
                              <span class="text-white">Total:</span>
                              <span class="text-white" style="font-size: 20px; font-weight: bolder;">&#8369; <?php echo number_format(($row_order['order_total'])) ?>.00</span>
                            </div>
                            <?php 
                                $sql_cart = "SELECT * FROM tbl_user_checkout_details WHERE order_number = '$ordernumber'";
                                $result_cart = $conn->query($sql_cart);
                                  while($row_cart = mysqli_fetch_array($result_cart)) {
                                  $product_id = $row_cart['product_id'];
                                    $sql_product_details = "SELECT * FROM tbl_product WHERE product_id = '$product_id'";
                                    $result_product_details = mysqli_query($conn, $sql_product_details);
                                    $row_product_details = mysqli_fetch_assoc($result_product_details);
                            ?>
                            <div class="txn-history">
                              <p class="txn-list">
                                <span class="text-white">QTY: <b><?php echo $row_cart['quantity']; ?>x</b></span>
                                <span class="transaction-amount"><span class="text-white">Item: <b><?php echo $row_product_details['product_name']; ?></span>
                              </p>
                            </div>
                            <?php } ?>
                          </div>
                          <div class="modal-footer" style="border: 0px">
                            <button type="button" <?php if($row_order['order_status'] == 0) { ?> id="close_order<?php echo $row_order['id'] ?>" <?php } ?> class="btn btn-default btn-lg btn-block" style="background: transparent; color: #fff;" data-dismiss="modal">Close</button>
                          </div>
                        </div>
                      </div>
                    </div>

      <script type="text/javascript">
        $(document).ready(function(){
          $('#status_btn<?php echo $row_order['id'] ?>').click(function() {
            $('#modalnew<?php echo $row_order['id']; ?>').modal('hide');
          });

          $('#order_btn<?php echo $row_order['id'] ?>').click(function() {
            $('#modalnew<?php echo $row_order['id']; ?>').modal('hide');
          });

          $('#close_order<?php echo $row_order['id'] ?>').click(function() {
            $('#modalnew<?php echo $row_order['id']; ?>').modal('show');
          });

          $('#close<?php echo $row_order['id'] ?>').click(function() {
            $('#modalnew<?php echo $row_order['id']; ?>').modal('show');
          });
        });
      </script>    
            <?php } }else{ ?>
              <center>
                  <div class="text-center error-content p-3">
                   <i class="fas fa-box-open text-warning" style="margin-top: 100px; margin-bottom: 10px; font-size: 80px;"></i>
                   <h6 class="font-weight-bold text-white"> No <?php if($_GET['status'] == 0){ echo 'Pending'; }else{ echo 'Complete'; } ?> Order.</h6>
                </div>
              </center>
            <?php } ?>
            </div>
    </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 type="text/javascript">
$(document).ready(function () {
  var form = $('#myform');
    form.validate({
    rules: {
      complete_name: {
        required: true,
      }
    },
    errorElement: 'span',
    errorPlacement: function (error, element) {
      error.addClass('invalid-feedback');
      element.closest('.form-group').append(error);
    },
    highlight: function (element, errorClass, validClass) {
      $(element).addClass('is-invalid');
    },
    unhighlight: function (element, errorClass, validClass) {
      $(element).removeClass('is-invalid');
    }
  });
});
$(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>
