@extends('layouts.app')
@section('title')客户管理-项目查询@endsection

@section('content')
    @component('customer.project.menu')@endcomponent
    <div class="container-fluid" id="container">
        <div class="modal fade" tabindex="-1" role="dialog" id="modal">
            <div class="modal-dialog modal-dialog-scrollable modal-dialog-centered">
                <div class="modal-content">
                    <div class="modal-body">
                        <div class="container-fluid" v-if="priceModel[thisClickIndex]">
                            <div class="row" v-for="(model,i) in priceModel[thisClickIndex]['ownerStoragePriceModels']">
                                <label class="text-dark font-weight-bold col-3"><span v-if="i==0">仓储：</span></label>
                                <a target="_blank" :href="'{{url('maintenance/priceModel/storage')}}?id='+model.id" class="text-decoration-none">@{{ model.counting_type }}-@{{ model.using_type }}</a>
                            </div>
                            <hr class="m-1">
                            <div class="row" v-for="(model,i) in priceModel[thisClickIndex]['ownerPriceOperations']">
                                <label class="text-dark font-weight-bold col-3"><span v-if="i==0">作业：</span></label>
                                <a target="_blank" :href="'{{url('maintenance/priceModel/operation')}}?id='+model.id" class="text-decoration-none">@{{ model.name }}</a>
                            </div>
                            <hr class="m-1">
                            <div class="row" v-for="(model,i) in priceModel[thisClickIndex]['ownerPriceExpresses']">
                                <label class="text-dark font-weight-bold col-3"><span v-if="i==0">快递：</span></label>
                                <a target="_blank" :href="'{{url('maintenance/priceModel/express')}}?id='+model.id" class="text-decoration-none">@{{ model.name }}</a>
                            </div>
                            <hr class="m-1">
                            <div class="row" v-for="(model,i) in priceModel[thisClickIndex]['ownerPriceLogistics']">
                                <label class="text-dark font-weight-bold col-3"><span v-if="i==0">物流：</span></label>
                                <a target="_blank" :href="'{{url('maintenance/priceModel/logistic')}}?id='+model.id" class="text-decoration-none">@{{ model.name }}</a>
                            </div>
                            <hr class="m-1">
                            <div class="row" v-for="(model,i) in priceModel[thisClickIndex]['ownerPriceDirectLogistics']">
                                <label class="text-dark font-weight-bold col-3"><span v-if="i==0">直发车：</span></label>
                                <a target="_blank" :href="'{{url('maintenance/priceModel/directLogistic')}}?id='+model.id" class="text-decoration-none">@{{ model.name }}</a>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                    </div>
                </div>
            </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="executeExport(false)" href="javascript:">导出勾选内容</a>
                <a class="dropdown-item" @click="executeExport(true)" href="javascript:">导出所有页</a>
            </div>
        </div>
        @if(Session::has('successTip'))
        <div class="alert alert-success h1">{{Session::get('successTip')}}</div>
        @endif
        <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 id="header"></tr>
                <tr v-for="(owner,i) in owners">
                    <td>
                        <label><input type="checkbox" :value="owner.id" v-model="checkData"></label>
                    </td>
                    <td>
                        <a :href="'{{url('customer/project')}}/'+owner.id+'/edit'"><button class="btn btn0sm btn-outline-info">编辑</button></a>
                    </td>
                    <td>@{{ owner.customer_name }}</td>
                    <td>@{{ owner.tax_rate ? owner.tax_rate+'%' : '' }}</td>
                    <td>@{{ owner.name }}</td>
                    <td>@{{ owner.code }}</td>
                    <td>@{{ owner.created_at }}</td>
                    <td colspan="2">
                        <div class="p-0">
                            <small v-for="contract in owner.contracts">@{{ contract.number }}<label class="ml-2">@{{ contract.salesman }}</label><br></small>
                        </div>
                    </td>
                    <td>@{{ owner.customer_company_name }}</td>
                    <td>@{{ owner.linkman }}</td>
                    <td>@{{ owner.phone_number }}</td>
                    <td>@{{ owner.user_owner_group_name }}</td>
                    <td>
                        <button class="btn btn-sm btn-info" @click="showModal(owner)">各项计价</button>
                    </td>
                    <td>@{{ owner.owner_storage_price_model_using_type }}</td>
                    <td>@{{ owner.owner_area_report_accounting_area }}</td>
                    <td>@{{ owner.waring_line_on }}</td>
                    <td>@{{ owner.is_activation }}</td>
                    <td>@{{ owner.description }}</td>
                </tr>
            </table>
        </div>
    </div>
