<?php require_once('header.php'); ?>
<?php if($allPermissions['client']['create']=="1"){ ?>

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

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

/*	if(empty($_POST['code'])) {
		$valid = 0;
		$error_message .= 'email can not be empty<br>';
	}*/
	if(empty($_POST['gst_no'])) {
		$valid = 0;
		$error_message .= 'GST Number can not be empty<br>';
	}
		/*if(empty($_POST['phone'])) {
		$valid = 0;
		$error_message .= 'Phone No can not be empty<br>';
	}*/

	if(empty($_POST['address'])) {
		$valid = 0;
		$error_message .= 'Address can not be empty<br>';
	}
		if(empty($_POST['location'])) {
		$valid = 0;
		$error_message .= 'Location can not be empty<br>';
	}
		if(empty($_POST['status'])) {
		$valid = 0;
		$error_message .= 'Status Code 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['pincode'])) {
		$valid = 0;
		$error_message .= 'pincode can not be empty<br>';
	}
	if(empty($_POST['air_amt'])) {
		$valid = 0;
		$error_message .= 'Air Amount can not be empty<br>';
	}
	if(empty($_POST['surface_amt'])) {
		$valid = 0;
		$error_message .= 'Surface Amount can not be empty<br>';
	}
	
$code=$_POST['phone'];
$branch_id = $_POST['branch_id'];
                  

    if($valid==1)
    {	$statement = $pdo->prepare("UPDATE tbl_client_new SET code=?, name=?, phone_no=?,gst_no=?, location=?
        	,state=?, city=?,address=? ,pincode=?,branch_id=?
        	,status=?,air_amt=?,surface_amt=? WHERE id=?");
    		$statement->execute(array($_POST['code'],$_POST['full_name'],$_POST['phone'],$_POST['gst_no'],$_POST['location'],
    		$_POST['state'],$_POST['city'],$_POST['address'],$_POST['pincode'],$_POST['branch_id'],
    		$_POST['status'],$_POST['air_amt'],$_POST['surface_amt'], $_REQUEST['id']));

	    $success_message = 'Client is updated successfully!';
	    $_POST['email']= $_POST['email'] ?? '';
	    
	    if(isset($_POST['same_as_consignor'])){
        	    $statement = $pdo->prepare("SELECT * FROM tbl_user WHERE client_id=? and client_same_consignor=? and full_name=?");
            	$statement->execute(array($_REQUEST['id'],1,$_POST['full_name']));
            	$total = $statement->rowCount();
            	$client_data = $statement->fetchAll(PDO::FETCH_ASSOC);
            	if($client_data) {
        	    
        	                $statementss = $pdo->prepare("UPDATE tbl_user SET full_name=?, email=?, phone=?,address=?, gst_no=?,
        	                location=?,city=?,state=?,pincode=?, branch_id=?,
        	                client_id=?,client_same_consignor=?,air_amt=?,surface_amt=? WHERE client_id=? ");
            		        $statementss->execute(array($_POST['full_name'],$_POST['email'],$_POST['phone'],$_POST['address'],$_POST['gst_no'],
            		        $_POST['location'],$_POST['city'],$_POST['state'],$_POST['pincode'],$_POST['branch_id'],
            		         $_REQUEST['id'],1,$_POST['air_amt'],$_POST['surface_amt'],$_REQUEST['id']));
        
        	    
            	}else{
            	    $statementsss = $pdo->prepare("INSERT INTO tbl_user (full_name, email, phone, address, location, 
            		    status,  city, state, pincode, branch_id,
            		    client_id,gst_no,client_same_consignor,air_amt,surface_amt) VALUES (?,?,?,?,?,
            		    ?,?,?,?,?,
            		    ?,?,?,?,?)");
            		   $statementsss->execute(array($_POST['full_name'],$_POST['email'],$_POST['phone'],$_POST['address'],$_POST['location'],
            		   'Active',$_POST['city'],$_POST['state'],$_POST['pincode'],$_POST['branch_id'],
            		   $_REQUEST['id'],$_POST['gst_no'],1,$_POST['air_amt'],$_POST['surface_amt']));
            	}
	    }else{
	        
	        $statementss = $pdo->prepare("UPDATE tbl_user SET client_same_consignor=? WHERE client_id=? and client_same_consignor=?");
            		        $statementss->execute(array('',$_REQUEST['id'],1));
	        
	    }
	        
	    }
    }
	

?>


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

$statementss = $pdo->prepare("SELECT * FROM tbl_user WHERE client_id=? and client_same_consignor=?");
$statementss->execute(array($_REQUEST['id'],1));
$totalss = $statementss->rowCount();
$user_data = $statementss->fetchAll(PDO::FETCH_ASSOC);
?>

<section class="content-header" style="background-color: white;">
	<!--<div class="content-header-left">
		<h1>Add Clients</h1>
	</div>
	<div class="content-header-right">
		<a href="user.php" class="btn btn-primary btn-sm">View All</a>
	</div>
</section>


<section class="content">-->

 <div class="row">
		<div class="col-md-4" style="text-align: left;">
			<h4><i class="fa fa-map-marker"></i> Update Client Details</h4>
		</div>
		<div class="col-md-8" style="text-align: right;"> 
		    <a href="client-new.php" class="btn btn-warning btn-sm"><i class="fa fa-arrow-left"></i> Back to Client 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" <?=$border_style?>>
					<div class="box-body">
					   
					    <div class="form-group">
							<label for="branch_id" class="col-sm-2 control-label required">Select Branch </label>
							<div class="col-sm-6">
								<select name="branch_id" id="branch_id" class="form-control select2" required>
									<?php   
									
									$condi='';
            	        
                        	        if($session['id']!=1 && $session_branch_ids!='0'){
                                        $condi.= " and id IN ($session_branch_ids)";
                                    }
									
    							       $statement = $pdo->prepare("SELECT * FROM `tbl_branch` WHERE status=1 $condi");
            							$statement->execute();
            							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
                						 foreach($result as $rows) { ?>
										<option value="<?php echo $rows['id']; ?>" <?php if($client_data[0]['branch_id']==$rows['id']){ echo "selected";}?>>
											<?php echo $rows['name']; ?>
										</option>
										<?php } ?>
								</select>
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label optional">Same As Consignor </label>
							<div class="col-sm-6">
								<input type="checkbox" autocomplete="off"   name="same_as_consignor" <?php if($user_data){ echo 'checked'; } ?>>
							</div>
						</div>
					    
					    
						<div class="form-group">
							<label for="" class="col-sm-2 control-label required">Name </label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" placeholder="Enter Name" class="form-control" name="full_name" value="<?=$client_data[0]['name']?>">
							</div>
						</div>
						<div class="form-group" hidden>
							<label for="" class="col-sm-2 control-label optional">Code </label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" placeholder="Enter Code" class="form-control" name="code" value="<?=$client_data[0]['code']?>">
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label optional">Phone Number </label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off"  placeholder="Enter Phone Number" class="form-control" name="phone" value="<?=$client_data[0]['phone_no']?>">
							</div>
						</div>
							<div class="form-group">
							<label for="" class="col-sm-2 control-label required">GST Number </label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" placeholder="Enter GST Number" class="form-control" name="gst_no" value="<?=$client_data[0]['gst_no']?>">
							</div>
						</div>
							<div class="form-group">
							<label for="" class="col-sm-2 control-label required">Address </label>
							<div class="col-sm-6">
								<textarea class="form-control" name="address" placeholder="Address" id="address" style="height:50px;"><?=$client_data[0]['address']?></textarea>
							</div>
						</div>
							<div class="form-group">
							<label for="" class="col-sm-2 control-label required">Location </label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" placeholder="Enter Location" class="form-control" name="location" value="<?=$client_data[0]['location']?>">
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label required">City </label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" placeholder="Enter City" class="form-control" name="city" value="<?=$client_data[0]['city']?>">
							</div>
						</div>
							<div class="form-group">
							<label for="" class="col-sm-2 control-label required">State </label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" placeholder="Enter State" class="form-control" name="state" value="<?=$client_data[0]['state']?>">
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label optional">Pincode </label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" placeholder="Enter Pincode" class="form-control" name="pincode" value="<?=$client_data[0]['pincode']?>">
							</div>
						</div>
						<div class="form-group">
							<label for="status" class="col-sm-2 control-label required">Select Status </label>
							<div class="col-sm-6">
								<select name="status" id="status" class="form-control select2" required>
									<option value="1" <?php if($client_data[0]['status']=='1'){ echo "selected";}?>>Active</option>
									<option value="2" <?php if($client_data[0]['status']=='0'){ echo "selected";}?>>In - Active</option>
								</select>
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Surface Calculation (ex 4500 or 5500) <span>*</span></label>
							<div class="col-sm-6">
								<input type="number" autocomplete="off" class="form-control" name="surface_amt" value="<?=$client_data[0]['surface_amt']?>" id="surface_amt" required>
							</div>
						</div>
						
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">Air Calculation (ex 4500 or 5500) <span>*</span></label>
							<div class="col-sm-6">
								<input type="number" autocomplete="off" class="form-control" name="air_amt" value="<?=$client_data[0]['air_amt']?>" id="air_amt" required>
							</div>
						</div>
						
						
						<input type="hidden" autocomplete="off" class="form-control" name="role" value="Modicare">
						
						<div class="form-group">
							<label for="" class="col-sm-2 control-label"></label>
							<div class="col-sm-6">
							    <br>
								<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 client </h4></center>
<?php } ?>
<?php require_once('footer.php'); ?>