// pages/billUse/flowAdd/flowAdd.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,
    formData:{
      listId: '', //票据ID
      billNum: '', //票据编号 必填
      billType: '4', //票据状态
      bookId: '', //账套ID
      deptId: '',  //系统机构代码
      billDate: '', //开票日期 必填
      accountSummary: '',//摘要 必填
      incomeExpensesType: "1", //收支类型
      settleStyle: '', //结算方式
      jieAmount: '', //收入金额
      daiAmount: '', //支出金额 
      preparedBy: '', //经办人 必填
      payee: '', //收款方 必填
      payer: '', //付款方 必填
      accountId: '', //出纳账户ID
      accountName: '', //出纳账户
      accountType: '', //账户类型 字典account_type
      cashierFlowId: '' //出纳流水ID
    },
    //收入时间弹窗显隐
    srShow:false,
    minDate:new Date().getTime() - 180* 24 * 60 * 60 * 1000,
    maxDate:new Date().getTime() + 180* 24 * 60 * 60 * 1000,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this ;
    this.setData({
      ["formData.billDate"]:this.getNewDate(new Date())
    })
    // 结算方式字典查询 
    UTIL.httpRequest(API.URL_GET_GETDICTTYPE + 'settle_style', {method:'GET'}, {
      success: (res) => {
        that.setData({
          jsfsOptions:res.data,
        })
      }
    })
    UTIL.httpRequest(API.URL_GET_GETLISTQUERY, {method:'GET'}, {
      success: (res) => {
        that.setData({
          pjbhOptions:res.rows,
        })
      }
    })
    UTIL.httpRequest(API.URL_GET_SUBJECTLIST , {method:'GET'}, {
      success: (res2) => {
        that.setData({
          subjectOptions:res2.rows,
        })
      }
    })

    //出纳账户
    var sendData = {
      pageNum:'1',
      pageSize:'100',
      accountName:'',
      bankAccountNumber:'',
      status:'N',
      method:'GET',
      accountType:102
    }
    UTIL.httpRequest(API.URL_GET_ACCOUNTLIST, sendData,{
      success: (res) => {
        console.log(res.rows)
        that.setData({
          accountList:res.rows
        })
      }
    })

  },
  back:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  onsrDate(){
    this.setData({srShow:true})
  },
  onClose(){ 
    this.setData({srShow:false})
  },
  onConfirm(e){
    console.log(e);
    let data = this.getNewDate(new Date(e.detail));
    console.log(data);
    this.setData({'formData.billDate':data})
    this.setData({srShow:false})
  },
  getNewDate(date){
    //date是传过来的时间戳，注意需为13位，10位需*1000
    //也可以不传,获取的就是当前时间
    var time = new Date(date);
    var year= time.getFullYear()  //年
    var month = ("0" + (time.getMonth() + 1)).slice(-2); //月
    var day = ("0" + time.getDate()).slice(-2); //日
    var mydate = year + "-" + month + "-" + day;
    return mydate
  },
  onpjbhShow(){ 
    this.setData({pjbhShow:true})
  },
  onjsfsShow(){ 
    this.setData({jsfsShow:true})
  },
  oncnzhShow(){ 
    this.setData({cnzhShow:true})
  },
  onCancelcrzh(){
    this.setData({crzhShow:false})
  },
  onCancelcrlx(){
    this.setData({pjbhShow:false,})
  },
  onCanceljsfs(){
    this.setData({jsfsShow:false,})
  },
  onCancelcrcnzh(){
    this.setData({cnzhShow:false,})
  },
  bindPickerChange (e) {
    console.log(e);
    let obj =  e.detail.value;
    this.setData({
      pjbhShow:false,
      ["formData.billNum"]:obj.billNum,
      ["formData.listId"]:obj.id,
    })
  },
  bindPickerChangeJsfs (e) {
    console.log(e);
    let obj =  e.detail.value;
    this.setData({
      jsfsShow:false,
      ["formData.settleStyle"]:obj.dictValue,
      ["formData.settleStyleText"]:obj.dictLabel,
    })
  },
  bindPickerChangeCnzh (e) {
    console.log(e);
    let obj =  e.detail.value;
    this.setData({
      cnzhShow:false,
      ["formData.accountName"]:obj.accountName,
      ["formData.accountId"]:obj.accountId,
    })
  },
  swichAccounting:function(e){
    var that = this;
    let data = this.data.formData;
    if(data.billNum == ''){
      UTIL.showToastNoneIcon('请选择票据编号！');
    }
    if(data.billDate == ''){
      UTIL.showToastNoneIcon('请选择开票日期！');
    }
    if(data.payee == ''){
      UTIL.showToastNoneIcon('请填写收款方！');
    }
    if(data.payer == ''){
      UTIL.showToastNoneIcon('请填写付款方！');
    }
    if(data.accountSummary == ''){
      UTIL.showToastNoneIcon('请填写结算事项！');
    }
    if(data.settleStyle == ''){
      UTIL.showToastNoneIcon('请选择结算方式！');
    }
    if(data.accountName == ''){
      UTIL.showToastNoneIcon('请选择出纳账户！');
    }
    data.method = "POST";
    UTIL.httpRequest(API.URL_GET_GETBILLADD, data, {
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          wx.showToast({
            title: '新增成功',
            icon: 'success',
            duration: 2000,
          })
          setTimeout(function(){
            that.back();
          },2000)
        }else{
          UTIL.showToastNoneIcon(res.msg);
        }
      }
    })
   
  },
  inputChange(e){
    var that = this ;
    var obj = e.detail;
    var name = e.currentTarget.dataset.name;
    that.setData({
      [name] : obj.value
    })
  },
  onChange(e){
    console.log(e);
    this.setData({
      ["formData.incomeExpensesType"] : e.detail,
      ["formData.jieAmount"]:'',
      ["formData.daiAmount"]:'',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})