@extends('layouts.app')
@section('title')回收站-问题件-订单管理@endsection
@section('content')
    <div id="nav2">
        @component('order.issue.menu')@endcomponent
    </div>
    <div class="container-fluid d-none" id="recycle_div">
        <div style="min-width: 1990px" class="card">
            <div class="card">
                <div class="card-body">
                    <div class="card">
                        <table class="col-sm-3 table table-sm table-bordered align-middle m-0">
                            <tr>
                                <td class="text-muted align-middle">每页显示条数:</td>
                                <td>
                                    <select name="paginate" id="paginate" class="form-control" @change="setPaginate" v-model="paginate">
                                    <option v-for="paginate in paginates" :value="paginate.name" >@{{ paginate.value }}</option>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td class="text-muted align-middle">操作选定记录:</td>
                                <td>
                                    <div class="btn btn-outline-danger" @click="recover">恢复</div>
                                </td>
                            </tr>
                        </table>
                    </div>
                    </div>

                    <table class="table table-sm table-striped table-bordered table-hover card-body p-0 m-0">
                        <tr>
                            <th>
                                <input id="all" type="checkbox" @click="checkAll($event)"/>
                            </th>
                            <th>id</th>
                            <th>登记时间</th>
                            <th>订单日期</th>
                            <th>客户订单号</th>
                            <th>原始承运商</th>
                            <th>店铺</th>
                            <th>原始运单号</th>
                            <th>退回单号</th>
                            <th>二次订单号</th>
                            <th>自定义单号</th>
                        </tr>

                        <tr v-for="orderIssue in orderIssues">
                            <td>
                                <input type="checkbox" :value="orderIssue.id" v-model="checkData" >
                            </td>
                            <td>@{{ orderIssue.id }}</td>
                            <td>@{{ orderIssue.created_at }}    </td>
                            <td>
                                <span v-if="orderIssue.order">@{{ orderIssue.order.created_at }}</span>
                            </td>
                            <td>
                                <span v-if="orderIssue.order">@{{ orderIssue.order.client_code }}</span>
                            </td>
                            <td>
                                <span v-if="orderIssue.order">
                                    <span v-if="orderIssue.order.logistic">
                                        @{{ orderIssue.order.logistic.name }}
                                    </span>
                                </span>
                            </td>
                            <td>
                                <span v-if="orderIssue.order">
                                    <span v-if="orderIssue.order.shop">
                                        @{{ orderIssue.order.shop.name }}
                                    </span>
                                </span>
                            </td>
                            <td>
                                <div v-if="orderIssue.order">
                                    <div v-if="orderIssue.order.packages">
                                        <p v-for="package in orderIssue.order.packages">@{{ package.logistic_number }}</p>
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div v-if="orderIssue.order_issue_rejected_bills">
                                    <div v-for="orderIssueRejectedBill in orderIssue.order_issue_rejected_bills">
                                        <p>@{{ orderIssueRejectedBill.logistic_number_return }}</p>
                                    </div>
                                </div>
                            </td>
                            <td>
                                <span>@{{ orderIssue.second_client_no }}</span>
                            </td>
                            <td>
                                @{{ orderIssue.custom_code }}
                            </td>
                        </tr>
                    </table>
                    {{$orderIssues->withQueryString()->links()}}
                </div>
            </div>
        </div>

    </div>
@endsection
@section('lastScript')
    <script type="text/javascript" src="{{mix('js/queryForm/queryForm.js')}}"></script>
    <script>
        let vue = new Vue({
            el: "#recycle_div",
            data: {
                orderIssues:  {!!  $orderIssues->toJson() !!}['data'],
                checkBool: false,
                checkData: [],
                paginate:{!! $paginate !!},
                paginates: [
                    {name: 50, value: '50条'},
                    {name: 100, value: '100条'},
                    {name: 200, value: '200条'},
                    {name: 500, value: '500条'},
                    {name: 1000, value: '1000条'},
                ],
            },
            watch: {
                checkData: function (value) {
                    if (value.length === this.orderIssues.length) {
                        this.checkBool = true;
                    }else{
                        this.checkBool = false;
                    }
                },
            },
            mounted: function () {
                $("#recycle_div").removeClass('d-none');
            },
            methods: {
                checkAll(e){
                    if (e.target.checked) {
                        this.orderIssues.forEach((el, i) => {
                            if (this.checkData.indexOf(el.id) == '-1') {
                                this.checkData.push(el.id);
                            }
                        });
                    } else {
                        this.checkData = [];
                    }
                },
                recover() {
                    if (this.checkData.length === 0) {
                        tempTip.setDuration(2000);
                        tempTip.show('请勾选需要恢复的问题件');
                        return;
                    }
                    if(!confirm('是否恢复所选问题件'))return;
                    let _this = this;
                    let data = {ids: this.checkData};
                    tempTip.setDuration(99999);
                    tempTip.waitingTip('处理中请稍后');
                    axios.post('{{url('apiLocal/order/issue/recycle/recover')}}', data)
                        .then(function (response) {
                            tempTip.cancelWaitingTip();
                            tempTip.setDuration(2000);
                            if (response.data.success) {
                                tempTip.showSuccess('恢复成功');
                                for (let i = _this.orderIssues.length -1;i >= 0;i--){
                                    if(_this.checkData.includes(_this.orderIssues[i].id))
                                        _this.orderIssues.splice(i,1);
                                }
                            } else {
                                tempTip.show(response.data.error);
                            }
                        }).catch(function (error) {
                            tempTip.cancelWaitingTip();
                            tempTip.setDuration(2000);
                            tempTip.show(error);
                    });
                },
                setPaginate:function(){
                    let form=$("<form method='get'></form>");
                    let paginate = $('#paginate').val();
                    form.append($("<input type='text' name='paginate' value='"+paginate+"'>"));
                    $("body").append(form);
                    form.submit();
                },

            }
        });

    </script>
@endsection

