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

	

  <div class="content-wrapper">

    <div class="container-fluid">

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

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

			<h4 class="page-title">Manage Distributors</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 Distributors</div>

                    <div class="card-body">

                        <?php if($this->uri->segment(3)){?>

                        <form action="<?php echo base_url();?>admin/update_agent" method="post" enctype="multipart/form-data">

                        	  <input type="hidden" name="ag_id" value="<?php echo $edit->ag_id;?>" />
                              
                              <?php // $this->db->select("*");

									//$this->db->from("states");

									$this->db->order_by("state_name","asc");

									$branches=$this->db->get_where("states",array("country_id"=>101))->result();//print_r($branches);?>

                              <div class="form-group">

                                <label for="email">Select State</label>

                                <select class="form-control" name="state_id" id="state_id" required>

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

                                <?php foreach($branches as $st){?>

                                <option value="<?php echo $st->state_id;?>"  <?php if($edit->ag_state==$st->state_id){echo "selected";}?>><?php echo $st->state_name;?></option>

                                <?php }?>

                                </select>

                              </div>
                              
                              

                              <?php //$this->db->select("*");

									$this->db->order_by("city_name","asc");

									$branches=$this->db->get_where('cities',array("city_state"=>$edit->ag_state))->result();//print_r($branches);?>

                              <div class="form-group">

                                <label for="email">Select City</label>
 
                                <select class="form-control" name="city" id="city_id" required>

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

                                <?php foreach($branches as $st){?>

           	<option value="<?php echo $st->city_id;?>" <?php if($edit->ag_city==$st->city_id){echo "selected";}?>><?php echo $st->city_name;?></option>

                                <?php }?>

                                </select>

                              </div>
                              
                               <div class="form-group">

                                <label for="email">Select Staff</label>

                                <select class="form-control" name="staff_id" required>

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

                                <?php $staff=$this->db->get("emps")->result();
								foreach($staff as $st){?>

           	<option value="<?php echo $st->uid;?>" <?php if($edit->staff_id==$st->uid){echo "selected";}?>><?php echo $st->fname;?></option>

                                <?php }?>

                                </select>

                              </div>

                              <div class="form-group "> <label for="email">Name</label>

                                <input type="text" required class="form-control" value="<?php echo $edit->ag_name;?>" name="ag_name">

                              </div>

                              <div class="form-group ">

                                <label for="email">Business/Firm Name</label>

                                <input type="text"  class="form-control" value="<?php echo $edit->firm_name;?>" name="firm_name" required>

                              </div>

                              <div class="form-group ">

                                <label for="email">Aadhar Front</label>

                                <input type="file"  class="form-control" name="ag_ad_front" />

                                <input type="hidden"  class="form-control" value="<?php echo $edit->ag_ad_front;?>" name="old_ag_ad_front" />

                                <?php if($edit->ag_ad_front!=""){?>

                                <img class="img img-responsive" width="100%" src="<?php echo $edit->ag_ad_front;?>" />

                                <?php }?>

                              </div>
                              <div class="form-group ">

                                <label for="email">Aadhar Back</label>

                                <input type="file"  class="form-control" name="ag_ad_back" />

                                <input type="hidden"  class="form-control" value="<?php echo $edit->ag_ad_back;?>" name="old_ag_ad_back" />

                                <?php if($edit->ag_ad_back!=""){?>

                                <img class="img img-responsive" width="100%" src="<?php echo $edit->ag_ad_back;?>" />

                                <?php }?>

                              </div>
                              <div class="form-group ">

                                <label for="email">PAN Doc</label>

                                <input type="file"  class="form-control" name="pan_card" />

                                <input type="hidden"  class="form-control" value="<?php echo $edit->pan_card;?>" name="old_pan_card" />

                                <?php if($edit->pan_card!=""){?>

                                <img class="img img-responsive" width="100%" src="<?php echo $edit->pan_card;?>" />

                                <?php }?>

                              </div>

                              <div class="form-group ">

                                <label for="email">Agreement</label>

                                <input type="file"  class="form-control" name="agreement" />

                                <input type="hidden"  class="form-control" value="<?php echo $edit->agreement;?>" name="oldagreement" />

                                

                              </div>

                              <div class="form-group ">

                                <label for="email">Aadhar Number</label>

                                <input type="text"  class="form-control" maxlength="12" onkeypress="return isNumberKey(event);" value="<?php echo $edit->aadhar_no;?>" name="aadhar_no">

                              </div>
                              <div class="form-group ">

                                <label for="email">Pan Number</label>

                                <input type="text" required value="<?php echo $edit->pannum;?>" class="form-control"  name="pannum">

                              </div>

                              <div class="form-group">

                                <label for="email">Phone 1 </label>

                                <input type="text"  class="form-control" maxlength="10" onkeypress="return isNumberKey(event);" value="<?php echo $edit->ag_mobile;?>" name="ag_mobile" required>

                               

                              </div>

                              <div class="form-group ">

                                <label for="email">Username</label>

                                <input type="text"  class="form-control" value="<?php echo $edit->ag_username;?>"  required name="ag_username">

                              </div>

                              <div class="form-group">

                                <label for="email">Address</label>

                                <textarea class="form-control" rows="3" name="ag_address" required><?php echo $edit->ag_address;?></textarea>

                              </div>                              

                              <button type="submit" class="btn btn-success">Update Agent</button>

                            </form>

                        <?php }else{?>

                        <form action="<?php echo base_url();?>admin/add_agent" method="post" enctype="multipart/form-data">

                            <input type="hidden" name="br_added_date" value="<?php echo date("Y-m-d");?>" />

                            <input type="hidden" name="br_status" value="" />

                               <?php // $this->db->select("*");

									//$this->db->from("states");

									$this->db->order_by("state_name","asc");

									$branches=$this->db->get_where("states",array("country_id"=>101))->result();//print_r($branches);?>

                              <div class="form-group">

                                <label for="email">Select State</label>

                                <select class="form-control" name="state_id" id="state_id" required>

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

                                <?php foreach($branches as $st){?>

                                <option value="<?php echo $st->state_id;?>"><?php echo $st->state_name;?></option>

                                <?php }?>

                                </select>

                              </div>
                              

                              <div class="form-group">

                                <label for="email">Select City</label>

                                <select class="form-control" id="city_id" name="city" required>


                                <option value="">Select City</option>
                                </select>

                              </div>
                              
                              <div class="form-group">

                                <label for="email">Select Staff</label>

                                <select class="form-control" name="staff_id" required>

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

                                <?php $staff=$this->db->get("emps")->result();
								foreach($staff as $st){?>

           	<option value="<?php echo $st->uid;?>"><?php echo $st->fname;?></option>

                                <?php }?>

                                </select>

                              </div>

                              <div class="form-group "> <label for="email">Name</label>

                                <input type="text" required class="form-control" name="ag_name">

                              </div>

                              <div class="form-group ">

                                <label for="email">Business/Firm Name</label>

                                <input type="text"  class="form-control" name="firm_name" required>

                              </div>

                              <div class="form-group ">

                                <label for="email">Aadhar Front</label>

                                <input type="file"  class="form-control"  name="ag_ad_front">

                              </div>
                              <div class="form-group ">

                                <label for="email">Aadhar Back</label>

                                <input type="file"  class="form-control"  name="ag_ad_back">

                              </div>
                              <div class="form-group ">

                                <label for="email">Pan Card</label>

                                <input type="file"  class="form-control"  name="pancard">

                              </div>
                              <div class="form-group ">

                                <label for="email">Pan Number</label>

                                <input type="text" required  class="form-control"  name="pannum">

                              </div>

                              <div class="form-group ">

                                <label for="email">Agreement</label>

                                <input type="file"  class="form-control" name="agreement" />

                              </div>

                              <div class="form-group ">

                                <label for="email">Aadhar Number</label>

                                <input type="text"  class="form-control" maxlength="12" onkeypress="return isNumberKey(event);" name="aadhar_no">

                              </div>

                              <div class="row">

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

                                    <label for="email">Username</label>

                                    <input type="text"  class="form-control" required name="ag_username">

                                  </div>

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

                                    <label for="email">Password</label>

                                    <input type="password"  class="form-control" required name="ag_password">

                                  </div>

                              </div>

                              <div class="form-group">

                                <label for="email">Phone 1 </label>

                                <input type="text"  class="form-control" maxlength="10" onkeypress="return isNumberKey(event);" name="ag_mobile" required>

                              </div>

                              <div class="form-group">

                                <label for="email">Address</label>

                                <textarea class="form-control" rows="3" name="ag_address" required></textarea>

                              </div>                              

                              <button type="submit" class="btn btn-success">Add Agent</button>

                            </form>

                        <?php }?>

                    </div>

               </div>                  

            </div>

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

               <div class="card">

                    <div class="card-header">Pending KYC Distributors</div>

                    <div class="card-body">

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

                            <thead><tr><th>S.No.</th><th>Admin Name</th><th>City</th><th>Contact/Address</th><th>Action</th></tr></thead>

                            <tbody>

                                <?php $i=1;foreach($agents as $c){?>

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

                                <td><?php echo $c->firm_name.'<br/>'.$c->ag_name;?>

                                <label class="badge badge-success"><?php echo $c->ag_username;?></label><br/>
                                <label class="badge badge-info"><?php echo 'KAD00'.$c->ag_id;?></label></td>

                                <td><?php echo '<b>'.$c->city_name.'</b><br/>'.$c->state_name;?><br/>
                                
                                <label class="badge badge-warning"><?php echo $c->fname;?></label></td>

                                <td><i class="fa fa-phone"></i> <?php echo $c->ag_mobile;?><br/>


                                    <i class="fa fa-map-marker"></i> <?php echo $c->ag_address;?></td>

                                <td>

                                <!-- <a target="_blank" href="<?php //echo base_url().'admin/agent_collection/'.$c->ag_id;?>" class="btn btn-sm btn-primary"><i class="fa fa-inr"></i> Orders</a>-->
<a title="<?php echo $c->firm_name;?>" href="<?php echo $c->ag_id;?>" data-toggle="modal" data-target="#myModalv" class="btn btn-sm btn-success view_details">View</a>
                                 <a href="#" class="btn btn-sm btn-dark change_pass" data-toggle="modal" data-target="#myModalp" id="<?php echo $c->ag_id;?>"><i class="fa fa-lock"></i></a>

                                <a href="<?php echo base_url().'admin/distributors/'.$c->ag_id;?>" class="btn btn-sm btn-warning"><i class="fa fa-edit"></i></a>

                                <a href="#" id="<?php echo $c->ag_id.'-'.$c->ag_city;?>" class="btn btn-sm btn-danger comp_delete"><i class="fa fa-trash-o"></i></a>

                                

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

                                    <a href="<?php echo $c->ag_id;?>" class="btn btn-sm btn-default enable"><?php echo $st;?></a>

                                </td></tr>

                            	<?php $i++;}?>

                            </tbody>

                        </table>

                        <div id="myModalv" class="modal fade" role="dialog">
