// pageage/project_checking/info/index.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id:"",
        infoData:{},
        imageList:[],
        minImageList:[],      
        loadShow:false,
        loginName:"",
        showDel:false,
        signFileNames:[],
        minSignFileImages:[],
        signFileImages:[],
        alterationFileNames:[],
        minAlterationFileImages:[],
        alterationFileImages:[],
        fileUrls:[],
        fileUrls2:[],
        activeName:"",
        flowRecordList:[],
        request:app.globalData.reqUrl
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let {id} = options
        //获取缓存数据
        wx.getStorage({
        key: 'userinfo',
        success:res=>{
          this.setData({
            id,
            loginName:res.data.loginName
          }) 
          this.getInfo(); 
          this.getAuditinfo();
        }  
        }) 
    },

    /**
     * 获取安全检查详情信息
     * 
     */
    getInfo(){
        let {id} = this.data
        let that = this
        wx.request({
          url: app.globalData.reqUrl+'/wechat/projectMaterialSeal/info',
          method:"get",
          data:{
              id:id          
          },
          success(res){
            res = res.data
            if(res.code == 200){
                let urls = [];
                let minUrls = [];
                if(res.data.imageUrls){
                  res.data.imageUrls.split(',').forEach(element => {
                    urls.push(that.data.request+element);
                    minUrls.push(that.data.request+element+'.min.jpg');
                  });
                }
                let fileNames = [];
                let minFileImages = [];
                let fileImages = [];
                let fileUrls = [];
                //判断附件
                if(res.data.signFiles){
                  res.data.signFiles.split(',').forEach(element => {
                    let _file = element.split('.');
                    _file = _file[_file.length-1].toLocaleUpperCase();
                    //判断附件类型，如果是图片直接展示，非图片则显示附件
                    if(_file=="PNG"||_file=="JPG"||_file=="JPEG"){
                      fileImages.push(that.data.request+element);
                      minFileImages.push(that.data.request+element+'.min.jpg');
                    }else{
                      let it = element.split('/');
                      fileNames.push(it[it.length-1]);
                      fileUrls.push(element);
                    }
                  });
                }
                let fileNames2 = [];
                let minFileImages2 = [];
                let fileImages2 = [];
                let fileUrls2 = [];
                //判断附件
                if(res.data.alterationFiles){
                  res.data.alterationFiles.split(',').forEach(element => {
                    let _file = element.split('.');
                    _file = _file[_file.length-1].toLocaleUpperCase();
                    //判断附件类型，如果是图片直接展示，非图片则显示附件
                    if(_file=="PNG"||_file=="JPG"||_file=="JPEG"){
                      fileImages2.push(that.data.request+element);
                      minFileImages2.push(that.data.request+element+'.min.jpg');
                    }else{
                      let it = element.split('/');
                      fileNames2.push(it[it.length-1]);
                      fileUrls2.push(element);
                    }
                  });
                }
                that.setData({
                    fileUrls,
                    fileUrls2,
                    signFileNames:fileNames,
                    minSignFileImages:minFileImages,
                    signFileImages:fileImages,
                    alterationFileNames:fileNames2,
                    minAlterationFileImages:minFileImages2,
                    alterationFileImages:fileImages2,
                    infoData:res.data,
                    imageList:urls,
                    minImageList:minUrls,
                    loadShow:false
                })
                //判断当前能否删除
                if(res.data.createBy==that.data.loginName && res.data.approveStatus!="4"){
                  that.setData({
                    showDel:true
                })
                }
            }
          }
        })
    },

  onDelete(){
    //弹出确认
    let that = this
    wx.showModal({
      title: '提示',
      content: '是否确定删除此条数据？',
      success: function (sm) {
        if (sm.confirm) {
            // 用户点击了确定 可以调用了
            that.deleteData();
          } else if (sm.cancel) {
            console.log('用户点击取消');
          }
        }
     }) 
  },  
  deleteData(){
      let {id} = this.data
      let that = this;
      wx.request({
        url: app.globalData.reqUrl+"/wechat/projectMaterialSeal/remove",
        header: {
          "Username": this.data.loginName,
        },
        data:{
          id:id          
        },
        method:"get",
        success(res){
          app.toast("删除成功！")
          wx.redirectTo({
            url: `../list/index`
          })
        }
      })
  },

  //跳转修改页面
  onUpdate:function(){
    let id = this.data.id;
    wx.redirectTo({
      url: `../edit/index?id=${id}`,
    })
  },

  /**
     * 查询流程日志
     */
    getAuditinfo(){
      let that = this
      wx.request({
        url: app.globalData.reqUrl+'/wechat/projectAuditinfo/selectProjectAuditinfo',
        method:"get",
        data:{
            fromType:"4",
            fromId:this.data.id          
        },
        success(res){
          res = res.data
          if(res.code == 200){
            that.setData({
              flowRecordList:res.data
            })
          }
        }
      })
  },

  // 手风琴
  onChange(event) {
    this.setData({
      activeName: event.detail,
    });
  },

    //展示图片
    showImg:function(e){
      var that=this;
      wx.previewImage({
          urls: that.data.imageList,
          current: that.data.imageList[e.currentTarget.dataset.index]
      })
  },

      //展示图片
      showSignFileImg:function(e){
        var that=this;
        wx.previewImage({
            urls: that.data.sifnFileImages,
            current: that.data.sifnFileImages[e.currentTarget.dataset.index]
        })
    },

      //展示图片
      showAlterationFileImg:function(e){
        var that=this;
        wx.previewImage({
            urls: that.data.alterationFileImages,
            current: that.data.alterationFileImages[e.currentTarget.dataset.index]
        })
    },

    downFile:function(e){
      let idx = e.currentTarget.dataset['index'];
      let that = this;
      wx.downloadFile({
        // 示例 url，并非真实存在
        url: app.globalData.uploadUrl+'/common/download/resource?resource='+that.data.fileUrls[idx],
        success: function (res) {
          const filePath = res.tempFilePath
          wx.openDocument({
              filePath: filePath,
              success: function (res) {
                  console.log('打开文档成功')
              },
              fail:function(res) {
                  console.log(res)
              }
          })
        }
    })
    //app.toast("暂不支持下载！如需下载请前往后台管理系统！！")
    },

    downFile2:function(e){
      let idx = e.currentTarget.dataset['index'];
      let that = this;
      wx.downloadFile({
        // 示例 url，并非真实存在
        url: app.globalData.uploadUrl+'/common/download/resource?resource='+that.data.fileUrls2[idx],
        success: function (res) {
          const filePath = res.tempFilePath
          wx.openDocument({
              filePath: filePath,
              success: function (res) {
                  console.log('打开文档成功')
              },
              fail:function(res) {
                  console.log(res)
              }
          })
        }
    })
    //app.toast("暂不支持下载！如需下载请前往后台管理系统！！")
    },

  returnToPage: function () {
    /*关闭当前页面，跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面*/
    wx.redirectTo({
        url: '../list/index'
    })
  },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

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

    }
})