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

  /**
   * 页面的初始数据
   */
  data: {
    isIPX: app.globalData.isIPX,
    exlName:'文件名',
    showUp:true,
    upText:'导入中...',
    bankType:'',
    bankAccountType:'1',
    isPeers:'Y',
    showBox:true,
    totalAmount:0
  },
           
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    //所属银行
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'bank_type', {method:'GET'}, {
      success: (res) => {
        this.setData({
          bankTypeOptions:res.data
        });
      }
    })
    //转账状态
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'transfer_status', {method:'GET'}, {
      success: (res) => {
        this.setData({
          transferStatusOptions:res.data
        });
      }
    })
    this.setData({
      bankType:options.bankType,
      index:options.index,
      totalAmount:options.totalAmount
    })
    if(options.bankType != '1'){
      this.setData({
        showBox:false,
        bankAccountType:''
      })
    }else{
      this.setData({
        showBox:true,
        isPeers:''
      })
    }
  },

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

  },

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

  },
  uploadFile(e){
    var that = this;
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      success (res) {
        console.log(res);
        that .setData({
          exlName:res.tempFiles[0].name,
          showUp:false
        })
        // tempFilePath可以作为 img 标签的 src 属性显示图片
        const tempFilePaths = res.tempFiles
        wx.uploadFile({
          url: API.URL_GET_UPLOADFILE,
          filePath: tempFilePaths[0].path,
          name: 'file',
          header: {
            "Content-Type": "multipart/form-data",//记得设置
            "chartset":"utf-8",
            'Authorization':'Bearer '+getApp().globalData.userInfo.token
          },
          formData:{
            bankType:'2',
            accountType:that.data.bankAccountType,
            isPeers:that.data.isPeers
          },
          success (response){
            console.log(response);
            let list = JSON.parse(response.data);
            console.log(list);
            if(list.code == 301){
              that.setData({
                upText:'导入失败'
              })
              wx.navigateTo({
                url: '../upError/upError?options='+JSON.stringify(list.rows),
              })
            }else if(list.code == 200){
              that.setData({
                upText:'导入成功',
                payeeList:list.rows
              })
            }else{
              UTIL.showToastNoneIcon('导入失败，未知错误'+list.code);
              that.setData({
                upText:'导入失败'
              })
            }
            // that.setData({
            //   ['form.transfers[0].payeeList']:list.rows
            // })
          },
          fail(res){
          }
        })
      }
    })
  },
  goSubmit(){
    var that = this ;
    if(this.data.upText != '导入成功'){
      UTIL.showToastNoneIcon('请根据提示重新导入收款方文件');
      return;
    }
    let moneyCount = 0;
    for(let i = 0 ; i < that.data.payeeList.length ; i++){
      that.data.payeeList[i].bankTypeText = UTIL.getTransform(that.data.payeeList[i].bankType,that.data.bankTypeOptions);
      that.data.payeeList[i].transferStatusText = UTIL.getTransform(that.data.payeeList[i].transferStatus,that.data.transferStatusOptions);
      moneyCount +=  parseFloat(that.data.payeeList[i].incomeAmount);
    }
    console.log(that.data.totalAmount);
    let totalAmount = parseFloat(that.data.totalAmount)+parseFloat(moneyCount);

    console.log(totalAmount);
    console.log(moneyCount);
    let pages = getCurrentPages();
    let currentPage = null;   //当前页面
    let prevPage = null;  //上一个页面
    currentPage = pages[pages.length - 1]; //获取当前页面，将其赋值
    prevPage = pages[pages.length - 2]; //获取上一个页面，将其赋值
    if (prevPage) {
      prevPage.setData({
        ["form.transfers["+that.data.index+"].payeeList"]: that.data.payeeList,//将想要传的信息赋值给上一个页面data中的值
        ["form.transfers["+that.data.index+"].expenditureAmount"]: moneyCount.toFixed(2),
        ["form.approvalItemTemplate.totalAmount"]:totalAmount.toFixed(2)
      })
      that.back()
    }

  },
  deleteimg(){
    var that = this;
    that.setData({
      upText:'导入中...',
      exlName:'文件名',
      showUp:true,
    })
  },
  back:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  onChange(e){
    this.setData({
      bankAccountType:e.detail
    })
  },
  onChangePee(e){
    this.setData({
      isPeers:e.detail
    })
  },
  downLoadExl(){
    UTIL.httpRequest(API.URL_GET_DOWNFILE, {method:'GET'}, {
      success: (res) => {
        console.log(res);
        let url = ' https://dazu.nongshen.net/api/common/download?fileName='+res.msg+'&delete=true'
        wx.setClipboardData({
          data: url,
          success (res) {
            wx.showModal({
              title: '提示',
              content: '下载链接已复制，请使用手机自带浏览器访问下载',
              showCancel:false,
              success (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

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

  },

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

  },

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

  },

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

  }
})