@endsection

@section('lastScript')
    <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:{
                owners : [
                    @foreach($owners as $owner)
                    {   id : "{{$owner->id}}",
                        customer_name:"{{$owner->customer ? $owner->customer->name : ''}}",
                        tax_rate  : "{{$owner->tax_rate}}",
                        name : "{{$owner->name}}",
                        code : "{{$owner->code}}",
                        created_at : "{{$owner->created_at}}",
                        contracts : [
                            @foreach($owner->contracts as $contract)
                            { number:"{{$contract->contract_number}}",salesman:"{{$contract->salesman}}" },
                            @endforeach
                        ],
                        customer_company_name:"{{$owner->customer ? $owner->customer->company_name : ''}}",
                        linkman:"{{$owner->linkman}}",
                        phone_number:"{{$owner->phone_number}}",
                        user_owner_group_name:"{{$owner->userOwnerGroup ? $owner->userOwnerGroup->name : ''}}",
                        owner_storage_price_model_using_type:"{{ implode(",",array_unique(array_column(($owner->ownerStoragePriceModels)->toArray(),"using_type")))  }}",
                        owner_area_report_accounting_area : "{{ $owner->ownerAreaReport ? $owner->ownerAreaReport->accounting_area : '' }}",
                        waring_line_on : "{{$owner->waring_line_on}}",
                        is_activation : "{{$owner->deleted_at ? '否' : '是'}}",
                        description : "{{$owner->description}}",
                        ownerStoragePriceModels : {!! $owner->ownerStoragePriceModels !!},
                    },
                    @endforeach
                ],
                checkData : [],
                sum : Number("{{ $owners->total() }}"),
                priceModel : {},
                thisClickIndex : "",
            },
            mounted(){
                $('#container').removeClass('d-none');
                let column = [
                    {name:'cloneCheckAll',customization:true,type:'checkAll',column:'id',
                        dom:$('#cloneCheckAll').removeClass('d-none'), neglect: true},
                    {name:'operating',value: '操作', neglect: true},
                    {name:'customer',value: '客户'},
                    {name:'tax_rate',value: '税率%', neglect: true},
                    {name:'name',value: '项目'},
                    {name:'code',value: '货主代码'},
                    {name:'created_at',value: '创建日期'},
                    {name:'contract_number',value: '合同号', neglect: true},
                    {name:'salesman',value: '销售名称', neglect: true},
                    {name:'customer_full_name',value: '公司全称'},
                    {name:'linkman',value: '联系人'},
                    {name:'phone_number',value: '联系电话'},
                    {name:'workgroup',value: '项目小组'},
                    {name:'relating_price',value: '关联报价', neglect: true},
                    {name:'type',value: '用仓类型'},
                    {name:'current_month_counting_area',value: '当月结算面积', neglect: true},
                    {name:'waring_line_on',value: '月单量预警', neglect: true},
                    {name:'is_activating',value: '是否激活', neglect: true},
                    {name:'description',value: '项目描述'},
                ];
                let _this=this;
                setTimeout(function () {
                    let header = new Header({
                        el: "#header",
                        column: column,
                        data: _this.owners,
                        restorationColumn: 'id',
                        fixedTop:($('#form_div').height())+2,
                        offset:0.5,
                        vue:vue
                    });
                    header.init();
                },0);
            },
            watch:{
                checkData:{
                    handler(){
                        if (this.checkData.length === this.owners.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:{
                executeExport(isAll){
                    let url = '{{url('customer/project/index/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.owners.forEach((el)=>{
                            this.checkData.push(el.id);
                        });
                    }else {
                        this.checkData = [];
                    }
                },
                showModal(owner){
                    if (this.priceModel[owner.id]){
                        this.thisClickIndex = owner.id;
                        $("#modal").modal('show');
                        return;
                    }
                    window.axios.post('{{url('customer/project/getOwnerPriceModel')}}',{id:owner.id})
                        .then(res=>{
                            if (res.data.success){
                                let model = res.data.data;
                                model["ownerStoragePriceModels"] = owner.ownerStoragePriceModels;
                                delete owner.ownerStoragePriceModels;
                                this.priceModel[owner.id] = model;
                                this.thisClickIndex = owner.id;
                                $("#modal").modal('show');
                                return;
                            }
                            window.tempTip.setDuration(3000);
                            window.tempTip.showSuccess(res.data.data);
                        }).catch(err=>{
                        window.tempTip.setDuration(3000);
                        window.tempTip.show("网络错误："+err);
                    });
                },
            },
        });
    </script>
@endsection