const _Http = getApp().globalData.http,
    currency = require("../../utils/currency"),
    CNY = (value, symbol = "￥", precision = 2) => currency(value, {
        symbol,
        precision
    }).format();
Component({
    options: {
        addGlobalClass: true
    },
    lifetimes: {
        attached: function () {
            getApp().globalData.Language.getLanguagePackage(this)
            let idName = 'sa_customersid',
                ParamId = 20230713104204;
            switch (this.data.mode) {
                case '项目':
                    ParamId = 20230715112304;
                    idName = 'sa_projectid';
                    break;
                case '业务员':
                    ParamId = 20230717101304;
                    idName = 'hrid';
                    this.setData({
                        dateTypes: ["全部", "本年", "本季", "本月"]
                    })
                    break;
            }
            this.setData({
                idName,
                ParamId,
            })
            _Http.basic({
                "classname": "sysmanage.develop.optiontype.optiontype",
                "method": "optiontypeselect",
                "content": {
                    "pageNumber": 1,
                    "pageSize": 1000,
                    "typename": "contracttype",
                    "parameter": {}
                },
            }).then(res => {
                console.log("合同类型", res)
                if (res.code == 1) {
                    res.data.unshift({
                        remarks: "全部",
                        value: ""
                    })
                    this.setData({
                        typeList: res.data
                    })
                }
            })
        }
    },
    properties: {
        mode: {
            type: String,
            value: "客户"
        },
    },
    data: {
        dateTypes: ["全部", "本年"],
        dateType: "本年",
        showList: false,
        list: [],
        "isAll": 0, //1全部 0本年
        "content": {
            "nocache": true,
            "pageNumber": 1,
            "pageTotal": 1,
            "total": null,
            "where": {
                type: "",
                begdate: "",
                enddate: "",
            }
        },
        typeList: []
    },
    methods: {
        getList(id, init) {
            let content = this.data.content;
            content[this.data.idName] = id;
            if (this.data.mode == "业务员") {
                content.type = 99;
                switch (this.data.dateType) {
                    case '全部':
                        content.type = 0
                        break;
                    case '本年':
                        content.type = 1
                        break;
                    case '本季':
                        content.type = 2
                        break;
                    case '本月':
                        content.type = 3
                        break;
                }
            } else {
                content.isAll = this.data.isAll;
            }
            if (init) {
                content.pageNumber = 1;
                content.total = null;
            }
            if (!this.data.showList && content.total != null) return;
            if (content.pageNumber > content.pageTotal) return;
            _Http.basic({
                "id": this.data.ParamId,
                content
            }).then(res => {
                console.log(this.data.mode + "关联合同", res)
                if (res.code != '1') return wx.showToast({
                    title: res.data,
                    icon: "none"
                })
                content.pageNumber = res.pageNumber + 1;
                content.pageSize = res.pageSize;
                content.pageTotal = res.pageTotal;
                content.total = res.total;
                res.data = res.data.map(v => {
                    let result = "";
                    switch (v.type) {
                        case "框架":
                            result = '经销商合作协议'
                            break;
                        case "直销":
                            result = '直销客户合作协议'
                            break;
                        case "工具借用":
                            result = '工具使用(借用)协议'
                            break;
                        case "居间":
                            result = '居间协议'
                            break;
                        default:
                            result = v.type + '协议'
                            break;
                    };
                    v.type = result
                    v.sumSignAmount = CNY(v.sumSignAmount / (wx.getStorageSync('languagecode') == 'ZH' ? 10000 : 1000));
                    v.inProgressAmount = CNY(v.inProgressAmount / (wx.getStorageSync('languagecode') == 'ZH' ? 10000 : 1000));
                    v.expireAmount = CNY(v.expireAmount / (wx.getStorageSync('languagecode') == 'ZH' ? 10000 : 1000));
                    v.convertOrderContractAmount = CNY(v.convertOrderContractAmount / (wx.getStorageSync('languagecode') == 'ZH' ? 10000 : 1000));
                    v.signAmount = CNY(v.signAmount);
                    return v
                })
                this.setData({
                    list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                    content,
                    id: id,
                });
                try {
                    this.selectComponent("#TimeRange").onCancel()
                } catch (error) {

                }
                try {
                    this.selectComponent("#Filtrate").onCancel()
                } catch (error) {

                }
            })
        },
        changeDate({
            detail
        }) {
            this.setData({
                dateType: detail.dateType,
                isAll: detail.isAll === "" ? 99 : detail.isAll,
                "content.where.begdate": detail.begdate || "",
                "content.where.enddate": detail.enddate || ""
            })
            this.getList(this.data.id, true)
        },
        changeType({
            detail
        }) {
            this.setData({
                "content.where.type": detail,
            })
            this.getList(this.data.id, true)
        },
        shrinkChange({
            detail
        }) {
            this.setData({
                showList: detail
            })
        },
        upDateList() {
            let content = JSON.parse(JSON.stringify(this.data.content));
            try {
                content[this.data.idName] = this.data.id;
                if (this.data.mode == "业务员") {
                    content.type = 99;
                    switch (this.data.dateType) {
                        case '全部':
                            content.type = 0
                            break;
                        case '本年':
                            content.type = 1
                            break;
                        case '本季':
                            content.type = 2
                            break;
                        case '本月':
                            content.type = 3
                            break;
                    }
                } else {
                    content.isAll = this.data.isAll;
                }
                content.pageSize = (content.pageNumber - 1) * content.pageSize;
                content.pageNumber = 1;
            } catch (error) {
                console.log("error", error)
            }
            _Http.basic({
                id: this.data.ParamId,
                content
            }).then(res => {
                console.log(this.data.mode + "更新关联合同", res);
                if (res.code == '1') {
                    res.data = res.data.map(v => {
                        v.sumSignAmount = CNY(v.sumSignAmount / (wx.getStorageSync('languagecode') == 'ZH' ? 10000 : 1000));
                        v.inProgressAmount = CNY(v.inProgressAmount / (wx.getStorageSync('languagecode') == 'ZH' ? 10000 : 1000));
                        v.expireAmount = CNY(v.expireAmount / (wx.getStorageSync('languagecode') == 'ZH' ? 10000 : 1000));
                        v.convertOrderContractAmount = CNY(v.convertOrderContractAmount / (wx.getStorageSync('languagecode') == 'ZH' ? 10000 : 1000));
                        v.signAmount = CNY(v.signAmount);
                        return v
                    })
                    this.setData({
                        list: res.data,
                        "content.total": res.total
                    })
                }
            })
        }
    }
})