@extends('layouts.main')
@section('content')
    <style>
        .table th {
            white-space: nowrap;
        }

        .table td {
            white-space: nowrap;
        }
    </style>

    @include('layouts.components.alert-prompt')
    @if (auth()->user()->role_id == 1)
        <div class="card shadow mb-4">
            <nav class="navbar navbar-expand navbar-light bg-light py-3" style="border-bottom: 1px solid #ddd;">
                <h5 class="m-0 font-weight-bold text-primary mt-2">Data Peti</h5>
                <ul class="navbar-nav ml-auto">
                    <li class="nav-item dropdown">
                        <button type="button" class="btn btn-info dropdown-toggle mb-1" id="navbarDropdown"
                            data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            Action
                        </button>
                        <div class="dropdown-menu dropdown-menu-right animated--grow-in" aria-labelledby="navbarDropdown">
                            <a class="dropdown-item" href="{{ route('dashboard.peti.create') }}">Tambah Data
                                Peti</a>
                            <form action="{{ route('dashboard.all-pdf.cetakpdf') }}" method="GET" id="pdfForm"
                                target="_blank">
                                @csrf
                                <input type="hidden" name="peti_ids" id="peti_ids" value="">
                                <button type="submit" class="dropdown-item">Cetak Label Peti</button>
                            </form>
                            <a class="dropdown-item" data-toggle="modal" data-target="#importDataModal"
                                href="#">Import Data Peti</a>
                            <a class="dropdown-item" href="{{ route('dashboard.Pettern_Lot_Peti.export') }}"
                                target="_blank">Export
                                Data Peti</a>
                            <div class="dropdown-divider"></div>
                            <a class="dropdown-item" href="#" id="hapusPetiBtn">Hapus Peti</a>
                        </div>
                    </li>
                </ul>
            </nav>
            <div class="card-body">
                <div class="row">
                    <div class="col-6">
                        <!-- Fitur page -->
                        <form method="GET" action="{{ route('dashboard.peti.index') }}">
                            <label for="perPage">Tampilkan:</label>
                            <select id="perPage" name="perPage" class="form-select" onchange="this.form.submit()">
                                <option value="5" {{ request('perPage', 5) == '5' ? 'selected' : '' }}>5</option>
                                <option value="10" {{ request('perPage', 5) == '10' ? 'selected' : '' }}>10</option>
                                <option value="25" {{ request('perPage', 5) == '25' ? 'selected' : '' }}>25</option>
                                <option value="50" {{ request('perPage', 5) == '50' ? 'selected' : '' }}>50</option>
                                <option value="100" {{ request('perPage', 5) == '100' ? 'selected' : '' }}>100</option>
                                <option value="{{ $peti->total() }}"
                                    {{ request('perPage', 5) == $peti->total() ? 'selected' : '' }}>Semua</option>
                            </select>
                            <input type="hidden" name="search" value="{{ $search }}">
                            <input type="hidden" name="page" value="{{ request('page', 1) }}">
                        </form>
                    </div>
                    <div class="col-6 d-flex justify-content-end mb-3">
                        <!-- Fitur search -->
                        <form method="GET" action="{{ route('dashboard.peti.index') }}" class="form-inline">
                            <input type="text" name="search" class="form-control mr-2" placeholder="Cari..."
                                value="{{ $search }}">
                            <input type="hidden" name="page" value="1">
                            <input type="hidden" name="perPage" value="{{ request('perPage', 5) }}">
                            <button type="submit" class="btn btn-primary">Cari</button>
                        </form>
                    </div>
                </div>
                <div class="table-responsive">
                    <table class="table table-bordered" width="100%" cellspacing="0">
                        <thead>
                            <tr>
                                <th class="text-center" style="width: 10px">No</th>
                                <th><input type="checkbox" class="checkbox " id="selectAll"></th>
                                <th>Fix Lot</th>
                                <th>Customer</th>
                                <th>Kode Customer</th>
                                <th>Tipe Peti</th>
                                <th>Ukuran Peti</th>
                                <th>Lot No</th>
                                <th>Packing No</th>
                                <th>GD</th>
                                <th>Kondisi Peti</th>
                                <th>Isi Peti</th>
                                <th>Warna Peti</th>
                                <th class="text-center">Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse ($peti as $data_peti)
                                <tr>
                                    <td class="text-center">{{ $i++ }}</td>
                                    <td><input type="checkbox" class="checkbox" value="{{ $data_peti->id }}">
                                    </td>
                                    <td>{{ optional($data_peti)->fix_lot }}</td>
                                    <td>{{ optional($data_peti->customer)->name }}</td>
                                    <td>{{ optional($data_peti->customer)->code_customer }}</td>
                                    <td>{{ optional($data_peti->tipe_peti)->type }}</td>
                                    <td>{{ optional($data_peti->tipe_peti)->size_peti }}</td>
                                    <td>{{ optional($data_peti->customer)->lot_no }}</td>
                                    <td class="text-right">{{ optional($data_peti)->packing_no }}</td>
                                    <td>{{ optional($data_peti->warehouse)->name }}</td>
                                    <td>{{ optional($data_peti->kondisipeti)->nama_kondisi }}</td>
                                    <td>
                                        @if (optional($data_peti)->status_isi == 1)
                                            ADA
                                        @elseif(optional($data_peti)->status_isi == 0)
                                            TIDAK ADA
                                        @else
                                            TIDAK ADA
                                        @endif
                                    </td>
                                    <td>{{ optional($data_peti)->warna }}</td>
                                    <td class="text-center">
                                        <a href="{{ route('dashboard.peti.show', [$data_peti->id]) }}">
                                            <i class="fa fa-eye mr-2" style="font-size: 20px"></i>
                                        </a>
                                        <a href="{{ route('dashboard.peti.edit', [$data_peti->id]) }}">
                                            <i class="fa fa-edit mr-2" style="font-size: 20px"></i>
                                        </a>
                                        <form action="{{ route('dashboard.peti.destroy', $data_peti->id) }}" method="POST"
                                            style="display: inline;">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit"
                                                onclick="return confirm('Apakah Anda yakin ingin menghapus data ini?')"
                                                style="border: none; background: none; cursor: pointer;">
                                                <i class="fa fa-trash text-danger" style="font-size: 20px"></i>
                                            </button>
                                        </form>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="14" class="text-center">Data Kosong</td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
                <div class="row mt-3">
                    <div class="col-4">
                        <!-- Tampilkan jumlah data yang ditampilkan -->
                        @if (!$peti->isEmpty())
                            <p class="d-inline-block">Showing {{ $peti->firstItem() }} to
                                {{ $peti->lastItem() }} of
                                {{ $peti->total() }} entries</p>
                        @endif
                    </div>
                    <div class="col-8 d-flex justify-content-end">
                        @if ($peti->total() > $peti->perPage())
                            <nav aria-label="Page navigation">
                                <ul class="pagination justify-content-center">
                                    <li class="page-item {{ $peti->onFirstPage() ? 'disabled' : '' }}">
                                        <a class="page-link"
                                            href="{{ $peti->appends(['perPage' => Request::get('perPage')])->url(1) }}"
                                            aria-label="First">
                                            <span aria-hidden="true">First</span>
                                        </a>
                                    </li>
                                    {{ $peti->appends(['perPage' => Request::get('perPage')])->links() }}
                                    <li class="page-item {{ $peti->hasMorePages() ? '' : 'disabled' }}">
                                        <a class="page-link"
                                            href="{{ $peti->appends(['perPage' => Request::get('perPage')])->url($peti->lastPage()) }}"
                                            aria-label="Last">
                                            <span aria-hidden="true">Last</span>
                                        </a>
                                    </li>
                                </ul>
                            </nav>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    @elseif (auth()->user()->role_id == 2)
        @include('pages.user.Master_Data.Manajemen_Peti.Peti.index')
    @endif

    <!-- Import Data Modal-->
    <div class="modal fade" id="importDataModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Import Data Peti</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form action="{{ route('dashboard.peti.import') }}" method="POST" enctype="multipart/form-data">
                        @csrf
                        <div class="form-group">
                            <div class="form-group">
                                <label for="name" class="col-form-label">File Import Peti:</label>
                                <input type="file" name="file" class="form-control" accept=".xlsx, .xls, .csv"
                                    required>
                                <small class="text-muted">Pilih file Excel (.xlsx, .xls) atau CSV (.csv) untuk
                                    diimpor.</small>
                            </div>
                            <!-- Tombol untuk mengunduh file -->
                            <div class="form-group">
                                <a href="{{ asset('assets/file/Format_Import_Peti.csv') }}" class="btn btn-primary"
                                    download>Unduh Format Peti</a>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>
                            <button type="submit" class="btn btn-success">Submit</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!-- checkbox peti -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function() {
            // Ketika checkbox "Select All" di-klik
            $('#selectAll').click(function() {
                $('.checkbox').prop('checked', this.checked);
            });

            // Ketika salah satu checkbox di-klik
            $('.checkbox').click(function() {
                if ($('.checkbox:checked').length == $('.checkbox').length) {
                    $('#selectAll').prop('checked', true);
                } else {
                    $('#selectAll').prop('checked', false);
                }
            });

            // Ketika tombol cetak PDF di-klik
            $('#pdfForm').submit(function() {
                var selectedIds = [];
                $('.checkbox:checked').each(function() {
                    var id = $(this).val();
                    if (id !== 'on') {
                        selectedIds.push(id);
                    }
                });

                if (selectedIds.length > 0) {
                    $('#peti_ids').val(selectedIds.join(','));
                } else {
                    alert('Pilih setidaknya satu ID untuk mencetak PDF.');
                    return false;
                }
            });

            $('#hapusPetiBtn').click(function() {
                var selectedIds = [];
                $('.checkbox:checked').each(function() {
                    var id = $(this).val();
                    if (id !== 'on') {
                        selectedIds.push(id);
                    }
                });

                if (selectedIds.length > 0) {
                    var confirmation = confirm('Apakah Anda yakin ingin menghapus data terpilih?');
                    if (confirmation) {
                        // Kirim data ke controller menggunakan AJAX
                        $.ajax({
                            url: '{{ route('dashboard.peti.delete-selected') }}',
                            type: 'POST',
                            data: {
                                _token: '{{ csrf_token() }}',
                                peti_ids: selectedIds
                            },
                            dataType: 'json', // Ensure that the response is treated as JSON
                            success: function(response) {
                                alert(response.message); // Tampilkan pesan sukses
                                location.reload(); // Muat ulang halaman setelah penghapusan
                            },
                            error: function(xhr, status, error) {
                                console.error(xhr
                                    .responseText); // Tampilkan pesan error (debugging)
                                alert('Terjadi kesalahan saat menghapus data.');
                            }
                        });
                    }
                } else {
                    alert('Pilih setidaknya satu ID untuk menghapus data.');
                }
            });
        });
    </script>

@endsection