<div class="modal-dialog modal-lg">
<!-- Modal content-->
<div class="modal-content">
  <div class="modal-header">
    <h4 class="modal-title pfirm_name">View Details</h4>
    <button type="button" class="close" data-dismiss="modal">&times;</button>
  </div>
  <div class="modal-body view_detail_div">
    
  </div>
</div>
</div>
</div>

                        <div id="myModalp" class="modal fade" role="dialog">

                          <div class="modal-dialog modal-sm">

                            <!-- Modal content-->

                            <div class="modal-content">

                              <div class="modal-header">

                                <h4 class="modal-title">Change Password</h4>

                                <button type="button" class="close" data-dismiss="modal">&times;</button>

                              </div>

                              <div class="modal-body">

                                <form action="" method="post" id="update_password">

                                    <input type="hidden" name="emp_id" id="emp_id" value="" />

                                    <div class="form-group">

                                        <label>New Password</label>

                                        <input type="password" name="pass" id="pass" class="form-control" />

                                    </div>

                                    <div class="form-group">

                                        <label>Confirm Password</label>

                                        <input type="password" name="cpass" id="cpass" class="form-control" />

                                    </div>

                                    <button type="submit" class="btn btn-warning">Change Password</button>

                                    <button type="button" class="btn btn-light pull-right" data-dismiss="modal">Close</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 © 2022

        </div>

      </div>

    </footer>

	<!--End footer-->

   

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





  <!-- Bootstrap core JavaScript-->

  

  <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>

  function isNumberKey(evt){

		var charCode = (evt.which) ? evt.which : evt.keyCode

		return !(charCode > 31 && (charCode < 48 || charCode > 57));

	}

