@extends('layouts.app', ['title' => 'Data Inventaris', 'heading' => 'Data Inventaris'])

@section('content')
<div class="row">
    <div class="col-12 mb-4">
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="mb-0">Filter Barang</h5>
                <a href="{{ route('items.create') }}" class="btn btn-primary">
                    <i class="ti tabler-plus me-1"></i> Tambah Barang
                </a>
            </div>
            <div class="card-body">
                <form method="GET" class="row g-3">
                    <div class="col-md-4">
                        <label class="form-label">Cari nama</label>
                        <input type="text" name="search" value="{{ request('search') }}" class="form-control" placeholder="Contoh: Proyektor">
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">Lokasi</label>
                        <select name="location_id" class="form-select">
                            <option value="">Semua lokasi</option>
                            @foreach ($locations as $location)
                                <option value="{{ $location->id }}" @selected(request('location_id') == $location->id)>{{ $location->name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">Tracking</label>
                        <select name="tracking_mode" class="form-select">
                            <option value="">Semua</option>
                            <option value="stock" @selected(request('tracking_mode') === 'stock')>Stock</option>
                            <option value="unit" @selected(request('tracking_mode') === 'unit')>Unit</option>
                        </select>
                    </div>
                    <div class="col-md-1 d-flex align-items-end">
                        <button class="btn btn-label-secondary w-100" type="submit">Cari</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="col-12">
        <div class="card">
            <div class="card-datatable table-responsive pt-0">
                <table class="datatables-basic table">
                    <thead>
                        <tr>
                            <th>Nama</th>
                            <th>Lokasi</th>
                            <th>Tracking</th>
                            <th>Stok</th>
                            <th>Kondisi</th>
                            <th>QR</th>
                            <th>Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse ($items as $item)
                            <tr>
                                <td>
                                    <strong>{{ $item->name }}</strong>
                                    <div class="small text-muted">{{ $item->category?->name ?: 'Tanpa kategori' }}</div>
                                </td>
                                <td>{{ $item->location?->name }}</td>
                                <td><span class="badge bg-label-info">{{ strtoupper($item->tracking_mode) }}</span></td>
                                <td>{{ $item->stock?->available_quantity ?? 0 }} / {{ $item->total_quantity }}</td>
                                <td>{{ $item->good_count }} baik, {{ $item->minor_damage_count }} RR, {{ $item->major_damage_count }} RB, {{ $item->lost_count }} HLG</td>
                                <td class="small"><a href="{{ route('maintenance.public.show', $item->qr_token) }}" target="_blank">Buka form</a></td>
                                <td>
                                    <div class="d-flex align-items-center">
                                        <a href="{{ route('items.show', $item) }}" class="btn btn-icon btn-text-info rounded-pill waves-effect" title="Detail & Riwayat"><i class="ti tabler-eye"></i></a>
                                        <a href="{{ route('items.edit', $item) }}" class="btn btn-icon btn-text-secondary rounded-pill waves-effect"><i class="ti tabler-edit"></i></a>
                                        <form method="POST" action="{{ route('items.destroy', $item) }}" class="d-inline">
                                            @csrf
                                            @method('DELETE')
                                            <button type="button" class="btn btn-icon btn-text-danger rounded-pill waves-effect confirm-delete"><i class="ti tabler-trash"></i></button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        @empty
                        @endforelse
                    </tbody>
                </table>
            </div>
            <div class="card-footer d-flex justify-content-center">
                {{ $items->links() }}
            </div>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script type="module">
    $(function() {
        var dt_basic_table = $('.datatables-basic');
        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 = 'Tabel Inventaris';

            dt_basic_table.DataTable({
                displayLength: 10,
                lengthMenu: [7, 10, 25, 50, 75, 100],
                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
