@extends('layouts.app')
@section('title')客户管理-即时账单@endsection

@section('content')
    @component('customer.finance.menu')@endcomponent
    <div class="container-fluid" id="container">
        <div id="form_div"></div>
        <div class="mt-1">
            <button type="button" class="btn btn-outline-dark btn-sm form-control-sm dropdown-toggle tooltipTarget" :class="[checkData.length>0?'btn-dark text-light':'']"
                    data-toggle="dropdown" title="导出所有页将会以搜索条件得到的筛选结果，将其全部记录（每一页）导出">
                导出Excel
            </button>
            <div class="dropdown-menu">
                <a class="dropdown-item" @click="billExport(false)" href="javascript:">导出勾选内容</a>
                <a class="dropdown-item" @click="billExport(true)" href="javascript:">导出所有页</a>
            </div>
        </div>
        <div>
            <label for="all" id="cloneCheckAll" class="d-none">
                <input id="all" type="checkbox" @click="checkAll($event)">全选
            </label>
            <table class="d-none" id="headerRoll"></table>
            <table class="table table-sm text-nowrap table-striped table-hover" id="headerParent">
                <tr class="text-center">
                    <td colspan="2"></td>
                    <td colspan="9" class="bg-light-info">单据信息</td>
                    <td colspan="4" class="bg-light-khaki">物流作业信息</td>
                    <td colspan="4" class="bg-light-cyanogen">费用信息</td>
                </tr>
                <tr id="header"></tr>
                <tr v-for="(bill,i) in bills">
                    <td>
                        <label><input type="checkbox" :value="bill.id" v-model="checkData"></label>
                    </td>
                    <td>@{{ i+1 }}</td>
                    <td>@{{ bill.customerName }}</td>
                    <td>@{{ bill.ownerName }}</td>
                    <td>@{{ bill.workedAt }}</td>
                    <td>
                        <label v-if="bill.type == '增值服务'">服务<span class="badge badge-primary">@{{ bill.methodName }}</span></label>
                        <label v-else>@{{ bill.type }}</label>
                    </td>
                    <td>@{{ bill.shopName }}</td>
                    <td>@{{ bill.operationBill }}</td>
                    <td>@{{ bill.consigneeName }}</td>
                    <td>@{{ bill.consigneePhone }}</td>
                    <td>@{{ bill.commodityAmount }}</td>
                    <td>@{{ bill.logisticBill }}</td>
                    <td>@{{ bill.volume }}</td>
                    <td>@{{ bill.weight }}</td>
                    <td>@{{ bill.logisticName }}</td>
                    <td>@{{ bill.workFee }}</td>
                    <td>@{{ bill.logisticFee }}</td>
                    <td>@{{ bill.total }}</td>
                </tr>
            </table>
            {{$details->appends($params)->links()}}
        </div>
    </div>
@stop

