@extends('layouts.app')

@section('title', 'My Attendance')

@section('content')
<div class="row">
    <div class="col-12">
        <!-- Filter Section -->
        <div class="card card-primary card-outline">
            <div class="card-header">
                <h3 class="card-title fw-bold"><i class="fas fa-filter mr-2"></i>Filter History</h3>
            </div>
            <div class="card-body">
                <form id="filterForm" class="row">
                    <div class="col-md-4">
                        <div class="form-group">
                            <label>Start Date</label>
                            <input type="date" id="start_date" class="form-control">
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="form-group">
                            <label>End Date</label>
                            <input type="date" id="end_date" class="form-control">
                        </div>
                    </div>
                    <div class="col-md-4" style="margin-top: 32px;">
                        <button type="button" id="applyFilter" class="btn btn-primary">
                            <i class="fas fa-filter mr-1"></i> Apply Filter
                        </button>
                        <button type="button" id="resetFilter" class="btn btn-default ml-1">
                            <i class="fas fa-undo"></i>
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <!-- Table Section -->
        <div class="card">
            <div class="card-header">
                <h3 class="card-title fw-bold"><i class="fas fa-list mr-2"></i>Attendance Log</h3>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-hover table-bordered table-striped w-100" id="myAttendanceTable">
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>Punch In</th>
                                <th>Punch Out</th>
                                <th>Duration (H:M)</th>
                            </tr>
                        </thead>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
$(function() {
    var table = $('#myAttendanceTable').DataTable({
        processing: true,
        serverSide: true,
        ajax: {
            url: "{{ route('attendances.data') }}?mine=true",
            data: function (d) {
                d.start_date = $('#start_date').val();
                d.end_date = $('#end_date').val();
            }
        },
        columns: [
            {data: 'date', name: 'date'},
            {data: 'punch_in', name: 'punch_in'},
            {data: 'punch_out', name: 'punch_out'},
            {data: 'duration', name: 'duration', orderable: false, searchable: false}
        ],
        scrollX: true,
        language: {
            search: "_INPUT_",
            searchPlaceholder: "Search records...",
            paginate: {
                previous: '<i class="fas fa-chevron-left"></i>',
                next: '<i class="fas fa-chevron-right"></i>'
            }
        },
        drawCallback: function() {
            $('.dataTables_paginate > .pagination').addClass('pagination-sm');
        }
    });

    $('#applyFilter').click(function(){ table.draw(); });
    $('#resetFilter').click(function(){
        $('#filterForm')[0].reset();
        table.draw();
    });
});
</script>
@endpush
