import { API } from './../../../utils/network/api'
import debounce from './../../../utils/debounce'
let api = new API()
const app = getApp()
Page({
  data: {
    navbarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '病历信息', //导航栏 中间的标题
    },
    height: app.globalData.height,
    order_inquiry_id: "",
    case_detail: {},
    imglist: []
  },
  onLoad(options){
    console.log("onLoad", options)
    this.setData({
      order_inquiry_id: options.order_inquiry_id
    })

  },
  goReport:debounce(function(event){
    wx.showLoading({
      mask:true,
      title: '正在打开文件...',
    })
    const url=event.currentTarget.dataset.url;
    const randfile = new Date().getTime() + '检测报告';
    const newPath = `${wx.env.USER_DATA_PATH}/${randfile}`;
    wx.downloadFile({
      // 示例 url，并非真实存在
      url:url,
      filePath: newPath,
      success: function (res) {
        //const filePath = res.tempFilePath
        wx.openDocument({
          fileType:"pdf",
          showMenu:true,
          filePath: newPath,
          success: function (res) {
            console.log('打开文档成功');
            wx.hideLoading()
          },
          fail:function(error){
            wx.hideLoading()
            wx.showToast({
              title:res,
              icon:"none"
            })
          }
        })
      },
      fail:function(error){
        wx.showToast({
          title:error,
          icon:"none"
        })
      }
     
    })
  }),
  formatImgList(){
    let diagnose_images = this.data.case_detail.diagnose_images?this.data.case_detail.diagnose_images:[];
    let img_list = [];
    diagnose_images.forEach(item => {
      let img = {};
      img.isImage = true;
      img.url = item;
      img_list.push(img);
    })
    this.setData({
      imglist: img_list
    })
  },
  onShow(){
    //获取患者问诊病例
    api.getDoctorInquiryCase({order_inquiry_id: this.data.order_inquiry_id}).then(response => {
      console.log(response);
      this.setData({
        case_detail: response.data
      })
    }).then(res => {
      this.formatImgList();
    }).catch(errors => {console.error(errors);})
  },

})