const _Http = getApp().globalData.http;

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"
        }],
        classShow: false,
        content: {
            nocache: true,
            deleted: 0,
            "type": 1,
            "pageNumber": 1,
            "pageSize": 20,
            "where": {
                "condition": "", //模糊搜索
                "startdate": "",
                "enddate": "",
                "status": "", //项目状态
                "projecttype": "", //项目类型
                "stagename": "", //项目阶段
                "tag": "" //标签
            },
            "sort": []
        },
        filter: {
            show: false,
            status: ['跟进中', '已成交', '已失败'], //状态项
            statusActive: "",
            projectTypeList: [], //项目类型项
            typeActive: "",
            tagList: [], //标签
            tagActive: "",
            stageList: [], //项目阶段项目
            stageActive: "",
            startdate: "",
            enddate: ""
        }
    },
    onLoad(options) {
        this.setData({
            classActions: wx.getStorageSync('templetList').map(v => {
                return {
                    name: v.templetname,
                    index: v.templetid
                }
            }),
            'navList[0].label': wx.getStorageSync('templetList')[0].templetname,
            "content.type": wx.getStorageSync('templetList')[0].templetid,
            insert: wx.getStorageSync('auth').wproject.options.some(v => v == 'insert'), //查询新增权限
        });
        this.getList();
        //获取所有阶段
        this.getStage();
        //获取所有标签
        this.getTagList()
    },
    /* 处理搜索 */
    onSearch({
        detail
    }) {
        this.data.content.where.condition = detail;
        this.getList(true);

    },
    /* 获取列表 */
    getList(init = false, data) {
        if (init.detail != undefined) init = init.detail;
        let content = this.data.content;
        if (init) content.pageNumber = 1;
        if (content.pageNumber > content.pageTotal) return;
        if (data) {
            content.where.status = data.statusActive;
            content.where.projecttype = data.typeActive;
            content.where.stagename = data.stageActive;
            content.where.tag = data.tagActive;
            content.where.startdate = data.startdate;
            content.where.enddate = data.enddate;
        }
        _Http.basic({
            "id": 20221020143502,
            content
        }).then(res => {
            console.log("客户商机列表", res)
            this.selectComponent('#ListBox').RefreshToComplete();
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            content.pageNumber = res.pageNumber + 1;
            content.pageTotal = res.pageTotal;
            content.total = res.total;
            content.sort = res.sort;
            res.data = res.data.map(v => {
                v.progress = v.stage / v.totalstage * 100;
                return v
            })
            this.data.list = res.pageNumber == 1 ? res.data : this.data.list.concat(res.data)
            this.setListHeight();
            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
            })
        })
    },
    /* 添加 */
    toAdd() {
        wx.navigateTo({
            url: '/packageA/project/addAndEdit'
        })
    },
    /* 处理筛选 */
    handleFilter({
        detail
    }) {
        const data = this.data.filter;
        switch (detail) {
            case 'confirm':
                this.setData({
                    'filter.show': false
                });
                this.getList(true, data);
                break;
            case 'reset':
                this.setData({
                    'filter.statusActive': "",
                    'filter.typeActive': "",
                    'filter.tagActive': "",
                    'filter.stageActive': "",
                    'filter.startdate': "",
                    'filter.enddate': "",
                });
                this.getList(true, this.data.filter)
                break;
            case 'close':
                this.setData({
                    'filter.show': false
                });
                break;
        }
    },
    /* 筛选状态选择 */
    selectStatus(e) {
        const {
            item
        } = e.currentTarget.dataset;
        this.setData({
            "filter.statusActive": this.data.filter.statusActive == item ? "" : item
        })
    },
    /* 筛选标签选择 */
    selectTag(e) {
        const {
            item
        } = e.currentTarget.dataset;
        this.setData({
            "filter.tagActive": this.data.filter.tagActive == item.tag ? "" : item.tag
        })
    },
    /* 筛选项目类 */
    selectTypeName(e) {
        const {
            value
        } = e.currentTarget.dataset.item;
        if (this.data.filter.typeActive == value) return;
        this.setData({
            "filter.typeActive": value
        });
    },
    /* 筛选阶段 */
    stageStatus(e) {
        const {
            stagename
        } = e.currentTarget.dataset.item;
        this.setData({
            "filter.stageActive": stagename
        })
    },
    /* 筛选日期范围 */
    changeDate(e) {
        const name = e.currentTarget.dataset.name,
            value = e.detail.value;
        this.setData({
            [`filter.${name}`]: value
        })
    },
    /* 顶部条件导航回调 */
    navClick({
        detail
    }) {
        switch (detail.id) {
            case '1':
                this.setData({
                    classShow: true
                })
                break;
            case '2':
                _Http.basic({
                    "classname": "sysmanage.develop.optiontype.optiontype",
                    "method": "optiontypeselect",
                    "content": {
                        nocache: true,
                        "pageNumber": 1,
                        "pageSize": 999,
                        "typename": "projecttype",
                        "parameter": {
                            "siteid": wx.getStorageSync('siteP').siteid
                        }
                    }
                }).then(res => {
                    console.log("项目类型", res)
                    if (res.msg != '成功') return wx.showToast({
                        title: res.data,
                        icon: "none"
                    })
                    this.setData({
                        'filter.show': true,
                        'filter.projectTypeList': res.data,
                    })
                })
                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
        })
        this.classClose();
        this.getList(true)
    },
    onReady() {
        this.setListHeight();
    },
    setListHeight() {
        this.selectComponent("#ListBox").setHeight(".total", this);
    },
    /* 获取所有阶段 */
    getStage() {
        _Http.basic({
            "id": 20221116104502,
            "content": {
                nocache: true,
            }
        }, false).then(res => {
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            })
            this.setData({
                "filter.stageList": res.data
            })
        })
    },
    /* 获取所有标签 */
    getTagList() {
        _Http.basic({
            "id": 20220929085401,
            "content": {
                "ownertable": 'sa_project',
                "ownerid": ""
            }
        }, false).then(res => {
            console.log("标签", res)
            this.setData({
                "filter.tagList": res.data.option
            })
        })
    },
    onShareAppMessage() {}
})