const _Http = getApp().globalData.http;
import {
    getTypes
} from "./modules/query";
Page({
    data: {
        loading: true,
        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"
        }],
        classShow: false,
        content: {
            nocache: true,
            deleted: 0,
            "type": 1,
            "pageNumber": 1,
            "pageSize": 20,
            "where": {
                "condition": "", //模糊搜索
                "startdate": "",
                "enddate": "",
                "status": "", //项目状态
                "projecttype": "", //项目类型
                "stagename": "", //项目阶段
                reportstatus: "",
                "tag": [] //标签
            },
            "sort": []
        }
    },
    async onLoad(options) {
        getApp().globalData.Language.getLanguagePackage(this, '项目商机');
        this.setData({
            classActions: wx.getStorageSync('templetList').map((v, i) => {
                return {
                    name: v.templetname,
                    index: v.templetid,
                    color: i == 0 ? '#3874F6' : '',
                    i: i,
                }
            }),
            'navList[0].label': wx.getStorageSync('templetList')[0].templetname,
            "content.type": wx.getStorageSync('templetList')[0].templetid,
            insert: wx.getStorageSync('userMsg').siteid != 'HY' && wx.getStorageSync('auth').wproject.options.some(v => v == 'insert'), //查询新增权限
        });
        this.getList();
        let filtratelist = [{
            label: "部门",
            index: null,
            showName: "depname", //显示字段
            valueKey: "sa_saleareaid", //返回Key
            selectKey: "sa_saleareaid", //传参 代表选着字段 不传参返回整个选择对象
            value: "", //选中值
            type: 'multilevelClass',
            list: await _Http.basic({
                "id": 20230620102004,
                "content": {},
            }).then(res => {
                console.log("获取部门", res)
                return res.data.dep
            })
        }, {
            label: "项目类型",
            index: null,
            showName: "value", //显示字段
            valueKey: "projecttype", //返回Key
            selectKey: "value", //传参 代表选着字段 不传参返回整个选择对象
            value: "", //选中值
            list: await getTypes('projecttype')
        }, {
            label: "项目等级",
            index: null,
            showName: "value", //显示字段
            valueKey: "grade", //返回Key
            selectKey: "value", //传参 代表选着字段 不传参返回整个选择对象
            value: "", //选中值
            list: await getTypes('projectgrade')
        }, {
            label: "当前项目阶段",
            index: null,
            showName: "stagename", //显示字段
            valueKey: "stagename", //返回Key
            selectKey: "stagename", //传参 代表选着字段 不传参返回整个选择对象
            value: "", //选中值
            list: await getTypes('stagename')
        }, {
            label: "领域",
            index: null,
            showName: "value", //显示字段
            valueKey: "tradefield", //返回Key
            selectKey: "value", //传参 代表选着字段 不传参返回整个选择对象
            value: "", //选中值
            list: await getTypes('tradefield')
        }, {
            label: "品牌",
            index: null,
            showName: "brandname", //显示字段
            valueKey: "brandname", //返回Key
            selectKey: "brandname", //传参 代表选着字段 不传参返回整个选择对象
            value: "", //选中值
            list: await getTypes('brandname')
        }, {
            label: "项目状态",
            index: null,
            showName: "value", //显示字段
            valueKey: "status", //返回Key
            selectKey: "value", //传参 代表选着字段 不传参返回整个选择对象
            value: "", //选中值
            list: [{
                value: "跟进中"
            }, {
                value: "已成交"
            }, {
                value: "已失败"
            }, {
                value: "已结案"
            }]
        }, {
            label: "报备进度",
            index: null,
            showName: "value", //显示字段
            valueKey: "reportstatus", //返回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
        })
    },
    /* 处理搜索 */
    onSearch({
        detail
    }) {
        this.data.content.where.condition = detail;
        this.getList(true);
    },
    /* 获取列表 */
    getList(init = false) {
        _Http.init(this.data.content, init).then(content => {
            _Http.basic({
                "id": 20221020143502,
                content
            }).then(res => {
                console.log("客户商机列表", res)
                this.selectComponent('#ListBox').RefreshToComplete();
                if (res.code != '1') return wx.showToast({
                    title: res.msg,
                    icon: "none"
                })
                res.data = res.data.map(v => {
                    v.progress = v.stage / v.totalstage * 100;
                    return v
                })
                this.setData({
                    list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                    content: _Http.paging(content, res),
                })
                this.getTags();
            })
        })
    },
    /* 获取列表标签 */
    getTags() {
        let list = this.data.list,
            ownerids = list.map(v => v.sa_projectid);
        _Http.basic({
            "id": 20221018102001,
            "content": {
                nocache: true,
                "ownertable": "sa_project",
                ownerids
            }
        }).then(res => {
            for (let key in res.data) {
                let index = list.findIndex(v => v.sa_projectid == key);
                list[index].tags = res.data[key]
            };
            this.setData({
                list,
                content: this.data.content,
                loading: false
            })
        })
    },
    /* 添加 */
    toAdd() {
        wx.navigateTo({
            url: '/packageA/project/addAndEdit'
        })
    },
    /* 处理筛选 */
    handleFilter({
        detail
    }) {
        detail.condition = this.data.content.where.condition;
        this.data.content.where = detail;
        this.getList(true);
    },
    /* 顶部条件导航回调 */
    navClick({
        detail
    }) {
        switch (detail.id) {
            case '1':
                this.setData({
                    classShow: true
                })
                break;
            case '2':
                this.setData({
                    filterShow: true
                })
                break;
        }
    },
    classClose() {
        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,
            classActions: this.data.classActions.map(v => {
                v.color = detail.i == v.i ? '#3874F6' : ''
                return v
            })
        })
        this.classClose();
        this.getList(true)
    }
})