<?php include('../includes/connection.php');
@session_start();

if(!isset($_SESSION['admin_login'])) {
  header("Location: login.php");
  }

 include('header.php'); 

$msg = 0;

if(isset($_POST['delete'])) {
    mysqli_query($DB_CONN,"DELETE from shipping WHERE id='{$_POST['hide_id']}'");

    $msg = 1;
    $msg_alert = '<br> <div class="alert alert-danger alert-dismissible"> <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
      <strong> Success! </strong>  Ad Deleted Successfully </div>';
}


 ?>
      <div id="content-wrapper">

        <div class="container-fluid">

          <!-- Breadcrumbs-->
          <ol class="breadcrumb">
            <li class="breadcrumb-item">
              <a href="#"> Dashboard </a>
            </li>
            <li class="breadcrumb-item active"> View Shipping </li>
          </ol>

          <?php if ($msg=='1') {
                echo $msg_alert;
              } ?>

          <!-- DataTables Example -->
          <div class="card mb-3">
            <div class="card-header">
              <i class="fas fa-table"></i>  View Shipping
              <a href="add_shipping.php"  style="float: right;" class="btn btn-info btn-sm" > Add New Shipping </a>
          </div>

           

            <div class="card-body">
              <div class="table-responsive">
                <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                  <thead>
                    <tr>
                      <th>Sr# </th>
                      <th>Seller Name</th>
                      <th>Seller Phone</th>
                      
                      <th>Current Status</th>
                      <th>Actions</th>
                    </tr>
                  </thead>

                  <tbody>

<?php $i = 1;
      $sql=  mysqli_query($DB_CONN,"SELECT * from shipping");
             while($rowsql = mysqli_fetch_assoc($sql)) { 
             
       $statusid = mysqli_fetch_assoc(mysqli_query($DB_CONN,"SELECT * from statuses WHERE shipping_id='{$rowsql['id']}' ORDER by id DESC limit 1 ")); 
             
             
             ?>

                    <tr id="rowid<?php echo $rowsql['id']; ?>">
                      <td> <?php echo $i; ?> </td>
                      <td> <?php echo $rowsql['sender_name']; ?> </td>
                      <td> <?php echo $rowsql['sender_phone']; ?></td>
                      
                      <td id="statusrow<?php echo $rowsql['id']; ?>"> <?php echo $statusid['status']; ?> </td>
                      <td> <form method="POST" action="">
                        <input type="hidden" id="hide_id<?php echo $rowsql['id']; ?>" name="hide_id" value="<?php echo $rowsql['id']; ?>" >

                       <a href="detail_shipping.php?id=<?php echo $rowsql['id']; ?>" title="View" class="btn btn-sm btn-info"> View </a> &nbsp; 

                       <button type="button" name="delete" class="btn btn-sm btn-danger" onclick="deleteshipping(<?php echo $rowsql['id']; ?>)" > Delete </button>
                     
<button type="button" class="btn btn-warning btn-sm" data-toggle="modal" data-target="#myModal<?php echo $rowsql['id']; ?>"> Update Status </button>

<!-- Modal -->
<div id="myModal<?php echo $rowsql['id']; ?>" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <b class="modal-title"> Update Status </b>
      </div>
      <div class="modal-body">
               <label> Shipping Status  </label>
               <select id="status<?php echo $rowsql['id']; ?>" class="form-control" >
                  <option value=""> - Select Status - </option>

                  <option <?php if($statusid['status']=='In Warehouse') { echo 'selected'; } ?> value="In Warehouse"> In Warehouse </option>
                  <option <?php if($statusid['status']=='Processing') { echo 'selected'; } ?> value="Processing"> Processing   </option>
                  <option <?php if($statusid['status']=='Intransit') { echo 'selected'; } ?> value="Intransit"> Intransit </option>
                  <option <?php if($statusid['status']=='Custom') { echo 'selected'; } ?> value="Custom"> Custom</option>
                  <option <?php if($statusid['status']=='Held by custom') { echo 'selected'; } ?> value="Held by custom"> Held by custom </option>
                  <option <?php if($statusid['status']=='Available for pick up') { echo 'selected'; } ?> value="Available for pick up"> Available for pick up  </option>
                  <option <?php if($statusid['status']=='Delivered') { echo 'selected'; } ?> value="Delivered"> Delivered </option>

               </select>

              <label> Destination  </label>
              <input type="text" id="destination<?php echo $rowsql['id']; ?>" value="<?php echo $statusid['destination'] ?>" class="form-control" required >
              
              <label> Shipping Date  </label>
              <input type="date" id="shippingdate<?php echo $rowsql['id']; ?>" value="<?php echo date('Y-m-d',strtotime($statusid['date'])) ?>" class="form-control" required >

              <label> Status Remark  </label>
              <textarea id="status_remark<?php echo $rowsql['id']; ?>" class="form-control" required ><?php echo $statusid['status_remark'] ?></textarea>


      </div>
      <div class="modal-footer">  
        <button type="button" onclick="updatestatus(<?php echo $rowsql['id']; ?>)" class="btn btn-primary" data-dismiss="modal"> Update Status </button>
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</div>
                       

                        </form> </td>
                    </tr>

    <?php $i++; } ?>

                    
                  </tbody>
                </table>
              </div>
            </div>
          </div>
      </div>
     

    </div>
    <!-- /#wrapper -->

    <!-- Scroll to Top Button-->
    <a class="scroll-to-top rounded" href="#page-top">
      <i class="fas fa-angle-up"></i>
    </a>



<script type="text/javascript">

  function updatestatus(id) {
     var status = $("#status"+id).val();
     var status_remark = $("#status_remark"+id).val();
     var destination = $("#destination"+id).val();
     var shippingdate = $("#shippingdate"+id).val();
     var hide_id = $("#hide_id"+id).val();

     $.ajax({
         url:'ajax.php?h=updatestatus',
         method:'POST', 
         data:{hide_id:hide_id,status:status,status_remark:status_remark,destination:destination,shippingdate:shippingdate},

         success:function(){
             $("#statusrow"+id).html(status);

         }
     });
  }



function deleteshipping(id) {
       var hide_id = $("#hide_id"+id).val();

       bootbox.confirm(" Confirm Delete ??? ", function(result){ 
        if(result==true) {

          $.ajax({
             url:'ajax.php?h=delete_shipping',
             method:'POST', 
             data:{hide_id:hide_id},

             success:function(){
                 $("#rowid"+id).html(status);

             }
         });
      }
});
}


</script>


<?php  include('footer.php'); ?>