@extends('layouts.admin')
@section('content')
<div class="card shadow-sm p-3 mb-4">
    <form class="" action="{{ route('admin.making.orders.view') }}" id="sort_orders" method="GET">
        <div class="row gutters-5 my-2">
            <div class="col col-md-12 col-lg-4">
                <h5 class="mb-md-0 h6">Order Labels For Making / Packing</h5>
            </div>

            <div class="col-lg-2 col-md-6 ml-auto m-0 p-0">
                <div id="startDateDiv">
                    <label for="date_from" class="form-label small mb-1">From Date</label>
                    <input type="date" name="date_from" id="date_from" class="form-control" value="{{ $date_from }}" max="{{ date('Y-m-d') }}">
                </div>
            </div>
            <div class="col-lg-2 col-md-6">
                <div id="endDateDiv">
                    <label for="date_to" class="form-label small mb-1">To Date</label> 
                    <input type="date" name="date_to" id="date_to" class="form-control" value="{{ $date_to }}" max="{{ date('Y-m-d') }}">
                </div>
            </div>
            <div class="col-lg-4 col-md-12 row align-items-end mt-2">
                <div class="form-group mb-0 col-6 align-items-end m-0 p-0">
                    <input type="text" class="form-control" id="search"
                        name="search" @isset($sort_search) value="{{ $sort_search }}" @endisset
                        placeholder="Type Order code & hit Enter">
                </div>
                <div class="form-group mb-0 col-6 align-items-end">
                    <button type="submit" class="btn btn-primary">Filter</button>
                    <input type="hidden" name="status" value="{{ $status }}">
                    <a href="{{ route('admin.making.orders.pdf', [
                            'status' => $status,
                            'date_from' => $date_from,
                            'date_to' => $date_to,
                            'search' => $sort_search
                        ]) }}"
                    class="btn btn-info"
                    onclick="window.requestAnimationFrame(() => {
                        this.classList.add('btn-loading');
                        setTimeout(() => {
                            this.classList.remove('btn-loading');
                            location.reload();
                        }, 5000);
                    })">
                        Download
                    </a>
                </div>
               
            </div>      
        </div>
    </form>

    <!-- Order Status Filter Buttons -->
    
        <div class="status-filter-pills-container mb-2">
            @php
                $statusList = [
                    'placed' => 'Order Placed',
                    'confirmed' => 'Order Confirmed',
                    'packed' => 'Order Packed',
                    'awaiting_pickup' => 'Shipment Created',
                ];               
                $currentStatus = request('status') ?? 'placed';
            @endphp

            {{-- Loop through each status (skip empty key) --}}
            @foreach($statusList as $key => $label)
                @continue($key === '')
                @php
                    $color = match($key) {
                        'placed'     => 'purple',
                        'confirmed'  => 'blue',
                        'packed' => 'green',
                        'awaiting_pickup' => 'white',
                    };

                    $icon = match($key) {
                        'placed'     => 'cube',
                        'confirmed'  => 'cubes',
                        'packed' => 'box-archive',
                        'awaiting_pickup' => 'truck',
                    };

                    $isActive = $currentStatus === $key;
                    $count = $orderStatusCounts[$key] ?? 0;
                @endphp

                <a href="{{ route('admin.making.orders.view', [
                            'status' => $key,
                            'date_from' => $date_from,
                            'date_to' => $date_to,
                            'search' => $sort_search
                        ]) }}"
                    class="status-filter-pill pill-{{ $color }} {{ $isActive ? 'active' : '' }}">
                    <i class="fa-solid fa-{{$icon}}"></i> {{ $label }} ({{ $count }})
                </a>
            @endforeach
        </div>
        @if($status === 'placed')
            <div class="alert alert-warning" role="alert">
                <h5 class="fw-bold">
                    📝 Note
                </h5>
                <p>
                    Generate <strong>Making Labels</strong> only for orders currently in the 
                    <span class="badge badge-inline bg-secondary text-white">Order Placed</span> status.
                </p>
                <p>
                    Once the labels are generated and downloaded, the order status will automatically update from 
                    <span class="badge badge-inline bg-secondary text-white">Order Placed</span> to 
                    <span class="badge badge-inline bg-success text-white">Order Confirmed</span>.
                </p>
                <ul class="mb-2">
                    <li>Generate <strong>Making Labels</strong> for items in <em>Order Placed</em>.</li>
                    <li>Send the labels to the <strong>Making Team</strong> or <strong>Packing Team</strong>.</li>
                    <li>After packing, generate the <strong>Delivery Label</strong> and attach it to the package.</li>
                </ul>
                <p class="mb-0">
                    <em>This is the standard process to be followed.</em>
                </p>
            </div>
        @endif
    </div>
    <style>
        @page { margin: 15px; }
        body {
            font-family: DejaVu Sans, sans-serif;
            font-size: 16px;
        }
        .order-filter-btn {
            margin-right: 8px;
            margin-bottom: 8px;
        }
        table {
            width: 100%;
            border-collapse: separate;
            border-spacing: 10px 12px; /* adds visual gap between boxes */
        }
        td {
            width: 50%;
            border: 1px solid #000;
            vertical-align: top;
            padding: 10px;
            box-sizing: border-box;
            border-radius: 6px;
        }
        .label {
            font-size: 16px;
            line-height: 2;
        }
        .product-list {
            margin-top: 6px;
        }
        .product-item {
            padding-left: 8px;
            line-height: 2;
            font-size: 16px;
        }

        .btn-loading {
            position: relative;
            pointer-events: none;
            opacity: 0.8;
        }

        .btn-loading::after {
            content: "";
            position: absolute;
            top: 50%;
            left: 50%;
            width: 1.2rem;
            height: 1.2rem;
            border: 2px solid #fff;
            border-top-color: transparent;
            border-radius: 50%;
            animation: spin 0.8s linear infinite;
            transform: translate(-50%, -50%);
        }
        .status-filter-pills-container {
            display: flex;
            flex-wrap: wrap;
            gap: 0.75rem;
            align-items: center;
        }
        .status-filter-pill {
            display: inline-block;
            padding: 0.5rem 1rem;
            border-radius: 20px;
            font-size: 0.875rem;
            font-weight: 500;
            text-decoration: none;
            transition: all 0.2s ease;
            cursor: pointer;
            border: 1px solid #E5E7EB;
            white-space: nowrap;
            background-color: #ffffff;
            color: #1F2937;
        }
        .status-filter-pill:hover {
            background-color: #F9FAFB;
            border-color: #D1D5DB;
            color: #111827;
        }
        /* Only the selected pill gets colored - Purple when active */
        .status-filter-pill.pill-purple.active {
            background-color: #DDD6FE;
            border-color: #A78BFA;
            color: #581C87;
        }

        /* Only the selected pill gets colored - Blue when active */
        .status-filter-pill.pill-blue.active {
            background-color: #BFDBFE;
            border-color: #3B82F6;
            color: #1E3A8A;
        }
        .status-filter-pill.pill-green.active {
            background-color: #A7F3D0;
            border-color: #10B981;
            color: #047857;
        }
        .status-filter-pill.pill-white.active  {
            background-color: var(--admin-primary);
            border-color:2px solid var(--admin-primary);
            color: #ffffff;
            box-shadow:  #888888;
        }
        @keyframes spin {
            to { transform: translate(-50%, -50%) rotate(360deg); }
        }
    </style>
    <table>
        <tbody>
            @foreach ($orders->chunk(2) as $pair)
                <tr>
                    @foreach ($pair as $order)
                        <td>
                            <div class="label"><b>Order ID:</b> #{{ $order->id }}</div>
                            <div class="label"><b>Order Date:</b> {{ date('d-m-Y h:i A', $order->date) }}</div>
                            <div class="label"><b>Products:</b></div>
                            <div class="product-list">
                                @foreach ($order->items as $item)
                                    <div class="product-item">
                                        <span> • {{ $item->product?->name ?? '' }} </span>
                                        @if(!empty($item->product->tags))
                                         - <b>{{ $item->product->tags }} </b> 
                                        @endif
                                         - ( {{ $item->qty }} )
                                    </div>
                                @endforeach
                            </div>
                        </td>
                    @endforeach

                    @if ($pair->count() < 2)
                        <td></td>
                    @endif
                </tr>
            @endforeach
        </tbody>
    </table>
@endsection

@section('scripts')
<script>
    $(document).ready(function () {

        $('#making_orders_pdf').on('click', function () {

            var $btn = $(this);
            $btn.addClass('btn-loading').prop('disabled', true);

            var status = '{{ $status }}';
            var date_from = '{{ $date_from }}';
            var date_to = '{{ $date_to }}';
            var search = '{{ $sort_search }}';

            var url = "{{ route('admin.making.orders.pdf') }}" +
                "?status=" + encodeURIComponent(status) +
                "&date_from=" + encodeURIComponent(date_from) +
                "&date_to=" + encodeURIComponent(date_to) +
                "&search=" + encodeURIComponent(search);

            var iframe = document.createElement('iframe');
            iframe.style.display = 'none';
            iframe.src = url;
            document.body.appendChild(iframe);

            setTimeout(function () {
                $btn.removeClass('btn-loading').prop('disabled', false);
                location.reload();
            }, 4000);
        });

    });
</script>
@endsection
