// pages/workBench/components/tenement/components/bindMeter/index.js
// 0015980101
import { bindMeter, } from "../../../../../../service/tenement"
import { getWorkMeterDetail } from "../../../../../../service/meter"
import { alertInfo, alertSuccess } from "../../../../../../utils/index";
import request from "../../../../../../utils/request"
import dayjs from "../../../../../../utils/dayjs"
const { OK } = request
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    tenement: String,
    tenementName: String,
    park: String,
    visible: Boolean,
    meterId: String,
  },

  /**
   * 组件的初始数据
   */
  data: {
    dateTimeShow: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onMeterFocus(e) {
      const { park } = this.data;
      if (!park) {
        alertInfo("请先选择园区")
        return;
      }
      this.setData({
        show: true,
        title: "电表",
        type: 'meter'
      })
    },
    onCancel() {
      this.setData({
        show: false,
        title: "",
        type: "",
      })
      // this.triggerEvent("close")
    },
    onClose() {
      this.setData({
        show: false,
        title: "",
        type: "",
      })
    },
    onChange(e) {
      const { name } = e.currentTarget.dataset;
      this.setData({
        [name]: e.detail
      })
    },
    async onSubmit() {
      const { overall, critical, peak, valley, deepValley, park, tenement, meter, readAt, flat } = this.data;
      const { code, message } = await bindMeter(
        park, 
        tenement, 
        { 
          code: meter,  
          overall:overall? Number(overall) : (overall === "" ? null : overall), 
          critical : critical ? Number(critical) : (critical === "" ? null : critical), 
          peak: peak ? Number(peak) : (peak === "" ? null : peak), 
          flat: flat ? Number(flat) : (flat === "" ? null : flat), 
          valley : valley ? Number(valley) : (valley === "" ? null : valley), 
          deepValley: deepValley ? Number(deepValley) : (deepValley === "" ? null : deepValley), 
          readAt: readAt || dayjs().format('YYYY-MM-DD HH:mm:ss') 
        }
        )
      if (code !== OK) {
        alertInfo(message)
        return;
      }
      alertSuccess("绑定成功")
      this.triggerEvent("ok")
    },
    onConfirm(e) {
      const { type, data } = e.detail;
      const that = this;
      switch(type) {
        case "meter":
          this.setData({
            meter: data.id,
            meterName: data.address,
            show: false,
          })
          this.getMeterDetail(data.id);
          break;
      }
    },
    async getMeterDetail(id) {
      const { code, message, data } = await getWorkMeterDetail(id)
      if (code !== OK) {
        alertInfo(message)
        return;
      }
      this.setData({
        overall: data?.overall ? Number(data?.overall || 0) : null,
        critical: data?.overall ? Number(data?.sharp || 0) : null,
        peak: data?.overall ? Number(data?.peak || 0) : null,
        flat: data?.overall ? Number(data?.flat || 0) : null,
        valley: data?.overall ? Number(data?.valley || 0) : null,
        deepValley: data?.deepValley ? Number(data?.deepValley || 0) : null,
        meter04kvType: data?.meter04kvType,
        status: data?.breakType ? '合闸' : "分闸",
      })
    },
    dateTimeConfirm(e) {
      this.setData({ readAt: e.detail.time, dateTimeShow: false })
    },
    dateTimeCancal(e) {
      this.setData({ dateTimeShow: false })
    },
    onTimeFocus() {
      this.setData({ dateTimeShow: true })
    }
  }
})