<?php $this->load->view('admin/header'); ?>
<?php $this->load->view('admin/sidebar'); ?>
<style type="text/css">
    .regenerate_qr{
        background-color:#638585;
        color:white;
        border:0;
        font-size:14px;
        font-weight:500;
        border-radius:7px;
        padding:10px 10px;
        cursor:pointer;
        white-space:nowrap;
    }
    .active2{
        color: green !important;
    }
    .suspend{
        color: red !important;
    }
    .not_registered{
        color: gray !important;
    }
   
</style>
  <div class="main-panel">

    <div class="content-wrapper">
      <div class="row">
        <div class="col-lg-12 grid-margin stretch-card">
          <div class="card">
            <div class="card-body">
            <div class="form-group text-left" style="color:#a4dcec;">
                            
                            <H5>MANAGE USER</H5>

                        </div>
              <div class="table-responsive pt-3">
                <table id="dataTable" class="table table-bordered" width="100%">
                  <thead>
                    <tr>
                        <th>Id</th>
                        <th>Name</th>
                        <th>Email</th>
                        <th>Mobile Number</th>
                        <th>Status</th>
                        <th>Verified</th>          
                    </tr>
                  </thead>
                  <tbody>
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </div>                        
      </div>      
    </div>
<?php $this->load->view('admin/footer'); ?>

<script type="text/javascript">

var table_ajax;
table_ajax = jQuery('#dataTable').dataTable({
    "processing": true,
    "searching": true,
    "serverSide": true,
    "oLanguage": {
        "sEmptyTable": "No data found",
    },
    "ajax":{
        url: "<?php echo base_url('admin/user/user/manage_user_ajax'); ?>",
        type: "post",
        data: function(d) {
            d.search_data = returnData();
            var order_by  = d.order[0]['dir'];
            var order_for = d.order[0]['column'];
        },
        dataFilter: function(response){
            var data = JSON.parse(response);
            var length = data.recordsTotal;                
            return response;
        },
        complete : function() {
            // jQuery('.page-loader-wrapper').hide();
        },
        error: function(){  
            // jQuery('.page-loader-wrapper').hide();
        }
    },
    "order": [[0, "desc"]],
    "columns": [  
        {"data": "user_id" ,orderable: true, name: "user_id" },             
        {
            data: "name" ,
            orderable: true, 
            name: "name",
             render: function(data, type, row, meta) {   
             
                var html = "<a href='<?php echo base_url('admin/user/add/'); ?>"+row['user_id']+"'>"+row['name']+"</a>";                
                return html;
            } 
        },          
        {"data": "email" ,orderable: true, name: "email" },          
        {"data": "mobile_number" ,orderable: true, name: "mobile_number" },
        {
            data: 'status',
            orderable: false,
            name:'status',
            render: function(data, type, row, meta) {   
                
                var class_name = '';                    
                
                if(row['status'] == '1') {
                    var active = "selected";
                    var class_name = 'active2';
                } else {
                    var active = "";
                }

                if(row['status'] == '0') {
                    var suspend = "selected";
                    var class_name = 'suspend';                    
                } else {
                    var suspend = "";
                }

                if(row['status'] == '2') {
                    var not_registered = "selected";
                    var class_name = 'not_registered';                    
                } else {
                    var not_registered = "";
                }

                var html = "";
                html += "<select class='form-control "+class_name+" ' onchange='status_changes("+row['user_id']+");' id='"+row['user_id']+"_is_status'>";                                   
                    html += "<option value='1' class='active2' "+active+">Active</option>";
                    html += "<option value='0' class='suspend' "+suspend+" >Suspend</option>"; 
                    html += "<option value='2' class='not_registered' "+not_registered+">Not Registered</option>"; 

                html += "</select>";
                return html;
            }
        },
        {
            data: 'is_verify',
            orderable: false,
            name:'is_verify',
            render: function(data, type, row, meta) {

                var class_name = '';

                if(row['is_verify'] == '1') {
                    var is_yes = "selected";
                    var class_name = 'active2';                    
                } else {
                    var is_yes = "";
                }

                if(row['is_verify'] == '0') {
                    var is_no = "selected";
                    var class_name = 'suspend';                    
                } else {
                    var is_no = "";
                }               

                var html = "";
                html += "<select class='form-control "+class_name+" ' onchange='status_changes("+row['user_id']+");' id='"+row['user_id']+"_is_verify' >"; 
                    html += "<option value='1' class='active' "+is_yes+">Yes</option>";
                    html += "<option value='0' class='suspend' "+is_no+">No</option>";
                html += "</select>";
                return html;
            }
        },           
    ],
    fnServerParams: function (data) {
        data["order"].forEach(function (items, index) {
            data["order"][index]["column"] = data["columns"][items.column]["name"];
        });
    },
});

function returnData()
{
    var searchAry           = {};
    searchAry['user_id']    = "<?php echo @$this->uri->segment(4); ?>";
    return searchAry;
}

function modal_called(qr_id) {
    var image_url = $('#'+qr_id+'_no_qr').attr('src');
    $("#qr_id_append").text('idoor-'+qr_id);
    $("#qr_code_append").attr("src",image_url);
}

function regenerate_qr(qr_id) {

    $.ajax({
        url:"<?php echo base_url('admin/lock/lock/regenerate_qr'); ?>",
        type:'POST',
        data:{qr_id:qr_id},
        success:function(response) {
            var response = JSON.parse(response);
            if(response.result == 'failure'){
                toastr.error(response.message);                     
            } else {
                toastr.success(response.message);
                // table_ajax.api().ajax.reload();   
            }
        }
    }); 
}

function status_changes(user_id) {
        
    $.ajax({
        url:"<?php echo base_url('admin/user/user/status_changes'); ?>",
        type:'POST',
        data:{is_status:$("#"+user_id+'_is_status').val(),is_verify:$("#"+user_id+'_is_verify').val() , user_id:user_id},
        success:function(response) {
            var response = JSON.parse(response);
            if(response.result == 'failure'){
                toastr.error(response.message);                     
            } else {
                toastr.success(response.message);
                table_ajax.api().ajax.reload();                   
            }
        }
    });
}  
</script>
