// pages/workBenchNew/components/workBenchReading/index.js
import { getMeterReadingRouteList, changeMeterRouteStatus, checkReadingFinish } from "../../../../service/workBench"
import { alertError, alertSuccess, loadingFunc } from "../../../../utils/index"
import request from "../../../../utils/request"
const { OK } = request


Page({

  /**
   * 页面的初始数据
   */
  data: {
    park: "",
    parkName: "",
  },
  onParkFocus(e) {
    this.setData({
      show: true,
      title: "园区",
      type: 'park'
    })
  },
  async init() {
    const { parkName, park } = this.data;
    const { code, message, data } = await getMeterReadingRouteList(parkName)
    if (code !== OK) {
      alertError(message)
      return
    }
    this.setData({
      list: data
    })
  },
  onConfirm(e) {
    const { data } = e.detail;
    const that = this;
    this.setData({
      parkName: data.name,
      park: data.id,
    }, () => {
      loadingFunc(async () => {
        await that.init();
      })
    })
    this.onConcal();
  },
  onConcal() {
    this.setData({
      show: false,
      title: "",
      type: "",
    })
  },
  jumpToReading(e) {
    const { id, type } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/writeReading/index?id=${id}&type=${type}`,
    })
  },
  async changeStartStatus(e) {
    const that = this;
    const { id, routeid: routeId } = e.currentTarget.dataset;
    const { code, message } = await changeMeterRouteStatus({ id, status: 1, routeId })
    if (code !== OK) {
      alertError(message)
      return
    }
    alertSuccess("操作成功")
    this.init()
    setTimeout(() => {
      that.jumpToReading({ currentTarget: { dataset: { id } } })
    }, 500)
  },
  // async checkFinish(id) {
    
  //   return {data, num};
  // },
  async changeEndStatus(e) {
    const that = this;
    wx.showModal({
      title: '提示',
      content: '确认要结束吗？',
      complete: async (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
            const { id, routeid: routeId } = e.currentTarget.dataset;
          const { code, message, data, num } = await checkReadingFinish(id || this.data.routeId);
          if (code !== OK) {
            alertError(message)
            return;
          } 
          if (!data) {
            wx.showModal({
              title: '提示',
              content: `该路线还有${num}块电表还没有抄完，是否确认结束结束抄表？`,
              complete: async (res) => {
                if (res.cancel) {
                  
                }
            
                if (res.confirm) {
                  const { code: cd, message } = await changeMeterRouteStatus({ id, status: 0, routeId })
                  if (cd !== OK) {
                    alertError(message)
                    return
                  }
                  alertSuccess("操作成功")
                  that.init()
                }
              }
            })
            return
          }
          const { code: c, message: m } = await changeMeterRouteStatus({ id, status: 0, routeId })
          if (c !== OK) {
            alertError(m)
            return
          }
          alertSuccess("操作成功")
          that.init()
        }
      }
    })

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

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.init()
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})