// home/components/onGrid/onGrid.js
const {
  getFormSet,getProviceCodeList,IMG_BASE_URL 
} = require('../../../api/api')
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    photo: Object,
    detailObj: Object,
    detail: Object,
  },

  /**
   * 组件的初始数据
   */
  data: {
    powerCompanyList:[],
    contractFile: [],
    contractVideoUrl: [],
    otherAttachment: [],
  },
  ready(){
    this.getProviceCodeList()
    this.getFormSet()
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /** 获取自定义字段 */
    getFormSet() {
      getFormSet({
        stage: '04', //01:信息登记,02：踏勘设计,03：电站安装,04：电站并网
        partner: this.data.detail.management,
      }).then((res) => {
        this.setData({
          formSetObj: res.data.columns
        })
        let resp = this.data.detail       
        if (resp.contract) {//合同文件
          let contractUrl  = resp.contract ? resp.contract.split(",") : [];
          contractUrl.forEach(itm=>{
            this.data.contractFile.push({
              name:itm,
              url:IMG_BASE_URL + itm
            });
          });
          this.setData({
            contractFile:resp.contract? this.data.contractFile: [],
          })         
        }
        if (resp.contract_video_url) {//合同视频
          let contractVideoUrl  = resp.contract_video_url ? resp.contract_video_url.split(",") : [];
          contractVideoUrl.forEach(itm=>{
            this.data.contractVideoUrl.push({
              name:itm,
              url:IMG_BASE_URL + itm
            });
          });
          this.setData({
            contractVideoUrl:resp.contract_video_url? this.data.contractVideoUrl: [],
          })         
        }
        if (resp.other_attachment) {//补充资料
          let url = resp.other_attachment ? resp.other_attachment.split(",") : [];
          url.forEach(itm=>{
            this.data.otherAttachment.push({
              name:itm,
              url:IMG_BASE_URL + itm
            });
          });
          this.setData({
            otherAttachment:resp.other_attachment? this.data.otherAttachment: [],
          })         
        }    
      })
    },
    /** 选择电力公司 **/
    bindpowerCompanyList(e) {
      this.setData({
        [`detail.powerCompanyName`]: this.data.powerCompanyList[e.detail.value].power_company,
        [`detail.provinceCode`]: this.data.powerCompanyList[e.detail.value].code
      })
    },
    /** 获取电力公司 **/
    getProviceCodeList(e) {
      getProviceCodeList({}).then((res) => {
        if (res.code == 200) {
          let list = res.data.map(itm=>itm.columns);
          this.setData({
            powerCompanyList: list
          })
        }
      })
    },
    //打开pdf文件或者大图查看图片
    openDocument(res) {
      let path = res.currentTarget.dataset.path
      if (path) {
        let suffix = path.substring(path.lastIndexOf('.') + 1) //获取文件后缀
        if (suffix == 'pdf') {
          wx.showLoading({
            title: '加载中',
          })
          wx.downloadFile({
            url: IMG_BASE_URL + path,
            complete: function (res) {
              wx.hideLoading()
              const filePath = res.tempFilePath
              wx.openDocument({
                filePath: filePath,
                showMenu: true,
                success: function (res) {}
              })
            }
          })
        } else {
          wx.previewImage({
            urls: [IMG_BASE_URL + path] // 需要预览的图片http链接列表
          })
        }
      }
    },
    //打开外链
    openWeb(e){
      wx.navigateTo({
        url: '../webView/webView',
        success: function (res) {
          // 通过eventChannel向被打开页面传送数据
          res.eventChannel.emit('acceptDataFromOpenerPage', {
            data: e.currentTarget.dataset.path
          })
        }
      })
    },
    //打开pdf文件或者大图查看图片
    openDocument(res) {
      let path = res.currentTarget.dataset.path
      if (path) {
        let suffix = path.substring(path.lastIndexOf('.') + 1) //获取文件后缀
        if(suffix=='mp4'){// 开始预览多媒体内容
          wx.previewMedia({ 
            sources: [{
              url :IMG_BASE_URL + path,
              type: 'video'
            }], // 需要预览的资源列表
          });     
        }else if(suffix != 'jpg' && suffix != 'png' && suffix != 'gif' && suffix != 'jpeg') {
          wx.showLoading({
            title: '加载中',
          })
          wx.downloadFile({
            url: IMG_BASE_URL + path,
            complete: function (res) {
              wx.hideLoading()
              const filePath = res.tempFilePath
              wx.openDocument({
                filePath: filePath,
                fileType: suffix,
                showMenu: true,
                success: function (res) {}
              })
            }
          })
        }else{
          wx.previewImage({
            urls: [IMG_BASE_URL + path] // 需要预览的图片http链接列表
          })
        }
      }
    },
  }
})