const _Http = getApp().globalData.http;

Page({
    data: {
        loading: false,
        showAll: false,
        "content": {
            "sat_orderclueid": 0,
            "enterprisename": "",
            "name": "",
            "phonenumber": "",
            "province": "",
            "city": "",
            "county": "",
            "address": "",
            "notes": "",
            "cluesource": "",
            "sat_campaignid": 0, //活动id，当活动id大于0是默认业务员创建
            "isprivate": 0 //0表示公海，1表示私域
        },
        disabled: true
    },
    onLoad(options) {
        const isHy = ['HY', "YOSTEST1"].includes(wx.getStorageSync('userMsg').siteid)
        getApp().globalData.Language.getLanguagePackage(this, '添加线索');
        let form = [{
            label: "项目名称",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "项目名称",
            valueName: "projectname",
            checking: "base",
            required: false
        }, {
            label: "医院名称",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            placeholder: "医院名称",
            valueName: "enterprisename",
            checking: "base",
            slot: "info",
            required: false
        }, {
            label: "联系人",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "联系人名称",
            valueName: "name",
            checking: "base",
            slot: "TXL",
            required: true
        }, {
            label: "联系方式",
            error: false,
            errMsg: "",
            hint: "",
            type: "radio",
            value: "0",
            radioList: [{
                id: '0',
                name: '手机号'
            }, {
                id: '1',
                name: '座机电话'
            }],
            valueName: "isTelephone", //绑定的字段名称
            required: true, //必填
            interrupt: true,
            checking: `true`,
            phonenumber: "",
            telephone: ["", ""]
        }, {
            label: "手机号",
            error: false,
            errMsg: "",
            type: "number",
            value: "",
            placeholder: "联系人手机号码",
            valueName: "phonenumber",
            required: true,
            checking: "phone"
        }, {
            label: "微信",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            placeholder: "微信号",
            valueName: "wechatnum",
            checking: "base",
            required: false
        }, {
            label: isHy ? '项目地址' : "省市县",
            error: false,
            errMsg: "",
            type: "region",
            value: "",
            placeholder: "省市县",
            valueName: "region",
            checking: "base",
            required: false
        }, {
            label: isHy ? ' ' : "详细地址",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            placeholder: "详细地址",
            valueName: "address",
            checking: "base",
            required: false
        }, {
            label: "项目规模",
            error: false,
            errMsg: "",
            type: "digit",
            value: "",
            placeholder: "请填写数值",
            valueName: "scale",
            checking: "base",
            required: false
        }, {
            label: "项目规模单位",
            error: false,
            errMsg: "",
            type: "option",
            optionNmae: "scaleunitname",
            optionType: "radio", //复选   radio 单选
            value: "",
            placeholder: "选择单位",
            valueName: "unitname",
            required: false
        }, {
            label: "总投资额(万元)",
            error: false,
            errMsg: "",
            type: "digit",
            value: "",
            placeholder: "请填写金额",
            valueName: "totalinvestment",
            checking: "base",
            required: false
        }, {
            label: "造价(万元)",
            error: false,
            errMsg: "",
            type: "digit",
            value: "",
            placeholder: "请填写金额",
            valueName: "costofconstruction",
            checking: "base",
            required: false
        }, {
            label: "预计开工时间",
            error: false,
            errMsg: "",
            type: "date",
            fields: "month",
            value: "",
            placeholder: "预计开工时间",
            valueName: "begdate_due",
            checking: "base",
            required: false
        }, {
            label: "预计完工时间",
            error: false,
            errMsg: "",
            type: "date",
            fields: "month",
            value: "",
            placeholder: "预计完工时间",
            valueName: "enddate_due",
            checking: "base",
            required: false
        }, {
            label: "市场活动",
            error: false,
            errMsg: "",
            type: "route",
            url: "/packageA/select/selectActivity/index",
            value: "",
            params: {
                "id": 20221101095102,
                "content": {
                    nocache: true,
                    isAll: 0,
                    "where": {
                        'condition': '',
                        "unend": 1
                    }
                }
            },
            query: "&radio=true",
            placeholder: "选择市场活动",
            valueName: "campaign_name",
            checking: "base",
            required: false
        }, {
            label: "领域",
            error: false,
            errMsg: "",
            type: "selector",
            range: [],
            rangeKey: "name",
            selectKey: "tradefield",
            rangeIndex: "",
            value: "",
            placeholder: "领域",
            valueName: "tradefield",
            required: true, //必填
        }, {
            label: "来源",
            error: false,
            errMsg: "",
            type: "selector",
            range: [],
            rangeKey: "name",
            selectKey: "value",
            rangeIndex: "",
            value: "",
            placeholder: "来源",
            valueName: "cluesource",
            checking: "base",
            required: true
        }, {
            label: "线索概括",
            error: false,
            errMsg: "",
            type: "text",
            value: "",
            placeholder: "线索概括",
            valueName: "notes",
            checking: "base",
            required: false
        }];
        //市场活动进入
        if (options.sat_campaignid) {
            let item = form.find(item => item.label == '市场活动');
            item.value = [options.name, [options.sat_campaignid]];
            item.disabled = true;
            this.data.content.sat_campaignid = options.sat_campaignid;
        }
        //海盐站点
        if (wx.getStorageSync('userMsg').siteid == 'HY') form = form.filter(v => !["项目预算(万元)", "总投资额(万元)", "造价(万元)"].includes(v.label))
        //编辑进入
        if (options.rowData) {
            let data = JSON.parse(options.rowData);
            if (data.phonenumber.includes("-")) {
                let index = form.findIndex(v => v.label == '手机号');
                data.telephone1 = data.phonenumber.split("-");
                form[index] = {
                    label: "座机电话",
                    error: false,
                    errMsg: "",
                    type: "telephone",
                    value: ["", ""],
                    valueName: "telephone1",
                    required: true,
                    checking: "telephone"
                }
            }
            this.setData({
                disabled: false,
                form: form.map(v => {
                    if (v.valueName == 'region') {
                        v.value = data.province ? [data.province, data.city, data.county] : []
                    } else if (v.valueName == 'isTelephone') {
                        if (data.phonenumber.includes("-")) {
                            v.value = '1'
                            v.telephone = data.telephone1;
                        } else {
                            v.phonenumber = data.phonenumber
                        }
                    } else if (v.valueName == 'campaign_name') {
                        v.value = data[v.valueName] ? [data[v.valueName],
                            [data.sat_campaignid]
                        ] : ""
                    } else {
                        v.value = data[v.valueName];
                    }
                    return v
                }),
                'form[1].value': data.enterprisename_customer,
                'content.sat_orderclueid': data.sat_orderclueid,
                'content.sat_campaignid': data.sat_campaignid
            })
        } else {
            _Http.basic({
                "classname": "sysmanage.develop.optiontype.optiontype",
                "method": "optiontypeselect",
                "content": {
                    pageNumber: 1,
                    "pageSize": 1,
                    "typename": "scaleunitname",
                    "parameter": {
                        "siteid": wx.getStorageSync('siteP').siteid
                    }
                }
            }).then(res => {
                console.log("项目规模", res)
                if (res.code == '1') {
                    let index = form.findIndex(v => v.label == '项目规模单位');
                    if (index != -1) form[index].value = res.data[0].value;
                }
                this.setData({
                    form
                })
            })
        }
        /* 获取领域 */
        _Http.basic({
            "id": 20221223141802,
            "content": {
                "pageNumber": 1,
                "pageSize": 99999,
                "where": {
                    "condition": ""
                }
            }
        }).then(res => {
            console.log("获取领域", res)
            if (res.code == '1') {
                let item = form.find(v => v.label == '领域');
                item.range = res.code == '1' ? res.data.map(v => {
                    v.name = getApp().globalData.Language.getMapText(v.tradefield)
                    return v
                }) : [];
                this.setData({
                    form
                })
            }
        })
        /* 获取来源 */
        _Http.basic({
            "classname": "sysmanage.develop.optiontype.optiontype",
            "method": "optiontypeselect",
            "content": {
                "typename": "cluesource",
                pageSize: 9999
            }
        }).then(res => {
            console.log("获取线索来源", res)
            if (res.code == '1') {
                let item = form.find(v => v.label == '来源');
                item.range = res.code == '1' ? res.data.map(v => {
                    v.name = getApp().globalData.Language.getMapText(v.value)
                    return v
                }) : [];
                this.setData({
                    form
                })
            }
        })
        this.selectComponent("#Form").confirm();
    },
    /* 打断 */
    interrupt({
        detail
    }) {
        let {
            data,
            form,
            temporary
        } = detail;
        if (data.label == '联系方式') {
            let index = temporary.index + 1
            if (data.value == 0) {
                data.telephone = form[index].value;
                if (data.telephone.join() == ',') data.telephone = "";
                form[index] = {
                    label: "手机号",
                    error: false,
                    errMsg: "",
                    type: "number",
                    value: data.phonenumber,
                    placeholder: "联系人手机号码",
                    valueName: "phonenumber",
                    required: true,
                    checking: "phone"
                }
            } else {
                if (typeof (form[index].value) == 'object') form[index].value = "";
                data.phonenumber = form[index].value;
                form[index] = {
                    label: "座机电话",
                    error: false,
                    errMsg: "",
                    type: "telephone",
                    value: data.telephone || ["", ""],
                    valueName: "telephone1",
                    required: true,
                    checking: "telephone"
                }
            };
            this.setData({
                form
            })
        }
    },
    /* 打开通讯录 */
    openContacts() {
        let that = this;
        let page = that.selectComponent("#Form");
        let form = page.data.form,
            index = form.findIndex(v => v.label == '联系人');
        if (form[index].disabled) return;
        wx.navigateTo({
            url: '/packageA/setclient/contacts',
        })
        getApp().globalData.handleSelect = (item) => {
            wx.showModal({
                title: getApp().globalData.Language.getMapText('提示'),
                content: getApp().globalData.Language.getMapText('是否确定选择') + item.name,
                cancelText: getApp().globalData.Language.getMapText('取消'),
                confirmText: getApp().globalData.Language.getMapText('确定'),
                complete: ({
                    confirm
                }) => {
                    if (confirm) {
                        form[index].value = item.name;
                        if (item.phonenumber.indexOf('-') !== -1) {
                            form[index + 1].value = "1";
                            form[index + 2] = {
                                label: "座机电话",
                                error: false,
                                errMsg: "",
                                type: "telephone",
                                value: item.phonenumber.split("-"),
                                valueName: "telephone1",
                                required: true,
                                checking: "telephone"
                            }
                        } else {
                            form[index + 1].value = "0";
                            form[index + 2] = {
                                label: "手机号",
                                error: false,
                                errMsg: "",
                                type: "number",
                                value: item.phonenumber,
                                placeholder: "联系人手机号码",
                                valueName: "phonenumber",
                                required: true,
                                checking: "phone"
                            }
                        }
                        that.setData({
                            form
                        })
                        wx.navigateBack()
                        getApp().globalData.handleSelect = null;
                        page.confirm()
                    }
                }
            })
        };
    },
    introduce({
        detail
    }) {
        let item = detail;
        this.setData({
            [`form[1].value`]: item.companyName,
            [`form[6].value`]: [item.regProvince ? item.regProvince : item.regCity, item.regCity, item.regArea],
            [`form[7].value`]: item.address,
        })
    },
    queryClient() {
        let data = this.selectComponent("#Form").query();
        if (data.enterprisename == '') {
            wx.showToast({
                title: getApp().globalData.Language.getMapText('您还未填写企业名称'),
                icon: "none"
            });
        } else {
            this.setData({
                form: this.selectComponent("#Form").data.form,
            })
            this.selectComponent("#Info").queryClient(data.enterprisename)
        }
    },
    submit() {
        let content = {
            ...this.data.content,
            ...this.selectComponent("#Form").submit()
        };
        content.sat_campaignid = content.campaign_name ? content.campaign_name[1][0] : 0;
        content.city = content.region[1] || "";
        content.county = content.region[2] || "";
        content.province = content.region[0] || "";
        delete(content.region)
        delete(content.campaign_name)
        if (content.enterprisename == "" && content.projectname == "") return wx.showToast({
            title: getApp().globalData.Language.getMapText('您还未填写企业名称'),
            icon: "none"
        });
        if (content.scale.length != 0 && content.scale == 0) return wx.showToast({
            title: getApp().globalData.Language.getMapText('项目规模不可为0'),
            icon: "none"
        })
        content.phonenumber = content.isTelephone == 0 ? content.phonenumber || '' : content.telephone1.join("-") || '';
        this.setData({
            loading: true
        })
        delete content.telephone1;
        delete content.isTelephone;
        _Http.basic({
            "id": "20221205162402",
            content
        }).then(res => {
            console.log("添加线索", res)
            this.setData({
                loading: false
            })
            wx.showToast({
                title: res.code != '1' ? res.msg : getApp().globalData.Language.getMapText('保存成功'),
                icon: "none"
            })
            if (res.code != '1') return;
            setTimeout(() => {
                wx.navigateBack();
                getCurrentPages().forEach(v => {
                    console.log(v, '路径');
                    if (v.__route__ == 'packageA/publicClue/detail') {
                        v.getDetail();
                    } else if (v.__route__ == 'packageA/publicClue/index') {
                        v.getList(true);
                    } else if (v.__route__ == 'packageA/activity/detail') {
                        v.getDetail()
                        v.selectComponent('#Clue').getList(this.data.content.sat_campaignid, true)
                    }
                })
            }, 300)
        })
    },
    // 是否显示全部
    onChange({
        detail
    }) {
        this.setData({
            showAll: detail
        })
    },
    /* 表单必填项是否完成 */
    onConfirm({
        detail
    }) {
        this.setData({
            disabled: detail
        })
    }
})