(function ($) {
    "use strict";
  
    
  
    //datatables
    // table = $("#table").DataTable({
    //   language: {
    //     info: table_showing +
    //       " _START_ " +
    //       table_to +
    //       " _END_ " +
    //       table_of +
    //       " _TOTAL_ " +
    //       table_entries,
    //     sLengthMenu: table_show + " _MENU_ " + table_entries,
    //     sSearch: table_search,
    //     paginate: {
    //       previous: table_previous,
    //       next: table_next,
    //     },
    //   },
  
    //   processing: true, //Feature control the processing indicator.
    //   serverSide: true, //Feature control DataTables' server-side processing mode.
    //   order: [], //Initial no order.
    //   ajax: {
    //     url: BASE_URL + "admin/category/category_list",
    //     type: "POST",
    //   },
  
    //   //Set column definition initialisation properties.
    //   columnDefs: [{
    //     targets: [0, 2, 3, 5], //first column / numbering column
    //     orderable: false, //set not orderable
    //   }, ],
    // });

    var table = $('#scroll-vert-hor2').DataTable({
        "responsive": true,
        "searching": true,
        "bProcessing": true,
        "bServerSide": true,
        "lengthMenu": [
            [10, 25, 50, -1],
            [10, 25, 50, "All"]
        ],
        order: [
            [0, "desc"]
        ],
        ajax: {
            url: BASE_URL + 'admin/reportsAjax',
            type: 'POST',
            dataSrc: 'data',
            data: function ( d ) {
                return $.extend( {}, d, {
                    // "contract_id":function()
                    // {
                    //     return $(document).find('#Contract').val();
                    // },
                    "s_id":function()
                    {
                        return $(document).find('#state').val();
                    },
                    "d_id":function()
                    {
                        return $(document).find('#districts').val();
                    },
                    "f_id":function()
                    {
                        return $(document).find('#facilities').val();
                    },
                    "q_id":function()
                    {
                        return $(document).find('#quiz').val();
                    }
                    
                })
            }
        },
        "columnDefs": [{
            "targets": [-1], //last column
            "orderable": false, //set not orderable
        }, ]
        
        
    });

        $(document).on('change', '#state,#quiz,#facilities,#districts', function(){
            table.draw();
        });

        

        $(document).ready(function () {
            $('#state').change(function () {
               var state_id = $('#state').val();
               $(document).find('#districts').val(0); 
               
               if (state_id != '') {
                  $.ajax({
                     url: BASE_URL+"user/fetch_district",
                     method: "POST",
                     data: {
                        state_id: state_id
                     },
                     dataType:'json',
                     success: function (data) {
                        var  html;
                          if(data.length > 0){
                              html  = data.map((item) => {
                                return `<option value="${item.id}">${item.name}</option>`;
                             })
                          }else{
                             html = '<option value="">Select District</option>';
                          }
                       // console.log(html);
                       html = Array.isArray(html) ? ['<option value="">Select District</option>',...html] : html;
                        $('#districts').html(html);
                     }
                  });
               } else {
                  $('#districts').html('<option value="">Select City</option>');
               }
            });
        
         });
    
  
    
  
    
  
   
  })(jQuery);