@extends('layouts/base_view')
@section('title')
{!! $variables['header_routes'] !!}
@endsection
@section('content')
<section class="content">
    <div class="container-fluid">
        <div class="row">
            <div class="col-12">
                <div class="card card-outline card-warning">
                    <div class="card-header">
                        <div class="d-flex flex-wrap align-items-center">
                            @include('layouts.partials.excel_upload_buttons', ['module' => 'holidays'])
                            <div class="btn-group btn-group-sm mb-2 mb-md-0 mr-2" role="group">
                                <select id="yearFilter" class="form-control">
                                    @foreach($variables['years'] as $year)
                                    <option value="{{ $year }}" {{ $year == $variables['current_year'] ? 'selected' : '' }}>
                                        {{ $year }}
                                    </option>
                                    @endforeach
                                </select>
                            </div>
                            <a href="{{ route('holidays.create') }}" class="btn btn-sm btn-warning mb-2 mb-md-0 button-cek-permission" data-permission="holidays.create">
                                <i class="fas fa-plus"></i><span class="d-none d-sm-inline ml-1">Tambah Hari Libur</span>
                            </a>
                        </div>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table id="holidaysTable" class="table table-hover table-sm">
                                <thead class="bg-warning">
                                    <tr>
                                        <th width="50">No</th>
                                        <th>Nama</th>
                                        <th>Tanggal</th>
                                        <th>Tipe</th>
                                        <th>Berulang</th>
                                        <th>Deskripsi</th>
                                        <th width="100">Status</th>
                                    </tr>
                                </thead>
                                <tbody>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Holiday Summary Cards -->
        <div class="row">
            <div class="col-md-3">
                <div class="card bg-info">
                    <div class="card-body">
                        <div class="d-flex">
                            <div class="flex-grow-1">
                                <p class="text-white mb-0">Libur Berikutnya</p>
                                <h5 class="text-white" id="nextHoliday">Memuat...</h5>
                            </div>
                            <div class="align-self-center">
                                <i class="fas fa-calendar-day fa-2x text-white-50"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card bg-success">
                    <div class="card-body">
                        <div class="d-flex">
                            <div class="flex-grow-1">
                                <p class="text-white mb-0">Bulan Ini</p>
                                <h5 class="text-white" id="thisMonth">Memuat...</h5>
                            </div>
                            <div class="align-self-center">
                                <i class="fas fa-calendar-week fa-2x text-white-50"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card bg-secondary">
                    <div class="card-body">
                        <div class="d-flex">
                            <div class="flex-grow-1">
                                <p class="text-white mb-0">Tahun Ini</p>
                                <h5 class="text-white" id="thisYear">Memuat...</h5>
                            </div>
                            <div class="align-self-center">
                                <i class="fas fa-calendar-alt fa-2x text-white-50"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="card bg-danger">
                    <div class="card-body">
                        <div class="d-flex">
                            <div class="flex-grow-1">
                                <p class="text-white mb-0">Status Hari Ini</p>
                                <h5 class="text-white" id="todayStatus">Memuat...</h5>
                            </div>
                            <div class="align-self-center">
                                <i class="fas fa-calendar-check fa-2x text-white-50"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<script>
    $(document).ready(function() {
        // Initialize DataTable
        var table = $('#holidaysTable').DataTable({
            processing: true,
            serverSide: true,
            ajax: {
                url: "{{ route('holidays.index') }}",
                type: 'GET',
                data: function(d) {
                    d.year = $('#yearFilter').val();
                }
            },
            columns: [{
                    data: 0,
                    name: 'no',
                    orderable: false,
                    searchable: false
                },
                {
                    data: 1,
                    name: 'name'
                },
                {
                    data: 2,
                    name: 'date'
                },
                {
                    data: 3,
                    name: 'type'
                },
                {
                    data: 4,
                    name: 'is_recurring',
                    orderable: false
                },
                {
                    data: 5,
                    name: 'description'
                },
                {
                    data: 6,
                    name: 'status',
                    orderable: false
                }
            ],
            order: [
                [2, 'asc']
            ],
            pageLength: 25,
            responsive: false,
            language: {
                processing: "Memuat...",
                search: "Cari:",
                lengthMenu: "Tampilkan _MENU_ data",
                info: "Menampilkan _START_ sampai _END_ dari _TOTAL_ data",
                infoEmpty: "Menampilkan 0 sampai 0 dari 0 data",
                infoFiltered: "(difilter dari _MAX_ total data)",
                paginate: {
                    first: "Pertama",
                    last: "Terakhir",
                    next: "Selanjutnya",
                    previous: "Sebelumnya"
                },
                emptyTable: "Tidak ada data tersedia",
                zeroRecords: "Tidak ada data yang cocok"
            }
        });

        // Year filter change
        $('#yearFilter').on('change', function() {
            table.ajax.reload();
        });

        // Load holiday statistics
        loadHolidayStats();
    });

    function loadHolidayStats() {
        // This would typically be API calls, but for now we'll use placeholder
        $('#nextHoliday').text('25 Des 2024');
        $('#thisMonth').text('2 hari libur');
        $('#thisYear').text('15 hari libur');

        // Check if today is holiday
        var today = new Date().toISOString().split('T')[0];
        $.get("{{ route('holidays.check') }}", {
                date: today
            })
            .done(function(response) {
                if (response.is_holiday) {
                    $('#todayStatus').text('Hari Libur');
                    $('#todayStatus').parent().parent().removeClass('bg-danger').addClass('bg-success');
                } else {
                    $('#todayStatus').text('Hari Kerja');
                }
            })
            .fail(function() {
                $('#todayStatus').text('Hari Kerja');
            });
    }
</script>

@include('layouts.partials.excel_upload_modal', [
    'module' => 'holidays',
    'title' => 'Hari Libur',
    'uploadRoute' => route('holidays.upload_excel'),
    'downloadRoute' => route('holidays.download_template')
])

@endsection