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

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

 include('header.php'); 

$msg = 0;

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

    $sender_name = str_replace("'", "\'", $_POST['sender_name']);
    $sender_address = str_replace("'", "\'", $_POST['sender_address']);
    $sender_phone = str_replace("'", "\'", $_POST['sender_phone']);
    $sender_email = str_replace("'", "\'", $_POST['sender_email']);
    $content_remark = str_replace("'", "\'", $_POST['content_remark']);
    // $usd = str_replace("'", "\'", $_POST['usd']);
    $content_information = str_replace("'", "\'", $_POST['content_information']);
    $country = str_replace("'", "\'", $_POST['country']);
    $state = str_replace("'", "\'", $_POST['state']);
    $city = str_replace("'", "\'", $_POST['city']);
    $postal = str_replace("'", "\'", $_POST['postal']);
    $destination = str_replace("'", "\'", $_POST['destination']);
    $recipient_name = str_replace("'", "\'", $_POST['recipient_name']);
    $recipient_address = str_replace("'", "\'", $_POST['recipient_address']);
    $recipient_phone = str_replace("'", "\'", $_POST['recipient_phone']);
    $recipient_identity = str_replace("'", "\'", $_POST['recipient_identity']);
    $recipient_email = str_replace("'", "\'", $_POST['recipient_email']);
    $shipping_number = str_replace("'", "\'", $_POST['shipping_number']);
    $posting_date = str_replace("'", "\'", $_POST['posting_date']);
    $shipping_date = str_replace("'", "\'", $_POST['shipping_date']);
    $estimate_date = str_replace("'", "\'", $_POST['estimate_date']);
    // $status = str_replace("'", "\'", $_POST['status']);
    // $status_remark = str_replace("'", "\'", $_POST['status_remark']);


    

    mysqli_query($DB_CONN,"UPDATE `shipping` SET `sender_name`='{$sender_name}',`sender_address`='{$sender_address}',`sender_phone`='{$sender_phone}',`sender_email`='{$sender_email}',`content_remark`='{$content_remark}',`content_information`='{$content_information}',`country`='{$country}',`state`='{$state}',`city`='{$city}',`postal`='{$postal}',`destination`='{$destination}',`recipient_name`='{$recipient_name}',`recipient_address`='{$recipient_address}',`recipient_phone`='{$recipient_phone}',`recipient_identity`='{$recipient_identity}',`recipient_email`='{$recipient_email}',`shipping_number`='{$shipping_number}',`posting_date`='{$posting_date}',`shipping_date`='{$shipping_date}',`estimate_date`='{$estimate_date}' WHERE id='{$_GET['id']}'");

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



$quefetch = mysqli_query($DB_CONN,"SELECT * from shipping WHERE id='{$_GET['id']}'");
$rowfetch = mysqli_fetch_assoc($quefetch);

 ?>
      

