import { getLoginParkList } from "../../service/park";
import { getTenementList } from "../../service/tenement";
import request from '../../utils/request'

const { OK } = request

Component({

  /**
   * 组件的属性列表
   */
  properties: {
    label: String,
    type: Number,
    park: String,
    parkName: String,
    required: Boolean
  },

  /**
   * 组件的初始数据
   */
  data: {
    list: [],
    visible: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    async onInput(e) {
      switch(this.data.type) {
        case 0:
          const { data = [], code, message } = await getLoginParkList({ keyword: e.detail });
          this.setData({
            list: data,
            visible: true,
          })
          break;
        case 1:
          const park = this.data.park;
          if (!park) {
            return;
          }
          const { data: tenementData = [], code: tenementCode, message: tenementMessage } = await getTenementList({ park, keyword: e.detail });
          this.setData({
            list: tenementData,
            visible: true,
          })
          break;
      }
    },
    close() {
      this.setData({
        visible: false,
        list: []
      })
    },
    onChoose(e) {
      const { data = {} } = e.currentTarget.dataset;
      this.setData({
        visible: false,
        value: data.name
      })
      this.triggerEvent("choose", {type: this.data.type, data})
    },
  }
})