@extends('layouts.admin-layout')
@section('title')
    User Orders
@endsection
@section('content')
    <div class="content-wrapper">

        <section class="content">
            <div class="container-fluid">

                <div class="row">
                    <!-- left column -->
                    <div class="col-md-12 mt-5">

                        <div class="card table-shadow">

                            <div class="card-body">
                                <h2>User Orders</h2>
                                <table id="example" class="table table-striped table-bordered" style="width:100%">
                                    <thead>
                                        <tr>
                                            <th>User Name</th>
                                            <th>Total Price</th>
                                            <th>Tracking No</th>
                                            <th>Order Status</th>
                                            <th>Detail View</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>

                                    <tbody>
                                        @foreach ($get_orders as $order)
                                            <tr>
                                                <td>{{ $order->first_name ?? 'Guest' }}</td>
                                                <td>{{ $order->total_price ?: '' }}</td>
                                                <td>{{ $order->tracking_no ?: '' }}</td>
                                                <td>
                                                    <span
                                                        class="
                                                @if ($order->order_status == 'Pending') badge badge-pill badge-info
                                                @elseif($order->order_status == 'In-progress') badge badge-pill badge-primary
                                                  @elseif($order->order_status == 'Complete') badge badge-pill badge-success
                                                @elseif($order->order_status == 'Decline') badge badge-pill badge-danger @endif">
                                                        {{ $order->order_status }}
                                                    </span>

                                                </td>

                                                <td><a href="{{ route('admin.order.detail', ['id' => $order->id]) }}">Detail
                                                        View</a></td>
                                                <td>
                                                    <div class="dropdown custom-dropdown">
                                                        <button class="btn btn-info" type="button" data-toggle="dropdown"
                                                            aria-expanded="false">
                                                            Click Here
                                                            <i class="fas fa-ellipsis-h fa-lg ml-2"></i>
                                                        </button>
                                                        <div class="dropdown-menu">

                                                            <a class="dropdown-item" id='delete-btn'
                                                                data-id="{{ $order->id }}}">Delete</a>
                                                        </div>
                                                    </div>

                                                </td>
                                            </tr>
                                        @endforeach
                                    </tbody>

                                </table>

                            </div>

                            <!-- /.card -->
                        </div>
                    </div>

                </div>
            </div>
        </section>

    </div>
@endsection

@section('script')
    <script>
        $(document).ready(function() {
            $("table").DataTable({
                order: [0, 'dec']
            });
        });

        $(document).ready(function() {

            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });


            $(document).on("click", '#delete-btn', function(e) {
                var id = $(this).data('id');

                e.preventDefault();
                swal({
                        title: "Are you sure?",
                        text: "Once deleted, you will not be able to recover this imaginary file!",
                        icon: "warning",
                        buttons: true,
                        dangerMode: true,
                    })
                    .then((willDelete) => {
                        if (willDelete) {

                            $.ajax({
                                url: '{{ route('admin.order.delete') }}',
                                type: "post",
                                dataType: "json",
                                data: {
                                    id: id
                                },
                                success: function(response) {
                                    toastr.success(response.success);

                                    window.location.reload();
                                },
                                error: function(response) {
                                    toastr.error('Something went wrong!');
                                }
                            });
                        } else {}
                    });
            });

        });
    </script>
@endsection
