@extends('layouts.app')
@section('title')客户@endsection

@section('content')
    <div id="nav2">
        @component('customer.menu')@endcomponent
        @component('customer.customer.menu')@endcomponent
    </div>
    <div class="d-none container-fluid card" id="container">
        <div class="card-body mt-2">
            <div id="form_div" class="mb-2"></div>
            @if(Session::has('successTip'))
                <div class="alert alert-success h1">{{Session::get('successTip')}}</div>
            @endif
            @include("customer.customer._customerLog")
            @include("customer.customer._addContract")
            @include("customer.customer._relatedOwner")
            <table class="table table-striped table-hover text-nowrap bg-white" id="table-customer">
                <tr>
                    <th>序号</th>
                    <th>名称</th>
                    <th>标签</th>
                    <th></th>
                    <th>客户全称</th>
                    <th>联系人</th>
                    <th>联系电话</th>
                    <th>公司备注</th>
                    <th>合同</th>
                    <th>发票地址</th>
                    <th>创建时间</th>
{{--                    <th class="text-center" style="min-width: 700px">--}}
{{--                        <div class="row">--}}
{{--                            <label class="col-12">日志</label>--}}
{{--                        </div>--}}
{{--                        <div class="row">--}}
{{--                            <div class="row text-secondary offset-1 col-11">--}}
{{--                                <label class="col-4">说明</label>--}}
{{--                                <label class="col-2">状态</label>--}}
{{--                                <label class="col-2">操作者</label>--}}
{{--                                <label class="col-3">操作时间</label>--}}
{{--                                <label class="col-1"></label>--}}
{{--                            </div>--}}
{{--                        </div>--}}
{{--                    </th>--}}
                    <th>操作</th>
                </tr>
                <tr v-for="(customer,i) in customers" :id="'model-'+customer.id" @click="selectTableRow(customer.id,$event)">
                    <td>@{{ i+1 }}</td>
                    <td>@{{ customer.name }}</td>
                    <td>
                        <div class="text-overflow-warp-200 row">
                            <span class="badge badge-pill badge-primary ml-1" v-for="(tag,i) in customer.tags">@{{ tag.name }}</span>
                        </div>
                    </td>
                    <td class="cursor-pointer text-dark font-weight-bold">
                        <div class="dropdown">
                            <span class="fa fa-plus" :id="'tag-'+i" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></span>
                            <div class="dropdown-menu" :aria-labelledby="'tag-'+i">
                                <div @click="$event.stopPropagation();" style="min-width: 250px">
                                    <label class="w-75 ml-2"><input placeholder="TAB键与回车键提交新标签" class="form-control form-control-sm" @keydown="submitTag(i,$event)" type="text" @input="search($event)"></label>
                                    <span v-if="searchTag.length>0">
                                        <a class="dropdown-item text-nowrap" v-for="tag in searchTag" @click="selected(i,tag)">
                                            @{{ tag.value }}
                                            <span class="fa fa-check text-info pull-right" style="top: 0.3rem" v-if="selectedTag[customer.id][tag.name]"></span>
                                        </a>
                                    </span>
                                    <span v-else>
                                        <a class="dropdown-item text-nowrap" v-for="tag in defaultTag" @click="selected(i,tag)">
                                            <span class="fa fa-fire text-danger"></span>
                                            &nbsp;@{{ tag.value }}
                                            <span class="fa fa-check text-info pull-right" style="top: 0.3rem" v-if="selectedTag[customer.id][tag.name]"></span>
                                        </a>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </td>
                    <td>@{{ customer.company_name }}</td>
                    <td>@{{ customer.contact_man }}</td>
                    <td>@{{ customer.phone }}</td>
                    <td>@{{ customer.remark }}</td>

                    <td v-if="customer.contractQuantity>0" class="cursor-pointer" @click="showContract(customer.id)"><span class="fa" :class="contractIds['_'+customer.id] === true ? 'fa-angle-double-down' : 'fa-angle-double-right'"></span>@{{ customer.contractQuantity }}份合同</td>
                    <td v-else>#</td>
                    <td>@{{ customer.invoice_address }}</td>
                    <td>@{{ customer.created_at }}</td>
{{--                    <td>--}}
{{--                        <div class="bg-white row">--}}
{{--                            <div class="col-1"><button class="btn btn-sm btn-outline-info" @click="addCustomerLog(i)">添加</button></div>--}}
{{--                            <div class="col-11">--}}
{{--                                <div class="w-100">--}}
{{--                                    <div class="text-center mb-0 row" v-for="(log,j) in customer.customer_logs" v-if="j<2" @click="edit(i,j,log.user)">--}}
{{--                                        <div @mouseenter="textClass($event,true)" @mouseleave="textClass($event,false)" class="col-4">--}}
{{--                                            <div class="text-overflow-replace cursor-pointer text-secondary" v-if="log.id && (editCustomer!=customer.id || j!=0)">--}}
{{--                                                @{{ log.description }}--}}
{{--                                            </div>--}}
{{--                                            <label v-if="!log.id">--}}
{{--                                                <textarea class="form-control form-control-sm" @blur="addLog(i,j)" v-model="log.description"></textarea>--}}
{{--                                            </label>--}}
{{--                                            <label v-if="editCustomer == customer.id && j==0">--}}
{{--                                                <textarea @blur="editLog(i,$event)" class="form-control form-control-sm">@{{ log.description }}</textarea>--}}
{{--                                            </label>--}}
{{--                                        </div>--}}
{{--                                        <div class="col-2">--}}
{{--                                            <span v-if="log.id && (editCustomer!=customer.id || j!=0)">@{{ log.status ? log.status.name : '' }}</span>--}}
{{--                                            <label v-if="!log.id"><select class="form-control form-control-sm" @change="addLog(i,j)" @click="editLogEvent(log)">--}}
{{--                                                    <option v-for="status in logStatuses" :value="status.id">@{{ status.name }}</option>--}}
{{--                                                </select></label>--}}
{{--                                            <label v-if="editCustomer == customer.id && j==0">--}}
{{--                                                <select class="form-control form-control-sm" @change="editLogStatus(i,log)" v-model="log.customer_log_status_id">--}}
{{--                                                    <option v-for="status in logStatuses" :value="status.id">@{{ status.name }}</option>--}}
{{--                                                </select>--}}
{{--                                            </label>--}}
{{--                                        </div>--}}
{{--                                        <div class="col-2 text-secondary">@{{ log.user ? log.user.name : '' }}</div>--}}
{{--                                        <div class="col-3 text-secondary">@{{ log.created_at }}</div>--}}
{{--                                        @can("客户-编辑")<div class="col-1 font-weight-bold text-danger h4 cursor-pointer" @click="delLog(i,j)">&times;</div>@endcan--}}
{{--                                    </div>--}}
{{--                                </div>--}}
{{--                                <div class="w-100 up" :id="'log-'+customer.id">--}}
{{--                                    <div class="text-center mb-0 row" v-for="(log,j) in customer.customer_logs" v-if="j>1">--}}
{{--                                        <div @mouseenter="textClass($event,true)" @mouseleave="textClass($event,false)" class="col-4"><div class="text-overflow-replace cursor-pointer text-secondary">@{{ log.description }}</div></div>--}}
{{--                                        <div class="col-2">@{{ log.status ? log.status.name : '' }}</div>--}}
{{--                                        <div class="col-2 text-secondary">@{{ log.user ? log.user.name : '' }}</div>--}}
{{--                                        <div class="col-3 text-secondary">@{{ log.created_at }}</div>--}}
{{--                                        @can("客户-编辑")<div class="col-1 font-weight-bold text-danger h4 cursor-pointer" @click="delLog(i,j)">&times;</div>@endcan--}}
{{--                                    </div>--}}
{{--                                </div>--}}
{{--                                <div class="row" @click="showLog(i)" v-if="customer.customer_logs.length > 2">--}}
{{--                                    <label class="text-center mt-0 p-0 cursor-pointer offset-5">--}}
{{--                                        <span class="fa" :class="customer.isShowLog ? 'fa-angle-double-down' : 'fa-angle-double-right'"></span>--}}
{{--                                        &nbsp;<span v-if="customer.isShowLog">收起</span><span v-if="!customer.isShowLog">展开</span>&nbsp;@{{ customer.customer_logs.length }} 条日志--}}
{{--                                    </label>--}}
{{--                                </div>--}}
{{--                            </div>--}}
{{--                        </div>--}}
{{--                    </td>--}}
                    <td>
                        @can("客户-编辑")
                            <button class="btn btn-sm btn-outline-success" @click="addContract(i)">新建合同</button>
                            <button class="btn btn-sm btn-outline-primary" @click="relatedOwner(i)">关联项目</button>
                            <a :href="'{{url('customer/customer')}}/'+customer.id+'/edit'"><button class="btn btn-sm btn-outline-info">改</button></a>
                        @endcan
                        @can("客户-删除")<button class="btn btn-sm btn-outline-danger" @click="destroy(customer.id,i,customer.name)">删</button>@endcan
                    </td>
                </tr>
            </table>
            {{$customers->appends($params)->links()}}
        </div>
    </div>
