// packageI/invoice/invoice-details/invoice-details.js
const app = getApp();
Page({
    data: {
        invoice_id: "",
        language: '',
        detailData: {},
        invoiceType: "",
        orderGoods: [],
        steps: ["订单提交","订单完成","开票完成"],
        stepIndex: 1,

        show: false,
        email: "",
    },

    onLoad: function (options) {
        this.setData({ invoice_id: options.invoice_id });
        this.getInvoiceDetail();
    },

    onReady: function () {
        let language = wx.getStorageSync('langIndex');
        this.setData({ 'language': language.en});
    },

    getInvoiceDetail () {
        let urlStr = app.getNetAddresss("plugin.invoice.frontend.index.details");
        app._getNetWork({
            url: urlStr,
            data: { id: this.data.invoice_id },
            success: ({data: res}) => {
                if (res.result !== 1) {
                    return app.tips(res.msg);
                }
                let steps = [];
                if (res.data.event == 1) {
                    steps = ["订单提交","订单完成","开票完成"];
                } else {
                    steps = ["订单提交","订单付款","开票完成"];
                }
                this.setData({
                    detailData: res.data,
                    invoiceType: res.data.invoice_type,
                    orderData: res.data.has_one_order,
                    orderGoods: res.data.has_one_order?.has_many_order_goods,
                    stepIndex: res.data.order_invoice_status,
                    steps
                });
            }
        })
    },

    showEditEmail () {
        this.setData({
            show: true
        })
    },

    hideEditEmail () {
        this.setData({
            show: false,
            email: "",
        })
    },

    changeEmail (e) {
        this.setData({ email: e.detail.value })
    },
    enterFun (e) {
        if (app._isTextEmpty(this.data.email)) {
            return app.tips("发送邮箱不能为空")
        }
        let urlStr = app.getNetAddresss("plugin.invoice.frontend.index.go-email");
        app._getNetWork({
            url: urlStr,
            data: {
                email: this.data.email,
                invoice_sn: this.data.detailData.invoice_sn
            },
            success: ({data: res}) => {
                app.tips(res.msg);
                if (res.result == 1) {
                    this.hideEditEmail();
                }   
            }
        })
    },

    //预览图片，放大预览
    preview(e) {
        let currentUrl = e.currentTarget.dataset.src;
        wx.previewImage({
            current: currentUrl,
        });
    },

    downpdf (event) {
        let fileUrl = event.currentTarget.dataset.download_url;
        if (app._isTextEmpty(fileUrl)) {
            return app.tips("暂无发票！")
        }
        wx.downloadFile({
            url: fileUrl,
            success (res) {
            var filePath = res.tempFilePath;
            wx.openDocument({
                filePath,
                success () {
                app.tips("文件打开成功");
                },
                fail () {
                app.tips("打开文件失败");
                }
            });
            },
            fail (err) {
            console.log(err, "是失败");
            wx.showModal({
                content: "下载失败，请重新下载"
            });
            }
        });
    },

    noop () {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})