@extends('layouts.app')
@section('title','Statements')
@section('pages')
<style type="text/css">
    .breadcrumb-title {
      border-right: none;
    }
</style>

    <!--breadcrumb-->
    <div class="page-breadcrumb row align-items-center justify-content-between mb-3 px-3 px-md-0">
      <div class="breadcrumb-title col-md-6"> Statements
      </div>
      <div class="col-md-6">
        <nav aria-label="breadcrumb">
          <ol class="breadcrumb justify-content-md-end mb-0">
            <li class="breadcrumb-item"><i class="bi bi-info-circle-fill text-info show-help-support" data-id="38" data-bs-toggle="modal" data-bs-target="#commonModal"></i></li>
            <li class="breadcrumb-item active" aria-current="page">Statements</li>
          </ol>
        </nav>
      </div>
    </div>
    <!--end breadcrumb-->

  <div class="card style_table shadow-none radius-5 min-height">
      <div class="card-header py-3">
        @include('layouts.partials.nav-menu')
        <div class="row gx-2">

          <div class="col-lg-3 col-md-3 col-3">
            <select class="form-select" id="gender" name="gender" aria-controls="example">
                <option value="">Select Gender</option>
                @foreach ($genders as $key => $record)
                <option value="{!! $record->id !!}">{!! $record->name !!}</option>
                @endforeach
            </select>
          </div>

          <div class="col-lg-3 col-md-3 col-3">
            <select class="form-select" id="house" name="house" aria-controls="example">
                <option value="">Select House</option>
                @foreach ($houses as $key => $record)
                <option value="{!! $record->id !!}">{!! $record->name !!}</option>
                @endforeach
            </select>
          </div>

          <div class="col-lg-3 col-md-3 col-3">
            <select class="form-select" id="class" name="class" aria-controls="example">
                <option value="">Select Class</option>
                @foreach ($classes as $key => $record)
                <option value="{!! $record->id !!}">{!! $record->class_name !!}</option>
                @endforeach
            </select>
          </div>

          <div class="col-lg-3 col-md-3 col-3">
            <select class="form-select" id="section" name="section" aria-controls="example">
                <option value="">Select Section</option>
                @foreach ($sections as $key => $record)
                <option value="{!! $record->id !!}">{!! $record->section !!}</option>
                @endforeach
            </select>
          </div>
        </div>

        <div class="row gx-2" style="padding-top: 5px;">
          <div class="col-lg-3 col-md-3 col-3">
            <select class="form-select" id="category" name="category" aria-controls="example">
                <option value="">Select Category</option>
                @foreach ($categories as $key => $record)
                <option value="{!! $record->id !!}">{!! $record->category_name !!}</option>
                @endforeach
            </select>
          </div>

          <div class="col-lg-3 col-md-3 col-8">
              <div class="position-relative">
                  <div class="position-absolute top-50 translate-middle-y search-icon px-3"><i class="bi bi-search"></i></div>
                  <input class="form-control ps-5" type="search" id="search" placeholder="Search">
              </div>
          </div>

          <div class="col-lg-3 col-md-3 col-3 dataTables_length" id="example_length">
            <i class="bi bi-funnel"></i>
            <select class="form-select sorting sorting-box" id="" name="example_length" aria-controls="example">
                <option value="10">Show: 10</option>
                <option value="30">Show: 30</option>
                <option value="50">Show: 50</option>
            </select>
          </div>

          <div class="col-lg-3 col-md-3">
            <div class="ms-auto float-md-end">
              <div class="btn-group">
                  <a href="{{route('students.index')}}" class="btn btn-outline-secondary me-1"><i class="bx bx-arrow-back ms-0 me-1"></i>Back</a>
              </div>
            </div>
          </div>
        </div>
      </div>

    <div class="card-body" id="tables">

      @include('students.statement-table')

      <input type="hidden" name="hidden_page" id="hidden_page" value="1" />
      <input type="hidden" name="hidden_column_name" id="hidden_column_name" value="name" />
      <input type="hidden" name="hidden_sort_type" id="hidden_sort_type" value="asc" />
    </div>
