<?php
session_start();
require_once 'includes/header.php';
require_once 'includes/config.php';
$error=false;
$errors=array();
// avoid user arriving to this page without logging in
if(!isset($_SESSION['user_logged_in']) && $_SESSION['user_logged_in']!=TRUE )
{
	header('Location:login.php');

}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    
	
	
	$code=$_POST['code'];
	$discount_type=$_POST['discount_type'];
	$discount=$_POST['discount'];
	$start_date=$_POST['start_date'];
	$end_date=$_POST['end_date'];	
	if(empty($code)){
		$error=true;
		$errors['code']='Please enter Coupon Code ';
	}
	
	$checkEmailExists=mysqli_query($con,"SELECT * FROM coupons WHERE coupon_code='".$code."'");
	
	if(mysqli_num_rows($checkEmailExists)>0){
		$error=true;
		$errors['code']='Coupon already exists, Please try new one.';
	}
	
	
	if($error==false)
	{
		$createCustomer=mysqli_query($con,"INSERT INTO coupons (	coupon_code,discount_type,discount,start_date,end_date,status) VALUES('".$code."','".$discount_type."','".$discount."','".$start_date."','".$end_date."',1)");
		
		if($createCustomer){
			$errors['success']='Coupon created successfully.';
		}else{
			$error=true;
			$errors['error']='Something went wrong!!!, Please try again.';
		}
	}
	
	
}

?>
<style>
.modal{
	z-index: 9999;
	max-height: 55%;
	
}
.modal-content{
	border:none;
}
</style>
 <div class="content">
        <div class="container-fluid">
          <div class="row">
            <div class="col-md-12">
              <div class="card">
                <div class="card-header card-header-primary">
                  <h4 class="card-title">Add Coupon- 
添加新类别</h4>
                  <p class="card-category"></p>
                </div>
                <div class="card-body">

<?php

if (isset($error) && $error==false && isset($errors['success'])) {
	echo '<div class="alert alert-success" role="alert" id="success_message">Success <i class="glyphicon glyphicon-thumbs-up"></i> '.$errors['success'].'</div>';
} 

if (isset($error) && $error==true && isset($errors['error'])) {
	echo '<div class="alert alert-danger" role="alert" id="success_message">Success <i class="glyphicon glyphicon-thumbs-up"></i> '.$errors['error'].'</div>';
} 

?>

  <form role="form" method="post" enctype="multipart/form-data">
   <div class="box-body">
   

   
   <div class="row">
   
   <div class="col-md-6">
   
   <div class="form-group">
	<input type="text" name="code" required class="form-control" placeholder="Coupon Code" >
	<?php if($error==true && isset($errors['code'])){?>
		<p class="error"><?php echo $errors['code'];?></p>
	<?php } ?>
   </div>
   </div>
   
    <div class="col-md-6">
	
	<div class="form-group">
	<select name="discount_type" class="form-control">
	<option value="percentage">Percentage</option>
	<option value="fix">Fixed</option>
	
	</select>
   </div>
   </div>
   
   </div>
   <div class="row">
   
   <div class="col-md-6">
   
   <div class="form-group">
	<input type="text" name="discount" required class="form-control" placeholder="Discount" >
	<?php if($error==true && isset($errors['discount'])){?>
		<p class="error"><?php echo $errors['discount'];?></p>
	<?php } ?>
   </div>
   </div>
   
    <div class="col-md-6">
	
	<div class="form-group">
	<input name="start_date" placeholder="Selected starting date " type="text" id="startingDate" class="form-control datepicker">
   </div>
   </div>
   
   </div>
   <div class="row">
   
   <div class="col-md-6">
   
   <div class="form-group">
	<input name="end_date" placeholder="Selected ending date" type="text" id="endingDate" class="form-control datepicker">
   </div>
   </div>
   
    <div class="col-md-6">
	
	
   </div>
   
   </div>
   
   
   <div class="box-footer">
		<button type="submit" class="btn btn-primary">Save</button>
   </div>
			  
   </form>
  </div>
  </div>
  </div>
  </div>
  </div>
  </div>

<?php include_once 'includes/footer.php'; ?>
<script type="text/javascript">
$(document).ready(function(){
    $('.datepicker').datepicker({ format: 'yyyy-mm-dd' });
  });
    
</script> 