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

Page({
    data: {
        showAll: false,
        form: [{
            label: "活动类型",
            error: false,
            errMsg: "",
            type: "option",
            optionNmae: "campaigntype",
            optionType: "radio", //复选   radio 单选
            value: "",
            placeholder: "选择类型",
            valueName: "type",
            checking: "base",
            required: true
        }, {
            label: "市场活动名称",
            error: false,
            errMsg: "",
            type: "label",
            value: "",
            placeholder: "市场活动名称",
            valueName: "name",
            checking: "base",
            required: true
        }, {
            label: "负责人",
            error: false,
            errMsg: "",
            type: "label",
            value: "",
            placeholder: "负责人",
            valueName: "director",
            checking: "base",
            required: false
        }, {
            label: "地址",
            error: false,
            errMsg: "",
            type: "label",
            value: "",
            placeholder: "地址",
            valueName: "address",
            checking: "base",
            required: false
        }, {
            label: "开始日期",
            error: false,
            errMsg: "",
            type: "date",
            start: getTime.formatTime(new Date(), '-').split(' ')[0],
            value: getTime.formatTime(new Date(), '-').split(' ')[0],
            placeholder: "报价日期",
            valueName: "begdate",
            checking: "base",
            required: true
        }, {
            label: "结束日期",
            error: false,
            errMsg: "",
            type: "date",
            start: getTime.formatTime(new Date(), '-').split(' ')[0],
            value: "",
            placeholder: "结束时间",
            valueName: "enddate",
            checking: "base",
            required: false
        }],
        "content": {
            "sat_campaignid": "0",
            "name":"",
            "director":"",
            "address":"",
            "type":"",
        },
        disabled: true
    },
    onLoad(options) {
        return console.log(options);
        if (options.data) {
            let data = JSON.parse(options.data);
            this.setData({
                disabled: false,
                form: this.data.form.map(v => {
                    if (v.valueName != 'region') {
                        v.value = data[v.valueName];
                    } else {
                        v.value = data.province ? [data.province, data.city, data.county] : []
                    }
                    return v
                })
            })
        }
    },
    /* 打断处理form */
    interrupt(e) {
        let {
            data,
            form
        } = e.detail;
        console.log("处理", data, form)
        //选择客户
        if (data.valueName == "sa_projectid") {
            let obj = {
                label: "选择客户",
                error: false,
                errMsg: "",
                type: "route",
                url: "/packageA/select/setclient/select",
                radio: true,
                value: "",
                params: {
                    id: 20221027143702,
                    "content": {
                        nocache: true,
                        "sa_projectid": data.value[1][0],
                        "pageNumber": 1,
                        "pageSize": 20,
                        "where": {
                            "condition": ""
                        }
                    },
                },
                placeholder: "选择客户",
                valueName: "sys_enterpriseid",
                checking: "base",
                required: true
            };
            if (form[1].label == "选择客户") {
                form[1] = obj
            } else {
                form.splice(1, 0, obj)
            }
            this.setData({
                form
            })
        } else if (data.valueName == "sys_enterpriseid") {
            let obj = {
                label: "联系人",
                error: false,
                errMsg: "",
                type: "route",
                url: "/packageA/select/contacts/select",
                model: "#Form",
                value: "",
                radio: true,
                params: {
                    "id": "20221022165503",
                    "version": 1,
                    "content": {
                        "sys_enterpriseid": data.value[1][0],
                        "where": {
                            "condition": "",
                            "workaddress": 0
                        }
                    }
                },
                placeholder: "选择客户联系人",
                valueName: "contactsid",
                checking: "base",
                required: true
            };
            if (form[2].label == "联系人") {
                form[2] = obj
            } else {
                form.splice(2, 0, obj)
            }
            this.setData({
                form
            })
        }
    },
    submit() {
        let content = {
            ...this.data.content,
            ...this.selectComponent("#Form").submit()
        };
        _Http.basic({
            "classname":"webmanage.saletool.orderclue.ordercluecampaign",
            "method": "insertormodify_campaign",
            "version": 1,
            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 => {
                    if (v.getList) v.getList(true);
                })
            }, 300)
        })
    },
    // 是否显示全部
    onChange({
        detail
    }) {
        this.setData({
            showAll: detail
        })
    },
    /* 表单必填项是否完成 */
    onConfirm({
        detail
    }) {
        this.setData({
            disabled: detail
        })
    }
})