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: false,
            checking: "base"
        }, {
            label: "账号",
            error: false,
            errMsg: "",
            type: "number",
            value: "",
            placeholder: "银行账户",
            valueName: "bankcardno",
            required: false
        }, {
            label: "开户行",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "开户行",
            valueName: "bank",
            required: false
        }, {
            label: "详细地址",
            error: false,
            errMsg: "",
            type: "textarea",
            value: "",
            placeholder: "例: 浙江省嘉兴市南湖区中山路...",
            valueName: "address",
            required: false,
            checking: "base"
        }, {
            label: "联系人号码",
            error: false,
            errMsg: "",
            type: "phone",
            value: "",
            placeholder: "联系人手机号码",
            valueName: "phonenumber",
            required: false,
            checking: "phone"
        }],
        disabled: true,
        "content": {
            "sys_enterprise_financeid": 0,
            "remarks": "",
            "isdefault": 0,
            sys_enterpriseid: 0
        },
    },
    onLoad(options) {
        if (options.sys_enterpriseid) this.setData({
            "content.sys_enterpriseid": options.sys_enterpriseid
        });
        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.sys_enterpriseid": item.sys_enterpriseid,
                "content.remarks": item.remarks,
                "content.isdefault": item.isdefault,
                disabled: false
            })
        };
    },
    /* 提交数据 */
    submit() {
        let data = this.selectComponent("#Form").submit();
        if (!data || this.data.disabled) return;
        let content = {
            ...this.data.content,
            ...data
        };
        _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(() => {
                getCurrentPages().forEach(v => {
                    switch (v.__route__) {
                        //详情界面更新数据
                        case 'packageA/setclient/modules/financing/detail/index':
                            v.getDetail();
                            break;
                            //列表页更新数据
                        case 'packageA/setclient/detail':
                            let page = v.selectComponent("#Financing"),
                                list = res.data.isdefault == 0 ? page.data.list : page.data.list.map(v => {
                                    v.isdefault = 0;
                                    return v
                                }),
                                index = list.findIndex(value => value.sys_enterprise_financeid == res.data.sys_enterprise_financeid);
                            if (index != -1) {
                                //列表中存在说明是编辑，返回上一级页面并更新数据
                                list[index] = res.data;
                                page.setData({
                                    list
                                });
                                wx.navigateBack();
                            } else {
                                //列表中不存在说明是新增，返回上一级页面更新数据 并进入详情
                                list.push(res.data);
                                page.setData({
                                    list,
                                    "content.total": page.data.content.total + 1
                                });
                                wx.redirectTo({
                                    url: '/packageA/setclient/modules/financing/detail/index?sys_enterprise_financeid=' + res.data.sys_enterprise_financeid
                                })
                            };
                            /* if (res.data.isdefault == 1) setTimeout(() => {
                                page.selectComponent("#List").queryDefault()
                            }, 300) */
                            break;
                        case 'packageA/setclient/modules/financing/search/index':
                            v.getList(true);
                            wx.navigateBack();
                            break;
                    }
                });
            }, 300)
        })
    },
    /* 设置默认 */
    checkboxChange() {
        this.setData({
            "content.isdefault": this.data.content.isdefault == 1 ? 0 : 1
        })
    },
    /* 表单是否填写完成 */
    onConfirm({
        detail
    }) {
        this.setData({
            disabled: detail
        })
    },
    onShareAppMessage() {}
})