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

Page({
    data: {
        loading: false,
        showAll: false,
        disabled: true,
        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",
            model: "",
            value: "",
            radio: true,
            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": "",
            "enddate": '',
            "begdate": ""
        }
    },
    onLoad(options) {
        if (options.rowData) {
            let data = JSON.parse(options.rowData);
            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
                }),
                'content.sat_campaignid': data.sat_campaignid
            })
        }
    },
    submit() {
        this.setData({
            loading: true
        })
        let content = {
            ...this.data.content,
            ...this.selectComponent("#Form").submit()
        };
        if (!content.enddate) content.enddate = '';
        _Http.basic({
            "classname": "webmanage.saletool.orderclue.ordercluecampaign",
            "method": "insertormodify_campaign",
            "version": 1,
            content
        }).then(res => {
            this.setData({
                loading: false
            })
            console.log("添加市场活动", res)
            wx.showToast({
                title: res.msg != '成功' ? res.msg : '保存成功',
                icon: "none",
                mask: true
            })
            if (res.msg == '成功') setTimeout(() => {
                const page = getCurrentPages()[getCurrentPages().length - 2];
                if (page.__route__ == 'packageA/activity/detail') {
                    page.getDetail();
                    wx.navigateBack();
                } else if (page.__route__ == 'packageA/activity/index') {
                    wx.redirectTo({
                        url: '/packageA/activity/detail?id=' + res.data.sat_campaignid,
                    })
                }
            }, 300)
        })
    },
    // 是否显示全部
    onChange({
        detail
    }) {
        this.setData({
            showAll: detail
        })
    },
    /* 表单必填项是否完成 */
    onConfirm({
        detail
    }) {
        this.setData({
            disabled: detail
        })
    }
})