const _Http = getApp().globalData.http,
    currency = require("../../utils/currency"),
    CNY = num => currency(num, {
        symbol: "￥",
        precision: 2
    }).format();

Page({
    loading: true,
    data: {
        tabsActive: 2,
        sa_invoiceapplyid: "",
        tabsList: [{
            label: "详细信息",
            icon: "icon-tabchanpin"
        }, {
            label: "开票单位",
            icon: "icon-tabchanpin"
        }, {
            label: "开票明细",
            icon: "icon-tabchanpin",
            model: "#DetailLine"
        }, {
            label: "发票",
            icon: "icon-tabchanpin",
            model: "#InvoiceList"
        }]
    },
    onLoad(options) {
        this.setData({
            sa_invoiceapplyid: options.id
        });
        this.getDetail();
    },
    getDetail() {
        _Http.basic({
            "id": "20221216143003",
            "version": 1,
            "content": {
                "sa_invoiceapplyid": this.data.sa_invoiceapplyid
            }
        }).then(res => {
            console.log("开票单详情", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            this.setData({
                detail: res.data,
                loading: false
            });
            this.setPreview();
            this.partialRenewal();
        })
    },
    /* 设置显示信息 */
    setPreview() {
        let item = this.data.detail;
        let list1 = [{
            label: "申请单号",
            value: item.billno
        }, {
            label: "发票限额",
            value: CNY(item.quota)
        }, {
            label: "是否手工单",
            value: item.byhand ? '是' : '否'
        }, {
            label: "发票种类",
            value: item.invoiceline
        }, {
            label: "发票收款人",
            value: item.invoicepayee
        }, {
            label: "开票员",
            value: item.invoiceclerk
        }, {
            label: "发票复核人",
            value: item.invoicechecker
        }, {
            label: "提交人",
            value: item.submitby
        }, {
            label: "提交日期",
            value: item.submitdate
        }, {
            label: "备注",
            value: item.remarks
        }];
        let list2 = [{
            label: "状态",
            value: item.status
        }, {
            label: "创建人",
            value: item.createby
        }, {
            label: "创建时间",
            value: item.createdate
        }, {
            label: "审核人",
            value: item.checkby
        }, {
            label: "审核时间",
            value: item.checkdate
        }, {
            label: "修改人",
            value: item.changeby
        }, {
            label: "修改时间",
            value: item.changedate
        }, ];
        let riseLine = [{
            label: "抬头",
            value: item.enterprisename
        }, {
            label: "税号",
            value: item.taxno
        }, {
            label: "开户行",
            value: item.bank
        }, {
            label: "开户账号",
            value: item.bankcardno
        }, {
            label: "开票地址",
            value: item.address
        }, {
            label: "联系方式",
            value: item.phonenumber
        }];
        this.setData({
            list1,
            list2,
            riseLine
        })
    },
    //tabs 切换
    tabsChange({
        detail
    }) {
        this.setData({
            tabsActive: detail
        });
        this.partialRenewal();
    },
    //局部数据更新 tabs
    partialRenewal(init = false) {
        let model = this.data.tabsList[this.data.tabsActive].model;
        if (model) {
            let Component = this.selectComponent(model),
                {
                    total,
                    pageNumber,
                    pageTotal
                } = Component.data.content,
                id = this.data.sa_invoiceapplyid;
            if (total == null || init) {
                Component.getList(id, init);
            } else if (pageNumber <= pageTotal) {
                Component.getList(id, false);
            }
        }
    },
    submit() {
        if (this.selectComponent("#DetailLine").data.list.length == 0) {
            wx.showToast({
                title: '还未添加开票订单行不可提交!',
                icon: "none"
            });
            this.setData({
                tabsActive: 2
            })
        } else {
            let that = this;
            wx.showModal({
                title: '提示',
                content: '是否确认提交该申请单？',
                complete: ({
                    confirm
                }) => {
                    if (confirm) _Http.basic({
                        "id": "20221219133803",
                        "version": 1,
                        "content": {
                            "sa_invoiceapplyid": that.data.sa_invoiceapplyid
                        }
                    }).then(res => {
                        console.log("提交申请单", res);
                        if (res.msg != '成功') return wx.showToast({
                            title: res.msg,
                            icon: "none"
                        });
                        that.getDetail();
                    })
                }
            })
        }
    },
    onReachBottom() {
        this.partialRenewal();
    },
    onUnload() {
        let page = getCurrentPages().find(v => v.__route__ == 'packageA/invoice/index');
        let content = JSON.parse(JSON.stringify(page.data.content));
        content.pageNumber = 1;
        content.pageSize = (page.data.content.pageNumber - 1) * page.data.content.pageSize;
        _Http.basic({
            "id": 20221216143103,
            "version": 1,
            content
        }).then(res => {
            console.log("开票列表", res)
            page.setData({
                list: res.data
            })
        })
    },
})