<style type="text/css">
  .settings{
    cursor: pointer;
    border: 1px solid #8c2574;
    padding: 10px;
  }
 
</style>
<?php
//echo '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'.$role.'<<>>>'.ADMIN.'<>'.DISTRICT_AGENT;
?>
<script type="text/javascript"> 
      $(document).ready( function() {
		  var $taluk_sec = $('#taluk_sec');
	$("#muncipality_drop").hide();
	$("#panchayath_drop").hide();
	$("#corporation_drop").hide();
	$("#block_drop").hide();
	
	
       // $('#status_div').delay(3000).fadeOut();
        /*$( "#status_div" ).slideToggle( "slow", function() {
            $('#status_div').delay(3000).fadeOut();
        });*/

$('#status_div').slideToggle('2000',"swing", function () { })   



      });
	  
function get_type(tid)
{
	var role = '<?php echo $role;?>';
	if(role == 1)//ADMIN
		{
			var district = $("#district_id").val();
		}
	if(role == 3)//DISTRICT_AGENT
		{
			var district = '<?php echo $district;?>';//session value of logined user
		}	
	//var district = $("#district_id").val();
	$("#muncipality_drop").hide();
	if(district== '' || district== null || district == undefined)
	{
		return false;
	}
	if(tid == 'muncipality'){$("#muncipality_drop").show(); getlocation = 'getlocation';$("#block_drop").hide();}
	if(tid == 'panchayath'){$("#panchayath_drop").show();$("#block_drop").show(); getlocation = 'block';}
	if(tid == 'corporation'){$("#corporation_drop").show(); getlocation = 'getlocation';$("#block_drop").hide();}
	var toAppend;
	//toAppend = '<option>Any</option>';
	$.ajax({
            type: "POST",
            dataType:"json",
            url: "<?php echo site_url('user/getLocationDetails')?>",
            data: "district_id="+district+"&type="+tid+"&action="+getlocation,
            success: function(data){
			//console.log(data)
            if(data.location.length >0)
				{
					var $muncipality 	= $('#muncipality_data');             
					var $panchayath 	= $('#panchayath_data');             
					var $corporation 	= $('#corporation_data');             
					var $block 			= $('#block_data');             
					$muncipality.empty();
					$panchayath.empty();
					$corporation.empty();
					$block.empty();
					$("#muncipality_data").append("<option value='0'>Any</option>");
					$("#block_data").append("<option value='0'>Any</option>");
					$("#corporation_data").append("<option value='0'>Any</option>");
					$.each(data,function(key, value)
						{
							toAppend = '';
							$.each(value,function(k, v)
								{
									if(tid == 'muncipality')
										{
											$("#panchayath_drop").hide();
											$("#corporation_drop").hide();
											$("#muncipality_data").append('<option value=' + v.id + '>' + v.name + '</option>');
											//toAppend += '<option value=' + v.id + '>' + v.name + '</option>';
											//$("#muncipality_data").append(toAppend);
										}
									if(tid == 'panchayath')
										{
											$("#muncipality_drop").hide();
											$("#corporation_drop").hide();
											$("#panchayath_drop").hide();
											
											$("#block_data").append('<option value=' + v.id + '>' + v.name + '</option>');
										}
									if(tid == 'corporation')
										{
											$("#muncipality_drop").hide();
											$("#panchayath_drop").hide();
											$("#corporation_data").append('<option value=' + v.id + '>' + v.name + '</option>');
										}
								})
						});
				}
			else
				{
					$("#muncipality_drop").hide();
					$("#panchayath_drop").hide();
					$("#corporation_drop").hide();
				}	
            }
        });

}
function getPanch(bid)
{
	var $panchayath 	= $('#panchayath_data');
	$panchayath.empty();
	$("#panchayath_data").append("<option value='0'>Any</option>");
	 $.ajax({
            type: "POST",
            dataType:"json",
            url: "<?php echo site_url('user/getLocationDetails')?>",
            data: "district_id="+bid+"&type=panchayath&action=getlocation",
            success: function(data){
				//console.log(data)
            if(data.location.length >0)
				{
					//console.log(data)
					$("#panchayath_drop").show();
					$.each(data,function(key, value)
						{
							$.each(value,function(k, v)
								{
									$("#panchayath_data").append('<option value=' + v.id + '>' + v.name + '</option>');
								})
						});
				}
			else	
				{
					$("#panchayath_drop").hide();
				}	
            }
        });
}
function get_taluk(id)
{
	var $taluk_sec = $('#taluk_sec');
	$taluk_sec.hide();
	//+"&<?php echo $this->security->get_csrf_token_name(); ?>=<?php echo $this->security->get_csrf_hash(); ?>"
	 $.ajax({
            type: "POST",
            dataType:"json",
            url: "<?php echo site_url('user/getTaluk')?>",
            data: "district_id="+id+"&type=taluk",
            success: function(data){
				comsole.log(data)
            if(data.taluk_all.length >0)
				{
					comsole.log(data)
					var $taluk = $('#taluk');             
					$taluk.empty();
					$taluk_sec.show();
					$.each(data,function(key, value)
						{
							$.each(value,function(k, v)
								{
									$("#taluk").append('<option value=' + v.id + '>' + v.name + '</option>');
								})
						});
				}	
            }
        });
}
    </script>
	
