@extends('layouts.app')
@section('title')
    客户管理-项目报表
@endsection
@section('content')
    @component('customer.project.menu')@endcomponent
    <div class="container-fluid card d-none" id="container">
        <div id="form_div"></div>
        <div>
            <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="reportExport(false)" href="javascript:">导出勾选内容</a>
                <a class="dropdown-item" @click="reportExport(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 table-striped table-hover" id="headerParent">
                <tr class="text-center">
                    <td></td>
                    <td colspan="7" class="bg-light-khaki">客户信息</td>
                    <td colspan="4" class="bg-light-info">盘点信息</td>
                    <td colspan="3" class="bg-light-cyanogen">账单信息</td>
                </tr>
                <tr class="text-nowrap" id="header"></tr>
                <tr v-for="(report,i) in reports">
                    <td>
                        <label><input type="checkbox" :value="report.id" v-model="checkData"></label>
                    </td>
                    <td>@{{ i+1 }}</td>
                    <td>@{{ report.ownerGroupName }}</td>
                    <td>@{{ report.customerName }}</td>
                    <td>@{{ report.ownerName }}</td>
                    <td>@{{ report.ownerStatus }}</td>
                    <td>@{{ report.ownerCreatedAt }}</td>
                    <td>@{{ report.ownerStorageDuration }} 天</td>
                    <td>@{{ report.countingMonth }}</td>
                    <td>@{{ report.dailyAverageOrderAmount }}</td>
                    <td>@{{ report.lastMonthCountingArea }}</td>
                    <td>@{{ report.currentMonthCountingArea }}</td>
                    <td>@{{ report.ownerBillReportInitialFee }}</td>
                    <td>@{{ report.ownerBillReportConfirmFee }}</td>
                    <td>@{{ report.ownerBillReportConfirmUpdatedAt }}</td>
                </tr>
            </table>
            {{$reports->appends($params)->links()}}
        </div>
    </div>
@stop

@section('lastScript')
    <script type="text/javascript" src="{{mix('js/queryForm/queryForm.js')}}"></script>
    <script type="text/javascript" src="{{mix('js/queryForm/header.js')}}"></script>
    <script type="text/javascript" src="{{mix('js/queryForm/export.js')}}"></script>
    <script>
        let vue = new Vue({
            el:"#container",
            data:{
                reports : [
                    @foreach($reports as $report)
                    {
                        id : "{{$report->id}}",
                        ownerGroupName : "{{$report->owner ? ($report->owner->userOwnerGroup ? $report->owner->userOwnerGroup->name : '') : ''}}",
                        customerName : "{{$report->owner ? ($report->owner->customer ? $report->owner->customer->name : '') : ''}}",
                        ownerName : "{{$report->owner ? $report->owner->name : ''}}",
                        ownerStatus : "{{$report->owner ? ($report->owner->deleted_at ? "冻结" : "激活") : ''}}",
                        ownerStorageDuration : "{{$report->owner ? ($report->owner->created_at ? ((new DateTime())->diff(new DateTime($report->owner->created_at))->days)  : '') : ''}}",
                        ownerCreatedAt : "{{$report->owner ? $report->owner->created_at : ''}}",
                        countingMonth : "{{$report->counting_month}}",
                        dailyAverageOrderAmount : "{{$report->daily_average_order_amount}}",
                        lastMonthCountingArea : "{{$report->last_month_counting_area}}",
                        currentMonthCountingArea : "{{$report->current_month_counting_area}}",
                        ownerBillReportInitialFee : "{{$report->ownerBillReport ? $report->ownerBillReport->initial_fee : ''}}",
                        ownerBillReportConfirmFee : "{{$report->ownerBillReport ? $report->ownerBillReport->confirm_fee : ''}}",
                        ownerBillReportConfirmUpdatedAt : "{{$report->ownerBillReport ? $report->ownerBillReport->updated_at : ''}}",
                    },
                    @endforeach
                ],

                owners : [
                    @foreach($owners as $owner)
                    {name:"{{$owner->id}}",value:"{{$owner->name}}"},
                    @endforeach
                ],
                workgroup : [
                    @foreach($ownerGroups as $ownerGroup)
                    {name:"{{$ownerGroup->id}}",value:"{{$ownerGroup->name}}"},
                    @endforeach
                ],
                customers : [
                    @foreach($customers as $customer)
                    {name:"{{$customer->id}}",value:"{{$customer->name}}"},
                    @endforeach
                ],
                status : [
                    {name:"激活",value:"激活"},
                    {name:"冻结",value:"冻结"},
                ],
                checkData : [],
                sum : Number("{{ $reports->total() }}"),
            },
            mounted(){
                $('#container').removeClass('d-none');
                let data=[
                    [
                        {name:'owner_group_id',type:'select',tip:'项目小组',placeholder: '项目小组',data:this.workgroup},
                        {name:'counting_month_start',type:'dateMonth',tip:'起始结算月'},
                        {name:'owner_id',type:'select_multiple_select',tip:['输入关键词快速定位下拉列表，回车确定','选择要显示的项目'],
                            placeholder:['项目','定位或多选项目'],data:this.owners},
                    ],[
                        {name:'customer_id',type:'select',tip:'客户',placeholder: '客户',data:this.customers},
                        {name:'counting_month_end',type:'dateMonth',tip:'结束结算月'},
                        {name:'status',type:'select',placeholder:'状态',data:this.status},
                    ],
                ];
                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 ,class:"bg-khaki"},
                    {name:'ownerGroupName',value: '项目小组',class:"bg-khaki"},
                    {name:'customerName',value: '客户',class:"bg-khaki"},
                    {name:'ownerName',value: '子项目',class:"bg-khaki"},
                    {name:'ownerStatus',value: '状态',class:"bg-khaki"},
                    {name:'ownerCreatedAt',value: '创建日期',class:"bg-khaki"},
                    {name:'ownerStorageDuration',value: '在库时长',class:"bg-khaki"},
                    {name:'countingMonth',value: '结算月', class:"bg-info"},
                    {name:'dailyAverageOrderAmount',value: '日均单量', neglect: true, class:"bg-info"},
                    {name:'lastMonthCountingArea',value: '结算月上月盘点面积', neglect: true, class:"bg-info"},
                    {name:'currentMonthCountingArea',value: '结算月盘点面积', neglect: true, class:"bg-info"},
                    {name:'ownerBillReportInitialFee',value: '初始账单金额', neglect: true, class:"bg-cyanogen"},
                    {name:'ownerBillReportConfirmFee',value: '确认账单金额', neglect: true, class:"bg-cyanogen"},
                    {name:'ownerBillReportConfirmUpdatedAt',value: '确认日期', class:"bg-cyanogen"},
                ];
                let _this=this;
                setTimeout(function () {
                    let header = new Header({
                        el: "#header",
                        column: column,
                        data: _this.reports,
                        restorationColumn: 'id',
                        fixedTop:($('#form_div').height())+2,
                        offset:0.5,
                        vue:vue
                    });
                    header.init();
                },0);
            },
            watch:{
                checkData:{
                    handler(){
                        if (this.checkData.length === this.reports.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 : {
                reportExport(isAll){
                    let url = '{{url('customer/project/report/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.reports.forEach((el)=>{
                            this.checkData.push(el.id);
                        });
                    }else {
                        this.checkData = [];
                    }
                },
            }
        });
    </script>
@stop