import { alertInfo, alertSuccess } from "../../../../utils/index";
import { getTenementBackInfo, unbindMeter, } from "../../../../service/tenement"
import { getBackTenementMeters, } from "../../../../service/meter"
import request from "../../../../utils/request"
const { OK } = request
// pages/workBench/components/tenement/index.js
Component({

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

  },

  /**
   * 组件的初始数据
   */
  data: {
    defaultValue: {},
    editType: "",
    meterList: [],
    // bindMeter
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onParkFocus(e) {
      this.setData({
        show: true,
        title: "园区",
        type: 'park'
      })
    },
    onTenementFocus(e) {
      const { park } = this.data;
      const that = this;
      if (!park) {
        alertInfo("请先选择园区")
        return;
      }
      this.setData({
        show: true,
        title: "商户",
        type: 'tenement'
      })
    },
    async init() {

    },
    async getTenementInfo() {
      const { tenement, park } = this.data;
      if (!tenement || !park) {
        return;
      }
      const { code, message, tenement: data } = await getTenementBackInfo(park, tenement)
      if (code !== OK) {
        alertInfo(message)
        return;
      }
      this.setData({
        tenementInfo: data,
      })
    },
    onConfirm(e) {
      const { type, data } = e.detail;
      const that = this;
      switch(type) {
        case "park":
          this.setData({
            park: data.id,
            parkName: data.name,
            show: false,
          })
          break;
        case "tenement":
          this.setData({
            tenement: data.id,
            tenementName: data.name,
            show: false,
          } , () => {
            that.getTenementInfo();
            that.getTenementMeters();
          })
          break;
      }
    },
    onCancel() {
      this.setData({
        show: false,
      })
    },
    startKh() {
      this.setData({
        kaihuVisible: true,
        editType: "add",
        title: "开户",
        defaultValue: { feeType: 0, feeTypeName: "华昌宝能收费" }
      })
    },
    startUpdateInfo() {
      const { tenementInfo } = this.data;
      this.setData({
        kaihuVisible: true,
        defaultValue: tenementInfo,
        editType: "update",
        title: "开户"
      })
    },
    async getTenementMeters() {
      const { park, tenement } = this.data;
      const { code, message, data } = await getBackTenementMeters(park, tenement)
      if (code !== OK) {
        alertInfo(message)
        return
      }
      this.setData({ meterList: data });
    },
    onKaihuClose() {
      this.setData({
        kaihuVisible: false,
        defaultValue: {},
        title: "编辑",
        editType: "",
      })
    },
    onBindClose() {
      this.setData({
        bindVisible: false,
      })
    },
    async onKaihuConfirm() {
      this.getTenementInfo();
      this.onKaihuClose();
    },
    unbind(e) {
      const that = this;
      const { park, tenement } = this.data;
      const { address, id } = e.currentTarget.dataset;
      this.setData({
        unbindVisible: true,
        meterId: id,
        meterAddress: address
      })
      // wx.showModal({
      //   title: '提示',
      //   content: `确认要解绑${address}吗？`,
      //   complete: async (res) => {
      //     if (res.cancel) {
            
      //     }
      
      //     if (res.confirm) {
      //       const { code, message, } = await unbindMeter(park, tenement, id);
      //       if (code !== OK) {
      //         alertInfo(message)
      //         return
      //       }
      //       alertSuccess("解绑成功")
      //       that.getTenementMeters();
      //       return
      //     }
      //   }
      // })
    },
    onUnbindOk() {
      this.setData({
        unbindVisible: false,
        meterId: "",
        meterAddress: "",
      })
      this.getTenementMeters();
    },
    startBind() {
      this.setData({
        bindVisible: true
      })
    },
    onBindOk() {
      this.setData({
        bindVisible: false,
      })
      this.getTenementMeters();
    },
    onTimeConfirm(e) {
 
    }
  }
})