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

$today_date = date('Y-m-d');
$image_today_date = date('YmdHis');

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_address'])) {
		$valid = 0;
		$error_message .= 'Address can not be empty<br>';
	}
        
        	if(empty($_POST['cust_state'])) {
		$valid = 0;
		$error_message .= 'State can not be empty<br>';
	}

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

$code=$_POST['cust_code'];


    if($valid==1)
    {
        
        $coloader_proof = $_FILES['coloader_proof']['name'];
            
            
             if($coloader_proof!=''){
    		    $path_tmp = $_FILES['coloader_proof']['tmp_name'];  
                $ext = pathinfo( $coloader_proof, PATHINFO_EXTENSION );
    		    $file_name = basename( $coloader_proof, '.' . $ext );
    		    $final_name = $today_date.'-'.$image_today_date.'-1'.'.'.$ext;
    		    
    		    if (file_exists('img/coloader-proof/'.$_POST['old_coloader_proof'])) {
                    unlink('img/coloader-proof/'.$_POST['old_coloader_proof']);
                }
    		    
    		    
    		    move_uploaded_file( $path_tmp, 'img/coloader-proof/'.$final_name );
            }else{
                $final_name=$_POST['old_coloader_proof'];
            }

        	$statement = $pdo->prepare("UPDATE tbl_coloader SET name=?, city=?, state=?,address=?, pincode=?,mobile=?, status=?, coloader_proof=? WHERE id=?");
    		$statement->execute(array($_POST['cust_name'],$_POST['cust_city'],$_POST['cust_state'],$_POST['cust_address'],$_POST['cust_zip'],$_POST['cust_phone'],$_POST['status'],$final_name,$_REQUEST['id']));

     		
		$success_message = 'Co Loader Details is Update Successfully!';

    }
	
}

$statement = $pdo->prepare("SELECT * FROM tbl_coloader WHERE id=?");
$statement->execute(array($_REQUEST['id']));
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
?>


<section class="content">

        <div class="row">
    		<div class="col-md-4" style="text-align: left;">
    			<h4><i class="fa fa-map-marker"></i> Update Co Loader Details</h4>
    		</div>
    		<div class="col-md-8" style="text-align: right;"> 
    		    <a href="co-loader.php" class="btn btn-warning btn-sm"><i class="fa fa-arrow-left"></i> Back to Co Loader 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="cust_name" value="<?=$result[0]['name']?>">
							</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" id="cust_city" value="<?=$result[0]['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" id="cust_state" value="<?=$result[0]['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;"><?=$result[0]['address']?></textarea>
							</div>
							<div class="col-sm-4">
								<span id="remain" >110</span><span id="remain_txt" >characters remaining</span>
							</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="<?=$result[0]['pincode']?>">
							</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="<?=$result[0]['mobile']?>">
							</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 Status</option>
							        <option value="1"  <?php if(isset($result[0]['status'])) {if($result[0]['status']=='1'){echo "selected";}} ?>>Active</option>
							        <option value="2" <?php if(isset($result[0]['status'])) {if($result[0]['status']=='2'){echo "selected";}} ?>>Inactive</option>
							    </select>
							</div>
						</div>
						
						<div class="form-group" >
							<label for="" class="col-sm-2 control-label">Co Loader Proof </label>
							<div class="col-sm-6">
								<input type="file"  class="form-control" name="coloader_proof" id="coloader_proof" placeholder="Co Loader Proof" value="">
								<input type="hidden" name="old_coloader_proof" value="<?php if($result){ echo $result[0]['coloader_proof'];} ?>">
							</div>
						</div>
						
						
						 <?php if($result && $result[0]['coloader_proof']!=''){ ?>	   
						<div class="form-group" >
							<label for="" class="col-sm-3 control-label">Existing Proof </label>
							<div class="col-sm-6">
								<img src="img/coloader-proof/<?=$result[0]['coloader_proof']?>"  class="form-control" style="width:200px;height:80px;object-fit:contain">
							</div>
						</div>
						
						 <?php } ?>
					
						<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>
<script>
    var maxchars = 110;

$('textarea').keyup(function () {
    var tlength = $(this).val().length;
    $(this).val($(this).val().substring(0, maxchars));
    var tlength = $(this).val().length;
    remain = maxchars - parseInt(tlength);
   
    if(remain<10){
         $('#remain').css('color','red');
         $('#remain_txt').css('color','red');
    }
    else{ $('#remain').css('color','green');
    $('#remain_txt').css('color','green');
    }
     $('#remain').text(remain);
});
</script>

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