<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"> Shipping Detail </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> &nbsp;  Shipping Detail
                <a href="view_shipping.php" class="btn btn-info btn-sm view" style="float: right;"> View Shipping </a> &nbsp;
                <button type="button" onclick="editshipping()" id="edit_shipping" class="btn btn-success btn-sm edit" > Edit </button> &nbsp;
                <button type="button" onclick="donteditshipping()" id="dontshipping" class="btn btn-warning btn-sm dontedit" > Don't Edit </button> &nbsp;
            </div>

         

            <div class="card-body">
               <form method="POST" action="">
                  <div class="container">
                      <div class="row">
                         
                              <div class="col-md-6">
                                 <label> Sender Name  </label>
                                 <input type="text" name="sender_name" placeholder="Enter Sender Name" class="form-control" required value="<?php echo $rowfetch['sender_name'] ?>" readonly>
                              </div>

                              <div class="col-md-6">
                                  <label> Sender Email  </label>
                                  <input type="email" name="sender_email" placeholder="Enter Sender Email" class="form-control" required value="<?php echo $rowfetch['sender_email'] ?>" readonly>
                              </div>

                              <div class="col-md-6">
                                 <label> Sender Phone  </label>
                                 <input type="text" name="sender_phone" placeholder="Enter Sender Phone" class="form-control" required value="<?php echo $rowfetch['sender_phone'] ?>" readonly>
                              </div>

                              <div class="col-md-6">
                                  <label> Sender Address  </label>
                                  <textarea name="sender_address" class="form-control" required readonly><?php echo $rowfetch['sender_address'] ?></textarea>
                              </div>


                                      <div class="col-md-12">  <h5> SHIPPING INFORMATION </h5> <br> </div>

   
                              

                              <div class="col-md-6">
                                 <label> Content Remark  </label>
                                 <textarea name="content_remark" class="form-control" required readonly><?php echo $rowfetch['content_remark'] ?></textarea>
                              </div>

                              <div class="col-md-6">
                                 <label> Content Information  </label>
                                 <textarea name="content_information" class="form-control" required readonly><?php echo $rowfetch['content_information'] ?> </textarea>
                              </div>


                                    <div class="col-md-12"> <br> <h5> RECIPIENT INFORMATION </h5>  <br></div>

                              <div class="col-md-6">
                                 <label> Identification Card </label>
                                 <input type="text" name="recipient_identity" placeholder="Enter Identification Card" class="form-control" required value="<?php echo $rowfetch['recipient_identity'] ?>" readonly>
                              </div>


                              <div class="col-md-6">
                                 <label> Recipient Name  </label>
                                 <input type="text" name="recipient_name" placeholder="Enter Sender Name" class="form-control" required value="<?php echo $rowfetch['recipient_name'] ?>" readonly>
                              </div>

                              <div class="col-md-6">
                                  <label> Recipient Email  </label>
                                  <input type="email" name="recipient_email" placeholder="Enter Recipient Email " class="form-control" required value="<?php echo $rowfetch['recipient_email'] ?>" readonly>
                              </div>

                              <div class="col-md-6">
                                 <label> Recipient Phone  </label>
                                 <input type="text" name="recipient_phone" placeholder="Enter Recipient Phone" class="form-control" required value="<?php echo $rowfetch['recipient_phone'] ?>" readonly >
                              </div>

                              <div class="col-md-6">
                                  <label> Recipient Address  </label>
                                  <textarea name="recipient_address" class="form-control" required readonly ><?php echo $rowfetch['recipient_address'] ?></textarea>
                              </div>
                              
                              <div class="col-md-6">
                                  <label> Country  </label>
                                  <input type="text" name="country" placeholder="Enter Country" class="form-control" required value="<?php echo $rowfetch['country'] ?>" readonly>
                              </div>

                              <div class="col-md-6">
                                  <label> State  </label>
                                  <input type="text" name="state" placeholder="Enter State" class="form-control" required value="<?php echo $rowfetch['state'] ?>" readonly>
                              </div>

                              <div class="col-md-6">
                                  <label> City  </label>
                                  <input type="text" name="city" placeholder="Enter City" class="form-control" required value="<?php echo $rowfetch['city'] ?>" readonly>
                              </div>

                              <div class="col-md-6">
                                  <label> Destination  </label>
                                  <input type="text" name="destination" placeholder="Enter Destination" class="form-control" required value="<?php echo $rowfetch['destination'] ?>" readonly>
                              </div>

                              <div class="col-md-6">
                                  <label> Postal Code  </label>
                                  <input type="text" name="postal" placeholder="Enter Postal Code " class="form-control" required value="<?php echo $rowfetch['postal'] ?>" readonly>
                              </div>



                                    <div class="col-md-12"> <br> <h5> More Details </h5>  <br></div>

                              <div class="col-md-6">
                                 <label> Shipping Number </label>
                                 <input type="text" name="shipping_number" class="form-control shipping_number" readonly value="<?php echo $rowfetch['shipping_number'] ?>" readonly >
                              </div>


                              <div class="col-md-6">
                                 <label> Posting Date  </label>
                                 <input type="date" name="posting_date" class="form-control" required value="<?php echo date('Y-m-d',strtotime($rowfetch['posting_date'])) ?>" readonly>
                              </div>

                              <div class="col-md-6">
                                  <label> Shipping Date  </label>
                                  <input type="date" name="shipping_date" class="form-control" required value="<?php echo date('Y-m-d',strtotime($rowfetch['shipping_date'])) ?>" readonly>
                              </div>

                              <div class="col-md-6">
                                 <label> Estimated Date of Delivery  </label>
                                 <input type="text" name="estimate_date" placeholder="Enter Recipient Phone" class="form-control" required value="<?php echo date('Y-m-d',strtotime($rowfetch['estimate_date'])) ?>" readonly>
                              </div>

<!--                               <div class="col-md-6">
                                  <label> Status Remark  </label>
                                  <textarea name="status_remark" class="form-control" required readonly><?php echo $rowfetch['status_remark'] ?></textarea>
                              </div>

                              <div class="col-md-6">
                                  <label> Shipping Status  </label>
                                  <select name="status" class="form-control" readonly >
                                      <option value=""> - Select Status - </option>

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

                                  </select>
                              </div> -->



                              <div class="col-md-6">
                                  <br> <button class="btn btn-success form-control savebtn" type="submit" name="submit" > Click to Update </button>
                              </div>
                              

                                      

                              
                            
                           

                           
                      </div>
                  </div> 
                </form> 
            </div>
            
            
            <h3> SHIPPING HISTORY </h3>
            
            <table class="table table-striped">
                <tr>
                    <th> Sr# </th>
                    <th> Datetime </th>
                    <th> Status </th>
                    <th> Status Remarks </th>
                    <th> Destination </th>
                    
                </tr>

<?php
   $j = 1;
   $rowst = mysqli_query($DB_CONN,"SELECT * from statuses WHERE shipping_id='{$_GET['id']}' ORDER by id DESC"); 
   while($rowefetchst = mysqli_fetch_assoc($rowst)) {
   
   
   ?>                
                
                <tr>
                    <td> <?php echo $j; ?> </td>
                    <td> <?php echo date("H:i:s",strtotime($rowefetchst['timestamp'])). ' '. date("d-m-Y",strtotime($rowefetchst['date'])); ?> </td>
                    <td> <?php echo $rowefetchst['status']; ?> </td>
                    <td> <?php echo $rowefetchst['status_remark']; ?> </td>
                    <td> <?php echo $rowefetchst['destination']; ?> </td>
                </tr>
                
    <?php  $j++; } ?>

                
            </table>

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


    <!-- 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 editshipping() {
     $('.form-control').attr('readonly', false);
     $('.shipping_number').attr('readonly', true);
     $('#dontshipping').show();
     $('#edit_shipping').hide();
     $(".savebtn").show();
  }

  function donteditshipping() {
     $('.form-control').attr('readonly', true);
     $('#dontshipping').hide();
     $('#edit_shipping').show();
     $(".savebtn").hide();
  }

</script>

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