@extends('admin.layouts.app')

@section('page-title', 'Shipment Management')

@section('content')
<style>
    /*Remove horizontal scroll bar*/
    html, body {
        overflow-x: hidden !important;
    }
    
    .table thead th,
    .table tbody td {
        white-space: nowrap !important;
        padding: 8px 4px !important;
    }

    .table th:nth-child(10),  /* if Date is column 10 */
    .table td:nth-child(10) {
        max-width: 110px !important;
        width: 110px !important;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .table {
        table-layout: fixed !important;
    }

    
    .table, .custom-table-modal, .shipment-details-table {
        min-width: unset !important;
        width: 100% !important;
        table-layout: auto !important;
    }
    
    .table thead th,
    .table tbody td,
    .custom-table-modal th,
    .custom-table-modal td,
    .shipment-details-table th,
    .shipment-details-table td {
        white-space: normal !important;
        word-break: break-word;
    }
    
    .table-responsive, .modal .table-responsive {
        overflow-x: auto !important;
    }

    :root {
        --primary: #4361ee;
        --primary-dark: #3a56d4;
        --secondary: #f72585;
        --success: #4cc9f0;
        --warning: #f8961e;
        --danger: #e63946;
        --light: #f8f9fa;
        --dark: #212529;
        --gray: #6c757d;
        --border: #e2e8f0;
        --card-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
        --hover-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
    }

    .search-shipment-bar {
        display: flex;
        align-items: center;
        gap: 15px;
        padding: 20px;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border-radius: 16px;
        box-shadow: var(--card-shadow);
        flex-wrap: wrap;
        margin-bottom: 30px;
        color: white;
        position: relative;
        overflow: hidden;
    }
    
    .search-shipment-bar::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(255,255,255,0.1);
        z-index: 0;
    }
    
    .search-shipment-bar > * {
        position: relative;
        z-index: 1;
    }
    
    .search-shipment-bar input, 
    .search-shipment-bar select {
        padding: 12px 16px;
        border: 1px solid rgba(255,255,255,0.2);
        border-radius: 10px;
        flex: 1;
        min-width: 150px;
        background: rgba(255,255,255,0.9);
        font-weight: 500;
        transition: all 0.3s ease;
    }
    
    .search-shipment-bar input:focus, 
    .search-shipment-bar select:focus {
        background: white;
        box-shadow: 0 0 0 3px rgba(255,255,255,0.3);
        outline: none;
    }
    
    .btn-add-shipment {
        background: rgba(255,255,255,0.2);
        backdrop-filter: blur(10px);
        color: white;
        border: 1px solid rgba(255,255,255,0.3);
        padding: 12px 24px;
        border-radius: 10px;
        cursor: pointer;
        display: flex;
        align-items: center;
        gap: 8px;
        transition: all 0.3s ease;
        white-space: nowrap;
        font-weight: 600;
    }
    
    .btn-add-shipment:hover {
        background: rgba(255,255,255,0.3);
        transform: translateY(-2px);
        box-shadow: 0 10px 20px rgba(0,0,0,0.1);
    }
    
    .search-icon {
        font-size: 20px;
        filter: drop-shadow(0 2px 4px rgba(0,0,0,0.1));
    }
    
    .truck-icon {
        font-size: 18px;
    }

    @media (max-width: 768px) {
        .search-shipment-bar {
            flex-direction: column;
            align-items: stretch;
        }
        .search-shipment-bar input, 
        .search-shipment-bar select {
            width: 100%;
        }
    }
    
    /* Card Styles */
    .card {
        border: none;
        border-radius: 16px;
        box-shadow: var(--card-shadow);
        transition: all 0.3s ease;
        overflow: hidden;
    }
    
    .card:hover {
        transform: translateY(-5px);
        box-shadow: var(--hover-shadow);
    }
    
    .card-header {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        border: none;
        padding: 20px 25px;
        border-radius: 16px 16px 0 0 !important;
    }
    
    .card-header h5 {
        margin: 0;
        font-weight: 700;
        display: flex;
        align-items: center;
        gap: 10px;
    }
    
    /* Table Styles */
    .table-responsive {
        border-radius: 0 0 16px 16px;
        overflow-x: auto;
    }
    
    .table {
        margin: 0;
        border-collapse: separate;
        border-spacing: 0;
        width: 100%;
        min-width: 1200px;
        padding: 0;
    }
    
    .table thead th {
        background: #f8f9fa;
        border: none;
        padding: 16px 12px;
        font-weight: 700;
        color: var(--dark);
        text-align: center;
        vertical-align: middle;
        border-bottom: 2px solid var(--border);
        position: relative;
        white-space: nowrap;
    }
    
    .table thead th:first-child {
        border-radius: 0;
    }
    
    .table thead th:last-child {
        border-radius: 0;
    }
    
    .table tbody tr {
        transition: all 0.3s ease;
        border-radius: 10px;
    }
    
    .table tbody tr:hover {
        background-color: #f8f9ff;
        transform: scale(1.01);
        box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    }
    
    .table tbody td {
        padding: 14px 12px;
        text-align: center;
        vertical-align: middle;
        border-bottom: 1px solid var(--border);
        font-weight: 500;
        white-space: nowrap;
    }
    
    .table tbody tr:last-child td {
        border-bottom: none;
    }
    
    /* UPDATED: Status Badge Styles - ALL SAME SIZE */
    .badge {
        padding: 7px 17px !important;
        border-radius: 20px !important;
        font-weight: 600 !important;
        font-size: 13px !important;
        border: 2px solid transparent !important;
        min-width: 40px !important;
        text-align: center !important;
        display: inline-block !important;
        line-height: 1.2 !important;
    }
    
    /* Pending Status - SAME SIZE */
    .badge-pending {
        background: linear-gradient(135deg, #fef3c7, #fde68a) !important;
        color: #d97706 !important;
        border-color: #f59e0b !important;
        width: 110px;
    }
    
    /* In Transit Status - SAME SIZE */
    .badge-in_transit {
        background: linear-gradient(135deg, #dbeafe, #93c5fd) !important;
        color: #1e40af !important;
        border-color: #3b82f6 !important;
        width: 110px;
    }
    
    /* Dispatched Status - SAME SIZE */
    .badge-dispatched {
        background: linear-gradient(135deg, #e9d5ff, #c4b5fd) !important;
        color: #6b21a8 !important;
        border-color: #8b5cf6 !important;
        width: 110px;
    }
    
    /* Delivered Status - SAME SIZE */
    .badge-delivered {
        background: linear-gradient(135deg, #d1fae5, #a7f3d0) !important;
        color: #065f46 !important;
        border-color: #10b981 !important;
        width: 110px;
    }
    
    /* Default badge styles - SAME SIZE */
    .badge.bg-info {
        background: linear-gradient(135deg, #4cc9f0, #4361ee) !important;
        color: white !important;
    }
    
    .badge.bg-success {
        background: linear-gradient(135deg, #4ade80, #22c55e) !important;
        color: white !important;
    }
    
    .badge.bg-warning {
        background: linear-gradient(135deg, #fbbf24, #f59e0b) !important;
        color: white !important;
    }
    
    .badge.bg-danger {
        background: linear-gradient(135deg, #f87171, #ef4444) !important;
        color: white !important;
    }

    /* Light badges for quantity, kg, cbm */
    .badge.bg-light {
        background: #f8f9fa !important;
        color: #212529 !important;
        border: 1px solid #dee2e6 !important;
        min-width: 80px !important;
        padding: 6px 12px !important;
    }
    
    /* NEW: Action Button Styles */
    .action-container {
        position: relative;
        display: inline-block;
    }
    
    .btn-edit-status {
        background: linear-gradient(135deg, #667eea, #764ba2);
        color: white;
        border: none;
        border-radius: 8px;
        padding: 8px 10px;
        cursor: pointer;
        transition: all 0.3s ease;
        display: flex;
        align-items: center;
        justify-content: center;
        width: 36px;
        height: 36px;
    }
    
    .btn-edit-status:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
        background: linear-gradient(135deg, #5a6fd8, #6a42a0);
    }
    
    .status-dropdown {
        position: absolute;
        top: 100%;
        right: 50px;
        background: white;
        border-radius: 10px;
        box-shadow: 0 10px 25px rgba(0,0,0,0.2);
        padding: 8px;
        min-width: 150px;
        display: none;
        flex-direction: column;
        gap: 5px;
        margin-top: -50px;
        border: 1px solid var(--border);
        z-index: 1050;
    }
    
    .status-dropdown.show {
        display: flex;
    }
    
    .status-option {
        padding: 10px 12px;
        border: none;
        border-radius: 6px;
        cursor: pointer;
        font-size: 0.8rem;
        font-weight: 600;
        transition: all 0.2s ease;
        text-align: left;
        display: flex;
        align-items: center;
        gap: 8px;
        background: transparent;
        width: 100%;
    }
    
    .status-option:hover {
        transform: translateX(5px);
    }
    
    .status-option.pending {
        background: rgba(248, 150, 30, 0.1);
        color: #f8961e;
    }
    
    .status-option.pending:hover {
        background: rgba(248, 150, 30, 0.2);
    }
    
    .status-option.in_transit {
        background: rgba(67, 97, 238, 0.1);
        color: #4361ee;
    }
    
    .status-option.in_transit:hover {
        background: rgba(67, 97, 238, 0.2);
    }
    
    .status-option.dispatched {
        background: rgba(139, 92, 246, 0.1);
        color: #8b5cf6;
    }
    
    .status-option.dispatched:hover {
        background: rgba(139, 92, 246, 0.2);
    }
    
    .status-option.delivered {
        background: rgba(16, 185, 129, 0.1);
        color: #10b981;
    }
    
    .status-option.delivered:hover {
        background: rgba(16, 185, 129, 0.2);
    }
    
    .status-indicator {
        width: 8px;
        height: 8px;
        border-radius: 50%;
        display: inline-block;
    }
    
    .status-indicator.pending {
        background: #f8961e;
    }
    
    .status-indicator.in_transit {
        background: #4361ee;
    }
    
    .status-indicator.dispatched {
        background: #8b5cf6;
    }
    
    .status-indicator.delivered {
        background: #10b981;
    }

    /* Modal Styles */
    .modal-content {
        border-radius: 20px;
        border: none;
        box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
        overflow: hidden;
    }
    
    .modal-header {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        border: none;
        padding: 25px 30px 15px;
        border-radius: 20px 20px 0 0;
        position: relative;
    }
    
    .modal-header::after {
        content: '';
        position: absolute;
        bottom: 0;
        left: 5%;
        width: 90%;
        height: 1px;
        background: rgba(255,255,255,0.2);
    }
    
    .modal-title {
        font-weight: 700;
        font-size: 1.5rem;
    }
    
    .btn-close {
        filter: invert(1);
        opacity: 0.8;
    }
    
    .btn-close:hover {
        opacity: 1;
    }
    
    .modal-body {
        padding: 25px 30px;
    }
    
    /* Form Styles */
    .form-label {
        font-weight: 600;
        color: #5a6c7d;
        margin-bottom: 8px;
        font-size: 0.9rem;
    }
    
    .form-control {
        border-radius: 10px;
        border: 1px solid #e2e8f0;
        padding: 12px 16px;
        font-size: 15px;
        transition: all 0.3s ease;
        background: #fafbfc;
        color: #4a5568;
    }
    
    .form-control:focus {
        border-color: #a3bffa;
        box-shadow: 0 0 0 3px rgba(163, 191, 250, 0.2);
        background: white;
    }
    
    .form-control::placeholder {
        color: #a0aec0;
    }
    
    /* Date Input Styling */
    input[type="date"] {
        position: relative;
    }
    
    input[type="date"]::-webkit-calendar-picker-indicator {
        background: transparent;
        bottom: 0;
        color: transparent;
        cursor: pointer;
        height: auto;
        left: 0;
        position: absolute;
        right: 0;
        top: 0;
        width: auto;
    }
    
    input[type="date"] {
        position: relative;
        padding-right: 40px;
    }
    
    input[type="date"]:after {
        content: "📅";
        position: absolute;
        right: 12px;
        top: 50%;
        transform: translateY(-50%);
        pointer-events: none;
        font-size: 16px;
    }
    
    /* Button Styles */
    .btn-cancel {
        background: #f7fafc;
        color: #718096;
        border: 1px solid #cbd5e0;
        border-radius: 10px;
        font-weight: 600;
        padding: 12px 30px;
        transition: all 0.3s ease;
    }
    
    .btn-cancel:hover {
        background: #edf2f7;
        color: #4a5568;
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    }
    
    .btn-create {
        background: linear-gradient(135deg, #48bb78, #38a169);
        color: white;
        font-weight: 600;
        font-size: 16px;
        border-radius: 10px;
        padding: 12px 35px;
        border: none;
        transition: all 0.3s ease;
    }
    
    .btn-create:hover {
        transform: translateY(-2px);
        box-shadow: 0 10px 20px rgba(72, 187, 120, 0.3);
        background: linear-gradient(135deg, #38a169, #2f855a);
    }
    
    /* Custom Table in Modal */
    .custom-table-modal {
        width: 100%;
        border-collapse: separate;
        border-spacing: 0 8px;
        margin: 0;
        min-width: 1300px;
    }
    
    .custom-table-modal thead th {
        background: linear-gradient(135deg, #ebf8ff, #f0fff4);
        color: #2b6cb0;
        font-weight: 600;
        padding: 16px 10px;
        border: 1px solid #e2e8f0;
        text-align: center;
        position: relative;
        font-size: 0.9rem;
        letter-spacing: 0.3px;
        white-space: nowrap;
    }
    
    .custom-table-modal thead th:first-child {
        border-top-left-radius: 12px;
        border-bottom-left-radius: 12px;
    }
    
    .custom-table-modal thead th:last-child {
        border-top-right-radius: 12px;
        border-bottom-right-radius: 12px;
    }
    
    .custom-table-modal tbody tr {
        background: #fdfdfe;
        border-radius: 10px;
        box-shadow: 0 1px 3px rgba(0,0,0,0.04);
        transition: all 0.3s ease;
        border: 1px solid #f1f5f9;
    }
    
    .custom-table-modal tbody tr:hover {
        transform: translateY(-1px);
        box-shadow: 0 4px 12px rgba(0,0,0,0.08);
        border-color: #e2e8f0;
        background: white;
    }
    
    .custom-table-modal tbody td {
        padding: 14px 10px;
        text-align: center;
        vertical-align: middle;
        border: none;
        font-weight: 500;
        position: relative;
        color: #4a5568;
        white-space: nowrap;
    }
    
    .custom-table-modal tbody tr td:first-child {
        border-top-left-radius: 10px;
        border-bottom-left-radius: 10px;
    }
    
    .custom-table-modal tbody tr td:last-child {
        border-top-right-radius: 10px;
        border-bottom-right-radius: 10px;
    }
    
    /* Checkbox Styling */
    input[type="checkbox"] {
        width: 20px;
        height: 20px;
        border-radius: 6px;
        border: 2px solid #cbd5e0;
        cursor: pointer;
        position: relative;
        transition: all 0.2s ease;
        background: #f7fafc;
    }
    
    input[type="checkbox"]:checked {
        background: var(--primary);
        border-color: var(--primary);
    }
    
    /* Link Styling */
    a.text-primary {
        color: var(--primary) !important;
        text-decoration: none;
        font-weight: 600;
        transition: all 0.3s ease;
        position: relative;
        color: #4361ee !important;
    }
    
    a.text-primary:hover {
        color: var(--primary-dark) !important;
        text-decoration: underline;
    }
    
    /* Shipment Details Modal */
    .shipment-details-header {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        border: none;
        padding: 25px 30px 15px;
        border-radius: 20px 20px 0 0;
    }
    
    .shipment-details-body {
        padding: 40px 45px;
    }
    
    .shipment-info-row {
        display: flex;
        justify-content: space-between;
        margin-bottom: 25px;
        padding: 20px;
        background: #f8fafc;
        border-radius: 12px;
        box-shadow: 0 2px 10px rgba(0,0,0,0.05);
    }
    
    .shipment-info-item {
        flex: 1;
        text-align: center;
        padding: 0 15px;
    }
    
    .shipment-info-label {
        font-weight: 600;
        color: #64748b;
        font-size: 14px;
        margin-bottom: 5px;
    }
    
    .shipment-info-value {
        font-weight: 700;
        font-size: 18px;
        color: var(--dark);
    }
    
    .shipment-details-table {
        width: 100%;
        border-collapse: separate;
        border-spacing: 0 8px;
        margin-top: 20px;
        min-width: 1400px;
    }
    
    .shipment-details-table th {
        background: #f1f5f9;
        padding: 16px 12px;
        text-align: center;
        font-weight: 700;
        color: var(--dark);
        border: none;
        position: relative;
        white-space: nowrap;
    }
    
    .shipment-details-table th:first-child {
        border-top-left-radius: 10px;
        border-bottom-left-radius: 10px;
    }
    
    .shipment-details-table th:last-child {
        border-top-right-radius: 10px;
        border-bottom-right-radius: 10px;
    }
    
    .shipment-details-table td {
        padding: 14px 12px;
        text-align: center;
        border: none;
        background: white;
        box-shadow: 0 2px 5px rgba(0,0,0,0.05);
        white-space: nowrap;
    }
    
    .shipment-details-table tr td:first-child {
        border-top-left-radius: 10px;
        border-bottom-left-radius: 10px;
    }
    
    .shipment-details-table tr td:last-child {
        border-top-right-radius: 10px;
        border-bottom-right-radius: 10px;
    }
    
    /* Shipment Totals Section */
    .shipment-totals {
        margin-top: 25px;
        padding: 25px;
        background: linear-gradient(135deg, #f8fafc, #e2e8f0);
        border-radius: 12px;
        box-shadow: 0 4px 6px rgba(0,0,0,0.05);
        border-left: 4px solid #4361ee;
    }
    
    .shipment-totals-row {
        display: flex;
        flex-wrap: wrap;
        gap: 20px;
        justify-content: space-between;
    }
    
    .shipment-total-item {
        flex: 1;
        min-width: 150px;
        text-align: center;
        padding: 15px;
        background: white;
        border-radius: 10px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.08);
    }
    
    .shipment-total-label {
        font-weight: 600;
        color: #64748b;
        font-size: 12px;
        margin-bottom: 8px;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }
    
    .shipment-total-value {
        font-weight: 800;
        font-size: 20px;
        color: #1e293b;
        line-height: 1.2;
    }
    
    .total-amount {
        color: #059669 !important;
        background: linear-gradient(135deg, #d1fae5, #a7f3d0);
        padding: 10px;
        border-radius: 8px;
        border: 2px solid #10b981;
    }
    
    .total-quantity {
        color: #0369a1 !important;
        background: linear-gradient(135deg, #e0f2fe, #bae6fd);
        padding: 10px;
        border-radius: 8px;
        border: 2px solid #0ea5e9;
    }
    
    .total-weight {
        color: #7c3aed !important;
        background: linear-gradient(135deg, #f3e8ff, #e9d5ff);
        padding: 10px;
        border-radius: 8px;
        border: 2px solid #8b5cf6;
    }
    
    .total-cbm {
        color: #dc2626 !important;
        background: linear-gradient(135deg, #fecaca, #fca5a5);
        padding: 10px;
        border-radius: 8px;
        border: 2px solid #ef4444;
    }
    
    .total-ctn {
        color: #d97706 !important;
        background: linear-gradient(135deg, #fef3c7, #fde68a);
        padding: 10px;
        border-radius: 8px;
        border: 2px solid #f59e0b;
    }
    
    .total-ttl-cbm {
        color: #7c3aed !important;
        background: linear-gradient(135deg, #e9d5ff, #c4b5fd);
        padding: 10px;
        border-radius: 8px;
        border: 2px solid #8b5cf6;
    }
    
    .total-ttl-kg {
        color: #0d9488 !important;
        background: linear-gradient(135deg, #ccfbf1, #99f6e4);
        padding: 10px;
        border-radius: 8px;
        border: 2px solid #14b8a6;
    }
    
    /* Animation for loading */
    @keyframes pulse {
        0% { opacity: 1; }
        50% { opacity: 0.5; }
        100% { opacity: 1; }
    }
    
    .loading {
        animation: pulse 1.5s infinite;
    }

    /* Status Filter Styles */
    .status-filter-container {
        position: relative;
    }

    .status-filter-select {
        cursor: pointer;
        appearance: none;
        -webkit-appearance: none;
        -moz-appearance: none;
        background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
        background-repeat: no-repeat;
        background-position: right 12px center;
        background-size: 16px;
        padding-right: 40px !important;
    }

    /* Shipment row styling for filtering */
    .shipment-row {
        transition: all 0.3s ease;
    }

    .shipment-row.hidden {
        display: none !important;
    }

    .shipment-row.visible {
        display: table-row;
    }
</style>

<div class="container-fluid py-4">

    {{-- SUCCESS / ERROR MESSAGES --}}
    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <i class="bi bi-check-circle-fill me-2"></i>
            {{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    @endif

    @if(session('error'))
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            <i class="bi bi-exclamation-triangle-fill me-2"></i>
            {{ session('error') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    @endif

    <!-- ============================= -->
    <!--    SEARCH BAR AND ADD BUTTON  -->
    <!-- ============================= -->
    <div class="search-shipment-bar">
        <span class="search-icon">🔍</span>
        <input type="text" id="searchInput" placeholder="Search Shipments...">
        <div class="status-filter-container">
            <select id="statusFilter" class="status-filter-select">
                <option value="all">All Status</option>
                <option value="pending">Pending</option>
                <option value="in_transit">In Transit</option>
                <option value="dispatched">Dispatched</option>
                <option value="delivered">Delivered</option>
            </select>
        </div>
        <select id="carrierFilter">
            <option value="all">All Carriers</option>
            <option value="fedex">FedEx</option>
            <option value="ups">UPS</option>
            <option value="dhl">DHL</option>
        </select>
        <button type="button" class="btn-add-shipment" data-bs-toggle="modal" data-bs-target="#createShipmentModal">
            <span class="truck-icon">🚚</span>
            Add Shipments
        </button>
    </div>

    <!-- ============================= -->
    <!--   CREATE SHIPMENT MODAL -->
    <!-- ============================= -->
    <div class="modal fade" id="createShipmentModal" tabindex="-1" aria-labelledby="createShipmentModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-xl">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="createShipmentModalLabel">
                        <i class="bi bi-plus-circle me-2"></i>Create New Shipment
                    </h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <form action="{{ route('admin.shipments.store') }}" method="POST">
                        @csrf

                        <div class="row g-3 pb-2">
                            <div class="col-md-4">
                                <label class="form-label">Origin *</label>
                                <input type="text" class="form-control" name="origin" placeholder="Enter origin city" required>
                            </div>
                            <div class="col-md-4">
                                <label class="form-label">Destination *</label>
                                <input type="text" class="form-control" name="destination" placeholder="Enter destination city" required>
                            </div>
                            <div class="col-md-4">
                                <label class="form-label">Shipment Date *</label>
                                <input type="date" 
                                       name="shipment_date" 
                                       class="form-control"
                                       value="{{ date('Y-m-d') }}"
                                       min="{{ date('Y-m-d') }}"
                                       required>
                            </div>
                        </div>

                        <div class="table-responsive mt-4" style="max-height:400px; border-radius:12px;">
                            <table class="custom-table-modal">
                                <thead>
                                    <tr>
                                        <th></th>
                                        <th>Order ID</th>
                                        <th>Origin</th>
                                        <th>Destination</th>
                                        <th>CTN</th>
                                        <th>QTY</th>
                                        <th>TTL/QTY</th>
                                        <th>CBM</th>
                                        <th>TTL CBM</th>
                                        <th>KG</th>
                                        <th>TTL KG</th>
                                        <th>Amount (₹)</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @forelse($availableOrders as $order)
                                    <tr>
                                        <td>
                                            <input type="checkbox" name="order_ids[]" value="{{ $order->id }}">
                                        </td>
                                        <td>
                                            <a href="#" class="text-primary fw-bold">{{ $order->order_id }}</a>
                                        </td>
                                        <td>{{ $order->origin }}</td>
                                        <td>{{ $order->destination }}</td>
                                        <td>{{ $order->ctn }}</td>
                                        <td>{{ $order->qty }}</td>
                                        <td>{{ $order->ttl_qty }}</td>
                                        <td>{{ $order->cbm }}</td>
                                        <td>{{ $order->ttl_cbm }}</td>
                                        <td>{{ $order->kg }}</td>
                                        <td>{{ $order->ttl_kg }}</td>
                                        <td class="fw-bold text-success">₹{{ number_format($order->ttl_amount, 2) }}</td>
                                    </tr>
                                    @empty
                                    <tr>
                                        <td colspan="12" class="text-muted text-center py-4">No available orders to add to shipment</td>
                                    </tr>
                                    @endforelse
                                </tbody>
                            </table>
                        </div>
                        <div class="d-flex justify-content-end mt-4 mb-2">
                            <button type="button" class="btn-cancel me-3" data-bs-dismiss="modal">Cancel</button>
                            <button type="submit" class="btn-create">
                                <i class="bi bi-check-lg me-2"></i>Create Shipment
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!-- ============================= -->
    <!--      SHIPMENT LIST TABLE     -->
    <!-- ============================= -->
    <div class="card">
        <div class="card-header">
            <h5 class="mb-0"><i class="bi bi-truck me-2"></i> Shipments List</h5>
        </div>

        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Shipment ID</th>
                            <th>Origin</th>
                            <th>Destination</th>
                            <th>Total QTY</th>
                            <th>Total KG</th>
                            <th>Total CBM</th>
                            <th>Total Amount</th>
                            <th>Status</th>
                            <th>Date</th>
                            <th>Action</th>
                        </tr>
                    </thead>

                    <tbody id="shipmentsTableBody">
                        @php
                            $totalShipments = count($shipments);
                        @endphp
                        @forelse($shipments as $ship)
                        <tr class="shipment-row" data-status="{{ $ship->status }}" data-shipment-id="{{ $ship->shipment_id }}">
                            {{-- REVERSE INDEX: सर्वात वरच्या shipment ला सर्वात मोठा क्रमांक --}}
                            <td class="fw-bold">{{ $totalShipments - $loop->index }}</td>
                            <td>
                                <a href="#" class="text-primary fw-bold"
                                   onclick="openShipmentDetails({{ $ship->id }})">
                                    {{ $ship->shipment_id }}
                                </a>
                            </td>
                            <td>{{ $ship->origin }}</td>
                            <td>{{ $ship->destination }}</td>
                            <td><span class="badge bg-light text-dark">{{ $ship->total_qty }}</span></td>
                            <td><span class="badge bg-light text-dark">{{ $ship->total_kg }} kg</span></td>
                            <td><span class="badge bg-light text-dark">{{ $ship->total_cbm }} CBM</span></td>
                            <td class="fw-bold text-success">₹{{ number_format($ship->total_amount, 2) }}</td>
                            <td>
                                <span class="badge badge-{{ $ship->status }}">
                                    {{ ucfirst(str_replace('_', ' ', $ship->status)) }}
                                </span>
                            </td>
                            <td>{{ \Carbon\Carbon::parse($ship->shipment_date)->format('d M Y') }}</td>
                            <td>
                                <div class="action-container">
                                    <button type="button" class="btn-edit-status" onclick="toggleStatusDropdown(this, {{ $ship->id }})" title="Edit Status">
                                        <i class="bi bi-pencil"></i>
                                    </button>
                                    <div class="status-dropdown" id="statusDropdown-{{ $ship->id }}">
                                        <form action="{{ route('admin.shipments.updateStatus') }}" method="POST" class="status-form">
                                            @csrf
                                            <input type="hidden" name="shipment_id" value="{{ $ship->id }}">
                                            <button type="submit" name="status" value="pending" class="status-option pending">
                                                <span class="status-indicator pending"></span>
                                                Pending
                                            </button>
                                            <button type="submit" name="status" value="in_transit" class="status-option in_transit">
                                                <span class="status-indicator in_transit"></span>
                                                In Transit
                                            </button>
                                            <button type="submit" name="status" value="dispatched" class="status-option dispatched">
                                                <span class="status-indicator dispatched"></span>
                                                Dispatched
                                            </button>
                                            <button type="submit" name="status" value="delivered" class="status-option delivered">
                                                <span class="status-indicator delivered"></span>
                                                Delivered
                                            </button>
                                        </form>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="11" class="text-center py-5 text-muted">
                                <i class="bi bi-inbox display-4 d-block mb-3"></i>
                                No shipments found
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- ============================= -->
    <!--   SHIPMENT DETAILS MODAL     -->
    <!-- ============================= -->
    <div class="modal fade" id="shipmentDetailsModal" tabindex="-1">
        <div class="modal-dialog modal-xl modal-dialog-scrollable">
            <div class="modal-content">
                <div class="modal-header shipment-details-header">
                    <h5 class="modal-title fw-bold"><i class="bi bi-box-seam me-2"></i>Consolidated Shipment Details</h5>
                    <button class="btn-close" data-bs-dismiss="modal"></button>
                </div>

                <div class="modal-body shipment-details-body" id="shipmentDetailsContent">
                    <div class="text-center py-4 loading">
                        <div class="spinner-border text-primary" role="status">
                            <span class="visually-hidden">Loading...</span>
                        </div>
                        <p class="mt-2 text-muted">Loading shipment details...</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

</div>

<!-- ========================= -->
<!--  MODAL LOAD SCRIPT (AJAX) -->
<!-- ========================= -->
<script>
// Status Filter Functionality
document.addEventListener('DOMContentLoaded', function() {
    const statusFilter = document.getElementById('statusFilter');
    const searchInput = document.getElementById('searchInput');
    const carrierFilter = document.getElementById('carrierFilter');
    const shipmentRows = document.querySelectorAll('.shipment-row');

    // Function to filter shipments
    function filterShipments() {
        const selectedStatus = statusFilter.value;
        const searchTerm = searchInput.value.toLowerCase();
        const selectedCarrier = carrierFilter.value;

        shipmentRows.forEach(row => {
            const status = row.getAttribute('data-status');
            const shipmentId = row.getAttribute('data-shipment-id').toLowerCase();
            const origin = row.cells[2].textContent.toLowerCase();
            const destination = row.cells[3].textContent.toLowerCase();
            
            let statusMatch = selectedStatus === 'all' || status === selectedStatus;
            let searchMatch = searchTerm === '' || 
                            shipmentId.includes(searchTerm) || 
                            origin.includes(searchTerm) || 
                            destination.includes(searchTerm);
            let carrierMatch = selectedCarrier === 'all'; // You can add carrier data attribute if needed

            if (statusMatch && searchMatch && carrierMatch) {
                row.classList.remove('hidden');
                row.classList.add('visible');
            } else {
                row.classList.add('hidden');
                row.classList.remove('visible');
            }
        });

        // Show message if no results
        const visibleRows = document.querySelectorAll('.shipment-row:not(.hidden)');
        const noResultsRow = document.querySelector('.shipment-row[colspan]');
        
        if (visibleRows.length === 0 && !noResultsRow) {
            // Add no results message
            const tbody = document.getElementById('shipmentsTableBody');
            const noResultsHtml = `
                <tr>
                    <td colspan="11" class="text-center py-5 text-muted">
                        <i class="bi bi-search display-4 d-block mb-3"></i>
                        No shipments found matching your criteria
                    </td>
                </tr>
            `;
            tbody.innerHTML = noResultsHtml;
        } else if (visibleRows.length > 0 && noResultsRow) {
            // Remove no results message
            noResultsRow.remove();
        }
    }

    // Event listeners for filters
    statusFilter.addEventListener('change', filterShipments);
    searchInput.addEventListener('input', filterShipments);
    carrierFilter.addEventListener('change', filterShipments);

    // Initialize filter on page load
    filterShipments();
});

function openShipmentDetails(id) {
    let modal = new bootstrap.Modal(document.getElementById('shipmentDetailsModal'));
    let content = document.getElementById('shipmentDetailsContent');
    
    content.innerHTML = `
        <div class="text-center py-4 loading">
            <div class="spinner-border text-primary" role="status">
                <span class="visually-hidden">Loading...</span>
            </div>
            <p class="mt-2 text-muted">Loading shipment details...</p>
        </div>
    `;

    fetch(`/admin/shipments/${id}`)
        .then(res => {
            if (!res.ok) throw new Error('Network response was not ok');
            return res.json();
        })
        .then(data => {
            // Format date properly
            const shipmentDate = new Date(data.shipment.shipment_date);
            const formattedDate = shipmentDate.toLocaleDateString('en-GB', {
                day: '2-digit',
                month: 'short',
                year: 'numeric'
            });

            let html = `
                <!-- Shipment Basic Info -->
                <div class="shipment-info-row">
                    <div class="shipment-info-item">
                        <div class="shipment-info-label">Shipment ID</div>
                        <div class="shipment-info-value">${data.shipment.shipment_id}</div>
                    </div>
                    <div class="shipment-info-item">
                        <div class="shipment-info-label">Total Orders</div>
                        <div class="shipment-info-value">${data.orders.length}</div>
                    </div>
                    <div class="shipment-info-item">
                        <div class="shipment-info-label">Status</div>
                        <div class="shipment-info-value">${data.shipment.status}</div>
                    </div>
                    <div class="shipment-info-item">
                        <div class="shipment-info-label">Date</div>
                        <div class="shipment-info-value">${formattedDate}</div>
                    </div>
                </div>

                <h6 class="fw-bold mt-4 mb-3">Detailed view of all orders in this shipment consolidation</h6>

                <div class="table-responsive">
                    <table class="shipment-details-table">
                        <thead>
                            <tr>
                                <th>Order ID</th>
                                <th>Origin</th>
                                <th>Destination</th>
                                <th>Item No</th>
                                <th>Description</th>
                                <th>CTN</th>
                                <th>QTY</th>
                                <th>TTL/QTY</th>
                                <th>CBM</th>
                                <th>TTL CBM</th>
                                <th>KG</th>
                                <th>TTL KG</th>
                                <th>Amount (₹)</th>
                            </tr>
                        </thead>
                        <tbody>
            `;

            data.orders.forEach(order => {
                html += `
                    <tr>
                        <td class="fw-bold text-primary">${order.order_id}</td>
                        <td>${order.origin || 'N/A'}</td>
                        <td>${order.destination || 'N/A'}</td>
                        <td>${order.mark_no || 'ITEM001'}</td>
                        <td>${order.description || 'Manufacturing Equipment'}</td>
                        <td>${order.ctn}</td>
                        <td>${order.qty}</td>
                        <td>${order.ttl_qty}</td>
                        <td>${order.cbm || '0.00'}</td>
                        <td>${order.ttl_cbm || '0.00'}</td>
                        <td>${order.kg || '0.00'}</td>
                        <td>${order.ttl_kg || '0.00'}</td>
                        <td class="fw-bold text-success">₹${parseFloat(order.ttl_amount).toLocaleString('en-IN', {minimumFractionDigits: 2, maximumFractionDigits: 2})}</td>
                    </tr>
                `;
            });

            html += `
                        </tbody>
                    </table>
                </div>

                <!-- Totals Section -->
                <div class="shipment-totals">
                    <div class="shipment-totals-row">
                        <div class="shipment-total-item">
                            <div class="shipment-total-label">Total CTN</div>
                            <div class="shipment-total-value total-ctn">${data.shipment.total_ctn}</div>
                        </div>
                        <div class="shipment-total-item">
                            <div class="shipment-total-label">Total QTY</div>
                            <div class="shipment-total-value total-quantity">${data.shipment.total_qty}</div>
                        </div>
                        <div class="shipment-total-item">
                            <div class="shipment-total-label">Total CBM</div>
                            <div class="shipment-total-value total-cbm">${data.shipment.total_cbm}</div>
                        </div>
                        <div class="shipment-total-item">
                            <div class="shipment-total-label">Total KG</div>
                            <div class="shipment-total-value total-weight">${data.shipment.total_kg}</div>
                        </div>
                        <div class="shipment-total-item">
                            <div class="shipment-total-label">Total TTL/QTY</div>
                            <div class="shipment-total-value total-quantity">${data.shipment.total_ttl_qty}</div>
                        </div>
                        <div class="shipment-total-item">
                            <div class="shipment-total-label">Total TTL/CBM</div>
                            <div class="shipment-total-value total-ttl-cbm">${data.shipment.total_ttl_cbm}</div>
                        </div>
                        <div class="shipment-total-item">
                            <div class="shipment-total-label">Total TTL/KG</div>
                            <div class="shipment-total-value total-ttl-kg">${data.shipment.total_ttl_kg}</div>
                        </div>
                        <div class="shipment-total-item">
                            <div class="shipment-total-label">Total Amount</div>
                            <div class="shipment-total-value total-amount">₹${parseFloat(data.shipment.total_amount).toLocaleString('en-IN', {minimumFractionDigits: 2, maximumFractionDigits: 2})}</div>
                        </div>
                    </div>
                </div>
            `;

            content.innerHTML = html;
        })
        .catch(error => {
            console.error('Error loading shipment details:', error);
            content.innerHTML = `
                <div class="text-center py-5">
                    <i class="bi bi-exclamation-triangle display-1 text-danger"></i>
                    <h4 class="mt-3 text-danger">Error Loading Shipment Details</h4>
                    <p class="text-muted">Please try again later</p>
                    <button class="btn btn-primary mt-2" onclick="openShipmentDetails(${id})">Retry</button>
                </div>
            `;
        });

    modal.show();
}

// Toggle status dropdown
function toggleStatusDropdown(button, shipmentId) {
    const dropdown = document.getElementById(`statusDropdown-${shipmentId}`);
    const isShowing = dropdown.classList.contains('show');
    
    // Close all other dropdowns
    document.querySelectorAll('.status-dropdown.show').forEach(dropdown => {
        dropdown.classList.remove('show');
    });
    
    // Toggle current dropdown
    if (!isShowing) {
        dropdown.classList.add('show');
    }
    
    // Close dropdown when clicking outside
    document.addEventListener('click', function closeDropdown(e) {
        if (!button.contains(e.target) && !dropdown.contains(e.target)) {
            dropdown.classList.remove('show');
            document.removeEventListener('click', closeDropdown);
        }
    });
}

// Auto-close dropdown after form submission
document.addEventListener('DOMContentLoaded', function() {
    document.querySelectorAll('.status-form').forEach(form => {
        form.addEventListener('submit', function() {
            const dropdown = this.closest('.status-dropdown');
            if (dropdown) {
                dropdown.classList.remove('show');
            }
        });
    });
});
</script>

@endsection