<div id="page-wrapper">
<div class="container-fluid">
<div class="table widget">

    <div class="row">

                    <div class="col-lg-12">
                    <i class="fa fa-filter pull-right settings" aria-hidden="true" onclick="showSettings();"></i>
                        <h4 class="page-header">
                            Agent List
                        </h4>                         
                    </div>
              <div id="status_div" class="col-md-12"> 
                <?php if($this->session->flashdata('err')) { ?>
                  <?php echo $this->session->flashdata('err');?>
                <?php } ?>       
              </div>      
                   
              <div id="search_settings2">
			  <div class="row">
                <div class="col-md-3">                   
                   <input type="text" name="search" id="search" placeholder="Enter Name or Username" class="form-control" style="" onkeyup="search_faq();"> 
					<input type="button" class="btn btn-default btn-sm" value="Clear" onclick="reset()"></button>
                   </div>
                
                <!-- <div class="form-group col-md-4">
                <select class="form-control " id="user_type" name="user_type" onchange="search_faq()" >
                      <option value="" class="select">View All Status</option>
                      <option value="<?php echo ACTIVE ?>"  <?php if(ACTIVE == $user_type){?> selected <?php } ?>>Enabled</option>
                      <option value="<?php echo INACTIVE?>" <?php if(INACTIVE == $user_type){?> selected <?php } ?> >Disabled</option>                        
                </select>
                </div> -->
				<div class="col-md-8 " style="">
                <a href="<?php echo site_url('agent-bulk-upload'); ?>" class="btn btn-default btn-sm pull-right">Import</a> 
                <a href="<?php echo site_url('add-agent')?>" class="btn btn-default btn-sm pull-right" ><i class="fa fa-plus"></i> New</a>
                </div>
				</div>
				<div class="row">
				<?php if($role == ADMIN) {?>
				<div class="form-group col-md-2">
                                    <label>Select District</label>                                  
                                    <?php $array=array(''=>'Select District');
                                    foreach($district_all as $value ){
                                        $array[$value['id']] = $value['name'];
                                    }
                                    echo form_dropdown("district_id", $array, '' ,'class="form-control" id="district_id" onchange="clear_dropBox()"');
                                     ?>
                                    <label class="error">
                                  <?php if(form_error('taluk')) { echo form_error('taluk'); } ?>
                                </label>
                            </div>
				<?php }?>			
					<div class="form-group col-md-2" id="drp_sec">
					
				  <label>Select Type</label>
				  <select class="form-control" id="sel2" onchange="get_type(this.value)"  name="sel2" required="">
					<option value="0">Select any</option>
					<option value="muncipality">Municipality</option>
					<option value="panchayath">Panchayath</option>
					<option value="corporation">Corporation</option>
					</select>
				</label>
                </div>
