import { alertInfo, alertSuccess, loadingFunc } from "../../../../utils/index";
import { getParkMeterList, handleOperateMeterSwitch } from "../../../../service/meter"
import request from "../../../../utils/request"
const { OK } = request

// pages/workBench/components/record/index.js
Component({

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

  },

  /**
   * 组件的初始数据
   */
  data: {
    keyword: "",
    keywordTemp: "",
    page: 1,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onParkFocus(e) {
      this.setData({
        show: true,
        title: "园区",
        type: 'park'
      })
    },
    onMeterFocus(e) {
      const { park } = this.data;
      if (!park) {
        alertInfo("请先选择园区")
        return;
      }
      this.setData({
        show: true,
        title: "电表",
        type: 'meter'
      })
    },
    onConfirm(e) {
      const { data, type } = e.detail;
      const that = this;
      switch(type) {
        case "park":
          this.setData({
            parkName: data.name,
            park: data.id,
          })
          break;
        case "meter":
          this.setData({
            meterName: data.address,
            meter: data.id,
          }, () => {
            loadingFunc(async () => {
              await that.init();
            })
          })
          break;
      }
      
      this.onCancel();
    },
    onChangeKeyword(e) {
      this.setData({ keywordTemp: e.detail })
    },
    onSearch() {
      const that = this;
      that.setData({
        keyword: that.data.keywordTemp
      }, () => {
        loadingFunc(async () => {
          await that.init();
        })
      })
    },
    async init() {
      const { page, park, meter } = this.data;
      if (!park) {
        return;
      }
      const { code, message, data, total } = await getParkMeterList({ park, keyword: meter, page })
      if (code !== OK) {
        alertInfo(message);
        return;
      }
      this.setData({
        list: data,
        total: total,
        totalPage: Math.ceil(total / 20)
      })
    },
    async onChangePage(e) {
      const page = e.detail.currentIndex;
      const that = this;
      this.setData({
        page
      }, () => {
        loadingFunc(async () => {
          await that.init();
        })
      })
    },
    onCancel() {
      this.setData({
        show: false,
        title: "",
        type: "",
      })
    },
    onChangeSelectMeter(e) {
      this.setData({
        meter: e.detail
      })
    },
    async handleMeterSwitchOn() {
      const that = this;
      const { meter, meterName } = this.data;
      wx.showModal({
        title: '提示',
        content: `您确认要对${meterName || 当前电表}进行合闸吗？`,
        complete: async (res) => {
          if (res.cancel) {
            
          }
      
          if (res.confirm) {
            loadingFunc(async () => {
              const { code, message } = await handleOperateMeterSwitch({ ids: [meter], status: 0 })
              if (code !== OK) {
                alertInfo(message)
                return;
              }
              alertSuccess("合闸成功")
              that.init();
            })
            
          }
        }
      })
    },
    async handleMeterSwitchOff() {
      const that = this;
      const { meter, meterName } = this.data;
      wx.showModal({
        title: '提示',
        content: `您确认要对${meterName || 当前电表}进行拉闸吗？`,
        complete: async (res) => {
          if (res.cancel) {
            return;
          }
          if (res.confirm) {
            loadingFunc(async () => {
              const { code, message } = await handleOperateMeterSwitch({ ids: [meter], status: 1 })
              if (code !== OK) {
                alertInfo(message)
                return;
              }
              alertSuccess("拉闸成功")
              that.init();
            })
          }
        }
      })
    }
  }
})