<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">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_staff" method="post" enctype="multipart/form-data">

                        	  <input type="hidden" name="uid" value="<?php echo $edit->uid;?>" />                              
								
							  <div class="form-group "> <label for="email">Type</label>
                              	<select class="form-control" required name="emp_type">
                                	<option value="Distributor"  <?php if($edit->emp_type=='Distributor'){echo "selected";}?>>Distributor</option>
                                	<option value="Display Center"  <?php if($edit->emp_type=='Display Center'){echo "selected";}?>>Display Center</option>
                                	<option value="Service Center"  <?php if($edit->emp_type=='Service Center'){echo "selected";}?>>Service Center</option>
                                	<option value="Dealer"  <?php if($edit->emp_type=='Dealer'){echo "selected";}?>>Dealer</option>
                                	<option value="Showrooms"  <?php if($edit->emp_type=='Showrooms'){echo "selected";}?>>Showrooms</option>
                                	<option value="Subscribers"  <?php if($edit->emp_type=='Subscribers'){echo "selected";}?>>Subscribers</option>
                                </select>
                              </div>
                              <div class="form-group "> <label for="email">Business/Firm Name</label>

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

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

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

                              </div>



                              <div class="form-group">

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

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

                               

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

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

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

                              </div>

                              <div class="form-group ">

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

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

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

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

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

                              </div>
                              <?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">City</label>

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

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

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

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

                                <?php }?>

                                </select>

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

                                <label for="email">Address</label>
                                
                                <textarea class="form-control" rows="3" name="daddress"><?php echo $edit->daddress;?></textarea>
                              </div>
                              

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

                            </form>

                        <?php }else{?>

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

							  <div class="form-group "> <label for="email">Type</label>
                              	<select class="form-control" required name="emp_type">
                                	<option value="Distributor">Distributor</option>
                                	<option value="Display Center">Display Center</option>
                                	<option value="Service Center">Service Center</option>
                                	<option value="Dealer">Dealer</option>
                                	<option value="Showrooms">Showrooms</option>
                                	<option value="Subscribers">Subscribers</option>
                                </select>
                              </div>
                              <div class="form-group "> <label for="email">Business/Firm Name</label>

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

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

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

                              </div>


                              <div class="row">

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

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

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

                                  </div>

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

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

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

                                  </div>

                              </div>

                              <div class="form-group">

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

                                <input type="text"  class="form-control" maxlength="10" name="mob1" required>

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

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

                                <input type="text"  class="form-control" maxlength="10" name="dcode" required>

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

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

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

                              </div>
                              <?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">City</label>

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

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

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

                                <option value="<?php echo $st->city_id;?>"><?php echo $st->city_name;?></option>

                                <?php }?>

                                </select>

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

                                <label for="email">Address</label>
                                
                                <textarea class="form-control" rows="3" name="daddress"></textarea>
                              </div>

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

                            </form>

                        <?php }?>

                    </div>

               </div>                  

            </div>

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

               <div class="card">

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

                    <div class="card-body">

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

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

                            <tbody>

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

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

                                <td><?php echo $c->fname.' '.$c->lname;?><br/>

                                <label class="badge badge-warning"><?php echo $c->emp_type;?></label><br/>
                                <label class="badge badge-success"><?php echo $c->u_username;?></label><br/>
                                <label class="badge badge-info"><?php echo $c->dcode;?></label></td>


                                <td><i class="fa fa-phone"></i> <?php echo $c->mob1;?><br/>
                                	<i class="fa fa-envelope"></i> <?php echo $c->email;?><br/>
                                	<i class="fa fa-map-marker"></i> <?php echo $c->city_name;?></td>

                                <td>

                                 <a target="_blank" href="<?php echo base_url().'admin/dist_sells/'.$c->uid;?>" class="btn btn-sm btn-primary"><?php echo $this->db->get_where("orders",array("admin_id"=>$c->uid))->num_rows();?> Sells</a>

                                 <a href="#" class="btn btn-sm btn-dark change_pass" data-toggle="modal" data-target="#myModalp" id="<?php echo $c->uid;?>"><i class="fa fa-lock"></i></a>

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

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

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

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

                                </td></tr>

                            	<?php $i++;}?>

                            </tbody>

                        </table>

                        <div id="view_bank" 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">Bank Details</h4>

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

                              </div>

                              <div class="modal-body view_bankdiv">

                                <p>Loading...</p>

                              </div>

                              <div class="modal-footer">

                                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>

                              </div>

                            </div>

                          </div>

                        </div>

                        <div id="myModal" 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">Areas</h4>

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

                              </div>

                              <div class="modal-body row agent_areas_div">

                                <p>Loading...</p>

                              </div>

                              <div class="modal-footer">

                                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>

                              </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_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_staff_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/staff_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);

		});

$(".staff_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_staff_delete',

			 data: {id:id},

			 success: function(response){

				 //alert(response);

			 }

		});

	});

	

	$(".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>