<script>

     /***************** Global Delay Fucntion ***************/
     function delay(callback, ms) {
        var timer = 0;
        return function() {
            var context = this,
                args = arguments;
            clearTimeout(timer);
            timer = setTimeout(function() {
                callback.apply(context, args);
            }, ms || 0);
        };
    }

  $(document).ready(function(){
    function clear_icon(){
      $('#id_icon').html('');
      $('#post_title_icon').html('');
    }
    function fetch_data(page, sort_type, sort_by, query, gender, house, classId, section, category){
      $.ajax({
        url:"?page="+page+"&sortby="+sort_by+"&sorttype="+sort_type+"&query="+query+"&gender="+gender+"&house="+house+"&class="+classId+"&section="+section+"&category="+category,
        success:function(data){
          $('#tables').html('');
          $('#tables').html(data);
          $(document).ready(function(){$.switcher();});
          $('#example').dataTable({paging: false, searching: false, info: false});
        }
      })
    }

    $("body").on("click", '.export-statement', function(event) {
        var url      = "{!! route('students.statements') !!}";
        var category = $("#category").val();
        var section  = $('#section').val();
        var classId  = $('#class').val();
        var gender   = $('#gender').val();
        var query    = $('#search').val();
        var house    = $('#house').val();
        var queryStr = "query="+query+"&gender="+gender+"&house="+house+"&class="+classId+"&section="+section+"&category="+category+"&type=excel";

        window.open(url + "?" + queryStr);
    });

    $("body").on("click", '#gender', function(event) {
        var gender   = $(this).val();
        var query    = $('#search').val();
        var house    = $('#house').val();
        var classId  = $('#class').val();
        var section  = $('#section').val();
        var category = $('#category').val();

        if (query.length < 2 && query.length != 0) {
        return;
        }

        var column_name = $('.sorting').val();
        var sort_type = $('#hidden_sort_type').val();
        var page = 1?1:(this).attr('href').split('page=')[1];

        fetch_data(page, sort_type, column_name, query, gender, house, classId, section, category);
    });

    $("body").on("click", '#class', function(event) {
        var classId  = $(this).val();
        var gender   = $('#gender').val();
        var query    = $('#search').val();
        var house    = $('#house').val();
        var section  = $('#section').val();
        var category = $('#category').val();

        if (query.length < 2 && query.length != 0) {
        return;
        }

        var column_name = $('.sorting').val();
        var sort_type = $('#hidden_sort_type').val();
        var page = 1?1:(this).attr('href').split('page=')[1];

        fetch_data(page, sort_type, column_name, query, gender, house, classId, section, category);
    });

    $("body").on("click", '#section', function(event) {
        var section  = $(this).val();
        var classId  = $('#class').val();
        var gender   = $('#gender').val();
        var query    = $('#search').val();
        var house    = $('#house').val();
        var category = $('#category').val();

        if (query.length < 2 && query.length != 0) {
        return;
        }

        var column_name = $('.sorting').val();
        var sort_type = $('#hidden_sort_type').val();
        var page = 1?1:(this).attr('href').split('page=')[1];

        fetch_data(page, sort_type, column_name, query, gender, house, classId, section, category);
    });

    $("body").on("click", '#category', function(event) {
        var category = $(this).val();
        var section  = $('#section').val();
        var classId  = $('#class').val();
        var gender   = $('#gender').val();
        var query    = $('#search').val();
        var house    = $('#house').val();

        if (query.length < 2 && query.length != 0) {
        return;
        }

        var column_name = $('.sorting').val();
        var sort_type = $('#hidden_sort_type').val();
        var page = 1?1:(this).attr('href').split('page=')[1];

        fetch_data(page, sort_type, column_name, query, gender, house, classId, section, category);
    });

    $("body").on("click", '#house', function(event) {
        var house    = $(this).val();
        var category = $('#category').val();
        var section  = $('#section').val();
        var classId  = $('#class').val();
        var gender   = $('#gender').val();
        var query    = $('#search').val();

        if (query.length < 2 && query.length != 0) {
        return;
        }

        var column_name = $('.sorting').val();
        var sort_type = $('#hidden_sort_type').val();
        var page = 1?1:(this).attr('href').split('page=')[1];

        fetch_data(page, sort_type, column_name, query, gender, house, classId, section, category);
    });

     /******** searching ********/
     $(document).on('keyup', '#search', delay(function() {
        var query = $('#search').val();

        if (query.length < 2 && query.length != 0) {
            return;
        }

        var column_name = $('.sorting').val();
        var sort_type   = $('#hidden_sort_type').val();
        var page        = 1 ? 1 : (this).attr('href').split('page=')[1];
        fetch_data(page, sort_type, column_name, query);
    }, 700));

    $('body').on('change', '.sorting', function(){

      var column_name = $(this).val();
      console.log('perpage',column_name);
      var order_type = $(this).data('sorting_type');
      var reverse_order = '';
      var page = 1?1:(this).attr('href').split('page=')[1];
      var query = $('#search').val()?$('#search').val():'';
      fetch_data(page, reverse_order, column_name, query);
    });
    $('body').on('click', '.pager a', function(event){
      event.preventDefault();
      console.log('url', $(this).attr('href'));
      var page = $(this).attr('href').split('page=')[1];
      $('#hidden_page').val(page);
      var column_name = $('.sorting').val()?$('.sorting').val():'';
      var sort_type = $('#hidden_sort_type').val();
      var query = $('#search').val()?$('#search').val():'';
      $('li').removeClass('active');
      $(this).parent().addClass('active');
      fetch_data(page, sort_type, column_name, query);
    });

    /*********delete data**********/
    $("body").on("click",'.deleteDataCustom', function(event){
        dataString    = {"id":$(this).data('id')};
        var UrlValue  = $(this).data('url');
        var btn = $(this);

        Swal.fire({
            title: 'Are you sure you want to delete this?',
            icon: 'warning',
            showDenyButton: true,
            showCancelButton: false,
            confirmButtonText: 'Yes',
            denyButtonText: 'No',
        }).then((result) => {
            /* Read more about isConfirmed, isDenied below */
            if (result.isConfirmed) {
                $.ajax({
                url     : UrlValue,
                method  : 'DELETE',
                data    :{
                        "_token": $('meta[name="csrf-token"]').attr('content'),
                        "id": $(this).data('id')
                },
                headers:
                {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                beforeSend: function( xhr ) {
                    // xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
                },
                success : function(response){
                    if(response.success){
                        var ErroMsg = $(this).printErrorMsg(response.message);

                        $(this).Toastshow('success',ErroMsg);

                        var page        = $('.pager').find('.active a').attr('href') == undefined ? 1 : $('.pager').find('.active a').attr('href').split('page=')[1];
                        var column_name = $('.sorting').val()?$('.sorting').val():'';
                        var sort_type   = $('#hidden_sort_type').val();
                        var query       = $('#search').val()?$('#search').val():'';

                        fetch_data(page, sort_type, column_name, query);
                    }else{
                        var ErroMsg = $(this).printErrorMsg(response.message);
                        $(this).Toastshow('error',ErroMsg);
                    }
                },
                error: function (data) {
                  console.log("error ",data);
                }
            });
            }
        });
    });
  });
</script>
@endsection
