<?php include 'header.php';

$track_id = $_REQUEST['track_id'];
$type = $_REQUEST['type'];
$pick_id = "";
	
	if(!isset($_REQUEST['track_id'])){ 
	    echo "<script>
	                alert('Invalid Shipment track data');
	                window.location = 'shipment_tracking.php';
	           </script>";
	}
	
	
	
	if($type=='cscs_awb'){ 
	    $given='CSCS AWB'; 
	}elseif($type=='3pl_awb'){ 
	    $given='3PL AWB'; 
	}elseif($type=='ref_no'){ 
	    $given='Ref No'; 
	}						

						
							$statement = $pdo->prepare("SELECT * FROM `pickup_entry` WHERE $type=?");
							$statement->execute(array($track_id));
							$total_id = $statement->rowCount();
							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
							if(!$result){
							    echo "<script>
							                alert('Incorrect $given Number.');
							                window.location = 'shipment_tracking.php';
							          </script>";
							}
							
							
$statements = $pdo->prepare("SELECT * FROM `pickup_entry` WHERE $type=? ");
$statements->execute(array($track_id));
$pickup_entry_results = $statements->fetchAll(PDO::FETCH_ASSOC);
$pickup_entry_rows = $statements->rowCount();

$statementss = $pdo->prepare("SELECT * FROM `tbl_client_new` WHERE id=? ");
$statementss->execute(array($result[0]['new_client_id']));
$client_name = $statementss->fetchAll(PDO::FETCH_ASSOC);
$client_count = $statementss->rowCount();


$awb_number = $pickup_entry_results[0]['cscs_awb'];
$pickup_entry_id = $pickup_entry_results[0]['id'];
$pickup_entry_status = $pickup_entry_results[0]['status'];
$pickup_entry_remarks = $pickup_entry_results[0]['remarks'];
?>
<style>
    table {
  border-collapse: collapse;
}

table, td, th {
  border: 1px solid black;
}
</style>
<script>
    function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
}
</script>

<section class="content-header" style="background: white;">
	<div class="content-header-left">
		<h1>View Shipment Tracking</h1>
	</div>
	<div class="content-header-right" style="margin-right: 1%;">
		<a href="shipment_tracking.php" class="btn btn-primary btn-sm"><i class="fa fa-arrow-left"></i> Back</a>
	</div>
	
</section>


<section class="content" style="background: white;">

    <center><h4>Shipment Tracking</h4></center>
    <?php 
    foreach ($result as $row){ 
        $mani_fest=$row['manifest_id'];
        $pick_id=$row['id'];
        
        $c_city=$row['consignee_city'];
    ?>
    <table  style="width: 100%;line-height: 34px; font-size: 12px;">
        <tr>
            <td style="text-align:left;padding-left: 2%;" class="border_none">
                
                <?php if($row['cscs_awb']==""){
                    echo "3PL AWB : <b>".$row['3pl_awb']."</b>";
                }else{
                    echo "AWB No: <b>".$row['cscs_awb']."</b>";
                }
                ?>
                
            </td>
            <td style="text-align:left;padding-left: 2%;" class="border_none">
                Book Date :&emsp;<?php $day=date_create($row['date_added']); $new_date=date_format($day,'d-m-Y');  echo $new_date; ?>
            </td>
            <td style="text-align:left;padding-left: 2%;" class="border_none">
                Order Type :&emsp;<?php  echo strtoupper($row['order_type']);  ?>
            </td>
        </tr>
        <tr>
            <td style="text-align:left;padding-left: 2%;" class="border_none">
                Status :&emsp;<?php  echo strtoupper($row['status']);  ?>
            </td>
            <td style="text-align:left;padding-left: 2%;" class="border_none">
                Consignee Name :&emsp;<?php echo $row['consignee_name']; ?>
            </td>
            <td style="text-align:left;padding-left: 2%;" class="border_none">
                Consign.Address :&emsp;<?php echo $row['consignee_address'].', &emsp;'.$row['consignee_city'].', &emsp;'.$row['consignee_state']; ?>
            </td>
        </tr>
        <tr>
            <td style="text-align:left;padding-left: 2%;" class="border_none">
                Consignee Phone :&emsp;<?php echo $row['consignee_phone']; ?>
            </td>
            <td style="text-align:left;padding-left: 2%;" class="border_none">
                Consignee Pinode :&emsp;<?php echo $row['consignee_pincode']; ?>
            </td>
            <td style="text-align:left;padding-left: 2%;" class="border_none">
                Consignee Location :&emsp;<?php echo $row['consignee_location']; ?>
            </td>
        </tr>
        <tr>
            <td style="text-align:left;padding-left: 2%;" class="border_none">
                Client Name :&emsp;<?php echo $client_name[0]['name']; ?>
            </td>
            <td style="text-align:left;padding-left: 2%;" class="border_none">
                Client Phone :&emsp;<?php echo $client_name[0]['phone_no']; ?>
            </td>
            <td style="text-align:left;padding-left: 2%;" class="border_none">
                Client Location :&emsp;<?php echo $client_name[0]['location']; ?>
            </td>
        </tr>
         
        
    </table>
    <?php } ?>  
    </br></br>
    
    
    
    