<div class="form-group col-md-2" id="muncipality_drop">
                                    <label>Select Muncipality</label>
									<input type="hidden" id="drop_muncipality">
                                  <select id="muncipality_data" class="form-control" name='sel_muncipality' onchange="get_muncipalality_user(this.value)"></select>
								  
                                    <label class="error">
                                  <?php if(form_error('taluk')) { echo form_error('taluk'); } ?>
                                </label>
                            </div>
							
							<div class="form-group col-md-2" id="block_drop">
                                    <label>Select Block</label>
									<input type="hidden" id="drop_block">
                                  <select id="block_data" class="form-control" onchange="getPanch(this.value)" name='sel_block'></select>
                                     <label class="error">
                                  <?php if(form_error('taluk')) { echo form_error('taluk'); } ?>
                                </label>
                            </div>
							<div class="form-group col-md-2" id="panchayath_drop">
                                    <label>Select Panchayath</label>
									<input type="hidden" id="drop_panchayath">
                                  <select id="panchayath_data" class="form-control" name='sel_panchayath' onchange="get_panchayath_user(this.value)"></select>
                                     <label class="error">
                                  <?php if(form_error('taluk')) { echo form_error('taluk'); } ?>
                                </label>
                            </div>
							<div class="form-group col-md-2" id="corporation_drop">
                                    <label>Select Corporation</label>
									<input type="hidden" id="drop_corp">
                                   <select id="corporation_data" class="form-control"  name='sel_corporation'></select>
                                    <label class="error">
                                  <?php if(form_error('taluk')) { echo form_error('taluk'); } ?>
                                </label>
                            </div>				
              </div>
				</div>	
    </div><br>

<!--<div class="col-lg-9"> -->
	<div class="widget">
    <div id="filters" style="margin-bottom:20px;">

 
		<div class="users" id="update-div">
			<table id="partner_details" class="display table table-striped table-bordered" cellspacing="0" width="100%">
        <thead>
            <tr>
			<th></th>
                <th> SL.No</th>
                <th>Name</th>
                <th>Email</th>
				<th>UserName</th>
				<th>CDS</th>
            	  <th>Date</th>   
                                   
                                   
                <th>Action</th>
            </tr>
        </thead>
    </table>
   
   		</div>
      
    </div>
</div>
</div>
</div>
</div>


                            <div id="pwdModal" class="modal fade" 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>
                                          <h3 class="modal-title text-center">Change Password</h3>
                                        </div>
                                        <div class="modal-body"> 
                                          <?php if ($this->session->flashdata('error')) { ?>
                                            <div class="alert alert-danger" role="alert"> <?php echo $this->session->flashdata('error'); ?></div>
                                          <?php } ?>
                                          <?php $attributes = array('id' => 'chng_pwd', 'name' => 'chng_pwd');
                                                echo form_open_multipart('', $attributes);
                                            ?>
                                                <div class="form-group">
                                                    <input type="password" class="form-control" name="password" maxlength="50"  id="password" required class="left"  placeholder="New Password" pattern=".{3,}" required title="Must have atleast 3 characters" onchange="this.setCustomValidity(this.validity.patternMismatch ? this.title : ''); if(this.checkValidity()) form.confirm_password.pattern = this.value;">
                                                </div>
                                                <div class="form-group">
                                                    <input type="password" class="form-control" name="confirm_pass"  maxlength="50" id="confirm_pass" class="left" required placeholder="Confirm New Password" required title="Both Passwords must be same" onchange="this.setCustomValidity(this.validity.patternMismatch ? this.title : '');">
                                                </div>
                                                <input type="hidden" name="uid" id="uid" value="" >
                                                  
                                                <div class="form-group text-center">
                                                    <input type="submit" name="submit" id="submit" value="SUBMIT" class="btn btn-primary">
                                                </div>
                                            </form>
                                            <div class="clearfix"></div>
                                            
                                            <p id="fail"></p>
                                        
										</div>
									</div>
								</div>
							</div>




					<div id="statModal" class="modal fade" role="dialog">
                        <div class="modal-dialog"> 
							<input type="hidden" value="" id='uids'>
							<input type="hidden" value="" id='status'>

                                <!-- Modal content-->
                            <div class="modal-content">
                                        <div class="modal-header">
                                          <button type="button" class="close" data-dismiss="modal">&times;</button>
                                          <h3 class="modal-title text-center">Change User Status</h3>
                                        </div>
                                        <div class="modal-body"> 
                                          
                                          Are you sure you want to <span id="type"></span> this user ?
                                                <div class="form-group text-center">
                                                    
                                                </div>
                                            </form>
                                            <div class="clearfix"></div>
                                            
                                            <p id="fail"></p>
                                        <div class="modal-footer">
										<input type="button" name="submit" id="submit" value="No" class="btn btn-primary" data-dismiss="modal">
                                                    <input type="button" name="submit" id="submit" value="Yes" class="btn btn-primary" onClick="changeStatus()";>
									  </div>
                                </div>
                            </div>
                        </div>
					</div>
