<?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_phone'])) {
        $valid = 0;
        $error_message .= "Phone can not be empty<br>";
    }
    if(empty($_POST['cust_address'])) {
        $valid = 0;
        $error_message .= "Address can not be empty<br>";
    }
    if(empty($_POST['cust_code'])) {
        $valid = 0;
        $error_message .= "DP Code can not be empty<br>";
    }
    if(empty($_POST['cust_zip'])) {
        $valid = 0;
        $error_message .= "Pincode can not be empty<br>";
    }
     if(empty($_POST['cust_state'])) {
        $valid = 0;
        $error_message .= "State can not be empty<br>";
    }
     if(empty($_POST['cust_city'])) {
        $valid = 0;
        $error_message .= "City can not be empty<br>";
    }

    if($valid == 1) {    	
		// updating into the database
		$statement = $pdo->prepare("UPDATE tbl_customer SET cust_name=?,cust_phone=?,cust_address=?,cust_code=?,cust_city=?,cust_zip=?,destination_city=?,cust_state=?,cust_type=? WHERE id=?");
		$statement->execute(array($_POST['cust_name'],$_POST['cust_phone'],$_POST['cust_address'],$_POST['cust_code'],$_POST['cust_city'],$_POST['cust_zip'],$_POST['destination_city'],$_POST['cust_state'],$_POST['type'],$_REQUEST['id']));
    	$success_message = 'Consignee 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 tbl_customer WHERE id=?");
	$statement->execute(array($_REQUEST['id']));
	$total = $statement->rowCount();
	$result = $statement->fetchAll(PDO::FETCH_ASSOC);
	if( $total == 0 ) {
		header('location: logout.php');
		exit;
	}
}
?>
<?php							
foreach ($result as $row) {
    
	$cust_name = $row['cust_name'];
    $cust_email = $row['cust_email'];
    $cust_phone = $row['cust_phone'];
    $cust_address = $row['cust_address'];
    $cust_code = $row['cust_code'];
    $cust_city = $row['cust_city'];
    $cust_state = $row['cust_state'];
    $cust_zip = $row['cust_zip'];
    $destination_city = $row['destination_city'];
    $cust_type=$row['cust_type'];

}
?>

<section class="content">
 <div class="row">
    		<div class="col-md-4" style="text-align: left;">
    			<h4><i class="fa fa-map-marker"></i> Update 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">

        <div class="box box-info">

            <div class="box-body">
                
                    <div class="form-group">
							<label for="" class="col-sm-2 control-label">Consignee Name <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" class="form-control" name="cust_name" value="<?php echo $cust_name; ?>">
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">DP Code <span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" class="form-control" name="cust_code" value="<?php echo $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" value="<?php echo $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" value="<?php echo $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 echo $cust_address; ?></textarea>
							</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 echo $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 echo $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 echo $destination_city; ?>">
							</div>
						</div>
						
							<div class="form-group" hidden>
							<label for="" class="col-sm-2 control-label">Customer Type </label>
							<div class="col-sm-6">
							<select name="type" class="form-control" required>
							    <option value='one_time' <?php if($cust_type=='one_time'){ echo "selected";} ?>>One time Customer</option>
							    <option value="regular" <?php if($cust_type=='regular'){ echo "selected";} ?>>Regular Customer</option>
							</select>
							</div>
						</div>
                
                
                            
                <div class="form-group">
                	<label for="" class="col-sm-3 control-label"></label>
                    <div class="col-sm-6">
                      <button type="submit" class="btn btn-success pull-left" name="form1">Update</button>
                    </div>
                </div>

            </div>

        </div>

        </form>



    </div>
  </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">
                Are you sure want to delete this item?
            </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'); ?>