// pages/dangerousProject/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    loadShow:false,
    loginName:'',
    userName:'',
    deptId:'',
    projectName:'',
    projectId:'',
    prjectRelation:{},
    //参数
    timeline:[],
    changeNameList:[],
    changeName:'',
    changeContext:'',
    allPoint:0,
    outPoint:0,
    comPoint:0,
    approval:0,
    titleindex:0,

    fileList: [],
    id:'',
    initData:{},

  },
 
  //项目切换 返回值
  onProjectSelect(e){
    this.onClickShow();
    let projectId = e.detail.id;
    let projectName = e.detail.text;
    app.globalData.projectId = projectId;
    app.globalData.projectName = projectName;
    this.setData({
      projectId:projectId,
      projectName:projectName,
      changeNameList:[],
      titleindex:0,
      changeName:'',
      allPoint:0,
      outPoint:0,
      comPoint:0,
      approval:0,
      timeline:[]
    })
	  this.onLoad();
  },

  onImagesArr(e){
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      //图片上传地址
      url: app.globalData.reqUrl+'/weixin/security/fileUpload',
      filePath: e.detail[0],
      name: 'file',
      header: {
        "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      formData: { user: 'test' },
      success:res => {
        // 上传完成需要更新 fileList
        let data = JSON.parse(res.data);
        let fileList = this.data.fileList
        fileList.push({url: data.url});
        this.setData({ fileList:fileList});
        this.updateDanger(res.data,e.currentTarget.dataset.id)
      },
    });
  },
  
  showPopup() {
    this.setData({ show: true });
    
  },

  onClose() {
    this.setData({ show: false });
  },

  onClickShow() {
    this.setData({ loadShow: true });
  },

  onClickHide() {
    this.setData({ loadShow: false });
  },

  // 项目头部切换
  onClickTitle(e){
    this.setData({
      titleindex:e.target.dataset.id,
      changeName:'',
      changeNameList:[]
    });
    this.getChangeSignature();
  },

  onSelectDangerName(e){
    this.setData({
      changeName:e.detail.text,
    })
    this.getChangeSignature();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    //加载蒙版
    that.onClickShow();
    //获取缓存数据
    wx.getStorage({
      key: 'userinfo',
      success:function(res){
        that.setData({
          loginName:res.data.loginName,
          userName:res.data.userName,
          deptId:res.data.deptId,
          projectName: app.globalData.projectName,
          projectId:app.globalData.projectId,
          initData:{text:app.globalData.projectName,id:app.globalData.projectId}
        }) 
        that.getProjectCorrespondence(); 
      }
    }) 
  },

    /**
   * 项目对应关系
   */
  getProjectCorrespondence() {
    var that = this;
    wx.request({
      url: app.globalData.reqUrl + '/weixin/training/getProjectCorrespondence',
      data: {
        "projectId": that.data.projectId,
      },
      method: "get",
      success: function (res) {
        that.setData({
          prjectRelation:res.data
        })
        that.getChangeSignature();
      }
    })
  },

  /**
   * 变更签认
   */
  getChangeSignature(){
    var that = this;
    wx.request({
      url: app.globalData.szhUrl+'/system/alterationEndorseConfig/list1',
      method: 'post',
      data: {
        pageNum: 1,
        pageSize: 9999,
        projectId: that.data.prjectRelation.szh,
        alterationType:that.data.titleindex,
        alterationName:that.data.changeName
      },
      success: resData => {
        that.onClickHide();
        let data = resData.data.rows;
        //总数
        let allPoint = data.length;
        //已通过
        let comPoint = 0;
        //审批中
        let approval = 0;
        //不通过
        let outPoint = 0;
        //变更名称
        let changeNameList = [];
        //变更列表
        let timeline = [];
        if(data.length > 0){
          for(let i = 0;i<data.length;i++){
            if(data[i].alterationStatus == 0){
              comPoint++;
            }else if(data[i].alterationStatus == 1){
              approval++;
            }else{
              outPoint++;
            }
  
            if(data[i].alterationName != '' && data[i].alterationName != null){
              if(changeNameList.length > 0){
                for(let j = 0;j<changeNameList.length;j++){
                  if(changeNameList[j].text != data[i].alterationName){
                    changeNameList.push({id:data[i].id,text:data[i].alterationName,context:data[i].alterationContext});
                  }
                }
              }else{
                changeNameList.push({id:data[i].id,text:data[i].alterationName,context:data[i].alterationContext});
              }   
            }
  
            let supervisor = {};
            let design = {};
            let owner = {};
            for (var key in data[i]) {
              if(key == 'uploadSupervisorTime'){
                supervisor.escalation = data[i][key];
                supervisor.title = '监理'
                supervisor.fieldNameOne = '上报监理日期'
              }else if(key == 'approvalSupervisorTime'){
                supervisor.approval = data[i][key];
                supervisor.title = '监理'
                supervisor.fieldNameTwo = '监理批复日期'
              }else if(key == 'approvalSupervisorStatus'){
                supervisor.approvalContent = data[i][key];
                supervisor.title = '监理'
                supervisor.fieldNameThree = '监理审核情况'
              }else if(key == 'uploadDesignTime'){
                design.escalation = data[i][key];
                design.title = '设计'
                design.fieldNameOne = '上报设计日期'
              }else if(key == 'approvalDesignTime'){
                design.approval = data[i][key];
                design.title = '设计'
                design.fieldNameTwo = '设计批复日期'
              }else if(key == 'approvalDesignStatus'){
                design.approvalContent = data[i][key];
                design.title = '设计'
                design.fieldNameThree = '设计审核情况'
              }else if(key == 'uploadOwnerTime'){
                owner.escalation = data[i][key];
                owner.title = '业主'
                owner.fieldNameOne = '上报业主日期'
              }else if(key == 'approvalOwnerTime'){
                owner.approval = data[i][key] == null?'':data[i][key];
                owner.title = '业主'
                owner.fieldNameTwo = '业主批复日期'
              }else if(key == 'alterationStatus'){
                if(data[i][key] == 0){
                  owner.approvalContent = '已通过';
                }else if(data[i][key] == 1){
                  owner.approvalContent = '审批中';
                }else{
                  owner.approvalContent = '未通过';
                } 
                owner.title = '业主'
                owner.fieldNameThree = '业主批复状态'
              }
            }
            timeline.push(supervisor);
            timeline.push(design);
            timeline.push(owner);
          } 
        }       
        that.setData({
          allPoint:allPoint,
          comPoint:comPoint,
          approval:approval,
          outPoint:outPoint,
          changeNameList:changeNameList,
          changeName:changeNameList.length>0?changeNameList[0].text:'',
          changeContext:changeNameList.length>0?changeNameList[0].context:'',
          timeline:timeline
        })
      }
    })
  },


  /**
   * 图片上传
   */
  updateDanger:function(fileUrl,id){
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      url:app.globalData.reqUrl+'/weixin/security/updateDanger',
      data:{
        "deptId":this.data.deptId,
        "projectId":this.data.projectId,
        "id":id,
        "fileUrl":fileUrl,
        "subName":this.data.loginName,
      },
      method:"POST",
     success:function(res){
       console.log("======"+res.data.code);
     }
    })
  },

  /**
   * 文件信息查看
   * @param {*} e 
   */
  previewImg(e){
    let suffix = e.currentTarget.dataset.suffix;
    let url = e.currentTarget.dataset.url;
    let images = [url];
    if(suffix == 'pdf'){
      wx.downloadFile({
        url: url,
        success: function (res) {
          const filePath = res.tempFilePath
          wx.openDocument({
            filePath: filePath,
            success: function (res) {
              console.log('打开文档成功')
            }
          })
        }
      })
    }else{
      wx.previewImage({
        current: url,     //当前图片地址
        urls: images,               //所有要预览的图片的地址集合 数组形式
        success: function(res) {},
        fail: function(res) {},
        complete: function(res) {},
      })
    }
  },

  /**
   * 返回到技术管理
   */
  goGCLB:function(){
    wx.redirectTo({
      url: '../../pages/technical-management/index'
    })
  },
})