<script src="<?php echo base_url()?>js/jquery.js"></script>
 <!-- Bootstrap Core JavaScript -->
<script src="<?php echo base_url()?>js/bootstrap.min.js"></script><link rel="stylesheet" href="<?php echo base_url();?>css/dataTables.bootstrap.min.css">
<script type="text/javascript" src="<?php echo base_url(); ?>js/jquery-ui.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>js/jquery.dataTables.js"></script>
<script type="text/javascript" src="<?php echo base_url();?>js/dataTables.bootstrap.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>js/pipeline.js"></script>
<script type="text/javascript">
$(document).ready(function(){

  search_faq();
  });

$(document).ready(function(){
$("#search_settings").hide();
  $("#chng_pwd").on('submit',(function(e){ 

         e.preventDefault();
         var response = $.ajax({
              url: "<?php echo site_url('user/change_password_admin')?>",
              type: "POST",
              data:  new FormData(this),
              contentType: false,
              cache: false,
              processData:false,
              async:false,
              success: function(data){           
               },
              error: function(){}             
           }).responseText;
        
     if(response == 'NO_UPDATION' ){
       //$("#sucess_verification").addClass('alert alert-danger');
       $("#fail").addClass('text-danger');
       $("#fail").html('Sorry, No updation.');
     }else if(response ==  'FAIL'){
     // $("#sucess_verification").addClass('alert alert-danger');
      $("#fail").addClass('text-danger');
       $("#fail").html('Sorry, Please try Again.');
     }else if(response == 'MIS_MATCH'){
      //$("#sucess_verification").addClass('alert alert-danger');
      $("#fail").addClass('text-danger');
       $("#fail").html('Sorry, Please specify your New Password Again.');
     }
        else{
    $("#password").val('');
    $("#confirm_pass").val('');
  //  $("#sucess_verification").removeClass('alert-danger');
 // $("#sucess_verification").addClass('alert-success');
  $("#fail").removeClass('text-danger');
  $("#fail").addClass('success');
  $("#fail").html('Password changed');
 }

        }));
  });
  function reset()
  {
	  $("#search").val('');
	  $("#sel2").val('0');
	  $("#muncipality_data").val('0');
	  $("#block_data").val('0');
	  $("#panchayath_data").val('0');
	  $("#muncipality_drop").hide();
	  $("#block_drop").hide();
	  $("#panchayath_drop").hide();
	  search_faq();
	  showSettings()
  }
  function clear_dropBox()
{
	$("#sel2").val(0);
	$("#block_drop").hide();
	$("#muncipality_drop").hide();
	$("#panchayath_drop").hide();
}
  function changeStatus()
  {
	  var uid = $('#uids').val();
	  var status = $('#status').val();
	  $.ajax({
              type: "POST",
              url: "<?php echo site_url('user/changeStatus'); ?>",
              data: "user_id="+uid+"&status="+status+"&<?php echo $this->security->get_csrf_token_name(); ?>=<?php echo $this->security->get_csrf_hash(); ?>",
              success: function (msg)
              {
				  $('#statModal').modal('hide');
                $("#partner_details").dataTable().fnDestroy();
				var table = $('#partner_details').DataTable();
   table.clear().draw();
   table.state.clear();
                call_datatable();
              }
          });
  }
  function search_faq(){ 

    var user_type     =  $('#user_type').val();
    var search_value =  $('#search').val();
    $("#partner_details").dataTable().fnDestroy();
      $("#partner_details").dataTable().fnDestroy();
   var table = $('#partner_details').DataTable();
   table.clear().draw();
   table.state.clear();
   call_datatable(); 
}
function get_muncipalality_user(mid)
{
	$("#drop_muncipality").val(mid);
    $("#partner_details").dataTable().fnDestroy();
	var table = $('#partner_details').DataTable();
   table.clear().draw();
   table.state.clear();
    call_datatable();
}
function get_panchayath_user(pid)
{
	$("#drop_panchayath").val(pid);
    $("#partner_details").dataTable().fnDestroy();
	var table = $('#partner_details').DataTable();
   table.clear().draw();
   table.state.clear();
    call_datatable();
}
function call_datatable(){ 
 $("#partner_details").dataTable().fnDestroy();
    $('#partner_details').dataTable( {
        dom: 'T<"clear">lfrtip',
        bFilter: false,
        "processing": true,
        "serverSide": true,
        "stateSave": true,
		"bInfo" : false,
        "oTableTools": {
          "aButtons": [
              {
                  "sExtends":    "collection",
                  "sButtonText": "Export"
              }
          ]
      },
        "ajax": $.fn.dataTable.pipeline( {
            url: "<?php echo site_url('user/get_agent_list') ?>",// get the parameters of your datatables using $_GET in your controller. Needed in order to implement filter (Search) in datatable. 
            method: 'POST',
            pages: 999999, // number of pages to cache
            data: function ( d ) { 
                //d.user_type = $('#user_type').val();
                d.search_text = $('#search').val();
                d.muncipality = $('#drop_muncipality').val();
                d.panchayath = $('#drop_panchayath').val();
             //   d.searchQuery1 = $('#searchQuery1').val();
               // d.skills = $('#skills').val();
               d.<?php echo $this->security->get_csrf_token_name(); ?> = '<?php echo $this->security->get_csrf_hash(); ?>';
                
            }
        } ),
        
        "aoColumnDefs": [  
        { "bVisible": false, "aTargets":[0]}, 
		
        { "aTargets":[1] ,"mData": null,
         "bSortable": false, "mRender": function (data) { return data[17];}},
         { "aTargets":[2] ,"mData": null,
         "bSortable": false, "mRender": function (data) { return data[1];}},
         { "aTargets":[3] ,"mData": null,
         "bSortable": false, "mRender": function (data) { return data[2];}},
          { "aTargets":[4] ,"mData": null,
         "bSortable": false, "mRender": function (data) { return data[3];}},
         { "aTargets":[5] ,"mData": null,
         "bSortable": false, "mRender": function (data) { return data[5];}},
         { "aTargets":[6] ,"mData": null,
         "bSortable": false, "mRender": function (data) { return data[4];}},
         
      {
        "sWidth": "18%",
         "aTargets": [7],
         "mData": null,
         "bSortable": false,
         "mRender": function (data) { 
          //console.log(data)
		
     // return '<a class=""><i class="fa fa-check" aria-hidden="true"></i></a><a class="btn btn-default btn-xs fa fa-pencil"  href="<?php echo site_url()?>update_taluk_member/'+data[0]+'" Title="Edit" ></a><a class="btn btn-default btn-xs fa fa-trash-o" href="#" onClick="delete_user('+data[0]+');"  Title="Delete" ></i></a>  <a class="btn btn-default btn-xs gear_ic" onclick="get_popup('+data[0]+')"   Title="Change Password" data-toggle="modal" data-target="#pwdModal"><i class="fa fa-gear"></i></a>';
		//<a class="btn btn-default btn-xs fa fa-pencil"  href="<?php echo site_url()?>update_taluk_member/'+data[0]+'" Title="Edit" ></a><a class="btn btn-default btn-xs fa fa-trash-o" href="#" onClick="delete_user('+data[0]+');"  Title="Delete" ></i></a>  <a class="btn btn-default btn-xs gear_ic" onclick="get_popup('+data[0]+')"   Title="Change Password" data-toggle="modal" data-target="#pwdModal"><i class="fa fa-gear"></i></a>
		if(data[6] == 1)//active
           {
           return '<a class="btn btn-default btn-xs fa" onclick="get_status('+data[0]+',2,'+data[6]+')" Title="Change Status" data-toggle="modal" data-target="#statModal"><i class="fa fa-times" aria-hidden="true" style="color: red;"> Click to Deactivate</i></a><a class="btn btn-default btn-xs fa fa-trash-o" href="#" onClick="delete_user('+data[0]+');"  Title="Delete" ></i></a>'
            }else
            {
             return '<a class="btn btn-default btn-xs fa" onclick="get_status('+data[0]+',1,'+data[6]+')" Title="Change Status" data-toggle="modal" data-target="#statModal"><i class="fa fa-check" aria-hidden="true" style="color: green;"> Click to Activate</i></a><a class="btn btn-default btn-xs fa fa-trash-o" href="#" onClick="delete_user('+data[0]+');"  Title="Delete" ></i></a>'; 
            }


	   
     }
       }
    
     ],
    } ); 
}
function showSettings()
  {
    $('#search_settings').slideToggle('2000',"swing", function () { }) 
  }

