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: "textarea",
            value: "",
            placeholder: "开户银行",
            valueName: "bank",
            required: false
        }, {
            label: "开户账号",
            error: false,
            errMsg: "",
            type: "number",
            value: "",
            placeholder: "开户银行账户",
            valueName: "bankcardno",
            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 => {
                    if (v.__route__ == 'packageA/setclient/modules/financing/detail/index') {
                        //详情界面更新数据
                        v.getDetail();
                    } else if (['packageA/setclient/detail', 'packageA/publicCustomer/detail'].includes(v.__route__)) {
                        //列表页更新数据
                        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) */
                    } else if (v.__route__ == 'packageA/setclient/modules/financing/search/index') {
                        v.getList(true);
                        wx.navigateBack();
                    }
                });
            }, 300)
        })
    },
    /* 设置默认 */
    checkboxChange() {
        this.setData({
            "content.isdefault": this.data.content.isdefault == 1 ? 0 : 1
        })
    },
    /* 表单是否填写完成 */
    onConfirm({
        detail
    }) {
        this.setData({
            disabled: detail
        })
    },
    onShareAppMessage() {}
})