@include('admin::layouts.header')
<?php
use App\Models\CustomerDetails;
use App\Models\User;
use App\Models\Services;

?>
<main id="main-container">
        <!-- Hero -->
        <div class="bg-body-light">
          <div class="content content-full">
            <div class="d-flex flex-column flex-sm-row justify-content-sm-between align-items-sm-center">
              <h1 class="flex-grow-1 fs-3 fw-semibold my-2 my-sm-3">Order Requests</h1>
              <nav class="flex-shrink-0 my-2 my-sm-0 ms-sm-3" aria-label="breadcrumb">
                <ol class="breadcrumb">
                  <li class="breadcrumb-item">Dashboard</li>
                  <li class="breadcrumb-item">Sales</li>
                  <li class="breadcrumb-item active" aria-current="page">Order Requests</li>
                </ol>
              </nav>
            </div>
          </div>
        </div>
        <!-- END Hero -->

        <!-- Page Content -->
        <div class="content">
          <!-- Your Block -->
          <div class="block block-rounded">
            <div class="block-header block-header-default">
              <h3 class="block-title">Order Requests</h3>
            </div>
            <div class="block-content">
            @if(Session::has('success')) <div class="alert alert-success mt-2 mb-2">{{ Session::get('success') }}</div>@endif
            @if(Session::has('fail')) <div class="alert alert-danger mt-2 mb-2">{{ Session::get('fail') }}</div>@endif
            <table  id="example" class="custom-datatable-no-export display responsive nowrap" style="width:100%">
                <thead>
                  <tr>
                  <th>Customer</th>
                  <th>Customer Phone</th>
                  <th>Customer Email</th>
                  <th>Order Date</th>
                  <th>Products</th>
                  <th>Status</th>
                  <th>Actions</th>
                  </tr>
                </thead>
                <tbody>
                <?php foreach($orders as $orders) {
                    $name = CustomerDetails::withTrashed()->where('user_id', $orders->customer_id)->value('customer_name');
                    $type = CustomerDetails::withTrashed()->where('user_id', $orders->customer_id)->value('customer_type');
                    $phone = CustomerDetails::withTrashed()->where('user_id', $orders->customer_id)->value('phone');
                    $email = User::withTrashed()->where('id', $orders->customer_id)->value('email');
                    $orderArray = $orders->products;
                ?>
                  <tr>

                    <td class="fw-semibold">
                      {{$name}}
                    </td>

                    <td class="fw-semibold">
                      {{$phone}}
                    </td>
                    <td class="fw-semibold">
                      {{$email}}
                    </td>
                    <td class="fw-semibold">
                      {{$orders->created_at}}
                    </td>
                    <td class="fw-semibold">
                   <?php 

                    foreach($orderArray as $key => $item){
                    $productId =$item;

                    // You can now access the product details if needed
                    $product = Services::withTrashed()->find($productId);
                
                    // Perform your logic here
                    echo "Name: $product->name<br>";
                    }
                    ?>
                    </td>
                    <td class="fw-semibold">
                    <?php if($orders->status == "pending") { ?>
                        <span class="badge bg-success">Pending</span>
                    <?php } else { ?>
                        <span class="badge bg-primary">Confirmed</span>
                    <?php } ?>
                    </td>
                    <td class="text-center">
                        <div class="btn-group">
         <!--<a href="{{ url('admin/convert-to-order/'.$orders->id.'') }}" ><button type="button" class="btn btn-sm btn-alt-success js-bs-tooltip-enabled" data-bs-toggle="tooltip" aria-label="Convert to order" data-bs-original-title="Convert to order">-->
         <!--                   <i class="fa fa-exchange"></i>-->
         <!--                 </button></a>-->
         <a href="{{ url('admin/confirm-order-request/'.$orders->id.'') }}" ><button type="button" class="btn btn-sm btn-alt-success js-bs-tooltip-enabled" data-bs-toggle="tooltip" aria-label="Convert to order" data-bs-original-title="Convert to order">
                            <i class="fa fa-check"></i>
                          </button></a>
                          <a href="{{ url('admin/delete-order-request/'.$orders->id.'') }}" ><button type="button" class="btn btn-sm btn-alt-danger js-bs-tooltip-enabled" data-bs-toggle="tooltip" aria-label="Delete" data-bs-original-title="Delete">
                            <i class="fa fa-trash"></i>
                          </button></a>
                       
                        </div>
                      </td>
                  </tr>
                <?php } ?>
                </tbody>
              </table>
            </div>
          </div>
          <!-- END Your Block -->
        </div>
        <!-- END Page Content -->
</main>
<!-- END Main Container -->
@include('admin::layouts.footer')