// pages/rechargeRecord/index.js
import { getRechargeList } from "../../service/recharge";
import { getTenementMeterList } from "../../service/meter";
import { getYears, alertInfo, loadingFunc } from "../../utils/index";
import request from "../../utils/request";
import Dialog from '@vant/weapp/dialog/dialog';
const { OK } = request;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    year: new Date().getFullYear(),
    month: new Date().getMonth() + 1,
    years: getYears(),
    list: [

    ],
    way: ["现金", "银行卡", "支付宝", "微信", "云闪付","对公转账", "小程序-微信支付"],
    show: false,
    columns: [],
    type: "",
    title: "",
    amount: 0,
    codeId: "",
    meterCode: ""
  },
  clickYear() {
    this.setData({
      type: "year",
      columns: this.data.years,
      show: true,
      title: "年份"
    })
  },
  clickMeter() {
    this.setData({
      type: "meter",
      columns: [{id: "", name: "全部", address: "", code: ""}, ...this.data.meterList.map(item => ({ id: item.id, name: `${item.code}-${item.address}`, code: item.code }))],
      show: true,
      title: "表计"
    })
  },
  clickType() {
    this.setData({
      type: "pay",
      columns: [
        {id: undefined, name: "全部" }, 
        {id: 1, name: "对公" }, 
        {id: 2, name: "微信" },
      ],
      show: true,
      title: "充值类型"
    })
  },
  onOk(e) {
    const { type, value = {} } = e.detail;
    if (type === "year") {
      this.onChangeYear(value.id)
      return;
    }
    if (type === "meter") {
      this.onChangeMeter(value)
      return;
    }
    if (type === "pay") {
      this.onChangePay(value)
      return;
    }
  },
  onChangeYear(e) {
    const { codeId, pay } = this.data;
    const currentYear = e
    this.setData({
      year: currentYear,
      type: "",
      show: false,
      title: ""
    })
    this.init(currentYear, codeId, pay)
  },
  showAnswer() {
    Dialog.alert({
      title: '提示',
      message: '只统计已完成充值的数据，处理中和已退回不计入总额内',
    }).then(() => {
      // on close
    });
  },
  onChangeMeter(e) {
    const { id, code,  } = e;
    const { year, pay } = this.data;
    this.setData({
      codeId: id,
      meterCode: code, 
      type: "",
      show: false,
      title: ""
    })
    this.init(year, id, pay)
  },
  onChangePay(e) {
    const { id, name } = e;
    const { year, codeId } = this.data;
    this.setData({
      payType: name, 
      type: "",
      show: false,
      title: ""
    })
    this.init(year,codeId, id, )
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const { year } = this.data;
    const tenement = wx.getStorageSync('tenement')
    loadingFunc(async () => {
      await this.init(year, '');
      await this.getMeters(tenement?.id);
    });

  },
  refreshEmpty() {
    const { year } = this.data;
    const tenement = wx.getStorageSync('tenement')
    this.init(year, '');
    this.init(year, tenement);
  },
  async getMeters(id) {
    const { code, message, data } = await getTenementMeterList(id);
    if (code !== OK) {
      alertInfo(message)
      return;
    }
    this.setData({
      meterList: data || [],
    })
  },
  async init(year, codeId, pay) {
    const { code, message, data = [], amount = 0 } = await getRechargeList(year, codeId, pay);
    if (code !== OK) {
      alertInfo(message)
      return;
    }
    this.setData({
      list: data,
      amount
    })
  },
  onCancel() {
    this.setData({
      show: false,
      title: "",
      type: "",
    })
  },
  jumpToDetail(e) {
    const { id } = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/pages/rechargeDetail/index?id=' + id,
    })
  },
  back() {
    wx.navigateBack({delta: 1  })
  },
})