$(document).ready(function(){

	$(".view_details").click(function(e){e.preventDefault();
		var id=$(this).attr("href");
		var title=$(this).attr("title");
		$('.pfirm_name').text(title);
		$.ajax({
			 type: "POST",
			 url: '<?php echo base_url();?>admin/ajax_view_details',
			 data: {id:id},
			 success: function(res){//alert(res);
				$(".view_detail_div").html(res);
			 }
		 });
	});

	$(".view_bank").click(function(){

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

		$.ajax({

			 type: "POST",

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

			 data: {id:id},

			 success: function(res){

				$(".view_bankdiv").html(res);

			 }

		});

	});

	$("#update_password").submit(function(e){e.preventDefault();

			var pass=$("#pass").val();

			var cpass=$("#cpass").val();

			if(pass.length<6 || pass!=cpass){alert("Please Enter Same Password and must be of atleast 6 characters.");return false;}

			var formdata=$(this).serialize();

			$.ajax({

				 type: "POST",

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

				 data: {formdata:formdata},

				 success: function(res){//alert(res);

				 	alert(res);

					//$("#emps_list").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/agent_enable',

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

							 success: function(response){

								 //alert(response);

							 }

							 });

		});

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

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

			$("#emp_id").val(id);

		});

$(".comp_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/ajax_agent_delete',

			 data: {id:id},

			 success: function(response){

				 //alert(response);

			 }

		});

	});

	$("#state_id").change(function(){
		var id=$(this).val();
		

		$.ajax({

			 type: "POST",

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

			 data: {id:id},

			 success: function(res){
				//alert(res);

				$("#city_id").html(res);

			 }

		});
	});

	$(".agent_areas").click(function(){

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

		$.ajax({

			 type: "POST",

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

			 data: {id:id},

			 success: function(res){

				$(".agent_areas_div").html(res);

			 }

		});

	});

});

</script>

	<script>

	$(document).ready(function(){$('#default-datatable').DataTable();

		$(".city_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/ajax_city_delete',

					 data: {id:id},

					 success: function(response){

						 //alert(response);

					 }

				});

			});

		});

</script>

	<!-- simplebar js -->

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

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

  <!-- Custom scripts -->



    

	

</body>

</html>

