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

<?php
if(isset($_POST['form1'])) {
   // var_dump($_POST);
	$valid = 1;
		if(empty($_POST['igst'])) {
		$valid = 0;
		$error_message .= 'IGST can not be empty<br>';
	}
		if(empty($_POST['cgst'])) {
		$valid = 0;
		$error_message .= 'CGST can not be empty<br>';
	}
    	if(empty($_POST['sgst'])) {
    		$valid = 0;
    		$error_message .= 'SGST can not be empty<br>';
    	}
	

//     $hsncode=$_POST['hsncode'];
// 	$statement = $pdo->prepare("SELECT * FROM tbl_hsncode where hsncode='$hsncode'");
// 							$statement->execute();
// 							$result = $statement->fetchAll(PDO::FETCH_ASSOC);
// 							$rows = mysqli_num_rows($result);
//                             if($result)
//                             {
//                                 $valid=0;
//                                 $error_message .= 'HSN Code Alreay Exist<br>';
//                             }
    if($valid==1)
    {
		$statement = $pdo->prepare("UPDATE tbl_hsncode set hsncode=?,igst=?,cgst=?,sgst=? WHERE id=?");
		$statement->execute(array($_POST['hsncode'],$_POST['igst'],$_POST['cgst'],$_POST['sgst'],$_POST['id']));
		
		$success_message = 'New HSN Code is Upated successfully!';

		unset($_POST['hsncode']);
		unset($_POST['igst']);
		unset($_POST['id']);
		unset($_POST['cgst']);
		unset($_POST['sgst']);
    }
	
}
?>

<section class="content-header">
	<div class="content-header-left">
		<h1>Add HSN Code</h1>
	</div>
	<div class="content-header-right">
		<a href="hsnCode.php" class="btn btn-primary btn-sm">View All</a>
	</div>
</section>
<?php 
                        
	                        $statement = $pdo->prepare("SELECT * FROM tbl_hsncode where id=?");
							$statement->execute(array($_REQUEST['id']));
							$result_hsn = $statement->fetchAll(PDO::FETCH_ASSOC);
						
?>

<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; ?>
			
			<?php foreach($result_hsn as $row){?>
			<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">HSN Code<span>*</span></label>
							<div class="col-sm-6">
								<input type="text" autocomplete="off" readonly class="form-control" name="hsncode" value="<?php echo $row['hsncode']; ?>">
							</div>
						</div>
							<input type="hidden" name="id" value="<?php echo $row['id']; ?>">
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">IGST<span>*</span></label>
							<div class="col-sm-6">
								<input type="number" step='any' autocomplete="off" class="form-control" name="igst" value="<?php echo $row['igst']; ?>">
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">CGST<span>*</span></label>
							<div class="col-sm-6">
								<input type="number" step='any' autocomplete="off" class="form-control" name="cgst" value="<?php echo $row['cgst']; ?>">
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-2 control-label">SGST<span>*</span></label>
							<div class="col-sm-6">
								<input type="number" step='any' autocomplete="off" class="form-control" name="sgst" value="<?php echo $row['sgst']; ?>">
							</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>
<?php }else{ ?>
<center><h4 style="color:red;">You don't have permission to edit hsncode</h4></center>
<?php } ?>
<?php require_once('footer.php'); ?>