@extends('layouts.agent.app')

@section('content')

<main id="main" class="main">
    <div class="container">
        <form action="{{url('/agent/assignorder/list/search')}}/{{encrypt($user->id)}}" method="post">
            @csrf
            <table class="table" styla="margin-top:10%;">
                <thead>
                    <tr>
                        <th>From Date</th>
                        <th>To Date</th>
                        <th>Select the Status</th>
                        <th></th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><input type="date" name="fromDate" id="FromDate" class="form-control" required /></td>
                        <td><input type="date" name="toDate" id="toDate" class="form-control" required/></td>
                        <td>
                            <select name="status" id="status" class="form-control" required>
                                <option value="" selected>SELECT STATUS</option>
                                <option value="P">PENDING</option>
                                <option value="PC">PROCESSED</option>
                                <option value="S">SHIPPED</option>
                                <option value="D">DELIVERED</option>
                                <option value="H">HOLD</option>
                                <option value="CL">CANCELED</option>
                            </select>
                        </td>
                        <td><button class="btn btn-primary" type="submit">Search</button></td>
                        <td><a href="{{url('/agent/assignorder/generateReport')}}" class="btn btn-primary" >Generate Report</a></td>
                    </tr>
                </tbody>
            </table>
        </form>
        <div class="card" style="width: 100%;">
            <div class="card-body">
                @if($agent_assigned->isEmpty())
                    No Data Found
                @else
                    <h5 class="card-title" style="text-align:center;text-decoration:underline;">AGENT PANEL</h5>
                    <h6 class="card-subtitle mb-2 text-body-secondary">{{$user->name}}</h6>
                    <p class="card-text"><input id="myInput" type="text" placeholder="Search orders" class="form-control"></p>
                    <table class="table">
                        <thead>
                            <tr>
                                <th>Sr No</th>
                                <th>Assign Order Id</th>
                                {{--<th>Order Id</th>--}}               
                                <th>Products</th>
                                <th>Delivery Date</th>
                                <th>Status</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody id="myTable">
                            @php $i = 0; @endphp
                            @foreach($agent_assigned as $agent_assigneds)
                                @php $i++; @endphp
                                <tr>
                                    <th scope="row">{{$i}}</th>
                                    <td>{{$agent_assigneds->agent_order_id}}</td>
                                    {{--<td>
                                        @php
                                            $orderId = null;
                                            $processedOrders = [];
                                        @endphp
                                        <!-- @foreach($assined as $assineds) -->
                                        @foreach($orders1 as $order)
                                            <!-- @php
                                                $string = $assineds->agent_order_id;
                                                $uid = substr($string, 0, -2);
                                            @endphp -->
                                            <!-- {{$order->id}} -->
                                            @if($agent_assigneds->OID == $order->id && $agent_assigneds->agent_id == $order->agent_id)
                                                @php
                                                    $duplicate_string = $order->unique_order_id;
                                                @endphp
                                                @if(!in_array($duplicate_string, $processedOrders))  
                                                    {{$order->unique_order_id}}

                                                    @php
                                                        $orderId = $order->id;
                                                        $processedOrders[] = $duplicate_string; 
                                                        break;                                                   
                                                    @endphp
                                                    <!-- break 2; -->
                                                @endif
                                            @endif
                                        @endforeach
                                        <!-- @endforeach -->
                                    </td>--}}
                                    <td>
                                        @php
                                            $printedProducts = [];
                                        @endphp
                                        @foreach($product_maped as $key)
                                            @foreach($key as $product_mapeds)
                                                @if($product_mapeds->agent_assinged_id == $agent_assigneds->id)
                                                    @foreach($agent_product_list as $product)
                                                        @if($product->id == $product_mapeds->product_id && !in_array($product->id, $printedProducts))
                                                            {{ $product->item }}<br>
                                                            @php
                                                                $printedProducts[] = $product->id; 
                                                            @endphp
                                                        @endif
                                                    @endforeach
                                                @endif
                                            @endforeach
                                        @endforeach
                                    </td>
                                    <td>
                                        @php
                                            $founddetails = false;
                                        @endphp
                                        @foreach($order_details as $order_detail)
                                            @if($order_detail->order_id == $agent_assigneds->OID)
                                                {{$order_detail->delivery_date}}
                                                @php
                                                    $founddetails = true;
                                                    break;
                                                @endphp
                                            @endif
                                        @endforeach
                                    </td>
                                    {{--<td>
                                        @if($agent_assigneds->status=='P')
                                            <div class="alert alert-danger" role="alert" style="width: 80%;text-align: center;">PENDING</div>
                                            @elseif($agent_assigneds->status=='PC')
                                            <div class="alert alert-secondary" role="alert" style="width: 80%;text-align: center;">PROCESSED</div>
                                            @elseif($agent_assigneds->status=='CL')
                                            <div class="alert alert-info" role="alert" style="width: 80%;text-align: center;">CANCEL</div>
                                            @elseif($agent_assigneds->status=='H')
                                            <div class="alert alert-dark" role="alert" style="width: 80%;text-align: center;">HOLD</div>
                                            @elseif($agent_assigneds->status=='D')
                                            <div class="alert alert-success" role="alert" style="width: 80%;text-align: center;">DELIVERED</div>
                                            @elseif($agent_assigneds->status=='S')
                                            <div class="alert alert-success" role="alert" style="width: 80%;text-align: center;">SHIPPED</div>
                                        @endif
                                    </td>--}}
                                    <td>
                                        @if($agent_assigneds->status=='P')
                                            <h6><span class="badge bg-danger">PENDING</span></h6>
                                        @elseif($agent_assigneds->status=='PC')
                                            <h6><span class="badge bg-info">PROCESSED</span></h6>
                                        @elseif($agent_assigneds->status=='CL')
                                            <h6><span class="badge bg-dark">CANCEL</span></h6>
                                        @elseif($agent_assigneds->status=='H')
                                            <h6><span class="badge bg-warning">HOLD</span></h6>
                                        @elseif($agent_assigneds->status=='D')
                                            <h6><span class="badge bg-success">DELIVERED</span></h6>
                                        @elseif($agent_assigneds->status=='S')
                                            <h6><span class="badge bg-primary">SHIPPED</span></h6>
                                        @endif
                                    </td>
                                    {{--<td>
                                        @if($agent_assigneds->status == "PC" ||$agent_assigneds->status == "CL" ||$agent_assigneds->status == "D" ||$agent_assigneds->status == "S")
                                        <div class="alert alert-primary" role="alert">
                                            Agent Assigned
                                        </div>
                                        @else
                                        <div class="alert alert-danger" role="alert">
                                            Agent Not Assigned Yet
                                        </div>
                                        @endif
                                    </td>--}}
                                    <td>
                                        <a href="{{url('/agent/assignorder/assign-order-view')}}/{{encrypt($agent_assigneds->agent_order_id)}}/{{encrypt($agent_assigneds->agent_id)}}" class="btn btn-dark btn-sm"><i class="ri-file-edit-fill"></i></a>
                                    </td>
                                </tr>
                            @endforeach
                        <tbody>
                    </table>
                @endif
            </div>
        </div>
    </div>
    {{--<a class="btn btn-danger" href="{{url('/agent/dashboard')}}">Back</a>--}}
</main>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>

<script>
    $(document).ready(function(){
        $("#myInput").on("keyup", function() {
            var value = $(this).val().toLowerCase();
            $("#myTable tr").filter(function() {
            $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
            });
        });
    });
</script>

@endsection