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

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

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

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

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


	if($valid == 1) {

        	$statement = $pdo->prepare("UPDATE tbl_status SET name=?, status_code=?,status=? WHERE id=?");
    		$statement->execute(array($_POST['name'],$_POST['code'],$_POST['status'],$_REQUEST['id']));

	    $success_message = 'Status 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_status 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
$statement = $pdo->prepare("SELECT * FROM tbl_status WHERE id=?");
$statement->execute(array($_REQUEST['id']));
$result = $statement->fetchAll(PDO::FETCH_ASSOC);

foreach ($result as $row) {
	$name = $row['name'];
	$status_code = $row['status_code'];
	$status = $row['status'];

}
?>

<section class="content">
    <div class="row">
		<div class="col-md-4" style="text-align: left;">
			<h4><i class="fa fa-map-marker"></i> Update Status Details</h4>
		</div>
		<div class="col-md-8" style="text-align: right;"> 
		    <a href="status.php" class="btn btn-warning btn-sm"><i class="fa fa-arrow-left"></i> Back to Status 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">
							<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="name" id="name" value="<?php echo $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" class="form-control" name="code" value="<?php echo $status_code; ?>">
							</div>
						</div>
						
							<div class="form-group">
							<label for="" class="col-sm-2 control-label">Status <span>*</span></label>
							<div class="col-sm-6">
							    <select class="form-control" name="status" id="status">
							        <option >Select Client Status</option>
							        <option value="1"  <?php if(isset($status)) {if($status=='1'){echo "selected";}} ?>>Active</option>
							        <option value="2" <?php if(isset($status)) {if($status=='2'){echo "selected";}} ?>>Inactive</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 Status </h4></center>
<?php } ?>

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