const _Http = getApp().globalData.http,
    getTime = require("../../utils/getTime");

Page({
    data: {
        showAll: false,
        form: [{
            label: "客户(企业)",
            error: false,
            errMsg: "",
            type: "label",
            value: "",
            placeholder: "客户(企业)",
            valueName: "enterprisename",
            checking: "base",
            slot: "info",
            required: false
        }, {
            label: "联系人",
            error: false,
            errMsg: "",
            type: "label",
            value: "",
            placeholder: "联系人",
            valueName: "name",
            checking: "base",
            required: false
        }, {
            label: "手机号码",
            error: false,
            checking: "phone",
            errMsg: "",
            type: "number",
            value: "",
            placeholder: "手机号码",
            valueName: "phonenumber", //绑定的字段名称
            required: true, //必填
        }, {
            label: "省市县",
            error: false,
            errMsg: "",
            type: "region",
            value: "",
            placeholder: "省市县",
            valueName: "region",
            checking: "base",
            required: false
        }, {
            label: "详细地址",
            error: false,
            errMsg: "",
            type: "label",
            value: "",
            placeholder: "详细地址",
            valueName: "address",
            checking: "base",
            required: false
        }, {
            label: "项目名称",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "项目名称",
            valueName: "projectname",
            checking: "base",
            required: false
        }, {
            label: "项目规模",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "请填写建筑面积(㎡)/吨位(t)",
            valueName: "scale",
            checking: "base",
            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: "label",
            value: "",
            placeholder: "来源",
            valueName: "cluesource",
            checking: "base",
            required: false
        }, {
            label: "线索概括",
            error: false,
            errMsg: "",
            type: "label",
            value: "",
            placeholder: "线索概括",
            valueName: "notes",
            checking: "base",
            required: 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) {
        //市场活动进入
        if (options.sat_campaignid) {
            let item = this.data.form.find(item => item.label == '市场活动');
            item.value = [options.name, [options.sat_campaignid]];
            item.disabled = true;
            this.setData({
                form: this.data.form
            })
        }
        //编辑进入
        if (options.rowData) {
            // wx.setNavigationBarTitle({
            //   title: 'title',
            // })
            let data = JSON.parse(options.rowData);
            this.setData({
                disabled: false,
                form: this.data.form.map(v => {
                    if (v.valueName != 'region') {
                        if (v.valueName == 'campaign_name') {
                            console.log(data[v.valueName])
                            v.value = data[v.valueName] ? [data[v.valueName],
                                [data.sat_campaignid]
                            ] : ""
                        } else {
                            v.value = data[v.valueName];
                        }
                    } else {
                        v.value = data.province ? [data.province, data.city, data.county] : []
                    }
                    return v
                }),
                'form[0].value': data.enterprisename_customer,
                'content.sat_orderclueid': data.sat_orderclueid,
                'content.sat_campaignid': data.sat_campaignid
            })
        }
    },
    introduce({
        detail
    }) {
        let item = detail;
        this.setData({
            [`form[0].value`]: item.companyName,
            [`form[4].value`]: item.address,
            [`form[3].value`]: [item.regProvince, item.regCity, item.regArea],
        })
    },
    queryClient() {
        let data = this.selectComponent("#Form").query();
        if (data.enterprisename == '') {
            wx.showToast({
                title: `您还未填写企业名称`,
                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()
        };
        console.log(content)
        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)
        _Http.basic({
            "id": "20221205162402",
            content
        }).then(res => {
            console.log("添加线索", res)
            wx.showToast({
                title: res.msg != '成功' ? res.msg : '保存成功',
                icon: "none"
            })
            if (res.msg != '成功') 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
        })
    }
})