// pages/contract/add/add.js
import * as UTIL from '../../../utils/util.js';
import * as API from '../../../utils/API.js';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isIPX: app.globalData.isIPX,
    id:null,
    voteTypeOptions: [],
    voteStatusOptions: [],
    voteResultOptions: [],
    fileList: [],
    form:{}
  },
  back:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({id:options.id})
    
  },
  goForm(e){
    wx.navigateTo({
      url: '../form/form?id='+e.currentTarget.dataset.id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
 
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'vote_type', {method:'GET'}, {
      success: (res) => {
        this.setData({
         voteTypeOptions:res.data,
        })
      }
     })
     UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'vote_status', {method:'GET'}, {
       success: (res) => {
         this.setData({
           voteStatusOptions:res.data,
         })
       }
     })
     UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'vote_result', {method:'GET'}, {
       success: (res) => {
         this.setData({
           voteResultOptions:res.data,
         })
       }
     })
     //获取收入合同状态
     UTIL.httpRequest(API.URL_GET_VOTEDETAIL + this.data.id , {method:'GET'}, {
       success: (res) => {
         res.data.voteStatusText = UTIL.getTransform(res.data.voteStatus,this.data.voteStatusOptions);
         res.data.voteTypeText = UTIL.getTransform(res.data.voteType,this.data.voteTypeOptions);
         res.data.yesForm = [];
         res.data.noForm = [];
         res.data.everForm = [];
         res.data.detailList.map(rr=>{
           rr.voteResultText = UTIL.getTransform(rr.voteResult,this.data.voteResultOptions);
           if(rr.voteResult == 1){
             res.data.yesForm.push(rr);
           }
           if(rr.voteResult == 2){
             res.data.noForm.push(rr);
           }
           if(rr.voteResult == 3){
             res.data.everForm.push(rr);
           }
         })
         console.log(res.data);
         this.setData({'form':res.data});
         this.asyncFun(this.data.id);
       }
     })
  },
  asyncFun(id){
    this.data.form.fileList = [];
    let uploadList = this.data.uploadOptions
    let newList = []
    let _this = this
    let oData = {
      tableId: id,
      tableName: "t_hz_vote", //上传表
      bizPath: "subcontract",
      method:'GET'
    }
    UTIL.httpRequest(API.URL_GET_ATTACHMENTLIST, oData, {
      success: (rr) => {
        if(rr.code==200&&rr.rows.length>0){
          rr.rows.map((rrr,index) => {
            rrr.url = wx.getStorageSync('dressCode') + rrr.fileUrl
            if(index==rr.rows.length-1){
              newList.push(Object.assign({},{"list":rr.rows}))
              _this.setData({"form.fileList":_this.data.fileList.concat(newList)});
            }
          })
          console.log(this.data.fileList);
        }else{
          let newuploadList = uploadList
          newuploadList.map(rd => {
            rd.list = newList
          })
          _this.setData({"form.fileList":newuploadList});
        }
      }
    })
  },
  lookDown(file,detail){
    // 获取指定字符串最后一次出现的位置，返回index
    var index1 = file.detail.url.lastIndexOf('.');
    // substr(start, length) 抽取从start下标开始的length个字符，返回新的字符串;
    var style = file.detail.url.substr(index1 + 1)
    //判断图片类型，不需要下载，不做处理
    if(style=='png'||style=='jpg'||style=='jpeg'||style=='bmp'||style=='gif'||style=='webp'||style=='psd'||style== 'svg'||style=='tiff'){
      //判断非图片类型
    }else{
      wx.downloadFile({
        url: file.detail.url,
        success(data){
          wx.openDocument({
            filePath: data.tempFilePath,
            fileType: style,
            showMenu:true,
            success(res){
            }
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

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

  },

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

  },

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

  },

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

  }
})