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

Page({
    data: {
        showAll: false,
        disabled: true,
        content: {
            "sa_paybillid": 0,
            "sys_enterpriseid": '',
            "payer": "",
            "paydate": "",
            "amount": "",
            "remarks": "",
            "bank": "",
            "bankcardno": ""
        },
        form: [{
                label: "经销商",
                error: false,
                errMsg: "",
                type: "route",
                url: "/select/agent/index",
                params: {
                    "classname": "webmanage.sale.agents.agents",
                    "method": "query_agentList",
                    "content": {
                        "pageNumber": 1,
                        "pageSize": 20,
                        "where": {
                            "condition": ""
                        }
                    }
                },
                query: '&radio=true',
                value: "",
                placeholder: "选择经销商信息",
                valueName: "sys_enterpriseid",
                checking: "base",
                required: true,
                interrupt: true
            },
            {
                label: "付款人",
                error: false,
                errMsg: "",
                type: "text",
                value: "",
                placeholder: "付款人",
                valueName: "payer",
                required: false,
                disabled: true
            }, {
                label: "银行账号",
                error: false,
                errMsg: "",
                type: "text",
                value: "",
                placeholder: "银行账号",
                valueName: "bankcardno",
                required: false,
                disabled: true

            }, {
                label: "银行",
                error: false,
                errMsg: "",
                type: "text",
                value: "",
                placeholder: "银行",
                valueName: "bank",
                required: false,
                disabled: true
            }, {
                label: "付款时间",
                error: false,
                errMsg: "",
                type: "date",
                value: getTime.formatTime(new Date(), '-').split(' ')[0],
                placeholder: "请选择付款时间",
                valueName: "paydate",
                required: true,
            }, {
                label: "打款总金额",
                error: false,
                errMsg: "",
                type: "digit",
                value: "",
                placeholder: "请输入打款总金额",
                valueName: "amount",
                required: true,

            }, {
                label: "转入银行",
                error: false,
                errMsg: "",
                type: "text",
                value: "",
                placeholder: "请输入转入银行",
                valueName: "inbank",
                required: false,
            },
            {
                label: "转入账户",
                error: false,
                errMsg: "",
                type: "text",
                value: "",
                placeholder: "请输入转入账户",
                valueName: "inbankcardno",
                required: false,
            },
            {
                label: "备注",
                error: false,
                errMsg: "",
                type: "text",
                value: "",
                placeholder: "请输入备注",
                valueName: "remarks",
                required: false,
            },
        ]
    },
    onLoad(options) {
        if (options.rowData) {
            // wx.setNavigationBarTitle({
            //   title: 'title',
            // })
            let data = JSON.parse(options.rowData);
            console.log(data);
            this.setData({
                form: this.data.form.map(v => {
                    if (v.valueName != 'region') {
                        if (v.valueName == 'enterprisename') {
                            v.value = [data[v.valueName]]
                        } else {
                            v.value = data[v.valueName];
                        }
                    } else {
                        v.value = data.province ? [data.province, data.city, data.county] : []
                    }
                    return v
                }),
                'form[0].value': [data.enterprisename, [data.sys_enterpriseid]],
                'content.sa_paybillid': data.sa_paybillid
            })
            this.selectComponent('#Form').confirm()
            console.log(this.data.form);
        }
    },
    /* 打断处理form */
    interrupt({
        detail
    }) {
        console.log("打断", detail)
        detail.form[1].value = ''
        detail.form[2].value = ''
        detail.form[3].value = ''
        detail.form[detail.temporary.index].value = detail.data.value
        this.queryMoneyInfo(detail.data.id[0], detail.form)

        this.selectComponent('#Form').confirm()
        wx.navigateBack()
    },
    /* 获取财务信息 */
    async queryMoneyInfo(id, form) {
        let res = await _Http.basic({
            "id": 20221013160602,
            "content": {
                "sys_enterpriseid": id,
                "pageNumber": 1,
                "pageSize": 20,
                "where": {
                    "condition": ""
                }
            },
        })
        form[1].value = res.data[0].enterprisename
        form[2].value = res.data[0].bank
        form[3].value = res.data[0].bankcardno
        this.setData({
            form: form
        })
        console.log(this.data.form);
    },
    async submit() {
        let content = {
            ...this.data.content,
            ...this.selectComponent("#Form").submit()
        }
        content.sys_enterpriseid = content.sys_enterpriseid[1][0] || ''
        let res = await _Http.basic({
            "id": "20221226153004",
            content
        })
        console.log(res);
        if (res.msg == '成功') {
            let page = getCurrentPages()[getCurrentPages().length - 2];
            if (page.__route__ == 'packageA/remitVoucher/index') {
                wx.redirectTo({
                    url: '/packageA/remitVoucher/detail?id=' + res.data.sa_paybillid,
                })
            } else if (page.__route__ == 'packageA/remitVoucher/detail') {
                wx.navigateBack();
                page.getDetail(true)
            }
        } else {
            wx.showToast({
                title: res.msg,
                icon: "none",
                mask: true
            })
        }
    },
    // 是否显示全部
    onChange({
        detail
    }) {
        this.setData({
            showAll: detail
        })
    },
    /* 表单必填项是否完成 */
    onConfirm({
        detail
    }) {
        this.setData({
            disabled: detail
        })
    }
})