// pages/workBench/components/recharge/components/reharge/index.js

import { handleRecharge } from "../../../../../../service/recharge";
import { alertInfo, alertSuccess, loadingFunc } from "../../../../../../utils/index";
import request from '../../../../../../utils/request'
const { OK } = request;

Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    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,
          })
          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 } = 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 
        })
        if (code !== OK) {
          alertInfo(message)
          return
        }
        alertSuccess("充值成功")
        setTimeout(() => {
          that.handleClear()
          that.setData({
            
          })
        }, 500)
      })
      
    }
  }
})