// pages/regular/index.js
import * as UTIL from '../../utils/util.js';
import * as API from '../../utils/API.js';
let EVN_CONFIG = require('../../env/env');

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isIPX: app.globalData.isIPX,
    option1: [
      {text:'年份',value:''}
    ],
    option2: [
      { text: '业务类型', value: '' },
      { text: '结算类', value: '1' },
      { text: '工程项目类', value: '2' },
      { text: '合同类', value: '4' },
      { text: '其他', value: '5' },
    ],
    option3: [
      { text: '票据状态', value: '' },
      { text: '银行卡转账', value: '1' },
      { text: '信用卡还款', value: '2' },
    ],
    value1: '',
    value2: '',
    value3: '',
    showPopup:false,
    result:[],
    showReceiptDialog:false,
    beforeClose(action) {
      return new Promise((resolve) => {
        setTimeout(() => {
          if (action === 'confirm') {
            // 拦截确认操作
             resolve(false);
          } else {
            resolve(true);
          }
        }, 0);
      });
    },
    deptOptions:[],
    deptName:'',
    deptId:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this ;
    console.log(new Date().getFullYear());
    //下发单位查询
    UTIL.httpRequest(API.URL_GET_GETDEPTLIST, {method:'GET'}, {
      success: (res2) => {
        console.log(res2);
        that.setData({
          deptOptions:res2.data,
        })
      }
    })
    var form = {
      pageNum:'1',
      pageSize:'999',
      year:'',
      billType:'',
      orderByColumn:'id',
      isAsc:'desc',
      method:'GET'
    }
    // 票据状态字典查询
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'bill_type', {method:'GET'}, {
      success: (res) => {
        let option3 = [{
          text: '票据状态', value: ''
        }];
        res.data.map(rr=>{
          if (rr.dictValue == 2 ||rr.dictValue == 3 ||rr.dictValue == 5) {
            option3.push({
              text: rr.dictLabel, value: rr.dictValue
            })
          }
        })
        that.setData({
          option3:option3,
          billTypeOptions:res.data,
        })
        UTIL.httpRequest(API.URL_GET_GETBILLLIST , form, {
          success: (res2) => {
            res2.rows.map(rrr=>{
              rrr.billTypeText = UTIL.getTransform(rrr.billType,res.data);
            })
            that.setData({
              billList:res2.rows
            })
          }
        })
      }
    })
    
    
    console.log(form);
    
  },
  bindSelectTypePickerChange: function(e) {
    var that = this;
    var dictValue = that.data.deptOptions[e.detail.value].deptId;
    var text = that.data.deptOptions[e.detail.value].deptName;
    this.setData({
      deptId:dictValue,
      deptName:text
    })
  },
  back:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  showPopup(){
    var that = this;
    that.setData({
      showPopup:true
    })
  },
  onClose() {
    this.setData({ showPopup: false });
  },
  onChange(event) {
    console.log(event.detail);
    this.setData({
      result: event.detail,
    });
  },
  showReceiptDialog(e){
    var that = this;
    that.setData({
      showReceiptDialog:true,
      projectId:e.currentTarget.dataset.id,
      deptId:'',
      deptName:''
    })
  },
  goQS(e){
    var that = this ;
    wx.showModal({
      title: '提示',
      content: '确认签收？',
      success (res) {
        if (res.confirm) {
          UTIL.httpRequest(API.URL_GET_GETBILLLISTGET+e.currentTarget.dataset.id , {method:'GET'}, {
            success: (res) => {
              if (res.code == API.SUCCESS_CODE) {
                wx.showToast({
                  title: '签收成功',
                  icon: 'success',
                  duration: 2000,
                })
                that.onLoad();
              }else{
                wx.showToast({
                  title: '签收失败',
                  icon: 'error',
                  duration: 2000,
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
  },
  goZF(e){
    var that = this ;
    wx.showModal({
      title: '提示',
      content: '确认作废',
      success (res) {
        if (res.confirm) {
          UTIL.httpRequest(API.URL_GET_GETBILLLISTCANCEL+e.currentTarget.dataset.id , {method:'GET'}, {
            success: (res) => {
              if (res.code == API.SUCCESS_CODE) {
                wx.showToast({
                  title: '作废成功',
                  icon: 'success',
                  duration: 2000,
                })
                that.onLoad();
              }else{
                wx.showToast({
                  title: '作废失败',
                  icon: 'error',
                  duration: 2000,
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    

  },
  goXF(e){
    var that = this ;
    if (that.data.deptId == '') {
      UTIL.showToastNoneIcon('请选择下发单位！');
      return;
    }
    UTIL.httpRequest(API.URL_GET_GETBILLLISTEDIT+that.data.projectId+'/'+that.data.deptId , {method:'GET'}, {
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          wx.showToast({
            title: '下发成功',
            icon: 'success',
            duration: 2000,
          })
          that.setData({
            showReceiptDialog:false
          })
          that.onLoad();
        }else{
          wx.showToast({
            title: '下发失败',
            icon: 'error',
            duration: 2000,
          })
        }
      }
    })
  },
  changeTab1(event){
    this.setData({
      value1: event.detail
    });
    var that = this ;
    var form = {
      pageNum:'1',
      pageSize:'999',
      year:that.data.value1,
      billType:that.data.value3,
      orderByColumn:'id',
      isAsc:'desc',
      method:'GET'
    }
    console.log(form);
    UTIL.httpRequest(API.URL_GET_GETBILLLIST , form, {
      success: (res) => {
        res.rows.map(rrr=>{
          rrr.billTypeText = UTIL.getTransform(rrr.billType,that.data.billTypeOptions);
        })
        that.setData({
          billList:res.rows
        })
      }
    })
  },
  changeTab3(event){
    this.setData({
      value3: event.detail
    });
    var that = this ;
    var form = {
      pageNum:'1',
      pageSize:'999',
      year:that.data.value1,
      billType:that.data.value3,
      orderByColumn:'id',
      isAsc:'desc',
      method:'GET'
    }
    console.log(form);
    UTIL.httpRequest(API.URL_GET_GETBILLLIST , form, {
      success: (res) => {
        res.rows.map(rrr=>{
          rrr.billTypeText = UTIL.getTransform(rrr.billType,that.data.billTypeOptions);
        })
        that.setData({
          billList:res.rows
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this ;
    let year = new Date().getFullYear()
    let years = that.data.option1;
    for (let i = 0; i < 10; i++) {
      years.push({
        text: year-i,
        value: year-i
      })
    }
    that.setData({
      option1:years,
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})