@extends("Front::Layout.Panel.master")
@section('content')

    <div class="dashboard_main">
        <div class="factor_sec mx-auto">
            <div class="dashboard_main_container_style  mx-auto mb-4">
                <h5 class="dashboard_main_container_style_header"> خدمت مربوطه</h5>
                <div class="data_text d-flex flex-wrap mb-md-0">
                    <div class="col-sm-3 align-items-center col-6 d-flex flex-column mb-3 mb-md-0">
                        <span class="text-muted small ">عنوان</span>
                        <span class="text-dark">{{$order->service->name }}</span>
                    </div>
                    <div class="col-sm-3 align-items-center col-6 d-flex flex-column mb-3 mb-md-0">
                        <span class="text-muted small ">کد سفارش</span>
                        <span class="text-dark persianumber">{{$order->code}}</span>
                    </div>
                    <div class="col-sm-3 align-items-center col-6 d-flex flex-column mb-3 mb-md-0">
                        <span class="text-muted small ">متخصص</span>
                        <span class="text-dark">{{$order->expert->name ?? 'بی نام'  }} {{ $order->expert->lname??'' }}</span>
                    </div>
                    <div class="col-sm-3 align-items-center col-6 d-flex flex-column mb-3 mb-md-0">
                        <span class="text-muted small ">قیمت کل</span>
                        <span class="text-dark persianumber">{{$order->price}} تومان</span>
                    </div>
                </div>
            </div>
            <div class="dashboard_main_container_style  mx-auto">
                <h5 class="dashboard_main_container_style_header">فاکتور نهایی</h5>
                <div
                    class="alert alert-secondary  m-1 mb-2 @if( $errors->any() || session()->has('status')) @else d-none @endif "
                    role="alert">
                    @if($errors->any())
                        @foreach ($errors->all() as $error)
                            {{ $error }}<br>
                        @endforeach
                    @endif
                    @if(session()->has('status'))
                        {{session()->get('status')}}
                    @endif
                </div>
                <div class="table_custom mx-auto">
                    <input type="hidden" name="order_id" id="order_id" value="{{ $order->id }}">

                    @includeIf("Front::service_use_club")
                    @if(is_array($order->meta) && array_key_exists('factor',$order->meta) && !is_null($order->meta["factor"]))
                        <table>
                            <caption class="d-none">فاکتور مربوطه</caption>
                            <thead>
                            <tr>
                                <th scope="col">عنوان</th>
                                <th scope="col">واحد</th>
                                <th scope="col">قیمت(تومان)</th>
                                <th scope="col">تخفیف خالص(تومان)</th>
                                <th scope="col">تخفیف درصدی(٪)</th>
                                <th scope="col">شمارش</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($order->meta['factor'] as $item)

                                <tr>
                                    <td data-label="عنوان">
                                        {{$item['name']}}
                                    </td>
                                    <td data-label="واحد" class="persianumber">
                                        {{$item['unit']}}
                                    </td>
                                    <td data-label="قیمت(تومان)" class="persianumber">
                                        {{$item['price']}}
                                    </td>
                                    <td data-label="تخفیف خالص(تومان)" class="persianumber">
                                        {{$item['discount_pure'] ?? '-'}}
                                    </td>
                                    <td data-label="تخفیف درصدی(٪)" class="persianumber">
                                        {{$item['discount_percent'] ?? '-'}}
                                    </td>
                                    <td data-label="شمارش" class="persianumber">
                                        {{$item['count']}}
                                    </td>
                                </tr>
                            @endforeach
                            @elseif(in_array($order->status,[\Mehrab\OrderService\Models\OrderService::DONE,\Mehrab\OrderService\Models\OrderService::DONE_AND_WAITING_FEEDBACK_FOR_EXPERT,\Mehrab\OrderService\Models\OrderService::WAITING_PAYMENT]))
                                <ul class="list-group p-0 small">
                                    <li class="list-group-item d-flex justify-content-between">
                                        <span class="persianumber">کد رهگیری سفارش</span>
                                        <span class="persianumber">{{$order->code}}</span>
                                    </li>
                                    <li class="list-group-item d-flex justify-content-between">
                                        <span>خدمت مد نظر</span>
                                        <span>{{$order->service->name}}</span>
                                    </li>
                                    <li class="list-group-item d-flex justify-content-between">
                                        <span>هزینه(تومان)</span>
                                        <span class="persianumber">{{$order->getPrice()}}</span>
                                    </li>
                                    @if(is_array($order->meta) &&  array_key_exists('start_work',$order->meta))
                                        <li class="list-group-item d-flex justify-content-between">
                                            <span>زمان شروع کار</span>
                                            <span
                                                class="persianumber">{{IR_TimestampToDate($order->meta['start_work'])}}</span>
                                        </li>
                                    @endif
                                    @if(is_array($order->meta) && array_key_exists('end_work',$order->meta))
                                        <li class="list-group-item d-flex justify-content-between">
                                            <span>زمان پایان کار</span>
                                            <span
                                                class="persianumber">{{IR_TimestampToDate($order->meta['end_work'])}}</span>
                                        </li>
                                    @endif
                                </ul>

                            @else
                                <div class="alert alert-primary" role="alert">
                                    فاکتوری ثبت نشده است !
                                </div>
                            @endif
                            </tbody>
                        </table>
                </div>

                @if($order->status == \Mehrab\OrderService\Models\OrderService::WAITING_PAYMENT)
                    <div id="coupon_box" style="    padding: 20px 0;">
                        @if(isset($order->meta["coupon"]))
                            <p>شما در حال استفاده از کوپن تخفیف {{ $order->meta["coupon"] }} هستید </p>
                            @else
                                <h3 id="order_review_heading">کوپن تخفیف</h3>
                                <div class="coupon_box_content">
                                    <input type="text" name="coupon" id="coupon" placeholder="کوپن تخفیف خود را وارد کنید...">
                                    <button style="padding: 9px;" type="button" class="apply_coupon btn btn-sm button alt">اعمال کردن کوپن تخفیف</button>
                                </div>
                        @endif
                    </div>
                    <form method="post" action="{{route('service_order_front.do_payment',$order->id)}}">
                        <div class="btnsPayment d-sm-flex justify-content-between mt-2">
                            <div class="col-sm-6 p-2 mx-auto">
                                <button class="btn btn-block" type="submit" name="paymentMethod" value="onlinePayment">
                                    پرداخت آنلاین
                                </button>
                            </div>
                            {{--@if($order->getCanCashPayment())
                                <div class="col-sm-6 p-2">
                                    <button class="btn btn-block" type="submit" name="paymentMethod"
                                            value="cashPayment">پرداخت نقدی
                                    </button>
                                </div>
                            @endif--}}
                        </div>
                        @csrf
                    </form>
                @endif
            </div>

        </div>
    </div>

    @push("panel-js")
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-toast-plugin/1.3.2/jquery.toast.min.js"></script>

        <script>

            jQuery(document).ready(function($){
                jQuery(".apply_coupon").click(function(e) {
                    e.preventDefault()
                    jQuery(".admin-ajax-loading").addClass("active")
                    jQuery.ajaxSetup({
                        headers: {
                            'X-CSRF-TOKEN': "{{ csrf_token() }}"
                        }
                    });

                    jQuery.ajax({
                        type: "post",

                        url: `{{ route("coupon.apply-service") }}`,

                        data: {
                            "coupon": jQuery('#coupon').val(),
                            "order_id": jQuery('#order_id').val()
                        },

                        success: function (data) {
                            jQuery(" .admin-ajax-loading").removeClass("active")
                            // var json_data=jQuery.parseJSON(data);


                            jQuery.toast({
                                text : data.message,
                                showHideTransition : 'slide',  // It can be plain, fade or slide
                                heading: data.heading,
                                icon: data.type,
                                loader: true,        // Change it to false to disable loader
                                loaderBg: "#90e084",
                                textAlign: 'right',
                                hideAfter : 5000,
                            })

                            if(data.type==="success"){
                                setTimeout(function(){
                                    window.location.reload(1);
                                }, 1000);
                            }

                        },
                        error: function (data) {
                            jQuery(" .admin-ajax-loading").removeClass("active")
                        }
                    })
                })

                jQuery("#use_club").click(function(e) {
                    e.preventDefault()
                    jQuery(".admin-ajax-loading").addClass("active")
                    jQuery.ajaxSetup({
                        headers: {
                            'X-CSRF-TOKEN': "{{ csrf_token() }}"
                        }
                    });

                    jQuery.ajax({
                        type: "post",

                        url: `{{ route("club.service_use") }}`,

                        data: {
                            "order_id": jQuery('#order_id').val()
                        },

                        success: function (data) {
                            jQuery(" .admin-ajax-loading").removeClass("active")
                            // var json_data=jQuery.parseJSON(data);


                            jQuery.toast({
                                text : data.message,
                                showHideTransition : 'slide',  // It can be plain, fade or slide
                                heading: data.heading,
                                icon: data.type,
                                loader: true,        // Change it to false to disable loader
                                loaderBg: "#90e084",
                                textAlign: 'right',
                                hideAfter : 5000,
                            })

                            if(data.type==="success"){
                                setTimeout(function(){
                                    window.location.reload(1);
                                }, 1000);
                            }

                        },
                        error: function (data) {
                            jQuery(" .admin-ajax-loading").removeClass("active")
                        }
                    })
                })

            })
        </script>
    @endpush

@endsection
