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

<?php if($session_id==1 || $allPermissions['pincode']['update']==1){ ?>

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

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

	if(empty($_POST['city'])) {
		$valid = 0;
		$error_message .= 'City can not be empty<br>';
	}
		if(empty($_POST['service'])) {
		$valid = 0;
		$error_message .= 'Service can not be empty<br>';
	}
		if(empty($_POST['state'])) {
		$valid = 0;
		$error_message .= 'State can not be empty<br>';
	}

	if(empty($_POST['b2b'])) {
		$valid = 0;
		$error_message .= 'B2B can not be empty<br>';
	}
	if(empty($_POST['b2c'])) {
		$valid = 0;
		$error_message .= 'B2C can not be empty<br>';
	}
	if(empty($_POST['location'])) {
		$valid = 0;
		$error_message .= 'Location can not be empty<br>';
	}

// $code=$_POST['pincode'];
// 	$statement = $pdo->prepare("SELECT * FROM pincode where pin_code='$code'");
// 							$statement->execute();
// 							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
// 							$rows = mysqli_num_rows($result);
//                             if($result)
//                             {
//                                 $valid=0;
//                                 $error_message .= 'Pincode Alreay Exist<br>';
//                             }


	if($valid == 1) {

        	$statement = $pdo->prepare("UPDATE pincode SET pin_code=?, city=?,state=?, service=?, air=?, road=?, b2b=?, b2c=? , oda=?, location=?  WHERE id=?");
    		$statement->execute(array($_POST['pincode'],$_POST['city'],$_POST['state'],$_POST['service'],$_POST['air'],$_POST['road'],$_POST['b2b'],$_POST['b2c'],$_POST['oda'],$_POST['location'],$_REQUEST['id']));

	    $success_message = 'Pincode is updated successfully!';
	}
}
?>

<?php
if(!isset($_REQUEST['id'])) {
	header('location: logout.php');
	exit;
} else {
	// Check the id is valid or not
	$statement = $pdo->prepare("SELECT * FROM pincode WHERE id=?");
	$statement->execute(array($_REQUEST['id']));
	$total = $statement->rowCount();
	$result = $statement->fetchAll(PDO::FETCH_ASSOC);
	if( $total == 0 ) {
		header('location: logout.php');
		exit;
	}
}
?>

<section class="content-header">
	<div class="content-header-left">
		<h1>Edit Service</h1>
	</div>
	<div class="content-header-right">
		<a href="pincode.php" class="btn btn-primary btn-sm">View All</a>
	</div>
</section>

<?php
$statement = $pdo->prepare("SELECT * FROM pincode WHERE id=?");
$statement->execute(array($_REQUEST['id']));
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $row) {
	$pincode = $row['pin_code'];
	$city = $row['city'];
	$service = $row['service'];
	$air = $row['air'];
	$road = $row['road'];
	$b2b = $row['b2b'];
	$b2c = $row['b2c'];
	$state=$row['state'];
	$oda=$row['oda'];
	$location=$row['location'];
}
?>

<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>
			</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">PinCode <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" class="form-control" name="pincode" value="<?php echo $pincode; ?>" readonly>
							</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" class="form-control" name="city" value="<?php echo $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" class="form-control" name="state" value="<?php echo $state; ?>">
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Location <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" class="form-control" name="location" value="<?php echo $location; ?>">
							</div>
						</div>
						
							<div class="form-group">
							<label for="" class="col-sm-2 control-label">Service <span>*</span></label>
							<div class="col-sm-6">
							    <select class="form-control" name="service">
							         <option value="<?php echo $service;?>"><?php echo $service;?></option>
							        <option value="Yes">Yes</option>
							        <option value="No">No</option>
							    </select>
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Air <span>*</span></label>
							<div class="col-sm-6">
							    <select class="form-control" name="air">
							         <option value="<?php echo $air;?>"><?php echo $air;?></option> 
							        <option value="Yes">Yes</option>
							        <option value="No">No</option>
							    </select>
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Surface <span>*</span></label>
							<div class="col-sm-6">
							    <select class="form-control" name="road">
							       <option value="<?php  echo $road;?>"><?php echo $road;?></option> 
							        <option value="Yes">Yes</option>
							        <option value="No">No</option>
							    </select>
							</div>
						</div>
							<div class="form-group">
							<label for="" class="col-sm-2 control-label">B2B <span>*</span></label>
							<div class="col-sm-6">
							    <select class="form-control" name="b2b">
							       <option value="<?php  echo $b2b;?>"><?php echo $b2b;?></option> 
							        <option value="Yes">Yes</option>
							        <option value="No">No</option>
							    </select>
							</div>
						</div>
							<div class="form-group">
							<label for="" class="col-sm-2 control-label">B2C <span>*</span></label>
							<div class="col-sm-6">
							    <select class="form-control" name="b2c">
							       <option value="<?php  echo $b2c;?>"><?php echo $b2c;?></option> 
							        <option value="Yes">Yes</option>
							        <option value="No">No</option>
							    </select>
							</div>
						</div>
							<div class="form-group">
							<label for="" class="col-sm-2 control-label">ODA <span>*</span></label>
							<div class="col-sm-6">
							    <select class="form-control" name="oda">
							       <option value="<?php  echo $oda;?>"><?php echo $oda;?></option> 
							        <option value="Yes">Yes</option>
							        <option value="No">No</option>
							    </select>
							</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 }else{ ?>
<center><h4 style="color:red;">You don't have permission to edit pincode </h4></center>
<?php } ?>
<?php require_once('footer.php'); ?>