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

<?php if($allPermissions['pincode']['create']=="1"){ ?>

<?php

if(isset($_POST['form1'])) {
   // var_dump($_POST);
	$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['state'])) {
		$valid = 0;
		$error_message .= 'State can not be empty<br>';
	}
		if(empty($_POST['service'])) {
		$valid = 0;
		$error_message .= 'Service 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("INSERT INTO pincode (pin_code,city,state,service,air,road,b2b,b2c,oda,location) VALUES (?,?,?,?,?,?,?,?,?,?)");
		$statement->execute(array($_POST['pincode'],$_POST['city'],$_POST['state'],$_POST['service'],$_POST['air'],$_POST['road'],$_POST['b2b'],$_POST['b2c'],$_POST['oda'],$_POST['location']));
			
		$success_message = 'Pincode is added successfully!';

		unset($_POST['pincode']);
		unset($_POST['city']);
		unset($_POST['state']);
		unset($_POST['service']);
		unset($_POST['air']);
		unset($_POST['road']);
		unset($_POST['b2b']);
		unset($_POST['b2c']);
		unset($_POST['oda']);
    }   unset($_POST['location']);
	
}
?>

<section class="content-header">
	<div class="content-header-left">
		<h1>Add Pincode</h1>
	</div>
	<div class="content-header-right">
		<a href="pincode.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>
			</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 if(isset($_POST['pincode'])){echo $_POST['pincode'];} ?>">
							</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="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" 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">Location <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" class="form-control" name="location" value="<?php if(isset($_POST['location'])){echo $_POST['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">
							        <?php if(isset($_POST['service'])) { ?> <option value="<?php echo $_POST['service'];?>"><?php echo $_POST['service'];?></option>  <?php } ?>
							        <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">
							        <?php if(isset($_POST['air'])) { ?> <option value="<?php echo $_POST['air'];?>"><?php echo $_POST['air'];?></option>  <?php } ?>
							        <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">
							        <?php if(isset($_POST['road'])) { ?> <option value="<?php echo $_POST['road'];?>"><?php echo $_POST['road'];?></option>  <?php } ?>
							        <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">
							        <?php if(isset($_POST['b2b'])) { ?> <option value="<?php echo $_POST['b2b'];?>"><?php echo $_POST['b2b'];?></option>  <?php } ?>
							        <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">
							        <?php if(isset($_POST['b2c'])) { ?> <option value="<?php echo $_POST['b2c'];?>"><?php echo $_POST['b2c'];?></option>  <?php } ?>
							        <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">
							        <?php if(isset($_POST['oda'])) { ?> <option value="<?php echo $_POST['oda'];?>"><?php echo $_POST['oda'];?></option>  <?php } ?>
							        <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 add pincode</h4></center>
<?php } ?>
<?php require_once('footer.php'); ?>
