@include('admin.layouts.components.asset_datatables')

@extends('admin.layouts.index')

@section('title')
    <h1>
        {{ strtoupper($judul) }}
    </h1>
@endsection

@section('breadcrumb')
    <li class="active">Laporan Penduduk</li>
@endsection

@section('content')
    @include('admin.layouts.components.notifikasi')
    @include('admin.layouts.components.konfirmasi_hapus')

    <div class="row">
        <div class="col-md-12">
            <div class="box box-info">
                <div class="box-header with-border">
                    <x-tambah-button modal="true" :url="$routePath . '/form'" />
                    <x-hapus-button confirmDelete="true" selectData="true" :url="$routePath . '/delete'" />                    
                    @if (can('u'))
                        @if (setting('sinkronisasi_opendk'))
                            <x-btn-button judul="Kirim Ke OpenDK" tooltip="Kirim Ke OpenDK" icon="fa fa-random" formAction="true" type="btn-primary" :url="ci_route($routePath . '.kirim')" />
                        @else
                            <x-btn-button judul="Kirim Ke OpenDK" tooltip="API Key Belum Ditentukan" disabled="true" icon="fa fa-random" type="btn-primary" :url="''" />
                        @endif
                    @endif

                </div>
                <div class="box-body">
                    <div class="row">
                        <div class="col-sm-2">
                            <select class="form-control input-sm select2" id="filter-tahun">
                                <option value="">Pilih Tahun</option>
                                @foreach ($tahun as $thn)
                                    <option value="{{ $thn->tahun }}">{{ $thn->tahun }}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>
                    <hr>
                    {!! form_open(null, 'id="mainform" name="mainform"') !!}
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover tabel-daftar" id="tabel-data">
                            <thead class="bg-gray">
                                <tr>
                                    <th><input type="checkbox" id="checkall" /></th>
                                    <th>No</th>
                                    <th>Aksi</th>
                                    <th>Judul</th>
                                    <th>{{ $kolom }}</th>
                                    <th>Tahun</th>
                                    <th>Tanggal Upload</th>
                                    <th>Tanggal Kirim</th>
                                </tr>
                            </thead>
                        </table>
                    </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
@endsection

@push('scripts')
    <script>
        $(document).ready(function() {
            let TableData = $('#tabel-data').DataTable({
                'processing': true,
                'serverSide': true,
                'autoWidth': false,
                'pageLength': 10,
                'order': [
                    [5, 'desc'],
                    [4, 'desc']
                ],
                'columnDefs': [{
                        'orderable': false,
                        'targets': [0, 1, 2]
                    },
                    {
                        'className': 'padat',
                        'targets': [0, 1, 4, 5, 6, 7]
                    },
                    {
                        'className': 'aksi',
                        'targets': [2]
                    },
                ],
                'ajax': {
                    'url': "{{ ci_route($routePath . '.datatables') }}",
                    'method': 'POST',
                    'data': function(d) {}
                },
                'columns': [{
                        'data': 'ceklist'
                    },
                    {
                        data: 'DT_RowIndex',
                        class: 'padat',
                        searchable: false,
                        orderable: false
                    },
                    {
                        'data': 'aksi'
                    },
                    {
                        'data': 'judul'
                    },
                    {
                        'data': 'semester'
                    },
                    {
                        'data': 'tahun'
                    },
                    {
                        'data': 'updated_at'
                    },
                    {
                        'data': 'kirim'
                    },
                ],
            });

            $('#kirim').on('click', function() {
                $('#loading').modal({
                    backdrop: 'static',
                    keyboard: false
                }).show();
            });


            if (hapus == 0) {
                TableData.column(0).visible(false);
            }

            if (ubah == 0) {
                TableData.column(2).visible(false);
            }
            $('#filter-tahun').change(function() {
                TableData.column(5).search($(this).val()).draw()
            })
        });
    </script>
@endpush
