@extends('layouts.app', ['title' => 'Manajemen Tiket Maintenance', 'heading' => 'Tiket Maintenance'])

@section('content')
<div class="card">
    <div class="card-header border-bottom">
        <h5 class="card-title mb-0">Daftar Laporan Kerusakan (Tiket)</h5>
    </div>
    <div class="card-datatable table-responsive pt-0">
        <table class="datatables-tickets table">
            <thead>
                <tr>
                    <th>Tiket #</th>
                    <th>Objek</th>
                    <th>Pelapor</th>
                    <th>Tanggal</th>
                    <th>Status</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
                @foreach($tickets as $ticket)
                    <tr>
                        <td><strong>{{ $ticket->ticket_number }}</strong></td>
                        <td>
                            {{ $ticket->maintainable?->name ?? 'Objek Terhapus' }}
                            <div class="small text-muted">{{ $ticket->maintainable_type == 'App\Models\Item' ? 'Inventaris' : 'Fasilitas' }}</div>
                        </td>
                        <td>{{ $ticket->reporter_name }}</td>
                        <td>{{ $ticket->created_at->translatedFormat('l, d F Y H:i') }}</td>
                        <td>
                            <span class="badge {{ $ticket->status_badge }}">{{ $ticket->status_label }}</span>
                        </td>
                        <td>
                            <div class="d-flex gap-2">
                                <a href="{{ route('tickets.show', $ticket) }}" class="btn btn-sm btn-icon btn-label-info">
                                    <i class="ti tabler-eye"></i>
                                </a>
                                <form action="{{ route('tickets.destroy', $ticket) }}" method="POST" class="d-inline">
                                    @csrf
                                    @method('DELETE')
                                    <button type="button" class="btn btn-sm btn-icon btn-label-danger confirm-delete">
                                        <i class="ti tabler-trash"></i>
                                    </button>
                                </form>
                            </div>
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</div>
@endsection

@push('scripts')
<script type="module">
    $(function() {
        var dt_basic_table = $('.datatables-tickets');
        if (dt_basic_table.length) {
            let tableTitle = document.createElement('h5');
            tableTitle.classList.add('card-title', 'mb-0', 'text-md-start', 'text-center', 'pb-md-0', 'pb-6');
            tableTitle.innerHTML = 'Daftar Tiket Maintenance';

            dt_basic_table.DataTable({
                displayLength: 10,
                lengthMenu: [7, 10, 25, 50, 75, 100],
                order: [[3, 'desc']],
                language: {
                    search: "Cari:",
                    lengthMenu: "Tampil _MENU_ data",
                    info: "Menampilkan _START_ sampai _END_ dari _TOTAL_ data",
                    paginate: {
                        next: '<i class="icon-base ti tabler-chevron-right scaleX-n1-rtl icon-18px"></i>',
                        previous: '<i class="icon-base ti tabler-chevron-left scaleX-n1-rtl icon-18px"></i>',
                        first: '<i class="icon-base ti tabler-chevrons-left scaleX-n1-rtl icon-18px"></i>',
                        last: '<i class="icon-base ti tabler-chevrons-right scaleX-n1-rtl icon-18px"></i>'
                    }
                },
                layout: {
                    top2Start: {
                        rowClass: 'row card-header flex-column flex-md-row border-bottom mx-0 px-3',
                        features: [tableTitle]
                    },
                    top2End: {
                        features: [
                            {
                                buttons: [
                                    {
                                        extend: 'collection',
                                        className: 'btn btn-label-primary dropdown-toggle me-2 waves-effect waves-light',
                                        text: '<span class="d-flex align-items-center gap-2"><i class="icon-base ti tabler-upload icon-xs me-sm-1"></i> <span class="d-none d-sm-inline-block">Export</span></span>',
                                        buttons: [
                                            { extend: 'print', text: '<span class="d-flex align-items-center"><i class="icon-base ti tabler-printer me-1"></i>Print</span>', className: 'dropdown-item' },
                                            { extend: 'csv', text: '<span class="d-flex align-items-center"><i class="icon-base ti tabler-file-text me-1"></i>Csv</span>', className: 'dropdown-item' },
                                            { extend: 'excel', text: '<span class="d-flex align-items-center"><i class="icon-base ti tabler-file-spreadsheet me-1"></i>Excel</span>', className: 'dropdown-item' },
                                            { extend: 'pdf', text: '<span class="d-flex align-items-center"><i class="icon-base ti tabler-file-description me-1"></i>Pdf</span>', className: 'dropdown-item' },
                                            { extend: 'copy', text: '<i class="icon-base ti tabler-copy me-1"></i>Copy', className: 'dropdown-item' }
                                        ]
                                    }
                                ]
                            }
                        ]
                    },
                    topStart: {
                        rowClass: 'row mx-0 px-3 my-0 justify-content-between border-bottom',
                        features: [
                            {
                                pageLength: {
                                    menu: [10, 25, 50, 100],
                                    text: 'Show_MENU_entries'
                                }
                            }
                        ]
                    },
                    topEnd: {
                        search: {
                            placeholder: ''
                        }
                    },
                    bottomStart: {
                        rowClass: 'row mx-3 justify-content-between',
                        features: ['info']
                    },
                    bottomEnd: 'paging'
                }
            });
        }
    });
</script>
@endpush
