// pages/writeReading/index.js
import { getMeterReadingRouteDetail } from "../../service/workBench"
import request from "../../utils/request"
import { alertSuccess, alertInfo, alertError } from "../../utils/index"
const { OK } = request
Page({

  /**
   * 页面的初始数据
   */
  data: {
    meterList: [],
    meterInfo: {},
    active: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const { id, type } = options;
    this.setData({ id, type })
    this.getMeterList(id)
  },
  async getMeterList(id) {
    const { code, message, data } = await getMeterReadingRouteDetail(id);
    if (code !== OK) {
      alertError(message)
      return;
    }
    this.setData({
      meterList: data?.meterRouteDetail || [],
      meterInfo: data?.meterRouteDetail?.[0] || {},
      routeId: id
    })
  },
  changeMeter(e) {
    this.setData({
      meterInfo: e.detail.meter,
    })
  },
  changeMeterAndIndex(e) {
    this.setData({
      meterInfo: e.detail.meter,
      active: e.detail.index,
    })
  },
  onSubmit() {
    this.getMeterList(this.data.routeId)
  },
  handleNext() {
    const { active = 0, meterList } = this.data;
    let newIndex = active + 1
    if (newIndex > meterList?.length - 1) {
      newIndex = meterList?.length - 1
    }
    this.setData({
      active: newIndex,
    })
  },
  handlePrev() {
    this.setData({
      active: this.data.active === 0 ? 0 : this.data.active - 1
    })
  },
  onSetFinishNumber(e) {
    this.setData({
      finishNumber: e.detail,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
})