<?php 
require_once('header.php'); 
?>
<?php
if(isset($_POST['form1'])) {
	$valid = 1;

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

	if(empty($_POST['cust_code'])) {
		$valid = 0;
		$error_message .= 'Customer code can not be empty<br>';
	}

    	if(empty($_POST['cust_address'])) {
		$valid = 0;
		$error_message .= 'Address can not be empty<br>';
	}
        
        	if(empty($_POST['cust_state'])) {
		$valid = 0;
		$error_message .= 'State can not be empty<br>';
	}

	if(empty($_POST['cust_zip'])) {
		$valid = 0;
		$error_message .= 'Pincode can not be empty<br>';
	}
		if(empty($_POST['cust_city'])) {
		$valid = 0;
		$error_message .= 'City can not be empty<br>';
	}
		if(empty($_POST['client_id'])) {
		$valid = 0;
		$error_message .= 'Select the Client <br>';
	}

$code=$_POST['cust_code'];
	$statement = $pdo->prepare("SELECT * FROM tbl_customer where cust_code='$code'");
							$statement->execute();
							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
                            if($result)
                            {
                                $valid=0;
                                $error_message .= 'Consignee Code Alreay Exist<br>';
                            }
							

    if($valid==1)
    {
    
		$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['cust_code'],$_POST['cust_name'],$_POST['cust_address'],$_POST['cust_zip'],$_POST['cust_phone'],$_POST['cust_city'],$_POST['cust_state'],$_POST['destination_city']));
			
		$success_message = 'Consignee is added successfully!';

		unset($_POST['cust_code']);
		unset($_POST['cust_name']);
		unset($_POST['cust_address']);
		unset($_POST['cust_zip']);
		unset($_POST['cust_phone']);
		unset($_POST['cust_city']);
		unset($_POST['destination_city']);
    }
	
}
?>


<section class="content">

        <div class="row">
    		<div class="col-md-4" style="text-align: left;">
    			<h4><i class="fa fa-map-marker"></i> Add Consignee Details</h4>
    		</div>
    		<div class="col-md-8" style="text-align: right;"> 
    		    <a href="customer.php" class="btn btn-warning btn-sm"><i class="fa fa-arrow-left"></i> Back to Consignee List</a>
    		</div>
    	</div> 
	<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>
			</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" hidden>
							<label for="" class="col-sm-2 control-label">Select Client <span>*</span></label>
							<div class="col-sm-6">
							    <select name="client_id" class="form-control select2">
							       <option value="1"></option>
							    </select>
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Name <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" class="form-control" name="cust_name" value="<?php if(isset($_POST['cust_name'])){echo $_POST['cust_name'];} ?>">
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Code <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" class="form-control" name="cust_code" value="<?php if(isset($_POST['cust_code'])){echo $_POST['cust_code'];} ?>">
							</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" class="form-control" name="cust_city" id="cust_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" class="form-control" name="cust_state" id="cust_state">
							</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="cust_address" id="cust_address" style="height:100px;"><?php if(isset($_POST['cust_address'])){echo $_POST['cust_address'];} ?></textarea>
							</div>
							<div class="col-sm-4">
								<span id="remain" >110</span><span id="remain_txt" >characters remaining</span>
							</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" class="form-control" name="cust_zip" value="<?php if(isset($_POST['cust_zip'])){echo $_POST['cust_zip'];} ?>">
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label"> Mobile <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" class="form-control" name="cust_phone" value="<?php if(isset($_POST['cust_phone'])){echo $_POST['cust_phone'];} ?>">
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Destination City </label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" 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"></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>

<script>
/*
$("#cust_zip").change(function () {
     get_pincode();
     
});


function get_pincode(){
    $('#cust_city').empty(); 
    $('#cust_state').empty(); 
    var cust_zip = $('#cust_zip').val();
            $.ajax({
                    url: 'apis/get-pincode-details.php',
                    type: 'post',
                    dataType: 'json',
                    data: {type:'get_picode_details',cust_zip:cust_zip},
                    success: function (response) {
                      //  console.log(response);
                         if(response['error']==false){
                                  $('#cust_city').val(response['data'][0]['city']);
                                  $('#cust_state').val(response['data'][0]['state']);
                              
                        }else{
                             $('#cust_city').val();
                             $('#cust_state').val();
                            
                        }
                    }
                });
}*/

</script>





<script>
    var maxchars = 110;

$('textarea').keyup(function () {
    var tlength = $(this).val().length;
    $(this).val($(this).val().substring(0, maxchars));
    var tlength = $(this).val().length;
    remain = maxchars - parseInt(tlength);
   
    if(remain<10){
         $('#remain').css('color','red');
         $('#remain_txt').css('color','red');
    }
    else{ $('#remain').css('color','green');
    $('#remain_txt').css('color','green');
    }
     $('#remain').text(remain);
});
</script>

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