@extends('admin.layouts.container')

@section('content')
    <div class="side-title">
        <h3>{{ $head ?? '' }}</h3>
    </div>

    <div class="border-item">
        <form class="form-row" method="post" action="{{ $action }}">
            @if($order ?? ''  && $order->id)
                @method('PUT')
            @endif
            @CSRF

            <div class="form-group col-lg-12">
                <h3>{{__('orders.status')}}</h3>
            </div>
            <div class="form-group col-lg-6">
                <label>{{  __('orders.status') }}  </label>
                <select class="form-control {{ $errors->has('status') ? 'is-invalid' : '' }}" name="status">
                    @foreach($statuses as $row)
                        <option @if($row == $order->status) selected @endif value="{{$row}}">{{__('orders.'.$row)}}</option>
                    @endforeach
                </select>
            </div>

            <div class="text-center mt-5 col-6">
                <button class="butn"><span>{{ __('dashboard.save') }}</span></button>
            </div>
        </form>
        <hr>

        <form class="form-row" >
            <div class="form-group col-lg-12">
                <h3>{{__('orders.info')}}</h3>
            </div>
            <div class="form-group col-lg-6">
                <label>{{  __('orders.id') }}  </label>
                <input disabled class="form-control" value="{{ $order->id }}">
            </div>
            <div class="form-group col-lg-6">
                <label>{{  __('orders.date') }}  </label>
                <input disabled class="form-control" value="{{ date('Y-m-d',strtotime($order->created_at)) }}">
            </div>
        </form>
        <hr>
        <form class="form-row" >
            <div class="form-group col-lg-12">
                <h3>{{__('orders.payment')}}</h3>
            </div>
            @if($payment)

                <div class="form-group col-lg-6">
                    <label>{{  __('web.user_status_paid') }}  </label>
                    <input disabled class="form-control" value="{{ __('web.'.$payment->user_status_paid) }}">
                </div>
                <div class="form-group col-lg-6">
                    <label>{{  __('orders.payment_status') }}  </label>
                    <input disabled class="form-control" value="{{__('orders.'.$payment->status)}}">
                </div>
                @if($payment->installment_id)
                    <div class="form-group col-lg-6">
                        <label>{{  __('orders.installment_id') }}  </label>
                        <input disabled class="form-control" value="{{$payment->installment->descriptions()->name}}">
                    </div>
                @endif
                <div class="form-group col-lg-6">
                    <label>{{  __('orders.total_products') }}  </label>
                    <input disabled class="form-control" value="{{ $payment->total_products - $payment->tax_cost }}">
                </div>
                <div class="form-group col-lg-6">
                    <label>{{  __('orders.tax_cost') }}  </label>
                    <input disabled class="form-control" value="{{ $payment->tax_cost }}">
                </div>
                <div class="form-group col-lg-6">
                    <label>{{  __('orders.total_product_tax') }}  </label>
                    <input disabled class="form-control" value="{{ $payment->total_products }}">
                </div>
                @if( ($order->payment ? ($order->payment->cobon_status == 'birthday' ? 1 : 0) : ($order->payment->cobon_id ? 1 : 0 ) ) != 0)
                    <div class="form-group col-lg-6">
                        <label>{{  __('orders.cobon') }}  </label>
                        <input disabled class="form-control" value="{{$order->payment->cobon_status == 'birthday' ? __('web.cobon_birthday') : ($order->payment ? $order->payment->cobon->code : '')}}">
                    </div>
                    <div class="form-group col-lg-6">
                        <label>{{  __('orders.cobon_discount') }}  </label>
                        <input disabled class="form-control" value="{{ $payment->cobon_discount }}">
                    </div>
                    @if($payment->cobon_id)
                    @if($payment->cobon->seller_id)
                        <div class="form-group col-lg-6">
                            <label>{{  __('orders.seller_commission') }}  </label>
                            <input disabled class="form-control" value="{{ $payment->seller_commission }}">
                        </div>
                        <div class="form-group col-lg-6">
                            <label>{{  __('orders.total_commission') }}  </label>
                            <input disabled class="form-control" value="{{ $payment->total_commission }}">
                        </div>
                    @endif
                    @endif
                @endif
                @if($payment->shipping_cost)
                <div class="form-group col-lg-6">
                    <label>{{  __('orders.shipping_cost') }}  </label>
                    <input disabled class="form-control" value="{{ $payment->shipping_cost }}">
                </div>
                @endif
                @if($payment->on_hand_payment != 0)
                <div class="form-group col-lg-6">
                    <label>{{  __('orders.on_hand_payment') }}  </label>
                    <input disabled class="form-control" value="{{ $payment->on_hand_payment }}">
                </div>
                @endif
                <div class="form-group col-lg-6">
                    <label>{{  __('orders.total') }}  </label>
                    <input disabled class="form-control" value="{{ $payment->total }}">
                </div>
                <div class="form-group col-lg-12">
                    <label>{{  __('orders.note') }}  </label>
                    <textarea disabled class="form-control" >{{$order->note}}</textarea>
                </div>
            @endif
        </form>
        <hr>
        <form class="form-row" >
            <div class="form-group col-lg-12">
                <h3>{{__('orders.user_info')}}</h3>
            </div>
            <div class="form-group col-lg-6">
                <label>{{ ($order->user ? $order->user->role : 'user' ) == 'company' ? __('orders.company_name') : __('orders.user_name') }}  </label>
                <input disabled class="form-control" value="{{$order->user ? ($order->user->role == 'company' ? $order->user->descriptions()->name : $order->user->name) : ''}}">
            </div>
            <div class="form-group col-lg-6">
                <label>{{ ($order->user ? $order->user->role : 'user' ) == 'company' ? __('orders.company_email') : __('orders.user_email') }}   </label>
                <input disabled class="form-control" value="{{$order->user ? $order->user->email : ''}}">
            </div>
            @if(($order->user ? $order->user->role : 'user' ) != 'company')
            <div class="form-group col-lg-6">
                <label>{{ ($order->user ? $order->user->role : 'user' ) == 'company' ? __('orders.company_phone') : __('orders.user_phone') }}   </label>
                <input disabled class="form-control" value="{{$order->user ? $order->user->phone : ''}}">
            </div>
            @endif
            <div class="form-group col-lg-6">
                <label>{{  __('orders.user_status') }}  </label>
                <input disabled class="form-control" value="{{($order->user ? $order->user->role : null) == null ? __('orders.visitor') : ($order->user->role == 'company' ? __('orders.company') : __('orders.user') ) }}">
            </div>
        </form>
        <hr>
            @if($address)
        <form class="form-row" >
            <div class="form-group col-lg-12">
                <h3>{{__('orders.address')}}</h3>
            </div>
                <div class="form-group col-lg-6">
                    <label>{{  __('orders.address') }}  </label>
                    <input disabled class="form-control" value="{{$address->address->address}}">
                </div>
                <div class="form-group col-lg-6">
                    <label>{{  __('orders.country_address') }}  </label>
                    <input disabled class="form-control" value="{{$address->address->country_address}}">
                </div>
                <div class="form-group col-lg-6">
                    <label>{{  __('orders.country') }}  </label>
                    <input disabled class="form-control" value="{{$address->address->country->currentDescription->name}}">
                </div>
                <div class="form-group col-lg-6">
                    <label>{{  __('orders.city') }}  </label>
                    <input disabled class="form-control" value="{{$address->address->city->currentDescription->name}}">
                </div>
                @if($address->address->area_id)
                <div class="form-group col-lg-6">
                    <label>{{  __('web.area') }}  </label>
                    <input disabled class="form-control" value="{{$address->address->area->descriptions()->name}}">
                </div>
                @endif
        </form>
        <hr>
            @endif
        <form class="form-row" >
            <div class="form-group col-lg-12">
                <h3>{{__('orders.shipper')}}</h3>
            </div>
            @if($shipping)
                @if($shipping->shipping_id)
                    <div class="form-group col-lg-6">
                        <label>{{  __('orders.shipping_type') }}  </label>
                        <input disabled class="form-control" value="{{$shipping->shipping_id ? $shipping->shipping->currentDescription->name : __('orders.'.$shipping->shipping_status)}} ">
                    </div>
                @endif
                @if($shipping->shipping_status)
                    <div class="form-group col-lg-6">
                        <label>{{  __('orders.shipping_status') }}  </label>
                        <input disabled class="form-control" value="{{__('orders.'.$shipping->shipping_status)}}">
                    </div>
                @endif
                @if($shipping->policy_number)
                    <div class="form-group col-lg-6">
                        <label>{{  __('orders.policy_number') }}  </label>
                        <input disabled class="form-control" value="{{$shipping->policy_number}}">
                    </div>
                @endif
                @if($shipping->representative_name)
                    <div class="form-group col-lg-6">
                        <label>{{  __('orders.representative_name') }}  </label>
                        <input disabled class="form-control" value="{{$shipping->representative_name}}">
                    </div>
                @endif
            @endif

            <div class="text-center row w-100">

                @if($order->ajex_response)
                    <div class="form-group col">
                        <label>رقم فاتورة AJEX</label>
                        <input type="text" class="form-control "
                               disabled value='{{ $order->ajex_number_tracking }}'/>
                    </div>
                    <a href="{{ asset($order->ajex_file) }}" target="_blank" class="badge badge-info">
                        {{ __('main.download') }}
                    </a>
                @endif
                
                @if($order->dhl_shipment_tracking_number)
                    <div class="form-group col">
                        <label>DHL Shipment Tracking Number</label>
                        <input type="text" class="form-control "
                               disabled value='{{ $order->dhl_shipment_tracking_number }}'/>
                    </div>
                @endif

                @foreach(['dhl_label_file', 'dhl_invoice_file'] as $field)
                    @if($order->{$field} && is_file(public_path('storage/'.$order->{$field})))
                        @php
                            $fileExt = getFileExtension($order->{$field});
                            $isPdf = $fileExt === 'pdf';
                        @endphp
                        <div class="form-group col">
                            <label>{{ __("orders.$field") }} </label>
                            <a href="{{ asset('storage/'.$order->{$field}) }}" target="_blank" class="d-block">
                                <img src="{{ $isPdf ? asset('assets/admin/images/icons/pdf_file.png') : asset('assets/admin/images/icons/file1.png') }}"
                                     alt="{{ __("orders.$field") }}" width="50">
                            </a>
                            <a href="{{ asset('storage/'.$order->{$field}) }}" download class="badge badge-info">
                                {{ __('main.download') }}
                            </a>
                        </div> <!-- end of form-group -->
                    @endif
                @endforeach

                @if($order->ups_shipment_identification_number)
                    <div class="form-group col">
                        <label>UPS Shipment Identification Number</label>
                        <input type="text" class="form-control "
                               disabled value='{{ $order->ups_shipment_identification_number }}'/>
                    </div>
                @endif

                @if($order->ups_tracking_number)
                    <div class="form-group col">
                        <label>UPS Tracking Number</label>
                        <input type="text" class="form-control "
                               disabled value='{{ $order->ups_tracking_number }}'/>
                    </div>
                @endif

                @foreach(['ups_label_file'] as $field)
                    @if($order->{$field} && is_file(public_path('storage/'.$order->{$field})))
                        <div class="form-group col">
                            <label>{{ __("orders.$field") }}</label>
                            <a data-magnify="gallery"
                               data-src=""
                               data-caption="{{ __("orders.$field") }}"
                               data-group="a"
                               class="d-block"
                               href="{{ asset('storage/'.$order->{$field}) }}">
                                <img src="{{ asset('storage/'.$order->{$field}) }}" alt="{{ __("orders.$field") }}" width="100">
                            </a>
                        </div> <!-- end of form-group -->
                    @endif
                @endforeach

            </div> <!-- end of div files -->

        </form>
        <hr>
        <form class="form-row" >
            <div class="form-group col-lg-12">
                <h3>{{__('orders.products')}}</h3>
            </div>
            @foreach($products as $i=>$product)
                <div class="row pricing-list-item border-item mb-4" >
                    <div class="col-lg-12">
                        <label>{{__('orders.product')}} : {{$i+1}}</label>
                    </div>

                    <div class="form-group col-lg-6">
                        <label>{{  __('products.code') }}  </label>
                        <input type="text" class="form-control " disabled value='{{  $product->get_code  }}'/>
                    </div>
                    <div class="form-group col-lg-6">
                        <label>{{  __('orders.product_name') }}  </label>
                        <input type="text" class="form-control " disabled value='{{  $product->product->currentDescription->name  }}'/>
                    </div>
                    <div class="form-group col-lg-6">
                        <label>{{  __('products.category') }}  </label>
                        <input type="text" class="form-control " disabled value='{{  $product->product->category->currentDescription->name  }} @if($product->product->model_id) {{  $product->product->model->currentDescription->name  }} @endif'/>
                    </div>
                    @if($product->choice_id)
                        <div class="form-group col-lg-6">
                            <label>{{  __('orders.choice_id') }}  </label>
                            <input type="text" class="form-control " disabled value='@if($product->choice->attribute->choiceAttr ){{  $product->choice->attribute->choiceAttr->choice->currentLangauge->name }} - @endif {{$product->choice->choice->currentLangauge->name}}'/>
                        </div>
                    @endif

                    <div class="form-group col-lg-6">
                        <label>{{  __('orders.product_count') }}  </label>
                        <input type="text" class="form-control " disabled value='{{  $product->count  }}'/>
                    </div>


                    <div class="form-group col-lg-6">
                        <label>{{  __('orders.product_not_tax') }}  </label>
                        <input type="text" class="form-control " disabled value='{{  $product->total - $product->tax  }}'/>
                    </div>

                    <div class="form-group col-lg-6">
                        <label>{{  __('orders.product_tax') }}  </label>
                        <input type="text" class="form-control " disabled value='{{  $product->tax  }}'/>
                    </div>

                    <div class="form-group col-lg-6">
                        <label>{{  __('orders.product_total') }}  </label>
                        <input type="text" class="form-control " disabled value='{{  $product->total  }}'/>
                    </div>
                </div>
            @endforeach
        </form>

    </div> <!-- end of border-item -->

@endsection

@section('inner_js')
    <!-- Editor JS files -->
    <script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
    <script>
        function countries(elem)
        {
            var data = $(elem).val();
            $.ajax({
                url : url_country,
                type : 'GET',
                data : 'id='+data,
                success:function(data)
                {
                    $('#city_id').html(data);
                }
            })
        }
    </script>
    <script>
        let route_prefix = "{{ url(config('lfm.prefix')) }}",
            url = "{{url('/')}}/",
            url_country = "{{url('/')}}/admin/country",
            imagePath = "{{asset('')}}";
    </script>
    <script src="{{ asset('assets/admin/js/editor.js') }}"></script>
    <script src="{{ asset('assets/admin/js/lfm.js') }}"></script>
@endsection