@stop

@section('lastScript')
    <script type="text/javascript" src="{{mix('js/queryForm/queryForm.js')}}"></script>
    <script>
        new Vue({
            el:"#container",
            data:{
                customers :  {!!  $customers->toJson() !!}['data'],
                index:'',
                logStatuses:null,
                contractIds:[],
                customerOwners:[],
                files:[],
                owners:[
                  @foreach($owners as $owner)
                    {id:"{{$owner->id}}",name:"{{$owner->name}}",customer_id:"{{$owner->customer_id}}"},
                  @endforeach
                ],
                ownerIds:[],
                ownerNames:[],
                tags:[
                    @foreach($tags as $tag)
                    {name:"{{$tag->id}}",value:"{{$tag->name}}"},
                    @endforeach
                ],
                defaultTag:[],
                selectedTag:[],
                searchTag:[],
                editCustomer:"",
                addCustomerLogIndex:"",
                selectTr:''
            },
            mounted(){
                this._formatOwner();
                $(".up").slideUp();
                $('#container').removeClass('d-none');
                this.rendering();
                let data=[
                    [
                        {name:'company_name',type:'input',tip:'可在检索值前加百分号(%)进行模糊搜索',placeholder:'公司名称'},
                        {name:'contact_man',type:'input',tip:'可在检索值前加百分号(%)进行模糊搜索',placeholder:'联系人'},
                        {name:'phone',type:'input',tip:'可在检索值前加百分号(%)进行模糊搜索',placeholder:'联系电话'},
                        {name:'tags',type:'select_multiple_select',tip:['输入关键词快速定位下拉列表，回车确定','选择要显示的客户'],
                            placeholder:['标签','定位或多选标签'],data:this.tags},
                    ]
                ];
                new query({
                    el:"#form_div",
                    condition:data,
                }).init();
                this.defaultTag = this.tags.slice(0,3);
                this.customers.forEach(customer=>{
                    let selectedTag = {};
                    if (customer.tags)
                        customer.tags.forEach(tag=>{
                            selectedTag[tag.id] = tag.name;
                        });
                    this.selectedTag[customer.id] = selectedTag;
                });
            },
            methods:{
                selectTableRow(id,e){
                    $('#table-customer tr[class=focusing]').removeClass('focusing')
                    if (id === this.selectTr) {
                        this.selectTr = '';
                        return;
                    }
                    this.selectTr = id;
                    $(e.target).parent('tr').addClass('focusing')
                },
                submitTag(index,e){
                    if (e.keyCode === 9)window.event.returnValue = false;
                    if (e.keyCode === 13 || e.keyCode === 9){
                        let val = e.target.value;
                        if (this.searchTag.length>0){
                            let sign = this.searchTag.every(tag=> {
                                if (tag.value === val){
                                    this.selected(index,tag);
                                    return false;
                                }
                                return true;
                            });
                            if (!sign)return;
                        }
                        window.tempTip.postBasicRequest("{{url('customer/customer/customerTag/save')}}",{name:val},res=>{
                            let tag = {name:res.id,value:res.name};
                            this.tags.unshift(tag);
                            this.selected(index,tag);
                            e.target.value = "";
                        });
                    }
                },
                selected(index,tag){
                    let id = this.customers[index].id;
                    if (!this.selectedTag[id])this.selectedTag[id] = {};
                    if (this.selectedTag[id][tag.name]){
                        window.tempTip.postBasicRequest("{{url('customer/customer/delTag')}}",{id:id,tag:tag.name},res=>{
                            this.$delete(this.selectedTag[id],tag.name);
                            this.customers[index].tags.some((t,i)=>{
                                if (t.id == tag.name){
                                    this.$delete(this.customers[index].tags,i);
                                    return true;
                                }
                            });
                            this.$forceUpdate();
                        });
                    }else window.tempTip.postBasicRequest("{{url('customer/customer/addTag')}}",{id:id,tag:tag.name},res=>{
                        this.selectedTag[id][tag.name] = tag.value;
                        let obj = {id:tag.name,name:tag.value};
                        if (this.customers[index].tags) this.customers[index].tags.unshift(obj);
                        else this.customers[index].tags = [obj];
                        this.$forceUpdate();
                    });
                },
                search(e){
                    let val = e.target.value;
                    if (!val){
                        this.searchTag = [];
                        return;
                    }
                    let arr = [];
                    this.tags.forEach(tag=>{
                        if (tag.value.indexOf(val) !== -1)arr.push(tag);
                    });
                    this.searchTag = arr;
                },
                editLogEvent(log){
                    log.isEdit = true;
                },
                textClass(event,isOver){
                    event = event.target.children[0];
                    if (isOver) event.className = "text-overflow-warp";
                    else event.className = "cursor-pointer text-overflow-replace text-secondary";
                },
                editLog(customer_index,event){
                    let description = event.target.value;
                    if (description ===this.customers[customer_index].customer_logs[0].description){
                        this.editCustomer = "";
                        return;
                    }
                    let url="{{url('customer/customer/editLog')}}";
                    let params = {id:this.customers[customer_index].customer_logs[0].id,description:description};
                    window.tempTip.confirm("确定要提交该条日志的修改信息吗？",()=>{
                        window.tempTip.postBasicRequest(url,params,(res)=> {
                            this.customers[customer_index].customer_logs[0].description = description;
                            this.editCustomer = "";
                            this.$forceUpdate();
                            return "修改说明成功！";
                        });
                    });
                },
                editLogStatus(customer_index,log){
                    let url="{{url('customer/customer/editLog')}}";
                    let params = {id:log.id,customer_log_status_id:log.customer_log_status_id};
                    window.tempTip.confirm("确定要提交该条日志的修改信息吗？",()=>{
                        window.tempTip.postBasicRequest(url,params,(res)=> {
                            this.customers[customer_index].customer_logs[0].customer_log_status_id = log.customer_log_status_id;
                            this.logStatuses.some(status=> {
                                if (status.id === log.customer_log_status_id){
                                    this.customers[customer_index].customer_logs[0].status = status;
                                    return true;
                                }
                            });
                            this.editCustomer = "";
                            this.$forceUpdate();
                            return "修改状态成功！";
                        });
                    });
                },
                showLog(index){
                    if(this.customers[index].isShowLog){
                        this.customers[index].isShowLog = false;
                        $("#log-"+this.customers[index].id).slideUp();
                    } else{
                        this.customers[index].isShowLog = true;
                        $("#log-"+this.customers[index].id).slideDown();
                    }
                    this.$forceUpdate();
                },
                delLog(customer_index,log_index){
                    let id = this.customers[customer_index].customer_logs[log_index].id;
                    if (!id){
                        this.$delete(this.customers[customer_index].customer_logs,log_index);
                        return;
                    }
                    window.tempTip.confirm("确定要删除该条日志吗？",()=>{
                        window.tempTip.postBasicRequest("{{url('customer/customer/destroyLog')}}",{id:id},res=>{
                            this.$delete(this.customers[customer_index].customer_logs,log_index);
                            return "已成功删除该条日志记录";
                        });
                    })
                },
                _formatOwner(){
                    let ownerIds=[];
                    let ownerNames=[];
                    this.owners.forEach(owner=>{
                        if (owner.customer_id){
                            if (ownerIds[owner.customer_id]){
                                ownerIds[owner.customer_id].push(owner.id);
                                ownerNames[owner.customer_id].push(owner.name);
                            }else{
                                ownerIds[owner.customer_id] = [owner.id];
                                ownerNames[owner.customer_id] = [owner.name];
                            }
                        }
                    });
                    this.ownerIds = ownerIds;
                    this.ownerNames = ownerNames;
                },
                relatedOwner(index){
                    this.index = index;
                    this.$forceUpdate();
                    let val = this.ownerIds[this.customers[index]['id']];
                    $("#ownerSelectpicker").selectpicker('val',val ? val : []);
                    $("#relatedOwner").modal('show');
                },
                selectFile(){
                    $("#files").click();
                },
                uploadFiles(event){
                    let files = event.target.files;
                    for(let i=0;i<files.length;i++){
                        this.files.push(files[i]);
                    }
                },
                deleteFile(index){
                    this.$delete(this.files,index);
                },
                rendering() {
                    this.customers.forEach((customer,i)=> {
                        let contracts = [];
                        if (customer.owners)
                        customer.owners.forEach(owner=> {
                            if (owner.contracts)
                            owner.contracts.forEach(contract=>{
                                contracts.push({
                                    number:contract.contract_number,
                                    salesman:contract.salesman,
                                    remark:contract.remark,
                                    ownerName:owner.name,
                                    files:contract.files ? contract.files : []});
                            });
                        });
                        customer["contractQuantity"] = contracts.length;
                        this._renderingHtml(customer.id,contracts);
                    });
                    this.$forceUpdate();
                },
                destroy(id,index,name){
                    window.tempTip.confirm("确定要删除客户“"+name+"”吗？",()=>{
                        window.axios.delete("{{url('customer/customer')}}/"+id)
                            .then(res=>{
                                if (res.data.success){
                                    this.$delete(this.customers,index);
                                    window.tempTip.setDuration(2000);
                                    window.tempTip.showSuccess("删除"+name+"成功");
                                    return;
                                }
                                window.tempTip.setDuration(3000);
                                window.tempTip.show(res.data.data);
                            }).catch(err=>{
                            window.tempTip.setDuration(3000);
                            window.tempTip.show("网络错误："+err);
                        })
                    })
                },
                addCustomerLog(index){
                    if (this.customers[index].customer_logs[0] && !this.customers[index].customer_logs[0].id)return;
                    if (!this.logStatuses)window.tempTip.postBasicRequest("{{url('customer/customer/getLogStatus')}}",{},(res)=>{
                        this.logStatuses = res;
                    },true);
                    this.customers[index].customer_logs.unshift({
                        id:"",
                        status:"1",
                        description:"",
                    });
                },
                edit(index,logIndex,user){
                    if (logIndex!==0)return;
                    if (!user)return;
                    let id = "{{\Illuminate\Support\Facades\Auth::id()}}";
                    if (id != user.id) return;
                    if (!this.logStatuses)window.tempTip.postBasicRequest("{{url('customer/customer/getLogStatus')}}",{},(res)=>{
                        this.logStatuses = res;
                    });
                    this.editCustomer = this.customers[index].id;
                },
                addLog(index,logIndex){
                    let exe = ()=>{
                        let url="{{url('customer/customer/storeLog')}}";
                        let params = {customer_id:this.customers[index].id,description:this.customers[index].customer_logs[logIndex].description,customer_log_status_id:this.customers[index].customer_logs[logIndex].status};
                        window.tempTip.postBasicRequest(url,params,(res)=>{
                            this.customers[index].customer_logs[logIndex] = res;
                            this.$forceUpdate();
                            $("#modal").modal('hide');
                            return "成功创建客户日志";
                        },true);
                    };
                    if (!this.customers[index].customer_logs[logIndex].description || !this.customers[index].customer_logs[logIndex].status)return;
                    if (this.customers[index].customer_logs[logIndex].isEdit){
                        exe();
                        return;
                    }
                    setTimeout(()=>{
                        if (this.customers[index].customer_logs[logIndex].isEdit) return;
                        exe();
                        this.customers[index].customer_logs[logIndex].isEdit = false;
                    },500);
                },
                showContract(id){
                    if (this.contractIds['_'+id] &&  this.contractIds['_'+id]===true){
                        this.contractIds['_'+id] = false;
                        $("#customer-"+id).slideUp(undefined,function () {
                            $("#customer-tr-"+id).addClass("d-none");
                        });
                    }else {
                        $("#customer-tr-"+id).removeClass("d-none");
                        this.contractIds['_'+id] = true;
                        $("#customer-"+id).slideDown();
                    }
                    this.$forceUpdate();
                },
                _renderingHtml(id,contracts){
                    let domId   = "customer-"+id;
                    let trDomId = "customer-tr-"+id;
                    let html = "<tr class='d-none' id='"+trDomId+"'><td colspan='12'>"+
                        "<div id='"+domId+"'><table class='table table-sm'>"+
                        "<tr id='contract-tr-"+id+"'><th>合同号</th><th>销售姓名</th><th>所属项目</th><th>备注</th><th>附件</th></tr>";
                    contracts.forEach(contract=> {
                        html = this._createContractList(html,contract.number,contract.salesman,contract.ownerName,contract.remark,contract.files);
                    });
                    html += "</table></div></td></tr>";
                    $("#model-"+id).after(html);
                    $("#"+domId).slideUp();
                },
                _createContractList(html,number,salesman,ownerName,remark,files){
                    html += "<tr><td>"+number+"</td><td>"+salesman+"</td><td>"+ownerName+"</td><td><div style='white-space: normal;max-width: 200px' '>"+remark+"</div</td><td class='p-0'>";
                    if (files) files.forEach(function (file) {
                        let name = file.file_name;
                        let url = '{{url('customer/ownerContract/down?file=')}}'+file.url+"&name="+name;
                        if (file.type === 'jpg' || file.type === 'png'){
                            html += "<a target='_blank' href='"+url+"' class='small'><span class='fa fa-file-image-o'></span>"+name+"</a><br>";
                        }else html += "<a target='_blank' href='"+url+"' class='small'><span class='fa fa-link'></span>"+name+"</a><br>";
                    });
                    html += "</td></tr>";
                    return html;
                },
                addContract(index){
                    this.index = index;
                    let id = this.customers[index].id;
                    if (!this.customerOwners[id]){
                        let url = "{{url("maintenance/owner/get")}}";
                        let params = {customer_id:id};
                        window.tempTip.postBasicRequest(url,params,(res)=> {
                            this.customerOwners[id] = res;
                            this.$forceUpdate();
                        });
                    }
                    $("#addContract").modal("show");
                },
                submitContract(){
                    window.tempTip.setIndex(1099);
                    let owner = document.getElementById("owner").value;
                    let number = document.getElementById("contract_number").value;
                    let remark = document.getElementById("remark").value;
                    if (!owner || !number){
                        window.tempTip.show("项目与合同号为必输项");
                        return;
                    }
                    let formData = new FormData();
                    this.files.forEach(file=>{
                        formData.append("files[]",file);
                    });
                    formData.append("owner_id",owner);
                    formData.append("contract_number",number);
                    formData.append("remark",remark);
                    window.tempTip.setDuration(9999);
                    window.tempTip.waitingTip("新建中,请耐心等候......");
                    window.axios.post('{{url('customer/ownerContract/store')}}',formData,{
                        'Content-Type':'multipart/form-data'
                    }).then(res=>{
                        if (res.data.success){
                            this.files = [];
                            let id = this.customers[this.index].id;
                            let html = this._createContractList('',
                                res.data.data.contract_number,
                                res.data.data.salesman,
                                res.data.data.owner ? res.data.data.owner.name : '',
                                res.data.data.remark,
                                res.data.data.files);
                            this.customers[this.index].contractQuantity += 1;
                            $("#contract-tr-"+id).after(html);
                            window.tempTip.cancelWaitingTip();
                            window.tempTip.setDuration(2000);
                            window.tempTip.showSuccess("新建合同“"+number+"”成功");
                            $("#addContract").modal("hide");
                            return;
                        }
                        window.tempTip.cancelWaitingTip();
                        window.tempTip.setDuration(3000);
                        window.tempTip.show(res.data.data);
                    }).catch(err=>{
                        window.tempTip.cancelWaitingTip();
                        window.tempTip.setDuration(3000);
                        window.tempTip.show('网络错误：'+err);
                    });
                },
                submitRelatedOwner(){
                    let val = $("#ownerSelectpicker").selectpicker('val');
                    let id = this.customers[this.index]['id'];
                    if (val.length>0){
                        let error = "";
                        let storage = [];
                        this.owners.forEach(owner=>{

                            if (owner.customer_id){
                                val.forEach((v,i)=>{
                                    if (v===owner.id&&(owner.customer_id!=id)){
                                        error += owner.name+",";
                                        storage.push(i);
                                    }
                                });
                            }
                        });
                        if (error){
                            window.tempTip.setIndex(1099);
                            window.tempTip.confirm(error+"已绑定其他客户,是否取消选中",()=>{
                                storage.forEach(s=>{
                                    val.splice(s,1);
                                });
                                $("#ownerSelectpicker").selectpicker('val',val);
                            });
                            return;
                        }
                    }
                    let url = "{{url('customer/customer/relatedOwner')}}";
                    let params = {ids:val,customer_id:id};
                    window.tempTip.postBasicRequest(url,params,(res)=>{
                        let names = [];
                        this.owners.forEach(owner=>{
                            if (val.indexOf(owner.id) !== -1)names.push(owner.name);
                        });
                        this.ownerNames[id] = names;
                        this.$forceUpdate();
                        return "关联成功";
                    },true);
                }
            },
            filters:{
                size:function (val) {
                    if (!val)return '';
                    val = Number(parseInt(val/1024));
                    if (val >= 1024){
                        return  parseInt(val/1024)+"MB";
                    }
                    return val+"KB";
                }
            },
        });
    </script>
@stop
