<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">Main Subcategory</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 Subcategory</div>
                    <div class="card-body">
                          <form action="<?php echo base_url();?>admin/add_subcategory" method="post" enctype="multipart/form-data">
         <div class="form-group ">
            <label for="email">Choose Main Category</label>
          	<select class="form-control" name="mcat" id="mcat">
            	<option value="">Choose Main Category</option>
                <?php $mcat=$this->db->get("mcategory")->result();
					foreach($mcat as $cat){?>
                <option value="<?php echo $cat->mid;?>"><?php echo $cat->mcat_name;?></option>
                <?php }?>
            </select>
          </div>
         <div class="form-group ">
            <label for="email">Choose Category</label>
          	<select class="form-control" name="cat" id="cat">
            	<option value="">Choose Category</option>
            </select>
          </div>
          <div class="form-group "> <label for="email">Subcategory Title</label>
            <input type="text" required class="form-control" name="subcat">
          </div>
          <div class="form-group ">
            <label for="email">Meta Title</label>
            <input type="text"  class="form-control" name="metatitle">
          </div>
          <div class="form-group ">
            <label for="email">Meta Keywords</label>
            <input type="text"  class="form-control" name="metakey">
          </div>
          <div class="form-group ">
            <label for="email">Meta Description</label>
            <textarea class="form-control" rows="3" name="metadesc"></textarea>
          </div>
          <div class="form-group ">
            <label for="email">Subcategory Image</label>
            <input type="file" class="form-control" name="img">
          </div><br/>
          <button type="submit" class="btn btn-success" name="submit">Add Subcategory</button>
        </form>
                    </div>
                    
               </div>                  
            </div>
            <div class="col-lg-8">
               <div class="card">
                    <div class="card-header">Listed Subcategory</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>Title</th><th>Action</th></tr></thead>
            <tbody>
            	<?php $i=1;foreach($subcats as $subcat){?>
                <tr id="tr<?php echo $subcat->sid;?>"><td><?php echo $i;?></td>
                <td width="100px"><img width="100%" src="<?php echo base_url();?>assets/images/category/<?php echo $subcat->subcat_image;?>" class="img img-responsive"></td>
                <td><?php echo $subcat->subcat_name;?><br/>
                <?php $sc=$this->db->get_where("mcategory",array("mid"=>$subcat->mid))->row();
				if($sc){echo '<label class="badge badge-success">'.$sc->mcat_name.'</label><br/>';}
				$sc=$this->db->get_where("category",array("cid"=>$subcat->cid))->row();
				if($sc){echo '<label class="badge badge-info">'.$sc->cat_name.'</label>';}?><br/>
                <input type="checkbox" name="in_trend" <?php if($subcat->in_trend!=""){echo "checked";}?> value="<?php echo $subcat->sid;?>" class="in_trend" /> In Trend</td>
                <td>
                 <?php if($subcat->subcat_status==""){$st="Disable";}else{$st="Enable";}?>
                <a href="<?php echo $subcat->sid;?>" class="btn btn-sm btn-default enable"><?php echo $st;?></a>
     <button id="<?php echo $subcat->sid;?>" type="button" class="btn btn-warning btn-sm editform" data-toggle="modal" data-target="#myModal"><i class="fa fa-edit"></i></button>
      <a href="<?php echo $subcat->sid;?>" id="<?php echo $subcat->subcat_image;?>" class="btn btn-sm btn-danger subcatdelete"><i class="fa fa-trash-o"></i></a></td>
                <?php $i++;}?>
            </tbody>
        </table>
        				  <div class="modal fade" id="myModal" role="dialog">
            <div class="modal-dialog">
              <!-- Modal content-->
              <div class="modal-content">
                <div class="modal-header vd_bg-blue vd_white">
                          <h4 class="modal-title" id="myModalLabel">Update Subcategory</h4>
                          <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="fa fa-times"></i></button>
                        </div>
                <div class="modal-body">
                  <form action="" method="post" enctype="multipart/form-data" id="updateform">
                  <div class="row">
                  <div class="form-group col-md-6">
                    <label for="email">Main Category</label>
                    <select class="form-control" name="mcat" id="mcat2">
                        <option value="">Main Category</option>
                        <?php $mcat=$this->db->get("mcategory")->result();
                            foreach($mcat as $cat){?>
                        <option value="<?php echo $cat->mid;?>"><?php echo $cat->mcat_name;?></option>
                        <?php }?>
                    </select>
                  </div>
                  <div class="form-group col-md-6">
                    <label for="email">Category</label>
                    <select class="form-control" name="cat" id="cat2">
                        <option value="">Category</option>
                        <?php foreach($cats as $cat){?>
                        <option value="<?php echo $cat->cid;?>"><?php echo $cat->cat_name;?></option>
                        <?php }?>
                    </select>
                  </div></div>
                  <div class="form-group"> <label for="email">SubCategory Title</label>
                    <input type="text" placeholder="SubCategory Title" value="" id="subcat" required class="form-control" name="subcat">
                  </div>
                  <div class="form-group">
                    <label for="email">Meta Title</label>
                    <input type="text"  class="form-control" name="metatitle" id="metatitle">
                  </div>
                  <div class="form-group">
                    <label for="email">Meta Keywords</label>
                    <input type="text"  class="form-control" name="metakey" id="metakey">
                  </div>
                  <div class="form-group">
                    <label for="email">Meta Description</label>
                    <input type="text"  class="form-control" name="metadesc" id="metadesc">
                  </div>
                  <div class="row">
                  <div class="col-md-6">
                  	<label for="file"> Upload File</label>
                    <input type="file" class="btn btn-success" value="" name="img">
                    <input type="hidden" name="oldimg" value="" id="oldimg" />
                    <input type="hidden" name="sid" value=""  id="sid"/>
                  </div>
                  <div class="col-md-6">
                  	<img src="" class="img img-responsive"  id="img" width="50%"/>
                  </div>
                  </div>
                  <button type="submit" class="btn btn-primary" name="submit">Update</button>
                  <button type="button" class="btn btn-default" data-dismiss="modal" id="cancelmodal">Cancel</button>
                </form>
                
                </div>
                
              </div>
              
            </div>
  		</div>
                    </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-->


  <!-- Bootstrap core JavaScript-->
	
	<!-- simplebar js -->
     <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>
	$(document).ready(function(){ 
		$('#default-datatable').DataTable();
		 
	
		$("#mcat").change(function(){
				var cid=$(this).val();
				$.ajax({
				 type: "POST",
				 url: '<?php echo base_url();?>admin/ajax_mcat',
				 data: {cid:cid},
				 success: function(res){
				//alert(res);
				$("#cat").html(res);
				}
				 }); 
			});
			$("#mcat2").change(function(){
				var cid=$(this).val();
				$.ajax({
				 type: "POST",
				 url: '<?php echo base_url();?>admin/ajax_mcat',
				 data: {cid:cid},
				 success: function(res){
				//alert(res);
				$("#cat2").html(res);
				}
				 }); 
			});
		$(".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/subcatenable',
							 data: {id:id,st:st},
							 success: function(response){
								 //alert(response);
							 }
							 });
		});
		
		$(".in_trend").click(function(){
			var id=$(this).val();
			//alert(id);
			var v="";
			if ($(this).is(':checked')) {
				var v=$(this).val();
			}
			//alert(v);
			$.ajax({
			 type: "POST",
			 url: '<?php echo base_url();?>admin/in_trend',
			 data: {v:v,id:id},
			 success: function(response){
				// alert(response);
			 }
			 });
		});
		
		$(".subcatdelete").click(function(e){e.preventDefault();
		if (!confirm("Sure you want to delete?")){
				  return false;
				}
			var id=$(this).attr("href");
			var img=$(this).attr("id");
		//alert(img);return false;
			$(this).closest("tr").remove();
			$.ajax({
							 type: "POST",
							 
							 url: '<?php echo base_url();?>admin/subcatdelete',
							 data: {id:id,img:img},
							 success: function(response){
								 //alert(response);
							 }
							 });
		});
		$(".editform").click(function(){
			var id=$(this).attr("id");
			$.ajax({
							 type: "POST",
							 dataType:"json",
							 url: '<?php echo base_url();?>admin/ajax_showsubcat',
							 data: {id:id},
							 success: function(response){
								 //alert(response);
								$("#subcat").val(response.subcat_name);
								$("#metakey").val(response.metakey);
								$("#metatitle").val(response.metatitle);
								$("#metadesc").val(response.metadesc);
							 	$("#oldimg").val(response.subcat_image);
								$("#img").attr("src","<?php echo base_url();?>assets/images/category/"+response.subcat_image);
								$("#sid").val(response.sid);
								$("#cat2").val(response.cid).prop("selected",true);
								$("#mcat2").val(response.mid).prop("selected",true);
							 }
							 });
		});
		$("form#updateform").submit(function(e){
								var sid=$("#sid").val();
								//alert(sid);
								e.preventDefault();
								$.ajax({
								url: "<?php echo base_url();?>admin/update_subcategory", // Url to which the request is send
								type: "POST",             // Type of request to be send, called as method
								data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
								contentType: false,       // The content type used when sending data to the server.
								cache: false,             // To unable request pages to be cached
								processData:false,        // To send DOMDocument or non processed data file it is set to false
								success: function(data)   // A function to be called if request succeeds
								{
									window.location.href = '<?php echo base_url();?>admin/subcategory';
								//$("#tr"+sid+":first-child").text(data.subcat_name);
								//$("#respond").html(data);
								//$("#myModal").modal('toggle'); 
								//$("#cancelmodal").trigger("click");
								}
								});
		});
	
	});
</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>
  
  <!-- Vector map JavaScript -->
  <!-- Index js -->
  <script src="<?php echo base_url();?>adminassets/js/index.js"></script>
  
 
</body>
</html>
