// pages/workBenchNew/components/recharge/index.js
import { handleRecharge } from "../../../../service/recharge";
import { alertInfo, alertSuccess, loadingFunc } from "../../../../utils/index";
import request from '../../../../utils/request'
const { OK } = request;
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  onParkFocus() {
    this.setData({
      show: true,
      title: "园区",
      type: 'park'
    })
  },
  onMeterFocus() {
    const { park } = this.data;
    if (!park) {
      alertInfo("请先选择园区")
      return;
    }
    this.setData({
      show: true,
      title: "电表",
      type: 'meter'
    })
  },
  onConfirm(e) {
    const { data, type, way } = e.detail;
    switch(type) {
      case "park":
        this.setData({
          parkName: data.name,
          park: data.id,
        })
        break;
      case "meter":
        this.setData({
          meterName: `${data.meterNo}-${data.address}${data.tenement?.name ? '-' + data.tenement?.name : ''}`,
          meter: data.id,
          meterInfo: data,
        })
        break;
      case "pay":
        this.setData({
          payName: data,
          way: way,
        });
        break;
    }
    this.onCancel();
  },
  onCancel() {
    this.setData({
      show: false,
      title: "",
      type: "",
    })
  },
  onPayFocus() {
    this.setData({
      show: true,
      title: "付款方式",
      type: 'pay'
    })
  },
  onChangeMoney(e) {
    this.setData({ money: e.detail })
  },
  onChangeVoucherNo(e) {
    this.setData({ voucherNo: e.detail })
  },
  handleClear() {
    this.setData({
      park: "",
      parkName: "",
      meter: "",
      meterName: "",
      way: "",
      payName: "",
      show: false,
      title: "",
      type: "",
      money: null,
      voucherNo: null
    })
  },
  async handleSubmit() {
    const that = this;
    const { park, meter, money, way, voucherNo, meterInfo } = this.data;
    if (!park) {
      alertInfo("请选择园区");
      return;
    }
    if (!meter) {
      alertInfo("请选择电表")
      return;
    }
    if (!money) {
      alertInfo("请输入金额")
      return
    }
    if (!way && way !== 0) {
      alertInfo("请选择付款方式")
      return
    }
    if (!voucherNo) {
      alertInfo("请输入凭证号")
      return
    }
    loadingFunc(async () => {
      const { code, message } = await handleRecharge(park, { 
        amount: `${money || ''}`, 
        meter, 
        paymentType: way, 
        voucherNo, 
        type: 0 ,
        tenement: meterInfo?.tenement,
      })
      if (code !== OK) {
        alertInfo(message)
        return
      }
      alertSuccess("充值成功")
      setTimeout(() => {
        that.handleClear()
        that.setData({
          
        })
      }, 500)
    })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})