const _Http = getApp().globalData.http;
import {
    getTypes
} from "../setclient/modules/query";
/* {
            label: "客户等级",
            index: null,
            showName: "value", //显示字段
            valueKey: "grade", //返回Key
            selectKey: "value", //传参 代表选着字段 不传参返回整个选择对象
            value: "", //选中值
            list: await getTypes('agentgrade')
        }, */
Page({
    data: {
        navList: [{
            label: "全部",
            icon: "icon-webxialaxuanxiangjiantou",
            color: "",
            width: "",
            id: "1"
        }, {
            label: "排序",
            icon: "icon-jiangxu1",
            color: "",
            width: "",
            id: "sort"
        }, {
            label: "筛选",
            icon: "icon-shaixuan",
            color: "",
            width: "",
            id: "2"
        }],
        content: {
            "nocache": true,
            "pageNumber": 1,
            "pageTotal": 1,
            "pageSize": 20,
            isExport: false,
            "where": {},
            "sort": []
        },
    },
    async onLoad(options) {
        getApp().globalData.Language.getLanguagePackage(this, '公海客户');
        this.setData({
            insert: wx.getStorageSync('auth').wPublicCustomer.options.some(v => v == 'insert'), //查询新增权限
        })
        this.getList();
        this.getType();
        let filtratelist = [{
            label: "医院类型",
            index: null,
            showName: "value", //显示字段
            valueKey: "type", //返回Key
            selectKey: "value", //传参 代表选着字段 不传参返回整个选择对象
            value: "", //选中值
            list: await getTypes('customertypemx')
        }, {
            label: "客户分类",
            index: null,
            showName: "value", //显示字段
            valueKey: "customergrade", //返回Key
            selectKey: "value", //传参 代表选着字段 不传参返回整个选择对象
            value: "", //选中值
            list: await getTypes('customergrade')
        }, {
            label: "所属行业",
            index: null,
            showName: "value", //显示字段
            valueKey: "industry", //返回Key
            selectKey: "value", //传参 代表选着字段 不传参返回整个选择对象
            value: "", //选中值
            list: await getTypes('industry')
        }, {
            label: "合作状态",
            index: null,
            showName: "value", //显示字段
            valueKey: "status", //返回Key
            selectKey: "value", //传参 代表选着字段 不传参返回整个选择对象
            value: "", //选中值
            list: [{
                value: "潜在"
            }, {
                value: "合作中"
            }, {
                value: "已终止"
            }, {
                value: "暂缓"
            }]
        }, {
            label: "成交状态",
            index: null,
            showName: "value", //显示字段
            valueKey: "tradingstatus", //返回Key
            selectKey: "value", //传参 代表选着字段 不传参返回整个选择对象
            value: "", //选中值
            list: [{
                value: "未成交"
            }, {
                value: "已成交"
            }, {
                value: "多次成交"
            }]
        }, {
            label: "标签",
            index: null,
            type: "checkbox",
            showName: "tag", //显示字段
            valueKey: "tag", //返回Key
            selectKey: "tag", //传参 代表选着字段 不传参返回整个选择对象
            value: "", //选中值
            list: await getTypes('tags')
        }]
        this.setData({
            filtratelist
        })
    },
    /* 获取列表 */
    getList(init = false) {
        _Http.init(this.data.content, init).then(content => {
            _Http.basic({
                "id": 20221011133602,
                content
            }).then(res => {
                console.log("公海客户列表", res)
                this.selectComponent('#ListBox').RefreshToComplete();
                if (res.code != '1') return wx.showToast({
                    title: res.data,
                    icon: "none"
                })
                this.setData({
                    content: _Http.paging(content, res),
                    list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data)
                });
                this.getTags();
            })
        })
    },
    /* 处理搜索 */
    onSearch({
        detail
    }) {
        this.data.content.where.condition = detail;
        this.getList(true);
    },
    /* 获取列表标签 */
    getTags() {
        let list = this.data.list,
            ownerids = list.map(v => v.sa_customersid);
        _Http.basic({
            "id": 20221018102001,
            "content": {
                nocache: true,
                "ownertable": "sa_customers",
                ownerids
            }
        }).then(res => {
            console.log("标签", res)
            for (let key in res.data) {
                let index = list.findIndex(v => v.sa_customersid == key);
                list[index].tags = res.data[key]
            };
            this.setData({
                list
            })
        })
    },
    /* 获取公海池列表 */
    getType() {
        _Http.basic({
            "classname": "sysmanage.develop.optiontype.optiontype",
            "method": "optiontypeselect",
            "content": {
                "pageNumber": 1,
                "pageSize": 1000,
                "typename": "customerpoolname",
                "parameter": {}
            },
        }, false).then(res => {
            console.log("公海池列表", res)
            let classList = res.data.map(v => {
                return {
                    name: v.poolname,
                    sa_customerpoolid: v.sa_customerpoolid
                }
            });
            classList.unshift({
                name: '全部',
                sa_customerpoolid: ""
            })
            this.setData({
                classList
            })
        })
    },
    classCancel() {
        this.setData({
            classShow: false
        })
    },
    classSelect({
        detail
    }) {
        if (this.data.content.type == detail.index) return this.classClose();
        this.setData({
            "content.type": detail.index,
            'navList[0].label': detail.name
        })
        this.classClose();
        this.getList(true)
    },
    /* 顶部条件导航回调 */
    navClick({
        detail
    }) {
        switch (detail.id) {
            case '1':
                this.setData({
                    classShow: true
                })
                break;
            case '2':
                this.setData({
                    filterShow: true
                })
                break;
        }
    },
    /* 处理筛选 */
    handleFilter({
        detail
    }) {
        detail.condition = this.data.content.where.condition;
        this.setData({
            "content.where": detail
        });
        this.getList(true);
    },
    addPublicCustomer() {
        wx.navigateTo({
            url: './addAndEditor'
        })
    }
})