@section("lastScript")
    <script type="text/javascript" src="{{mix('js/queryForm/header.js')}}"></script>
    <script type="text/javascript" src="{{mix('js/queryForm/queryForm.js')}}"></script>
    <script type="text/javascript" src="{{mix('js/queryForm/export.js')}}"></script>
    <script>
        let vue = new Vue({
            el:"#container",
            data:{
                checkData:[],
                bills : [
                    @foreach($details as $detail)
                    {   id : "{{$detail->id}}",
                        ownerName : "{{$detail->owner ? $detail->owner->name : ''}}",
                        customerName : "{{$detail->owner ? ($detail->owner->customer ? $detail->owner->customer->name : '') : ''}}",
                        workedAt : "{{$detail->worked_at}}",
                        type : "{{$detail->type}}",
                        shopName : "{{$detail->shop ? $detail->shop->name : ''}}",
                        operationBill : "{{$detail->operation_bill}}",
                        consigneeName : "{{$detail->consignee_name}}",
                        consigneePhone : "{{$detail->consignee_phone}}",
                        commodityAmount : "{{$detail->commodity_amount}}",
                        logisticBill : "{{$detail->logistic_bill}}",
                        volume : "{{$detail->volume}}",
                        weight : "{{$detail->weight}}",
                        logisticName : "{{$detail->logistic ? $detail->logistic->name : ''}}",
                        methodName : "{{$detail->processMethod ? $detail->processMethod->name : ''}}",
                        workFee : "{{$detail->work_fee}}",
                        logisticFee : "{{$detail->logistic_fee}}",
                        createdAt : "{{$detail->created_at}}",
                        total : "{{$detail->work_fee + $detail->logistic_fee}}",
                    },
                    @endforeach
                ],
                owners : [
                    @foreach($owners as $owner)
                    {name:"{{$owner->id}}",value:"{{$owner->name}}"},
                    @endforeach
                ],
                customers : [
                    @foreach($customers as $customer)
                    {name:"{{$customer->id}}",value:"{{$customer->name}}"},
                    @endforeach
                ],
                shops : [
                    @foreach($shops as $shop)
                    {name:"{{$shop->id}}",value:"{{$shop->name}}"},
                    @endforeach
                ],
                types : [
                    {name:"发货",value:"发货"},{name:"收货",value:"收货"},{name:"增值服务",value:"增值服务"},
                ],
                sum : Number("{{ $details->total() }}"),
            },
            mounted(){
                $('#container').removeClass('d-none');
                let data=[
                    [
                        {name:'shop_id',type:'select',tip:'店铺',placeholder: '店铺',data:this.shops},
                        {name:'worked_at_start',type:'dateTime',tip:'起始作业日期'},
                        {name:'owner_id',type:'select_multiple_select',tip:['输入关键词快速定位下拉列表，回车确定','选择要显示的项目'],
                            placeholder:['项目','定位或多选项目'],data:this.owners},
                        {name: 'operation_bill', type: 'input', tip: '发/收/退/提货单号：可在两侧增加百分号(%)进行模糊搜索', placeholder: '单据号'},
                    ],[
                        {name:'customer_id',type:'select',tip:'客户',placeholder: '客户',data:this.customers},
                        {name:'worked_at_end',type:'dateTime',tip:'结束作业日期'},
                        {name:'logistic_bill', type: 'input', tip: '物流/快递单号：可在两侧增加百分号(%)进行模糊搜索', placeholder: '快递单号'},
                        {name:'type',type:'select',tip:'类型',placeholder: '类型',data:this.types},
                    ],
                ];
                this.form = new query({
                    el:"#form_div",
                    condition:data,
                });
                this.form.init();
                let column = [
                    {name:'cloneCheckAll',customization:true,type:'checkAll',column:'id',
                        dom:$('#cloneCheckAll').removeClass('d-none'), neglect: true},
                    {name:'index',value: '序号', neglect: true},
                    {name:'customerName',value: '客户', class: "bg-info"},
                    {name:'ownerName',value: '项目', class: "bg-info"},
                    {name:'workedAt',value: '作业时间', class: "bg-info"},
                    {name:'type',value: '类型', class: "bg-info"},
                    {name:'shopName',value: '店铺', class: "bg-info"},
                    {name:'operationBill',value: '单号(发/收/退/提)', class: "bg-info"},
                    {name:'consigneeName',value: '收件人', class: "bg-info"},
                    {name:'consigneePhone',value: '收件人电话', class: "bg-info"},
                    {name:'commodityAmount',value: '商品数量', class: "bg-info", neglect: true},
                    {name:'logisticBill',value: '物流/快递单号', class: "bg-khaki"},
                    {name:'volume',value: '体积', class: "bg-khaki", neglect: true},
                    {name:'weight',value: '重量', class: "bg-khaki", neglect: true},
                    {name:'logisticName',value: '承运商', class: "bg-khaki"},
                    {name:'workFee',value: '操作费', class: "bg-cyanogen", neglect: true},
                    {name:'logisticFee',value: '物流费', class: "bg-cyanogen", neglect: true},
                    {name:'total',value: '合计', class: "bg-cyanogen", neglect: true},
                ];
                let _this=this;
                setTimeout(function () {
                    let header = new Header({
                        el: "#header",
                        column: column,
                        data: _this.bills,
                        restorationColumn: 'id',
                        fixedTop:($('#form_div').height())+2,
                        offset:0.5,
                        vue:vue
                    });
                    header.init();
                },0);
            },
            watch:{
                checkData:{
                    handler(){
                        if (this.checkData.length === this.bills.length){
                            document.querySelector('#all').checked = true;
                            document.querySelector('#all_temp').checked = true;
                        }else {
                            document.querySelector('#all').checked = false;
                            document.querySelector('#all_temp').checked = false;
                        }
                    },
                    deep:true
                }
            },
            methods:{
                billExport(isAll){
                    let url = '{{url('customer/finance/instantBill/export')}}';
                    let token='{{ csrf_token() }}';
                    //excelExport 定义在 js/queryForm/export.js
                    excelExport(isAll,this.checkData,url,this.sum,token);
                },
                //全选事件
                checkAll(e){
                    if (e.target.checked){
                        this.checkData = [];
                        this.bills.forEach((el)=>{
                            this.checkData.push(el.id);
                        });
                    }else {
                        this.checkData = [];
                    }
                },
            },
        });
    </script>
@stop