@extends('layouts.app')

@section('title', 'Warehouses')

@section('content')

    {{-- <div class="row">
        <div class="col-md-12">
            <div class="page-title-box">
                <div class="page-title-right">
                    <ol class="breadcrumb m-0">
                        <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
                        <li class="breadcrumb-item active">Warehouses</li>
                    </ol>
                </div>
                <h4 class="page-title">Warehouses</h4>
            </div>
        </div>
    </div> --}}

    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h4 class="header-title">Warehouse Management</h4>
                    <div>

                        <a href="{{ route('warehouses.create') }}" class="btn btn-primary">
                            <i class="fas fa-plus"></i> Add Warehouse
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table id="warehouses-table" class="table table-striped table-centered mb-0">
                            <thead>
                                <tr>
                                    {{-- <th>ID</th> --}}
                                    <th>Code</th>
                                    <th>Name</th>
                                    <th>Type</th>
                                    <th>Location</th>
                                    <th>Status</th>
                                    <th>Created</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

@push('plugin-scripts')
<script src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.11.5/js/dataTables.bootstrap5.min.js"></script>
@endpush

@push('plugin-styles')
<link href="https://cdn.datatables.net/1.11.5/css/dataTables.bootstrap5.min.css" rel="stylesheet">
@endpush

@push('scripts')
<script>
$(document).ready(function() {
    $('#warehouses-table').DataTable({
        processing: true,
        serverSide: false,
        ajax: '{{ route("warehouses.data") }}',
        columns: [
            // {data: 'id', name: 'id'},
            {data: 'code', name: 'code'},
            {data: 'name', name: 'name'},
            {data: 'type_label', name: 'type'},
            {data: 'location_display', name: 'location'},
            {data: 'status_badge', name: 'status'},
            {
    data: 'created_at',
    name: 'created_at',
    render: function (data, type, row) {
        if (!data) return '';

        const date = new Date(data);

        const day = String(date.getDate()).padStart(2, '0');
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const year = date.getFullYear();

        let hours = date.getHours();
        const minutes = String(date.getMinutes()).padStart(2, '0');
        const ampm = hours >= 12 ? 'PM' : 'AM';
        hours = hours % 12;
        hours = hours ? hours : 12; // hour '0' should be '12'
        const formattedTime = `${String(hours).padStart(2, '0')}:${minutes} ${ampm}`;

        return `${day}-${month}-${year} ${formattedTime}`;
    }
},
            {data: 'actions', name: 'actions', orderable: false, searchable: false}
        ],
        order: [[0, 'desc']],
        pageLength: 25,
        language: {
            search: "Search:",
            lengthMenu: "Show _MENU_ entries per page",
            info: "Showing _START_ to _END_ of _TOTAL_ entries",
            infoEmpty: "Showing 0 to 0 of 0 entries",
            infoFiltered: "(filtered from _MAX_ total entries)",
            processing: "Processing...",
            emptyTable: "No warehouses found"
        }
    });
});
</script>
@endpush
