<?php session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){
$_SESSION['cuPages']='off_offercre.php';   
$cuPage='off_offercre.php';    
$aid=$_SESSION['uid'];
$uty=$_SESSION['utype'];    
$brid=$_SESSION['abrid'];     
}else{
header('Location:../index.php');
exit;    
}
$mhead='offer';
$menuh='Set Offer &amp; Rebeat';
$phead='ofcre';
$page='Create Coupon &amp; Offer';
$dtnow = date("Y-m-d h:i:s", time());
$today = strftime("%Y-%m-%d", time());
?>
<?php 
if(isset($_POST['save_offer'])){
$name = ucfirst(remove_junk(escape($_POST['name'])));
$percent = remove_junk(escape($_POST['percent']));
$fixed = remove_junk(escape($_POST['fixed']));
$stdate = remove_junk(escape($_POST['stdate']));
$endate = remove_junk(escape($_POST['endate']));
$efton = remove_junk(escape($_POST['efton']));
$status = remove_junk(escape($_POST['status']));    
$description = remove_junk(escape($_POST['description']));
	
if(isset($_POST['name'])){
$ducode = mysqli_query($con,"SELECT * FROM tbl_offer WHERE name = '$name'");
}
	
if($ducode->num_rows > 0) {
save_msg('i','Offer already exists! Plz try another');
echo "<script>window.location='off_offercre.php'</script>";
}else{
$sql="INSERT INTO tbl_offer (name,description,percent,fixed,stdate,endate,aft,status,brid,uid,date) VALUES ('$name','$description','$percent','$fixed','$stdate','$endate','$efton','$status','$brid','$aid','$dtnow')";    
$result = mysqli_query($con,$sql) or die(mysqli_error($con));
$efid=mysqli_affected_rows($con);    
if($efid>0){
$act =remove_junk(escape('Offer name: '.$name));    
write_activity($aid,'OFF','New offer has been Added',$act);    
save_msg('s','Data Successfully Saved!');
}else{
save_msg('w','Data Fail to Saved!');    
}
}    
echo "<script>window.location='off_offercre.php'</script>";   
}
?>
<?php
include('../layout/head.php');
include('../layout/header.php');
include('../layout/side_nav.php');
?>

<div class="content-wrapper">
<?php include('../layout/page_head.php'); ?>
<!-- Main content -->
<section class="content">
<div class="row">
<div class="col-md-8">
<div class="box box-solid">
<div class="box-header with-border">
<h3 class="box-title">Add New Offer</h3>
</div>
<div class="box-body">
<?php echo display_msg();?>  
<form action="off_offercre.php" onsubmit="return validate()" enctype="multipart/form-data" method="post" accept-charset="utf-8">
<div class="col-md-12 popup_details_div">
<div class="col-md-1"></div>
<div class="col-md-10">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Name</label>
<div class="input-group">
<span class="input-group-addon">N</span>   
<input type="text" class="form-control" maxlength="45" name="name" id="name" placeholder="e.g Summer Offer" required="" autocomplete="off">
</div>    
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Percent</label>
<div class="input-group">
<span class="input-group-addon">%</span>   
<input type="text" class="form-control" maxlength="5" min="0" name="percent" id="percent" value="0"  placeholder="e.g 5" onkeypress="return isNumberKey(event)" required="" autocomplete="off">
</div>    
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Fixed</label>
<div class="input-group">
<span class="input-group-addon">Tk</span>   
<input type="text" class="form-control" maxlength="6" min="0" name="fixed" id="fixed" value="0"  placeholder="e.g 65" onkeypress="return isNumberKey(event)" required="" autocomplete="off">
</div>    
</div>
</div>    
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group" >
<label>Offer Start</label>
<div class="input-group">
<span class="input-group-addon">
<span class="fa fa-calendar"></span>
</span>
<input type="text" class="form-control datetimepicker" name="stdate" id="stdate" placeholder="e.g 2020-02-01" autocomplete="off" required="">
<span class="input-group-addon">SD</span>
</div>
</div>    
</div>
<div class="col-md-3">
<div class="form-group" >
<label>Offer End</label>
<div class="input-group">
<span class="input-group-addon">
<span class="fa fa-calendar"></span>
</span>
<input type="text" class="form-control datetimepicker" name="endate" id="endate" placeholder="e.g 2020-02-05" autocomplete="off" required="">
<span class="input-group-addon">ED</span>
</div>
</div>    
</div>
<div class="col-md-3">
<div class="form-group" >
<label>Effect On</label>
<div class="input-group">
<span class="input-group-addon">EF</span>    
<select class="form-control" name="efton">
<option value="0">Supplier</option>
<option value="1">Self</option>
</select>    
</div>
</div>    
</div>
<div class="col-md-3">
<div class="form-group" >
<label>Status</label>
<div class="input-group">
<span class="input-group-addon">ST</span>    
<select class="form-control" name="status">
<option value="1">Active</option>
<option value="0">De-Active</option>
</select>    
</div>
</div>    
</div>    
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Description</label>
<div class="input-group">
<span class="input-group-addon">DS</span>   
<textarea class="form-control" name="description" maxlength="350" rows="5" placeholder="Offer Details"></textarea>
</div>    
</div>    
</div>
<div class="col-md-6">
    
</div>    
</div>    

</div>    
<div class="col-md-1"></div>
</div>
    
<div class="clearfix" ></div>
<div class="col-md-12 nopadding widgets_area"></div>    
<div class="row"style="margin-top: 15px" >
<div class="col-md-8"></div>
<div class="col-md-4 text-right" >
<input type="submit" name="save_offer" class="btn btn-flat bg-purple btn-sm" value="Save"/> <a href="off_alloffer.php" class="btn btn-flat bg-gray  ">Close</a>
</div> 
</div>     
</form>    
</div>
</div>
</div>
<div class="col-md-4">
<div class="row">
<div class="col-md-12">
<div class="box box-solid">
<div class="box-header">
<h3 class="box-title">History </h3>
</div>
<!-- /.box-header -->
<div class="box-body" >
<?php if($_SESSION['utype']=='1'){echo read_activity($aid,'OFF','A');}else{echo read_activity($aid,'OFF','U');}?>
</div>
</div>
</div>
</div>
</div>
</div>    
<?php include('../layout/quick.php');?>    
</section>
<!-- /.main content -->    
</div>
<?php
include('../layout/footer.php');
?>
<!-- page script -->
<script type="text/javascript">
$(document).ready(function (){ 
var name = new LiveValidation('name');
name.add(Validate.Presence);
var percent = new LiveValidation('percent');
percent.add(Validate.Presence);
percent.add(Validate.Numericality, { minimum: 0, maximum: 100});    
var fixed = new LiveValidation('fixed');
fixed.add(Validate.Presence);
fixed.add(Validate.Length, {minimum: 1, maximum: 6});    
var stdate = new LiveValidation('stdate');
stdate.add(Validate.Presence);
var endate = new LiveValidation('endate');
endate.add(Validate.Presence);    
});
    
$(document).on('keyup change', '#percent', function() {
$('#fixed').val('0');    
});
$(document).on('keyup change', '#fixed', function() {
$('#percent').val('0');    
});    
</script>    
<!-- /page script -->
</html>    