const _Http = getApp().globalData.http;

Page({
    data: {
        "sa_projectid": 0,
        showAll: false,
        form: [{
            label: "项目名称",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "项目名称",
            valueName: "projectname",
            checking: "base",
            required: true
        }, {
            label: "项目类型",
            error: false,
            errMsg: "",
            type: "option",
            optionNmae: "projecttype",
            optionType: "radio", //复选   radio 单选
            value: "",
            placeholder: "选择类型",
            valueName: "projecttype",
            checking: "base",
            required: true
        }, {
            label: "项目等级",
            error: false,
            errMsg: "",
            type: "option",
            optionNmae: "projectgrade",
            optionType: "radio", //复选   radio 单选
            value: "",
            placeholder: "选择项目等级",
            valueName: "grade",
            checking: "base",
            required: false
        }, {
            label: "地区",
            error: false,
            errMsg: "",
            type: "region",
            value: [],
            placeholder: "所属地区 省/市/区",
            valueName: "region",
            required: false
        }, {
            label: "详细地址",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "详细地址",
            valueName: "address",
            checking: "base",
            required: false
        }, {
            label: "项目规模",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "项目规模",
            valueName: "scale",
            checking: "base",
            required: false
        }, {
            label: "项目预算",
            error: false,
            errMsg: "",
            type: "number",
            value: "",
            placeholder: "项目预算",
            valueName: "budgetary",
            checking: "base",
            required: false
        }, {
            label: "预计签约金额",
            error: false,
            errMsg: "",
            type: "number",
            value: "",
            placeholder: "预计签约金额",
            valueName: "signamount_due",
            checking: "base",
            required: false
        }, {
            label: "预计签约月份",
            error: false,
            errMsg: "",
            type: "date",
            fields: "month",
            value: "",
            placeholder: "预计签约月份",
            valueName: "signdate_due",
            checking: "base",
            required: false
        }],
        disabled: true
    },
    onLoad(options) {
        if (options.data) {
            let data = JSON.parse(options.data);
            this.setData({
                disabled: false,
                sa_projectid: data.sa_projectid,
                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
                })
            })
        }
    },
    /* 表单必填项是否完成 */
    onConfirm({
        detail
    }) {
        this.setData({
            disabled: detail
        })
    },
    // 是否显示全部
    onChange({
        detail
    }) {
        this.setData({
            showAll: detail
        })
    },
    submit() {
        let data = this.selectComponent("#Form").submit();
        if (data.region.length != 0) {
            data.province = data.region[0]
            data.city = data.region[1]
            data.county = data.region[2]
        };
        delete(data.region);
        _Http.basic({
            "id": 20221020144202,
            "content": {
                sa_projectid: this.data.sa_projectid,
                ...data
            }
        }).then(res => {
            console.log("新建客户", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            wx.showToast({
                title: '保存成功',
                icon: "none"
            })
            setTimeout(() => {
                wx.navigateBack()
                getCurrentPages().forEach(v => {
                    if (v.getList) v.getList(true);
                    if (['packageA/project/index', 'packageA/project/search'].includes(v.__route__)) {
                        if (this.data.sa_projectid == 0) {
                            v.data.list.push(res.data)
                        } else {
                            let i = v.data.list.findIndex(value => value.sa_projectid == this.data.sa_projectid);
                            if (i != -1) v.data.list[i] = res.data;
                        }
                        v.setData({
                            list: v.data.list
                        })
                    } else if (v.__route__ == 'packageA/project/detail') {
                        v.getDetail();
                    }
                })
                if (this.data.sa_projectid == 0) {
                    wx.navigateTo({
                        url: '/packageA/project/detail?sa_projectid=' + res.data.sa_projectid,
                    })
                }
            }, 300)
        })
    },
})