<?php require_once('header.php'); ?>

<?php
if(isset($_POST['form1'])) {
   // var_dump($_POST);
	$valid = 1;
//	print_r($_POST);

	if(empty($_POST['client_id'])) {
		$valid = 0;
		$error_message .= 'client identify can not be empty<br>';
	}

	if(empty($_POST['pincode'])) {
		$valid = 0;
		$error_message .= 'pincode can not be empty<br>';
	}
		if(empty($_POST['shipping_mode'])) {
		$valid = 0;
		$error_message .= 'shipping mode can not be empty<br>';
	}

		if(empty($_POST['address'])) {
		$valid = 0;
		$error_message .= 'address Code can not be empty<br>';
	}
	if(empty($_POST['city'])) {
		$valid = 0;
		$error_message .= 'city can not be empty<br>';
	}
	if(empty($_POST['state'])) {
		$valid = 0;
		$error_message .= 'state can not be empty<br>';
	}
	if(empty($_POST['phone'])) {
		$valid = 0;
		$error_message .= 'phone can not be empty<br>';
	}
	if(empty($_POST['c_weight'])) {
		$valid = 0;
		$error_message .= 'Charged weight can not be empty<br>';
	}
	if(empty($_POST['a_weight'])) {
		$valid = 0;
		$error_message .= 'Actual weight can not be empty<br>';
	}
	if(empty($_POST['content_desc'])) {
		$valid = 0;
		$error_message .= 'Content Description can not be empty<br>';
	}
		if(empty($_POST['payment_mode'])) {
		$valid = 0;
		$error_message .= 'Payment Mode can not be empty<br>';
	}
// 		if(empty($_POST['other_charges'])) {
// 		$valid = 0;
// 		$error_message .= 'Other Charges can not be empty<br>';
// 	}
// 		if(empty($_POST['total_charges'])) {
// 		$valid = 0;
// 		$error_message .= 'Total Charges can not be empty<br>';
// 	}
	if(empty($_POST['pics'])) {
		$valid = 0;
		$error_message .= 'No of pieces can not be empty<br>';
	}
	
	
	
// 	**********************************  Generate AWB No Start ********************************************
                            $statement = $pdo->prepare("SELECT MAX(awb_no) as big_id FROM pickup_entry");
							$statement->execute();
							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
							//$rows = mysqli_num_rows($result);
								foreach ($result as $row)
								{
								    $bid=$row['big_id'];
                                    //print_r($bid);
                                    
                                    if($bid!='')
                                    {
                                        $awb_no=$row['big_id']+1;
                                    }
                                    else
                                    {
                                        $awb_no=100001;
                                    }

								}
	
// 	**********************************  Generate AWB No End ********************************************
        $statement = $pdo->prepare("INSERT INTO tbl_customer (client_id,cust_code,cust_name,cust_address,cust_zip,cust_phone,cust_city,cust_state,destination_city) VALUES (?,?,?,?,?,?,?,?,?)");
		$statement->execute(array($_POST['client_id'],'',$_POST['customer_name'],$_POST['address'],$_POST['pincode'],$_POST['phone'],$_POST['city'],$_POST['state'],$_POST['destination_city']));
			
			
			// 	**********************************  Fetch Customer Id ********************************************
            $statement = $pdo->prepare("SELECT * FROM `tbl_customer` WHERE cust_phone=? and cust_name=? and client_id=? order by cust_id DESC LIMIT 1");
			$statement->execute(array($_POST['phone'],$_POST['customer_name'],$_POST['client_id']));
			$result = $statement->fetchAll(PDO::FETCH_ASSOC);
			//$rows = mysqli_num_rows($result);
				foreach ($result as $row)
				{
				    $cust_ids=$row['cust_id'];
				}

    
    if($clientType=='Admin')
{
    $pic_status='picked';
}
else
{
     $pic_status='pending';
}							

    if($valid==1)
    {
		$statement = $pdo->prepare("INSERT INTO pickup_entry (awb_no,ref_no,client_id,status,pincode,shipping_mode,customer_id,address,city,state,destination,phone,c_weight,a_weight,content_desc,total_charges,other_charges,payment_mode,pics,gst_no) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
		$statement->execute(array($awb_no,$_POST['awb_no'],$_POST['client_id'],$pic_status,$_POST['pincode'],$_POST['shipping_mode'],$cust_ids,$_POST['address'],$_POST['city'],$_POST['state'],$_POST['destination_city'],$_POST['phone'],$_POST['c_weight'],$_POST['a_weight'],$_POST['content_desc'],$_POST['total_charges'],$_POST['other_charges'],$_POST['payment_mode'],$_POST['pics'],$_POST['gst_no']));
			
		/*Tracking Module Start*/
		/* 	**********************************  Fetch Pickup Entry Id ******************************************** */
            $statement = $pdo->prepare("SELECT * FROM `pickup_entry` WHERE awb_no=? and ref_no=? and client_id=? order by client_id DESC LIMIT 1");
			$statement->execute(array($awb_no,$_POST['awb_no'],$_POST['client_id']));
			$result = $statement->fetchAll(PDO::FETCH_ASSOC);
			
				foreach ($result as $row)
				{
				    $pickup_ids=$row['id'];
				}
				
		$statement = $pdo->prepare("INSERT INTO tbl_tracking (pickup_entry_id,user_id,location,status,description,remark) VALUES (?,?,?,?,?,?)");
		$statement->execute(array($pickup_ids,$clientId,$clientLocation,'picked','Entry Successfully Picked','Material Safe...'));
			
		/*Tracking Module End*/
		$success_message = 'Pickup Entry is added successfully!';

		unset($_POST['client_id']);
		unset($_POST['pincode']);
		unset($_POST['shipping_mode']);
		unset($_POST['customer_name']);
		unset($_POST['address']);
		unset($_POST['city']);
		unset($_POST['state']);
		unset($_POST['phone']);
		unset($_POST['c_weight']);
		unset($_POST['content_desc']);
		unset($_POST['total_charges']);
		unset($_POST['other_charges']);
		unset($_POST['a_weight']);
		unset($_POST['gst_no']);
		unset($_POST['pics']);
		unset($_POST['payment_mode']);
    }
	
}
?>

<!--Script for onchange function on selection of pincode to get available shipping mode  -->
<!-- Script -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src='js/select2.full.min.js' type='text/javascript'></script>

<!-- CSS -->
<link href='css/select2.full.min.css' rel='stylesheet' type='text/css'>
<!--Script for onchange function on selection of pincode to get available shipping mode  -->
<script>
    $(document).ready(function(){
         
    /***********    For fetching gst number ***********/
         $('#client_id').on('change',function(){
           var client_id=$(this).val();
         //  alert(client_id);
           if(client_id){
                $.ajax({
                  type:'POST',
                  url:'get_client_data.php',
                  data:{id:client_id},
                   dataType: 'json',
                  success:function(response){
                      var len = response.length;
                      $('#gst').val(response[0]['gst']);
                  }
               });
           }
       });
     
        /***********    For fetching city ,state and shipping mode ***********/
       $('#pincode').on('change',function(){
          // alert('alertttttt');
           var pincode_id=$(this).val();
           if(pincode_id){
               $.ajax({
                  type:'POST',
                  url:'get_pincode_data.php',
                  data:{id:pincode_id},
                  success:function(html){
                      $('#shipping_mode').html(html);
                  }
               });
                $.ajax({
                  type:'POST',
                  url:'get_pin_data.php',
                  data:{id:pincode_id},
                   dataType: 'json',
                  success:function(response){
                      var len = response.length;
                    //   alert(response[0]['city']);
                      $('#city').val(response[0]['city']);
                      $('#state').val(response[0]['state']);
                  }
               });
           }
           else{
            $('#shipping_mode').html('<option value="">Select Pincode first</option>');
        }
       });
    });
     $(document).ready(function(){
       $('#payment_mode').on('change',function(){
            var mode=$(this).val();
            if(mode=='credit')
            {
                $('#total_charges').val('credit');
                $('#other_charges').val('credit');
                $('#total_charges').prop('readonly', true);
                $('#other_charges').prop('readonly', true);
            }
             if(mode=='cash')
            {
                $('#total_charges').val('');
                $('#other_charges').val('');
                $('#total_charges').prop('readonly', false);
                $('#other_charges').prop('readonly', false);
            }
            
       });
    });
</script>


<section class="content-header">
	<div class="content-header-left">
		<h1>Add Pickup Entry</h1>
	</div>
	<div class="content-header-right">
		<a href="pickup-entry.php" class="btn btn-danger btn-sm">View All</a>
	</div>
</section>


<section class="content">

	<div class="row">
		<div class="col-md-12">

			<?php if($error_message): ?>
			<div class="callout callout-danger">
				<p>
					<?php echo $error_message; ?>
				</p>
			</div>
			<?php endif; ?>

			<?php if($success_message): ?>
			<div class="callout callout-success">
				<p><?php echo $success_message; ?></p>
					<a href="pk_awb_number_print.php?list_id=<?php echo $awb_no; ?>" target="_blank"><button class="btn btn-md btn-success">Print AWB Number</button></a>
				<a href="pk_lable_print.php?list_id=<?php echo $awb_no; ?>" target="_blank"><button class="btn btn-md btn-success">Print Lable</button></a>
			</div>
			<?php endif; ?>
			
			<form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
				<div class="box box-info">
					<div class="box-body">
					    
					    	<div class="form-group">
							<label for="" class="col-sm-2 control-label">Client Name <span>*</span></label>
							<div class="col-sm-6">
							    <select class="form-control" name="client_id" id="client_id">
							        <option >Select Client Name </option>
							        <?php if(isset($_POST['client_id'])) { ?> <option value="<?php echo $_POST['client_id'];?>"><?php echo $_POST['client_id'];?></option>  <?php } ?>
							        <?php
									$statement = $pdo->prepare("SELECT * FROM tbl_user WHERE role='client' ORDER BY id ASC");
									$statement->execute();
									$result = $statement->fetchAll(PDO::FETCH_ASSOC);	
									foreach ($result as $row) {
										?>
										<option value="<?php echo $row['id']; ?>"><?php echo $row['full_name']; ?></option>
										<?php
									}
									?>
							    </select>
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Pincode <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" id="city" class="form-control" name="pincode" value="<?php if(isset($_POST['pincode'])){echo $_POST['pincode'];} ?>">
							</div>
						</div>
							<div class="form-group">
							<label for="" class="col-sm-2 control-label">Shipping Mode <span>*</span></label>
							<div class="col-sm-6">
							    <select class="form-control" name="shipping_mode" id="shipping_mode">
							        <option >Select Shipping Mode</option>
							        <option value="Air">Air</option>
							        <option value="Road">Road</option>
							    </select>
							</div>
						</div>
					
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Customer <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" id="customer_name" class="form-control" name="customer_name" value="<?php if(isset($_POST['customer_name'])){echo $_POST['customer_name'];} ?>">
							</div>
						</div>
						
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Address <span>*</span></label>
							<div class="col-sm-6">
								<textarea class="form-control" name="address" id="address" style="height:100px;"><?php if(isset($_POST['address'])){echo $_POST['address'];} ?></textarea>
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">City <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" id="city" class="form-control" name="city" value="<?php if(isset($_POST['city'])){echo $_POST['city'];} ?>">
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">State <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" id="state" class="form-control" name="state" value="<?php if(isset($_POST['state'])){echo $_POST['state'];} ?>">
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Destination City <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" id="destination_city" class="form-control" name="destination_city" value="<?php if(isset($_POST['destination_city'])){echo $_POST['destination_city'];} ?>">
							</div>
						</div>
							<div class="form-group">
							<label for="" class="col-sm-2 control-label">Phone Number <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" class="form-control" name="phone" id="phone" value="<?php if(isset($_POST['phone'])){echo $_POST['phone'];} ?>">
							</div>
						</div>
						
							<div class="form-group">
							<label for="" class="col-sm-2 control-label">Content Description <span>*</span></label>
							<div class="col-sm-6">
							    <select class="form-control" name="content_desc" >
							        <!--<option >Select Description</option>-->
							 
							        <?php
									$statement = $pdo->prepare("SELECT * FROM material_desc ORDER BY id ASC");
									$statement->execute();
									$result = $statement->fetchAll(PDO::FETCH_ASSOC);	
									foreach ($result as $row) {
										?>
										<option value="<?php echo $row['id']; ?>"><?php echo $row['short_desc']; ?></option>
										<?php
									}
									?>
							    </select>
							</div>
						</div>
						
							<div class="form-group">
							<label for="" class="col-sm-2 control-label">Payment Mode <span>*</span></label>
							<div class="col-sm-6">
							    <select class="form-control" name="payment_mode" id="payment_mode">
							        <option >Select Payment Mode</option>
							        <option value="cash"  <?php if(isset($_POST['payment_mode'])) {if($_POST['payment_mode']=='cash'){echo "selected";}} ?>>CASH</option>
							        <option value="credit" <?php if(isset($_POST['payment_mode'])) {if($_POST['payment_mode']=='credit'){echo "selected";}} ?>>CREDIT</option>
							    </select>
							</div>
						</div>
						
						<div class="form-group" id="oc">
							<label for="" class="col-sm-2 control-label">Other Charges <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" class="form-control" name="other_charges" id="other_charges" value="<?php if(isset($_POST['other_charges'])){echo $_POST['other_charges'];} ?>">
							</div>
						</div>
						<div class="form-group" id="oc">
							<label for="" class="col-sm-2 control-label">Total Charges <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" class="form-control" name="total_charges" id="total_charges" value="<?php if(isset($_POST['total_charges'])){echo $_POST['total_charges'];} ?>">
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Actual Weight <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" class="form-control" name="a_weight" value="<?php if(isset($_POST['a_weight'])){echo $_POST['a_weight'];} ?>">
							</div>
						</div>
							<div class="form-group">
							<label for="" class="col-sm-2 control-label">Charged Weight <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" class="form-control" name="c_weight" value="<?php if(isset($_POST['c_weight'])){echo $_POST['c_weight'];} ?>">
							</div>
						</div>
							<div class="form-group">
							<label for="" class="col-sm-2 control-label">Number of Pieces <span>*</span></label>
							<div class="col-sm-6">
								<input type="number" autocomplete="off" class="form-control" name="pics" value="<?php if(isset($_POST['pics'])){echo $_POST['pics'];} ?>">
							</div>
						</div>
					    	<div class="form-group">
							<label for="" class="col-sm-2 control-label">GST Number </label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" id="gst" class="form-control" name="gst_no" value="<?php if(isset($_POST['gst_no'])){echo $_POST['gst_no'];} ?>">
							</div>
						</div>
					    	<div class="form-group">
							<label for="" class="col-sm-2 control-label">AWB No <span>*</span></label>
							<div class="col-sm-6">
							    	<input type="text" autocomplete="off" id="awb_no" class="form-control" name="awb_no" value="<?php if(isset($_POST['awb_no'])){echo $_POST['awb_no'];} ?>">
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label"></label>
							<div class="col-sm-6">
								<button type="submit" class="btn btn-success pull-left" name="form1">Submit</button>
							</div>
						</div>
					</div>
				</div>
			</form>
		</div>
	</div>

</section>

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