<!-- Manifest Details Starts Here -->
    <h4><b>Manifest Details</b></h4>
    <?php 
    $statement_manifest = $pdo->prepare("SELECT manifest.*,
org.name as from_name,
des.name as to_name,
coload.name as co_loader_name
FROM `manifest` 
LEFT JOIN tbl_branch org ON org.id=manifest.origin
LEFT JOIN tbl_branch des ON des.id=manifest.destination
LEFT JOIN tbl_coloader coload ON coload.id=manifest.coloader_id
WHERE FIND_IN_SET ('$pickup_entry_id', ids)>0
");
	$statement_manifest->execute();
	$manifest_results = $statement_manifest->fetchAll(PDO::FETCH_ASSOC); 
	$manifest_totalData = $statement_manifest->rowCount();
	?>
    <table style="width: 100%;">
        <thead>
            <tr>
                <th style="text-align: center;" class="header">S.No</th>
                <th style="text-align: center;" class="header">Manifest ID</th>
                <th style="text-align: center;" class="header">Manifest Date</th>
                <th style="text-align: center;" class="header">Manifest From</th>
                <th style="text-align: center;" class="header">Manifest To</th>
                <th style="text-align: center;" class="header">Co Loader</th>
                <th style="text-align: center;" class="header">Receive Date To</th>
            </tr>
        </thead>
        <tbody>
        <?php if($manifest_totalData>0){ 
            $i=1;
            foreach($manifest_results as $manifest){
                
                if($manifest['update_date']!=""){
                    $updated_date = date("d-m-Y", strtotime($manifest['update_date']));
                }else{
                    $updated_date = "";
                }
        ?>
            <tr>
                <td style="text-align: center;" class="values"><?=$i++?></td>
                <td style="text-align: center;" class="values"><?=$manifest['manifest_id']?></td>
                <td style="text-align: center;" class="values"><?=$manifest['entry_date']?></td>
                <td style="text-align: center;" class="values"><?=$manifest['from_name']?></td>
                <td style="text-align: center;" class="values"><?=$manifest['to_name']?></td>
                <td style="text-align: center;" class="values"><?=$manifest['co_loader_name']?></td>
                <td style="text-align: center;" class="values"><?=$updated_date?></td>
            </tr>
        <?php } }else{ ?>
            <tr>
                <td colspan="7" style="text-align: center; padding: 5px;"><b style='color: red;'>No Manifest Found!</b></td>
            </tr>
        <?php } ?>
        </tbody>
    </table>
<!-- Manifest Details Endsss Here -->
 
    
<!-- DRS Details Starts Here -->
    <h4><b>DRS Details</b></h4>
    <?php 
    $statement_drs = $pdo->prepare("SELECT drs.*
FROM `tbl_delivery_run_sheet` drs
WHERE FIND_IN_SET ('$pickup_entry_id', drs.shipment_ids)>0");
	$statement_drs->execute();
	$drs_results = $statement_drs->fetchAll(PDO::FETCH_ASSOC); 
	$drs_totalData = $statement_drs->rowCount();
	?>
    <table style="width: 100%;">
        <thead>
            <tr>
                <th style="text-align: center;" class="header">Status Date</th>
                <th style="text-align: center;" class="header">Status</th>
                <th style="text-align: center;" class="header">DRS No</th>
                <th style="text-align: center;" class="header">Boy Name</th>
                <th style="text-align: center;" class="header">Address</th>
                <th style="text-align: center;" class="header">DRS Date</th>
                <th style="text-align: center;" class="header">Receiver Name</th>
                <th style="text-align: center;" class="header">Receiver Phone</th>
                <th style="text-align: center;" class="header">RTO Reason</th>
            </tr>
        </thead>
        <tbody>
        <?php if($drs_totalData>0){ 
            $i=1;
            foreach($drs_results as $drs){
                
                if($drs['date']!=""){
                    $updated_date = date("d-m-Y", strtotime($drs['date']));
                }else{
                    $updated_date = "";
                }
        ?>
            <tr>
                <td style="text-align: center;" class="values"><?=$updated_date?></td>
                <td style="text-align: center;" class="values"><?=$pickup_entry_status?></td>
                <td style="text-align: center;" class="values"><?=$drs['drs_no']?></td>
                <td style="text-align: center;" class="values"><?=$drs['delivery_boy_name']?></td>
                <td style="text-align: center;" class="values"></td>
                <td style="text-align: center;" class="values"><?=$updated_date?></td>
                <td style="text-align: center;" class="values"></td>
                <td style="text-align: center;" class="values"></td>
                <td style="text-align: center;" class="values"><?=$pickup_entry_remarks?></td>
            </tr>
        <?php } }else{ ?>
            <tr>
                <td colspan="9" style="text-align: center; padding: 5px;"><b style='color: red;'>No DRS Found!</b></td>
            </tr>
        <?php } ?>
        </tbody>
    </table>
<!-- DRS Details Endsss Here -->
    
    </br>
	<div class="row">
		<div class="col-md-12">
		    <div>
				<div>
			  
					<table style="width: 100%;line-height: 34px; font-size: 12px;" border=1>
			        	<thead>
			        <tr>
			            <th style="text-align:center">S.No</th>
			            <th style="text-align:center">Status </th>
			            <th style="text-align:center">Date</th>
			            <th style="text-align:center">Location</th>
			            <th style="text-align:center">Description</th>
			        </tr>
			        	</thead>
			        <tbody>
			       <?php 
			                $i=0;
			                
			                $statement = $pdo->prepare("SELECT * FROM tbl_tracking where pickup_entry_id=?");
							$statement->execute(array($pick_id));
							$result = $statement->fetchAll(PDO::FETCH_ASSOC); 
							foreach ($result as $row) {
							    $i++;
					?>
			        <tr>
			            <td style="text-align:center"><?php echo $i; ?></td>
			            <td style="text-align:center"><?php echo strtoupper($row['status']); ?></td>
			            <td style="text-align:center"><?php  $day=date_create($row['created_at']);
                        $new_date=date_format($day,'d-m-Y');                        
                         echo $new_date;  ?></td>
			            <td style="text-align:center"><?php if($row['status_code']==5 || $row['status_code']==6||$row['status_code']==7){ echo $c_city; } else{echo $row['location'];} ?></td>
			            <td style="text-align:center">
			                <?php  if($row['description']!=''){ echo $row['description'].$row['location'];} ?>
			                <?php  if($row['status_code']==7){  $day=date_create($row['delivery_date']);   $new_date=date_format($day,'d-m-Y');  echo "Delivery date : ".$new_date; } echo "&emsp;";  ?>
			                <?php  if($row['receiver_name']!=''){echo "Receiver Name : "; echo $row['receiver_name']; echo "&emsp;";}?>
			                <?php  if($row['receiver_mobile']!=''){echo "Receiver Mobile : "; echo $row['receiver_mobile']; echo "&emsp;";}?>
			                <?php  if($row['relation']!=''){echo "Relation : "; echo $row['relation']; } ?>
			                <?php  if($row['reason']!=''){echo "Reason : "; echo $row['reason']; }?>
			               
			            </td>
			        </tr>
			        <?php } ?>
			       
			        </tbody>
			    </table>
			    

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

	

<br>
<h4><b>Images</b></h4>
<tbody>
<?php 
    $statement = $pdo->prepare("SELECT * FROM tbl_pod WHERE awb_no = ?");
    $statement->execute(array($awb_number));
    $results = $statement->fetchAll(PDO::FETCH_ASSOC);

    foreach ($results as $rows) {
        // Make sure to properly echo the URL and file name
        echo '<a  title="ImageName">
                <img alt="ImageName" style="width: 20%;height:20%;border: 2px solid red;" class="img-thumbnail" src="img/POD/' . $rows['url_1'] . '">
              </a>
              
              <a download="' . $rows['url_1'] . '" href="img/POD/' . $rows['url_1'] . '" class="btn btn-primary"><i class="bi bi-download"></i></a>
             
                <a href="img/POD/' . $rows['url_1'] . '" class="btn btn-warning" ><i class="bi bi-eye"></i></a>
              ';
    }
?>
							

							
							
							

</tbody>
<!--	<div class="content-footer-right" style="margin-left:45%">
		<input type="button" class="btn btn-danger" onclick="printDiv('printableArea')" value="Print" />
	</div>-->
</section>



<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Delete Confirmation</h4>
            </div>
            <div class="modal-body">
                <p>Are you sure want to delete this item?</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <a class="btn btn-danger btn-ok">Delete</a>
            </div>
        </div>
    </div>
</div>


<?php require_once('footer.php'); ?>
<style>
    .border_none{
        border: none;
    }
</style>