// pages/invoiceInfo/index.js
import { getInvoiceInfoDetail, downloadInvoice } from "../../service/invoice";
import { alertError, alertInfo, alertSuccess, loadingFunc } from "../../utils/index";
import request from '../../utils/request'
const { OK } = request;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail: {},
    id: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const { id } = options;
    this.init(id);
  },
  async init(id) {
    const { code, message, data } = await getInvoiceInfoDetail(id)
    this.setData({ detail: data, id });
  },
  handleBack() {
    wx.navigateBack()
  },
  jumpToDetail() {
    wx.redirectTo({
      url: '/pages/invoiceDetailContent/index?id=' + this.data.id,
    })
  },
  download() {
    const that = this;
    wx.showActionSheet({
      itemList: ['发票XML文件下载', '发票PDF文件下载'],
      async success(sheetRes) {
        loadingFunc(async () => {
          const { code, message, data } = await downloadInvoice(that.data.id, sheetRes.tapIndex);
          if (code !== OK) {
            alertInfo(message)
            return;
          }
          if (!data) {
            alertInfo("暂无文档信息")
            return
          }
          if (sheetRes.tapIndex === 0) {
            wx.showModal({
              title: '提示',
              content: '暂不支持xml格式预览，请复制链接后打开浏览器下载',
              confirmText: '复制链接',
              showCancel: true,
              complete: (res) => {
                if (res.cancel) {
                  
                }
            
                if (res.confirm) {
                  wx.setClipboardData({
                    data,
                    success: () => {
                      alertSuccess("复制成功")
                    }
                  })
                }
              }
            })
            return;
          }
          wx.downloadFile({
            url: data,
            success(res) {
              if (res.statusCode === 200) {
                if (!res.tempFilePath) {
                  alertError("没有开票信息")
                  return;
                }
                wx.openDocument({
                  filePath: res.tempFilePath,
                  showMenu: true,
                  // fileType: sheetRes.tapIndex === 0 ? 'xml' : "pdf", // 3. 这个必须写合法类型，不然下载不了 ！！！
                  success: function (res) {
                    
                  },
                  fail: function (e) {
                    // alertError("打开失败")
                    wx.showModal({
                      title: '提示',
                      content: '打开失败，请复制链接后通过浏览器打开',
                      complete: (res) => {
                        if (res.cancel) {
                          
                        }
                    
                        if (res.confirm) {
                          wx.setClipboardData({
                            data: data,
                            success: () => {
                              alertSuccess("复制成功")
                            }
                          })
                        }
                      }
                    })
                  }
                })
              }
            },
            fail: (e) => {
              console.log('e', e)
              alertInfo("下载文件失败")
            }
          })
        })
      }
    })
  },
})