@extends('layouts.admin.e-commerce.app')
@section('title', 'Order Information')
@section('content')

    @if (session('success'))
        <div class="alert alert-success">
            {{ session('success') }}
        </div>
    @endif
    @if (session('error'))
        <div class="alert alert-danger">
            {{ session('error') }}
        </div>
    @endif

    <section class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1>Order</h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item"><a href="{{ routeHelper('dashboard') }}">Home</a></li>
                        <li class="breadcrumb-item active">Order Information</li>
                    </ol>
                </div>
            </div>
        </div><!-- /.container-fluid -->
    </section>

        {{-- <!-- Main content -->
        <section class="content">
            <div class="container-fluid">
                <div class="card">
                    @include('admin.e-commerce.order.order_show_temp.header')
                </div>
                <div class="modal fade" id="refund" tabindex="-1" role="dialog"
                    aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
                    <div class="modal-dialog modal-dialog-centered" role="document">
                        <div class="modal-content">
                            <div class="modal-body">
                                <form method="post" action="{{ route('admin.refund') }}">
                                    @csrf
                                    <div class="form-group">
                                        <input type="hidden" name="order" value="{{ $order->id }}">
                                        <input class="form-control" type="text" placeholder="amount" name="amount">
                                    </div>
                                    <div class="form-group">
                                        <select class="form-control" name="method">
                                            <option value="wallate">Wallate</option>
                                            <option value="Bank">Bank</option>
                                            <option value="Bkash">Bkash</option>
                                            <option value="Nagad">Nagad</option>
                                            <option value="Rocket">Rocket</option>
                                            <option value="Cash">Cash</option>
                                        </select>
                                    </div>
                                    <button type="submit" class="btn btn-primary">Refund</button>
                                </form>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal fade" id="refund2" tabindex="-1" role="dialog"
                    aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
                    <div class="modal-dialog modal-dialog-centered" role="document">
                        <div class="modal-content">
                            <div class="modal-body">
                                <form method="post" action="{{ route('admin.refund_2') }}">
                                    @csrf
                                    <div class="form-group">
                                        <input type="hidden" name="order" value="{{ $order->id }}">
                                        <input class="form-control" type="text" placeholder="amount" name="amount">
                                    </div>
                                    <div class="form-group">
                                        <select class="form-control" name="method">
                                            <option value="wallate">Wallate</option>
                                            <option value="Bank">Bank</option>
                                            <option value="Bkash">Bkash</option>
                                            <option value="Nagad">Nagad</option>
                                            <option value="Rocket">Rocket</option>
                                            <option value="Cash">Cash</option>
                                        </select>
                                    </div>
    
                                    <button type="submit" class="btn btn-primary">Refund</button>
                                </form>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div><!-- /.container-fluid -->
        </section>
        <!-- /.content --> --}}

    @push('admin_top_nav')
        <li class="nav-item">
            <a href="{{ route('admin.order.index') }}" class="btn btn-danger"><i class="nav-icon fas fa-arrow-left"></i> Back</a>
        </li>
    @endpush
    <main class="container">
        <div class="row">
            <div class="col-7">
                <div class="card">
                    <form action="{{ route('admin.order.update.order_info', $order->id) }}" method="POST">
                        @csrf
                        <div class="card-header">
                            <h3 class="card-title">Order Information</h3>
                            &nbsp;&nbsp;<span id="order_edit" class="mt-2" style="cursor:pointer;"><i class="fas fa-edit"></i></span>
                        </div>
                        <div class="card-body">
                            <ul class="list-unstyled border-bottom pb-2">
                                <li><span class="text-muted">Order ID:</span> {{ $order->order_id }}</li>
                                <li><span class="text-muted">Invoice:</span> {{ $order->invoice }}</li>
                                <li><span class="text-muted">Order Date:</span> {{ $order->created_at->format('d-m-Y') }}</li>
                                <li><span class="text-muted">Updated Date:</span> <span class="text-muted">{{ $order->updated_at->format('d-m-Y') }}</span></li>
                                <li><span class="text-muted">Payment Method:</span> <span class="badge badge-dark">{{ $order->payment_method }}</span></li>
                                @if($order->pay_date)<li><span class="text-muted">Payment Date:</span> <span class="badge badge-dark">{{ $order->pay_date }}</span></li>@endif
                                @if ($order->status == 5)
                                    <li><span class="text-muted">Refund Method:</span> {{ $order->refund_method }}</li>
                                @endif
                            </ul>

                            <ul class="list-unstyled">
                                <li class="d-flex justify-content-between">
                                    <span class="text-muted">Subtotal</span><span><input name="order_subtotal" id="order_subtotal" type="text" value="{{ $order->subtotal }}" class="bg-white text-right" disabled></span>
                                    @error('order_subtotal')
                                        <span class="text-danger">{{ $message }}</span>
                                    @enderror
                                </li>
                                <li class="d-flex justify-content-between">
                                    <span class="text-muted">Shipping Charge</span><span><input name="order_shipping_charge" id="order_shipping_charge" type="text" value="{{ $order->shipping_charge }}" class="bg-white text-right" disabled></span>
                                    @error('order_shipping_charge')
                                        <span class="text-danger">{{ $message }}</span>
                                    @enderror
                                </li>
                                <li class="d-flex justify-content-between">
                                    <span class="text-muted">Discount (-) @if ($order->coupon_code)<small>Applied Coupon: <span class="badge badge-dark">{{ $order->coupon_code }}</span></small>@endif</span><span><input name="order_discount" id="order_discount" type="text" value="{{ $order->discount }}" class="bg-white text-right" disabled></span>
                                    @error('order_discount')
                                        <span class="text-danger">{{ $message }}</span>
                                    @enderror
                                </li>
                                <li class="d-flex justify-content-between" style="border-bottom: 1px dashed #ccc;">
                                    <span class="text-muted">Total</span><span class="font-weight-bold"><input name="order_total" id="order_total" type="text" value="{{ $order->total }}" class="bg-white text-right" disabled></span>
                                    @error('order_total')
                                        <span class="text-danger">{{ $message }}</span>
                                    @enderror
                                </li>
                                <li class="d-flex justify-content-between"><span class="text-muted">Partial Payment (-)</span>
                                    <span class="font-weight-medium">
                                        @php
                                            $part = App\Models\PartialPayment::where('order_id', $order->id)
                                                ->where('status', 1)
                                                ->sum('amount');
                                        @endphp
                                        {{ number_format((float)$part, 2, '.', '') }}
                                    </span>
                                </li>
                                <li class="d-flex justify-content-between"><span class="text-muted">Due (@if ($order->pay_staus == 1) <span class="text-success"><i class="fas fa-check"></i> Paid</span> @else <span class="text-danger"><i class="fas fa-times"></i> Unpaid</span> @endif)</span><span class="font-weight-bold @if ($order->pay_staus == 1) text-success @else text-danger @endif">{{ $order->total - $part }}</span></li>
                            </ul>
                            <div class="d-flex justify-content-between">
                                <span class="text-muted"><button id="order_submit" type="submit" class="btn btn-success mt-2 d-none">Update</button></span>
                            </div>
                        </div>
                        @if ($order->payment_method !== 'cod')
                            <div class="card-footer">
                                    <span class="text-muted">Payment Method</span>
                                    <span class="badge badge-dark">{{ $order->payment_method }}</span>,
                                @if ($order->payment_method == 'Bkash' || $order->payment_method == 'Nagad' || $order->payment_method == 'Rocket')
                                    <span class="text-muted">Mobile Number</span>
                                    <span class="badge badge-dark">{{ $order->mobile_number }}</span>
                                    <span class="text-muted">Transaction ID</span>
                                    <span class="badge badge-dark">{{ $order->transaction_id }}</span>
                                @elseif ($order->payment_method == 'Bank')
                                    <span class="text-muted ">Bank Name</span>
                                    <span class="badge badge-dark">{{ $order->bank_name }}</span>,
                                    <span class="text-muted">Account Number</span>
                                    <span class="badge badge-dark">{{ $order->account_number }}</span>,
                                    <span class="text-muted">Holder Name</span>
                                    <span class="badge badge-dark">{{ $order->holder_name }}</span>,
                                    <span class="text-muted">Branch Name</span>
                                    <span class="badge badge-dark">{{ $order->branch_name }}</span>,
                                    <span class="text-muted">Routing Number</span>
                                    <span class="badge badge-dark">{{ $order->routing_number }}</span>
                                @endif
                            </div>
                        @endif
                    </form>
                    @push('js')
                    <script>
                        document.getElementById('order_edit').addEventListener('click', function() {
                            let inputs = document.querySelectorAll('input[id^="order_"]');
                            inputs.forEach(function(input) {
                                input.disabled = false;
                                input.classList.remove('bg-white');
                                input.classList.add('border', 'border-primary');
                            });
                            document.getElementById('order_submit').classList.remove('d-none');
                        });
                    </script>
                    @endpush
                </div>
            </div>
            <div class="col-5">
                <div class="card">
                    <div class="card-header">
                        <h3 class="card-title">Order Action</h3>
                    </div>
                    <div class="card-body">
                        @include('admin.e-commerce.order.order_show._order_status')
                        @include('admin.e-commerce.order.order_show._order_courier_status')
                    </div>
                    <div class="card-footer d-flex justify-content-end">
                        <a target="_blank" href="{{ route('admin.order.invoice', $order->id) }}" class="badge badge-dark mr-1"><i class="fas fa-print"></i> Print</a>
                        <a href="{{ route('admin.order.delete', ['did' => $order->id]) }}" class="badge badge-danger" onclick="return confirm('Are you sure you want to delete this order?')"><i class="nav-icon fas fa-trash-alt"></i> Delete</a>
                    </div>
                </div>

                @if($order->courier_status == 1)
                    <div class="card">
                        <div class="card-header">
                            Courier Information <b class="badge badge-dark">{{ json_decode($order->courier_data)->courier_service }}</b>
                        </div>
                        <div class="card-body">
                            @foreach(array_reverse((array) json_decode($order->courier_data)) as $key => $value)
                                <span class="text-muted">{{ ucfirst(str_replace('_', ' ', $key)) }}:</span> <span class="badge badge-info">{{ $value }}</span>
                            @endforeach
                        </div>
                    </div>
                @endif

                <div class="card">
                    <div class="card-header">
                        <h3 class="card-title">Customer Information</h3>
                    </div>
                    <div class="card-body">
                        @include('admin.e-commerce.order.order_show._customer_data')
                    </div>
                </div>
            </div>
        </div>


        <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-header">
                        <h2 class="card-title">Order Products</h2>
                    </div>
                    <div class="card-body">
                        @php
                            $vendors = DB::table('multi_order')
                                ->where('order_id', $order->id)
                                ->get();
                        @endphp
                        <style>
                            .gx {
                                display: flex;
                                background: #6dca6d24;
                                padding: 5px;
                                border-radius: 5px;
                                margin-bottom: 10px;
                            }
    
                            .gx div {
                                flex: 1;
                            }
                        </style>
                        @foreach ($vendors as $key => $vendor)
                            @php($us = App\Models\User::find($vendor->vendor_id))
                            <div class="gx" style="">
                                <div>
                                    Seller:{{ $us->name ?? ""}}
                                </div>
                                <div>
                                    Total:{{ $vendor->total }}
                                </div>
                                <div>
                                    Payment:{{ $vendor->partial_pay }}
                                </div>
                                <div>
                                    Discount:{{ $vendor->discount }}
                                </div>
                                <div>
                                    Status (by vendor):
                                    @if ($vendor->status == 0)
                                        <span class="badge badge-warning">Pending</span>
                                    @elseif ($vendor->status == 1)
                                        <span class="badge badge-primary">Processing</span>
                                    @elseif ($vendor->status == 2)
                                        <span class="badge badge-danger">Canceled</span>
                                    @elseif ($vendor->status == 4)
                                        <span class="badge" style="background: #7db1b1;">Shipping</span>
                                    @elseif ($vendor->status == 5)
                                        <span class="badge badge-danger">Refund</span>
                                    @elseif ($order->status == 6)
                                        <span class="badge" style="background: #7db1b1;">Return Request By User</span>
                                    @elseif ($order->status == 7)
                                        <span class="badge" style="background: #7db1b1;">Return process accept by
                                            Owner</span>
                                    @elseif ($order->status == 8)
                                        <span class="badge" style="background: #7db1b1;">Returned</span>
                                    @elseif ($order->status == 3)
                                        <span class="badge badge-success">Delivered</span>
                                    @endif
                                </div>
    
                            </div>
                            <div>
                                <a
                                    href="{{ route('admin.order.subStatus', ['id' => $order->id, 'status' => 1, 'vendor' => $vendor->vendor_id]) }}">Processing</a>
                                <a
                                    href="{{ route('admin.order.subStatus', ['id' => $order->id, 'status' => 4, 'vendor' => $vendor->vendor_id]) }}">Shipping</a>
                                <a
                                    href="{{ route('admin.order.subStatus', ['id' => $order->id, 'status' => 2, 'vendor' => $vendor->vendor_id]) }}">Canceled</a>
                                <a
                                    href="{{ route('admin.order.subStatus', ['id' => $order->id, 'status' => 3, 'vendor' => $vendor->vendor_id]) }}">Delivered</a>
                            </div>
    
                            <div class="table-responsive">
                                <table class="table table-bordered table-hover">
                                    <thead>
                                        <tr>
                                            <th>SL</th>
                                            <th>Product</th>
                                            <th>Title</th>
                                            <th>Size</th>
                                            <th>Color</th>
                                            <th>Qty</th>
                                            <th>Price</th>
                                            <th>Subtotal</th>
                                        </tr>
                                    </thead>
                                    <tbody>
    
                                        @foreach ($order->orderDetails as $key => $item)

                                            @if (isset($item->product->user_id) && $item->product->user_id == $vendor->vendor_id)
                                                <tr>
                                                    <td>{{ $key + 1 }}</td>
                                                    <td>
                                                        {{-- <img src="{{ asset('uploads/product/' . $item->product->image) }}" --}}
                                                        <img src="{{ asset('uploads/product/' . ($item->color_image ? $item->color_image : $item->product->image)) }}"
                                                            alt="Product Image" width="80px" height="80px">
                                                    </td>
                                                    <td>
                                                        <a href="{{ route('admin.product.show', $item->product->id) }}"
                                                            target="_blank">{{ $item->title }}</a>
                                                    </td>
                                                    <td><?php
                                                    $data = json_decode($item->size);
                                                    if ($data != null && $data != '""' && $data != '[]' && $data != '"\"\""') {
                                                        foreach ($data as $key => $attr) {
                                                            $value = DB::table('attribute_values')->where('id', $attr)->first();
                                                            $name = DB::table('attributes')->where('slug', $key)->first();
                                                            if ($name) {
                                                                echo $vl = $name->name . ':' . $value->name . ', ';
                                                            }
                                                        }
                                                    }
                                                    ?></td>
                                                    <td> 
                                                        <?php
                                                            if ($item->color != 'blank') {
                                                                echo $item->color;
                                                            }
                                                        ?>
                                                    </td>
                                                    <td>{{ $item->qty }}</td>
                                                    <td>{{ $item->price }}</td>
                                                    <td>{{ $item->total_price }}</td>
                                                </tr>
                                            @endif
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                        @endforeach
                    </div>
                </div>
            </div>
        </div>
    </main>
@endsection
