	<div class="col-md-12">
    <div class="panel panel-primary">
      <div class="panel-heading">Manage Project SubCategories</div>
      <div class="panel-body">
      
      	<form class="form-inline" action="<?php echo base_url();?>admin/add_subcategory" method="post" enctype="multipart/form-data">
          <div class="form-group">
            <label for="email">Choose Category</label>
          	<select class="form-control" name="cat">
            	<option value="">Choose Category</option>
                <?php foreach($cats as $cat){?>
                <option value="<?php echo $cat->cid;?>"><?php echo $cat->cat_name;?></option>
                <?php }?>
            </select>
          </div>
          <div class="form-group">
            <input type="text" placeholder="SubCategory Title" required class="form-control" name="subcat">
          </div>
          <div class="form-group">
            <input type="file" class="form-control" name="img">
          </div>
          <button type="submit" class="btn btn-primary" name="submit">Add</button>
        </form>
        <br/>
        <table class="table table-condensed table-bordered table-striped">
        	<thead><tr><th>S.No.</th><th>SubCategory Image</th><th>Title</th><th>Category</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 src="<?php echo base_url();?>assets/images/category/<?php echo $subcat->subcat_image;?>" class="img img-responsive"></td>
                <td><?php echo $subcat->subcat_name;?></td>
                <td><?php $sc=$this->db->get_where("category",array("cid"=>$subcat->cid))->row();echo $sc->cat_name;?></td>
                <td>
     <button id="<?php echo $subcat->sid;?>" type="button" class="btn btn-warning btn-xs editform" data-toggle="modal" data-target="#myModal">Edit</button>
                	<a href="<?php echo $subcat->sid;?>" class="btn btn-xs btn-danger subcatdelete">Delete</a></td>
                <?php $i++;}?>
            </tbody>
        </table>
        <?php echo $this->pagination->create_links();?>
        <div class="modal fade" id="myModal" role="dialog">
            <div class="modal-dialog">
              <!-- Modal content-->
              <div class="modal-content">
                <div class="modal-header">
                  <button type="button" class="close" data-dismiss="modal">&times;</button>
                  <h4 class="modal-title">Update Subcategory</h4>
                </div>
                <div class="modal-body">
                  <form action="" method="post" enctype="multipart/form-data" id="updateform">
                  <div class="form-group">
                    <label for="email">Choose Category</label>
                    <select class="form-control" name="cat" id="cat">
                        <option value="">Choose Category</option>
                        <?php foreach($cats as $cat){?>
                        <option value="<?php echo $cat->cid;?>"><?php echo $cat->cat_name;?></option>
                        <?php }?>
                    </select>
                  </div>
                  <div class="form-group">
                    <input type="text" placeholder="SubCategory Title" value="" id="subcat" required class="form-control" name="subcat">
                  </div>
                  <div class="row">
                  <div class="form-group col-md-6">
                    <input type="file" class="form-control" 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>
<script>
	$(document).ready(function(){
		$(".subcatdelete").click(function(e){e.preventDefault();
			var id=$(this).attr("href");$(this).closest("tr").remove();
			$.ajax({
							 type: "POST",
							 
							 url: '<?php echo base_url();?>admin/subcatdelete',
							 data: {id:id},
							 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);
							 	$("#oldimg").val(response.subcat_image);
								$("#img").attr("src","<?php echo base_url();?>assets/images/category/"+response.subcat_image);
								$("#sid").val(response.sid);
								$("#cat").val(response.cid).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
								{
								$("#tr"+sid+":first-child").text(data.subcat_name);
								//$("#respond").html(data);
								$("#cancelmodal").trigger("click");
								}
								});
		});
	});
</script>
</body>
</html>