// pages/apply/paymentTemplate/see/see.js
import * as UTIL from '../../../../utils/util.js';
import * as API from '../../../../utils/API.js';
let EVN_CONFIG = require('../../../../env/env');
const DISTRIBUTE_ENVIROMENT = 'IMGURL';
let {
    URL_PREFIX,
} = EVN_CONFIG[DISTRIBUTE_ENVIROMENT];
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isIPX: app.globalData.isIPX,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let array = JSON.parse(options.array);
    //所属银行
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'bank_type', {method:'GET'}, {
      success: (res) => {
        this.setData({
          bankTypeOptions:res.data
        });
      }
    })
    // 资金支出类别字典查询
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'capital_expenditure_type', {method:'GET'}, {
      success: (res) => {
        that.setData({
          capitalExpenditureTypeOptions:res.data,
        })
      }
    })
    // 付款方式类型字典查询 
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'transfer_type', {method:'GET'}, {
      success: (res) => {
        that.setData({
          transferTypeOptions:res.data,
        })
      }
    })
    // 项目工程列表 
    UTIL.httpRequest(API.URL_GET_GETPROJECTLIST , {method:'GET'}, {
      success: (res) => {
        that.setData({
          projectOptions:res.rows,
        })
      }
    })
    // 工程款类型字典查询 
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'project_fund_type', {method:'GET'}, {
      success: (res) => {
        that.setData({
          projectFundTypeOptions:res.data,
        })
      }
    })
    // 获取合同信息列表
    UTIL.httpRequest(API.URL_GET_CONTRACTIONLIST , {method:'GET'}, {
      success: (res) => {
        that.setData({
          contractionOptions:res.rows,
        })
      }
    })
    array.transfers.forEach( (item,index) => {
      item.capitalExpenditureTypeText = UTIL.getTransform(item.capitalExpenditureType,that.data.capitalExpenditureTypeOptions);
      item.transferTypeText = UTIL.getTransform(item.transferType,that.data.transferTypeOptions);

      item.bankTypeText = UTIL.getTransform(item.bankType,that.data.bankTypeOptions);

      item.payeeList.forEach( (response,i) => {
        item.payeeList[i].bankTypeText = UTIL.getTransform(response.bankType,that.data.bankTypeOptions);
      })
      //收据附件
      UTIL.httpRequest(
        API.URL_GET_FINDLIST, 
        {
          method:'GET',
          tableName:'t_yinnong_transfer',
          tableId:item.id,
          fileType:1
        }, 
        {
        success: (res) => {
          
          let list = [];
          
          res.data.forEach((item2,index2)=>{
            
            wx.downloadFile({
              url: URL_PREFIX+item2.fileUrl, //仅为示例，并非真实的资源
              success (response) {
                if (response.statusCode === 200) {
                  let fileForm = item.fileForm?item.fileForm:[];
                  fileForm.push({
                    file: response.tempFilePath,
                    fileType:'1',
                    bizPath:'transfer',
                    tableName:'t_yinnong_transfer',
                    tableId:''
                  })
                  console.log("--------------fileForm2 start----------------");
                  console.log(fileForm);
                  console.log("--------------fileForm2 end----------------");
                  list.push({
                    tempFilePath:response.tempFilePath
                  })
                  that.setData({
                    ["form.transfers["+index+"].SJimage"]:list,
                    ["form.transfers["+index+"].fileForm"]:fileForm
                  });
                }
              }
            })
          })
          
        }
      })
      //发票
      UTIL.httpRequest(
        API.URL_GET_FINDLIST, 
        {
          method:'GET',
          tableName:'t_yinnong_transfer',
          tableId:item.id,
          fileType:2
        }, 
        {
        success: (res) => {
          let list = [];
          res.data.forEach((item2,index2)=>{
            wx.downloadFile({
              url: URL_PREFIX+item2.fileUrl, //仅为示例，并非真实的资源
              success (response) {
                if (response.statusCode === 200) {
                  let fileForm = item.fileForm?item.fileForm:[];
                  fileForm.push({
                    file: response.tempFilePath,
                    fileType:'2',
                    bizPath:'transfer',
                    tableName:'t_yinnong_transfer',
                    tableId:item.id
                  })
                  list.push({
                    tempFilePath:response.tempFilePath
                  })
                  that.setData({
                    ["form.transfers["+index+"].FPimage"]:list,
                    ["form.transfers["+index+"].fileForm"]:fileForm
                  });
                }
              }
            })
          })
        }
      })
      //其他
      UTIL.httpRequest(
        API.URL_GET_FINDLIST, 
        {
          method:'GET',
          tableName:'t_yinnong_transfer',
          tableId:item.id,
          fileType:3
        }, 
        {
        success: (res) => {
          let list = [];
          let fileForm = item.fileForm?item.fileForm:[];
          res.data.forEach((item2,index2)=>{
            wx.downloadFile({
              url: URL_PREFIX+item2.fileUrl, //仅为示例，并非真实的资源
              success (response) {
                if (response.statusCode === 200) {
                  let fileForm = item.fileForm?item.fileForm:[];
                  fileForm.push({
                    file: response.tempFilePath,
                    fileType:'3',
                    bizPath:'transfer',
                    tableName:'t_yinnong_transfer',
                    tableId:item.id
                  })
                  list.push({
                    tempFilePath:response.tempFilePath
                  })
                  that.setData({
                    ["form.transfers["+index+"].QTimage"]:list,
                    ["form.transfers["+index+"].fileForm"]:fileForm
                  });
                }
              }
            })
          })
        }
      })
    })
    that.setData({
      form : array,
      isUpdate : true 
    })
  },

  back:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})