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

  /**
   * 组件的属性列表
   */
  properties: {
    visible: Boolean,
    title: String,
    timeProps: String,
    numberProps: String,
    idProps: String,
    meter: String,
    park: String,
    routeId: String,
  },
  observers: {
    "timeProps": function(newValue) {
      this.setData({
        time: newValue,
        // startTime: newValue
      })
    },
    "numberProps": function(newValue) {
      this.setData({
        number: newValue
      })
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    dateTimeShow: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    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 { time, number, idProps, park, meter, timeProps, routeId } = this.data;
      if (time == null || number == null) {
        alertInfo("请正确填写后保存")
        return;
      }
      if (!idProps) {
        const { code, message } = await createReading(park, meter, routeId, {
          overall: `${number}`,
          flat: `${number}`,
          readAt: time,
          source: 1,       
        })
        if (code !== OK) {
          alertError(message)
          return
        }
        alertSuccess("录入成功")
        this.setData({ number: null, time: null, startTime: null })
      } else {
        const { code, message } = await updateReading(park, meter, new Date(timeProps).getTime() * 1000, {
          overall: `${number}`,
          flat: `${number}`,
        })
        if (code !== OK) {
          alertError(message)
          return
        }
        alertSuccess("录入成功")
        this.setData({ number: null, time: null, startTime: null })
      }
      const that = this;
      setTimeout(() => {
        that.triggerEvent("ok")
      }, 300)
    },

    dateTimeConfirm(e) {
      this.setData({ time: e.detail.time, dateTimeShow: false })
    },
    dateTimeCancal(e) {
      this.setData({ dateTimeShow: false })
    },
    onTimeFocus() {
      this.setData({ dateTimeShow: true })
    }
  }
})