// pages/readingHistory/index.js
import { getMeterReadingRouteMeterDetail, getReadingList, deleteReading } from "../../service/workBench"
import request from "../../utils/request"
import { alertInfo, alertSuccess, alertError, loadingFunc } from "../../utils/index"
const { OK } = request;
import dayjs from "../../utils/dayjs"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    page: 1,
    size: 20,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const { meter, park, routeId, disabled } = options;
    const that = this;
    this.setData({
      meter,
      park,
      routeId,
      disabled
    }, () => {
      that.refresh();
    })
    this.getMeterInfo(meter);
  },
  refresh() {
    const that = this;
    loadingFunc(async () => {
      await that.getList();
    })
  },
  onEditFinish() {
    this.setData({
      title: null,
      visible: null,
      time: null,
      number: null,
      id: null,
      readAtTimestamp: null,
      type: null,
    })
    this.refresh()
  },
  async getMeterInfo(id) {
    const { code, message, data } = await getMeterReadingRouteMeterDetail(id)
    if (code !== OK) {
      alertError(message)
      return;
    }
    this.setData({ meterInfo: data })
  },
  async getList() {
    const { meter, park, page, size } = this.data;
    const { code, message, records: data } = await getReadingList(park, meter, page)
    if (code !== OK) {
      alertError(message)
      return;
    }
    this.setData({
      list: data,
      totalPage: Math.ceil(data?.length / size)
    })
  },
  async onChangePage(e) {
    const newPage = e.detail.currentIndex;
    const that = this;
    this.setData({
      page: newPage
    }, () => {
      that.getList();
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  handleCreate() {
    this.setData({
      title: "新增记录",
      visible: true,
      type: "create",
      time: dayjs().format("YYYY-MM-DD HH:mm:ss")
    })
  },
  handleUpdate(e) {
    const { data } = e.currentTarget.dataset;
    this.setData({
      title: "编辑记录",
      visible: true,
      time: data.readAt,
      number: data.overall,
      id: data.id,
      readAtTimestamp: data.readAtTimestamp,
      type: "edit",
    })
  },
  handleDetail(e) {
    const { data } = e.currentTarget.dataset;
    this.setData({
      detail: data,
      detailVisible: true
    })
  },
  handleDelete(e) {
    const { data } = e.currentTarget.dataset;
    const that = this;
    wx.showModal({
      title: '删除确认',
      content: '确认要删除这一项记录吗？',
      complete: async (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
          const { code, message } = await deleteReading(data?.id, data?.overall)
          if (code !== OK) {
            alertError(message)
            return
          }
          alertSuccess("删除成功")
          setTimeout(() => {
            that.refresh()
          }, 300)
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})