function delete_user(id)
{
  var response = confirm("Press 'Ok' to delete the User");
  if (response == true) {
  $.ajax({
              type: "POST",
              url: "<?php echo site_url('user/delete_user'); ?>",
              data: "user_id="+id+"&<?php echo $this->security->get_csrf_token_name(); ?>=<?php echo $this->security->get_csrf_hash(); ?>",
              success: function (msg)
              {
                $("#partner_details").dataTable().fnDestroy();
                call_datatable();
              }
          });
  }
}
function activate_user(id)
{
  var response = confirm("Press 'Ok' to activate the User");
  if (response == true) {
  $.ajax({
              type: "POST",
              url: "<?php echo site_url('user/activate_user'); ?>",
              data: "user_id="+id+"&<?php echo $this->security->get_csrf_token_name(); ?>=<?php echo $this->security->get_csrf_hash(); ?>",
              success: function (msg)
              {
                $("#partner_details").dataTable().fnDestroy();
                call_datatable();
              }
          });
  }
}
function user_edit(id)
{
  
  $.ajax({
              type: "POST",
              url: "<?php echo site_url('user/edit_user'); ?>",
              data: "user_id="+id+"&<?php echo $this->security->get_csrf_token_name(); ?>=<?php echo $this->security->get_csrf_hash(); ?>",
              success: function (msg)
              { 
                // $("#partner_details").dataTable().fnDestroy();
                //call_datatable();
              }
          });
  
  
}
function get_popup(id){ 
        $("#fail").html('');
        $('#uid').val(id);
}
function get_status(id,type,status){ 
	$('#uids').val(id);
	$('#status').val(status);
	if(type==1){
		$('#type').html('Activate');
	}
	if(type==2){
		$('#type').html('Deactivate');
	}
}

function export_data(id)
  {
    var url = "<?php echo site_url('userdata/export_agent');?>"+'/'+'0'+'/'+'0';
    console.log(url);
    window.location = url;    
  }

 </script>
<style type="text/css">

.btn-group-sm>.btn, .btn-sm {
    font-size: 14px;
    margin-left: 10px;
}
</style>