const _Http = getApp().globalData.http;
Page({
    data: {
        form: [{
            label: "名称",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "企业名称",
            valueName: "enterprisename",
            required: true,
            checking: "base"
        }, {
            label: "纳税人识别号",
            error: false,
            errMsg: "",
            type: "number",
            value: "",
            placeholder: "纳税人识别号",
            valueName: "taxno",
            required: true,
            checking: "base"
        }, {
            label: "账号",
            error: false,
            errMsg: "",
            type: "number",
            value: [],
            placeholder: "银行账户",
            valueName: "bankcardno",
            required: true
        }, {
            label: "开户行",
            error: false,
            errMsg: "",
            type: "textarea",
            value: [],
            placeholder: "开户行",
            valueName: "bank",
            required: true
        }, {
            label: "详细地址",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "例: 浙江省嘉兴市南湖区中山路...",
            valueName: "address",
            required: true,
            checking: "base"
        }, {
            label: "联系人号码",
            error: false,
            errMsg: "",
            type: "phone",
            value: "",
            placeholder: "联系人手机号码",
            valueName: "phonenumber",
            required: true,
            checking: "phone"
        }],
        disabled: true,
        "content": {
            "sys_enterprise_financeid": 0,
            "remarks": "",
            "isdefault": 0
        },
    },
    onLoad(options) {
        if (options.data) {
            let item = JSON.parse(options.data)
            this.setData({
                form: this.data.form.map(v => {
                    v.value = item[v.valueName]
                    return v
                }),
                "content.sys_enterprise_financeid": item.sys_enterprise_financeid,
                "content.remarks": item.remarks,
                "content.isdefault": item.isdefault,
                disabled: false
            })
        }
    },
    /* 提交数据 */
    submit() {
        let data = this.selectComponent("#Form").submit();
        if (!data || this.data.disabled) return;
        let pages = getCurrentPages(),
            content = {
                ...this.data.content,
                ...data
            };
        content.sys_enterpriseid = pages[pages.length - 2].data.detail.sys_enterpriseid;
        _Http.basic({
            "id": "20221013160402",
            content
        }).then(res => {
            console.log("添加财务信息", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            });
            wx.showToast({
                title: '保存成功',
                icon: "none"
            });
            setTimeout(() => {
                let pages = getCurrentPages();
                pages[pages.length - 2].partialRenewal(true);
                wx.navigateBack();
            }, 500)
        })
    },
    /* 设置默认 */
    checkboxChange() {
        this.setData({
            "content.isdefault": this.data.content.isdefault == 1 ? 0 : 1
        })
    },
    /* 表单是否填写完成 */
    onConfirm({
        detail
    }) {
        this.setData({
            disabled: detail
        })
    },
    onShareAppMessage() {}
})