<?php
session_start();
require("php_con.php");
require("check_user.php");
require("function.php");

$success_msg=0;

if(isset($_POST['add_bt'])){
	
	$cs_name=mysqli_real_escape_string($connation,$_POST['cs_name']);
	$cs_mobile=(int)mysqli_real_escape_string($connation,$_POST['cs_mobile']);
	$cs_nic=mysqli_real_escape_string($connation,$_POST['cs_nic']);
	$cs_address=mysqli_real_escape_string($connation,$_POST['cs_address']);
	$cs_pin=mysqli_real_escape_string($connation,$_POST['cs_pin']);
	$cs_bill_discount=mysqli_real_escape_string($connation,$_POST['cs_bill_discount']);
	
	if(mysqli_query($connation,"INSERT INTO
	customer_details (cs_id, cs_name, cs_pin, cs_mobile, cs_nic, cs_address, cs_bill_discount, cs_add_date, cs_add_time)
	VALUES (NULL, '$cs_name', '$cs_pin', '$cs_mobile', '$cs_nic', '$cs_address', '$cs_bill_discount', '$current_date', '$current_time')")){
		$success_msg=1;
	}
	else{
		$success_msg=2;
	}
	mysqli_query($GLOBALS['connation'],"UPDATE setting SET setting_value=setting_value+1 WHERE setting_text='customer_pin'");
}

if(isset($_GET['cs_id'])){	
	$cs_id=mysqli_real_escape_string($connation,$_GET['cs_id']);
	$view_qury=mysqli_query($connation,"SELECT * FROM customer_details WHERE cs_id='$cs_id'");
	$view_resalt=mysqli_fetch_assoc($view_qury);
}

if(isset($_POST['update_bt'])){
	$cs_id=mysqli_real_escape_string($connation,$_GET['cs_id']);
	$cs_name=mysqli_real_escape_string($connation,$_POST['cs_name']);
	$cs_mobile=(int)mysqli_real_escape_string($connation,$_POST['cs_mobile']);
	$cs_nic=mysqli_real_escape_string($connation,$_POST['cs_nic']);
	$cs_address=mysqli_real_escape_string($connation,$_POST['cs_address']);
	$cs_pin=mysqli_real_escape_string($connation,$_POST['cs_pin']);
	$cs_bill_discount=mysqli_real_escape_string($connation,$_POST['cs_bill_discount']);
	
	if(mysqli_query($connation,"UPDATE customer_details SET cs_name='$cs_name',cs_pin='$cs_pin',cs_mobile='$cs_mobile',cs_nic='$cs_nic',cs_address='$cs_address',cs_bill_discount='$cs_bill_discount' WHERE cs_id='$cs_id'")){
		mysqli_query($connation,"UPDATE sale_details SET sale_details_customer='$cs_mobile' WHERE sale_details_customer='$view_resalt[cs_mobile]'");
		echo "<script>window.location='edit-customer?cs_id=$cs_id&updated';</script>";
	}
	else{
		$success_msg=4;
	}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title><?php echo $system_name; ?></title>
<link rel="stylesheet" href="main_css.css">
	
<script src="data_table/js/jquery-3.5.1.js"></script>
<script src="data_table/js/jquery.dataTables.min.js"></script>
<script src="data_table/js/dataTables.bootstrap4.min.js"></script>
<link rel="stylesheet" href="data_table/css/bootstrap.css">
<link rel="stylesheet" href="data_table/css/dataTables.bootstrap4.min.css">
	
<link rel="stylesheet" href="alert/sweetalert2.min.css">
<script src="alert/sweetalert2.min.js"></script>
	
<link rel="stylesheet" href="icon/css/font-awesome.css">
<link rel="stylesheet" href="icon/css/font-awesome.min.css">
</head>
<style>
	table tr td{
		font-size: 14px;
		padding: 5px;
	}
</style>
<body>
<script>
window.addEventListener("load",function(){
	var load_screen=document.getElementById('load_screen');
	document.body.removeChild(load_screen);
});
</script>

<div id="load_screen"><div id="loading"><img src="image/loading.gif" width="80px;"></div></div>
	
<?php require("header.php"); ?>
	
<div class="contaner">
	
<div class="row">
	
<form method="post" class="input-form col-lg-6 col-sm-12 m-auto" autocomplete="off">
	
<div style="margin-bottom: 10px;"><a href="customer" class="btn btn-dark btn-sm"><i class="fa fa-backward"></i> Back to Customer</a></div>
	
<table style="width: 100%;">
<tbody>
	
<tr>
  <td colspan="2">
    Customer Name *<br>
  <input name="cs_name" type="text" required class="form-control form-control-sm" id="cs_name" value="<?php if(isset($_GET['cs_id'])){echo $view_resalt['cs_name'];} ?>">
  </td>
</tr>
	
<tr>
  <td style="width: 50%;"> Mobile
  *<br>
  <input name="cs_mobile" type="text" required class="form-control form-control-sm" id="cs_mobile" pattern="\d*" value="<?php if(isset($_GET['cs_id'])){echo '0'.(int)$view_resalt['cs_mobile'];} ?>">
  </td>
  <td style="width: 50%;"> NIC Number
  <br>
  <input name="cs_nic" type="text" class="form-control form-control-sm" id="cs_nic" value="<?php if(isset($_GET['cs_id'])){echo $view_resalt['cs_nic'];} ?>">
  </td>
</tr>
	
<tr valign="top">
  <td>Customer PIN *
    <input name="cs_pin" type="text" required class="form-control form-control-sm" id="cs_pin" value="<?php if(isset($_GET['cs_id'])){echo $view_resalt['cs_pin'];}else{echo customer_pin(); } ?>">
	</td>
  <td> Discount Rate (%)
    <input name="cs_bill_discount" type="number" class="form-control form-control-sm" id="cs_bill_discount" step="0.01" value="<?php if(isset($_GET['cs_id'])){echo $view_resalt['cs_bill_discount'];} ?>"></td>
  </tr>
<tr>
  <td> Address
    <br>
    <textarea name="cs_address" rows="4" class="form-control form-control-sm" id="cs_address"><?php if(isset($_GET['cs_id'])){echo $view_resalt['cs_address'];} ?></textarea>
    </td>
  <td>&nbsp;</td>
</tr>
	
<tr>
  <td colspan="2" align="right">
  <button name="<?php if(isset($_GET['cs_id'])){echo "update_bt";}else{echo "add_bt";} ?>" type="submit" class="btn btn-success btn-sm mt-2"><?php if(isset($_GET['cs_id'])){echo "Update Customer";}else{echo "Add Customer";} ?></button>
  </td>
</tr>
	
</tbody>
</table>
	
</form>
		
</div>
</div>	
	


<?php if($success_msg==2){ ?>
<script>
    swal({
	title: "Error",
       html: "<br> Your entered details something is wrong, Please try again.",
       type: "error",
	   titleColor: "text-primary",
       showCancelButton: false,
       confirmButtonText: 'OK',
       showLoaderOnConfirm:false,
       buttonsStyling: false,
       confirmButtonClass: 'btn btn-success',
       cancelButtonClass: '',    
     
    });
</script>
<?php } ?>

<?php if($success_msg==1){ ?>
<script>
    swal({
	title: "Congratulation",
       html: "<br> Your entered details has been saved successfully.",
       type: "success",
	   titleColor: "text-primary",
       showCancelButton: false,
       confirmButtonText: 'OK',
       showLoaderOnConfirm:false,
       buttonsStyling: false,
       confirmButtonClass: 'btn btn-success',
       cancelButtonClass: '',     
    });
</script>
<?php } ?>
	
<?php if($success_msg==4){ ?>
<script>
    swal({
	title: "Error",
       html: "<br> Your entered details something is wrong, Please try again.",
       type: "error",
	   titleColor: "text-primary",
       showCancelButton: false,
       confirmButtonText: 'OK',
       showLoaderOnConfirm:false,
       buttonsStyling: false,
       confirmButtonClass: 'btn btn-success',
       cancelButtonClass: '',    
     
    });
</script>
<?php } ?>

<?php if(isset($_GET['updated'])){ ?>
<script>
    swal({
	title: "Congratulation",
       html: "<br> Your customer details has been update successfully.",
       type: "success",
	   titleColor: "text-primary",
       showCancelButton: false,
       confirmButtonText: 'OK',
       showLoaderOnConfirm:false,
       buttonsStyling: false,
       confirmButtonClass: 'btn btn-success',
       cancelButtonClass: '',     
    });
</script>
<?php } ?>
	
</body>
</html>
<?php @mysqli_close($connation); ?>