<div class="clearfix"></div>

  <div class="content-wrapper">

    <div class="container-fluid">

      <!-- Breadcrumb-->

     <div class="row pt-2 pb-2">

        <div class="col-sm-9">

		    <h4 class="page-title">Manage Coupons</h4>

	   </div>

     

     </div>

    <!-- End Breadcrumb-->

      <div class="row">

			<div class="col-lg-4">

			   <?php if($this->session->flashdata("msg")){echo $this->session->flashdata("msg"); }?>

               <div class="card">

                    	  <div class="card-header">Add/Edit Coupons</div>

                    <div class="card-body">

                        <?php 

				  if($this->uri->segment(3))

					{

						$cedit=$this->db->get_where("coupons",array("id"=>$this->uri->segment(3)))->row();

						echo form_open_multipart("admin/add_coupon/".$this->uri->segment(3),array("class"=>"form-horizontal"));?>

                   	 

                      <div class="row">

                      <div class="form-group col-md-7">

                        <label class="">Code</label>

                        <input  class="form-control" name="code" value="<?php echo $cedit->code;?>"  type="text" required placeholder="Coupon Code">

                      </div>

                      <div class="form-group col-md-5">

                        <label class="">Min Order Value</label>

                        <input  class="form-control" name="min_order" value="<?php echo $cedit->min_order;?>"  type="number" required placeholder="500">

                      </div>

                      </div>

                   <div class="form-group">

                    <label>Title</label>

                    <input  class="form-control" name="title"  value="<?php echo $cedit->title;?>" type="text" required placeholder="Title">

                  </div><div class="row">

                      <div class="form-group col-md-7">

                        <label class="">Type</label>

                        <select  class="form-control" name="type">

                 			<option value="">Select Type</option>

                   			<option value="Percent" <?php if($cedit->distype=="Percent"){echo "selected";}?>>Percent</option>

                            <option value="Amount" <?php if($cedit->distype=="Amount"){echo "selected";}?>>Amount</option>

                      </select>

                      </div>

                      

                       <div class="form-group col-md-5">

                        <label class="">Value</label>

                          <input  class="form-control" name="value" value="<?php echo $cedit->disvalue;?>" type="text" required placeholder="Coupon Value">

                        

                      </div></div>

                      <div class="form-group">

                        <label class="">Description</label>

                        <textarea  class="form-control" name="desc" rows="2" placeholder="Description"><?php echo $cedit->desc;?></textarea>

                        

                      </div>

                       

                      

                      

                      <div class="form-group">

                     

                      <label class="">Image</label>

                      

                    <!-- The file input field used as target for the file upload widget -->

                    <input id="fileupload" type="file" name="cfile">

                     <input type="hidden" name="oldt" value="<?php echo $cedit->image;?>" />

                <img width="100px" class="img img-responsive" src="<?php echo base_url();?>assets/images/<?php echo $cedit->image;?>" />

                    

                    </div>

                    <div class="form-group">

                          <label class="">Expire Date</label>

                          <input class="form-control" type="date"  value="<?php echo $cedit->exp_date;?>"  required name="exp_date">

                      </div>

                       <button type="submit" name="submit" class="btn btn-primary">Update Coupon</button>

                       <a href="<?php echo base_url();?>admin/coupons" class="btn btn-default">Cancel</a>

                      

                    <?php echo form_close();

					}

					else

					{

				  echo form_open_multipart("admin/add_coupon",array("class"=>"form-horizontal"));?>

                  	 

                      <div class="row">

                      <div class="form-group col-md-7">

                        <label class="">Code</label>

                        <input  class="form-control" name="code" type="text" required placeholder="Coupon Code">

                      </div>

                      <div class="form-group col-md-5">

                        <label class="">Min Order Value</label>

                        <input  class="form-control" name="min_order" type="number" required placeholder="500">

                      </div>

                      </div>

                      <div class="form-group">

                        <label class="">Title</label>

                        <input  class="form-control" name="title" type="text" required placeholder="Title">

                      </div>

                      <div class="row">

                      <div class="form-group  col-md-7">

                        <label class="">Type</label>

                        <select class="form-control"name="type">

                 			<option value="">Select Type</option>

                   			<option value="Percent">Percent</option>

                            <option value="Amount">Amount</option>

                      </select>

                      </div>

                       <div class="form-group col-md-5">

                        <label class="">Value</label>

                          <input  class="form-control" name="value" type="number" required placeholder="Coupon Value">

                      </div>

                      </div>

                      <div class="form-group">

                        <label class="">Description</label>

                        <textarea class="form-control"name="desc" rows="2" placeholder="Description"></textarea>

                      </div>

                      <div class="form-group">

                          <label class="">Image</label>

                          <input id="fileupload"  class="form-control" type="file" name="cfile">

                      </div>

                      <div class="form-group">

                          <label class="">Expire Date</label>

                          <input class="form-control" type="date"  required name="exp_date">

                      </div>

                       <button type="submit" name="submit" class="btn btn-primary btn-block">Add Coupon</button>

                    <?php echo form_close();}?>

                    </div>

                    

               </div>                  

            </div>

            <div class="col-lg-8">

               <div class="card">

                    <div class="card-header">Listed Coupons</div>

                    <div class="card-body">

                    <table class="table table-condensed table-bordered" id="default-datatable">

                        <thead><tr><th>S.No.</th><th>Image</th><th>Code</th><th>Discount</th><th>Desc</th><th>Action</th></tr></thead>

                        <tbody>

                            <?php $i=1;foreach($coupons as $coupon){?>

                            <tr><td><?php echo $i;?></td>

                            <td><img width="100px" class="img img-responsive" src="<?php echo base_url();?>assets/images/<?php echo $coupon->image;?>" /></td>

                            <td>


							<?php echo $coupon->code;?><br/>

                            

							<label class="badge badge-success">Expire on <?php echo date("d-m-y",strtotime($coupon->exp_date));?></label><br/>
							<label class="badge badge-warning">Min Order <?php echo $coupon->min_order;?></label></td>

                            <td><span class="main"><span class="font-light"><?php if($coupon->distype!="Percent"){echo "<i class='fa fa-inr'></i> ";}?></span><span class="font-bold"><?php echo $coupon->disvalue;if($coupon->distype=="Percent"){echo "%";}?></span> </span> <span class="suffix">Off</span></td>

                            <td><h6><?php echo $coupon->title;?></h6><?php //echo $coupon->desc;?></td>

                            <td>

                            

                          <a class="btn btn-sm btn-warning" href="<?php echo base_url();?>admin/coupons/<?php echo $coupon->id;?>"><i class="fa fa-edit"></i> </a>

                                   <a class="btn btn-sm btn-danger coupon_delete" href="#" id="<?php echo $coupon->id;?>"><i class="fa fa-trash-o"></i></a>

                                   <?php if($coupon->status==""){$st="Disable";}else{$st="Enable";}?>

                    <a href="<?php echo $coupon->id;?>" class="btn btn-sm btn-default enable"><?php echo $st;?></a></td>

                            <?php $i++;}?></tr>

                        </tbody>

                    </table>

                    </div>

                    

               </div>                  

            </div>

            

        </div><!--End Row-->

	  

	  <!--End Row-->



    </div>

    <!-- End container-fluid-->

    

    </div><!--End content-wrapper-->

   <!--Start Back To Top Button-->

    <a href="javaScript:void();" class="back-to-top"><i class="fa fa-angle-double-up"></i> </a>

    <!--End Back To Top Button-->

	

	<!--Start footer-->

	<footer class="footer">

      <div class="container">

        <div class="text-center">

          Copyright © 2020 Grocery Store

        </div>

      </div>

    </footer>

	<!--End footer-->

   

  </div><!--End wrapper-->



  <script src="<?php echo base_url();?>adminassets/plugins/bootstrap-datatable/js/jquery.dataTables.min.js"></script>

  <script src="<?php echo base_url();?>adminassets/plugins/bootstrap-datatable/js/dataTables.bootstrap4.min.js"></script>

  <script>//CKEDITOR.replace( 'editor1' );

	$(document).ready(function(){

		$(".enable").click(function(e){e.preventDefault();

			var st=$(this).text();

			var id=$(this).attr("href");

			if(st=="Enable"){

			$(this).text("Disable");

			}else{

			$(this).text("Enable");	

			}

			$.ajax({

							 type: "POST",

							 url: '<?php echo base_url();?>admin/c_enable',

							 data: {id:id,st:st},

							 success: function(response){

								 //alert(response);

							 }

							 });

		});

		$(".coupon_delete").click(function(e){e.preventDefault();

				if (!confirm("Sure you want to delete?")){

				  return false;

				}

				var id=$(this).attr("id");

				$(this).closest("tr").remove();

				$.ajax({

					 type: "POST",

					 url: '<?php echo base_url();?>admin/coupon_delete',

					 data: {id:id},

					 success: function(response){

						 //alert(response);

					 }

				});

			});

	});

</script>

<script src="<?php echo base_url();?>adminassets/js/sidebar-menu.js"></script>

<!-- Custom scripts -->

<script src="<?php echo base_url();?>adminassets/js/app-script.js"></script>

</body>

</html>

