import { getLoginParkList, getParkBuildingList } from "../../service/park"
import { 
  getParkSimpleMeterList, getParkBoxList, 
  getCardList, getCollectionList, 
  getInventoryMeter, getCollectorList,
  getWorkOrderAllMeter, 
} from "../../service/meter"
import { alertInfo } from "../../utils/index";
import request from "../../utils/request"
import { payWays, feeType, meterType, noticeFlowType, noticeFlowStatus,
  meterMode
} from "../../utils/data";
import { getTenementList, getWxTenementList } from "../../service/tenement";
const { OK } = request;

// components/searchSelect/index.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    title: String,
    type: String,
    show: Boolean,
    park: String,
    building: String,
    isBack: Boolean,
    filterBind: Boolean,
    filterData: Array,
    filterType: String
  },
  observers: {
    "show,type,filterBind": function(newShow, newType) {
      if (newShow && newType) { 
        this.onSearch()
      }
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    columns: [],
    searchText: "",
    payWays,
    feeType,
    meterType,
    noticeFlowType,
    noticeFlowStatus,
    meterMode,
  },
  lifetimes: {
    attached() {
     
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onChangeSearch(e) {
      this.setData({
        searchText: e.detail,
      })
    },
    onCancel() {
      this.setData({
        columns: [],
        list: [],
        searchText: ""
      })
      this.triggerEvent("cancel")
    },
    onConfirm(event) {
      const { index } = event.detail;
      const { list = [], type } = this.data;
      const item = list[index];
      if (!item) {
        alertInfo("请选择一项")
        return
      }
      this.setData({
        columns: [],
        list: [],
        searchText: ""
      })
      this.triggerEvent("confirm", { data: item, type, index } );
    },
    onMeterModeConfirm(event) {
      const { index } = event.detail;
      const { meterMode = [], type } = this.data;
      const item = meterMode[index];
      this.setData({
        columns: [],
        list: [],
        searchText: ""
      })
      this.triggerEvent("confirm", { data: item, way: index, type } );
    },
    onPayConfirm(event) {
      const { index } = event.detail;
      const { payWays = [], type } = this.data;
      const item = payWays[index];
      this.setData({
        columns: [],
        list: [],
        searchText: ""
      })
      this.triggerEvent("confirm", { data: item, way: index, type } );
    },
    onNoticeFlowTypeConfirm(event) {
      const { index } = event.detail;
      const { noticeFlowType = [], type } = this.data;
      const item = noticeFlowType[index];
      this.setData({
        columns: [],
        list: [],
        searchText: ""
      })
      this.triggerEvent("confirm", { data: item, way: index + 1, type } );
    },
    onNoticeFlowStatusConfirm(event) {
      const { index } = event.detail;
      const { noticeFlowStatus = [], type } = this.data;
      const item = noticeFlowStatus[index];
      this.setData({
        columns: [],
        list: [],
        searchText: ""
      })
      this.triggerEvent("confirm", { data: item, way: index + 1, type } );
    },
    onFeeTypeConfirm(event) {
      const { index } = event.detail;
      const { feeType = [], type } = this.data;
      const item = feeType[index];
      this.setData({
        columns: [],
        list: [],
        searchText: ""
      })
      this.triggerEvent("confirm", { data: item, way: index, type } );
    },
    onMeterTypeConfirm(event) {
      const { index } = event.detail;
      const { meterType = [], type } = this.data;
      const item = meterType[index];
      this.setData({
        columns: [],
        list: [],
        searchText: ""
      })
      this.triggerEvent("confirm", { data: item, way: index, type } );
    },
    onSearch() {
      const { type, bind, filterBind } = this.data;
      switch(type) {
        case "park":
          this.onSearchPark();
          return;
        case "meter":
          this.onSearchMeter();
          return
        case "inventoryMeter":
          this.onSearchInventoryMeter();
          return
        case "tenement":
          this.onSearchTenement();
          return;
        case "building":
          this.onSearchBuilding();
          return;
        case "meterBox":
          this.onSearchMeterBox();
          return;
        case "card":
          this.onSearchCard();
          return;
        case "collection":
          this.onSearchCollection();
          return;
        case "collector":
          this.onSearchCollector();
          break;
        case "workOrderMeter":
          this.onSearchWorkOrderMeter();
          break;
      }
    },
    async onSearchCollector() {
      const { searchText = "", } = this.data;
      const { code, message, data = [] } = await getCollectorList(searchText);
      if (code !== OK) {
        alertInfo(message)
        return
      }
      this.setData({
        columns: data?.map(item => `${item.collectorAddress}-${item.manufacturer}`),
        list: data,
      })
    },
    async onSearchWorkOrderMeter() {
      const { searchText = "", park } = this.data;
      const { code, message, data = [] } = await getWorkOrderAllMeter({keyword: searchText, park: park, page: 1, size: 20});
      if (code !== OK) {
        alertInfo(message)
        return
      }
      this.setData({
        columns: data?.map(item => `${item.address}-${item.meter_no}`),
        list: data,
      })
    },
    async onSearchPark() {
      const { searchText = "" } = this.data;
      const { code, message, data: parks = [] } = await getLoginParkList({keyword: searchText});
      if (code !== OK) {
        alertInfo(message)
        return
      }
      this.setData({
        columns: parks?.map(item => item?.name),
        list: parks,
      })
    },
    async onSearchCollection() {
      const { searchText = "", filterData, filterType } = this.data;
      const { code, message, data: parks = [] } = await getCollectionList({keyword: searchText, page: 1, status: 1});
      if (code !== OK) {
        alertInfo(message)
        return
      }
      let columns = parks;
      if (filterType === 'collection') {
        columns = columns.filter(item => {
          return !filterData.includes(item.id)
        })
      }
      this.setData({
        columns: columns?.map(item => `${item.transformerId}-倍率:${item.ratio}-${item.manufacturer}`),
        list: columns,
      })
    },
    async onSearchMeter() {
      const { searchText = "", park, filterBind } = this.data;
      const { code, message, data: parks = [] } = await getParkSimpleMeterList({keyword: searchText, park, isNeedBind: !filterBind});
      if (code !== OK) {
        alertInfo(message)
        return
      }
      this.setData({
        columns: parks?.map(item => `${item.meterNo}-${item.address}${item.shortName ? '-' + item.shortName : ''}`) || [],
        list: parks || [],
      })
    },
    async onSearchInventoryMeter() {
      const { searchText = "", park, filterBind } = this.data;
      const { code, message, data: parks = [] } = await getInventoryMeter({keyword: searchText, park, isNeedBind: !filterBind, status: 1});
      if (code !== OK) {
        alertInfo(message)
        return
      }
      this.setData({
        columns: parks?.map(item => `${item.sn}`) || [],
        list: parks || [],
      })
    },
    async onSearchTenement() {
      const { searchText = "", park, isBack } = this.data;
      const { code, message, data = [] } = isBack ? await getWxTenementList({keyword: searchText, park}) : await getTenementList({keyword: searchText, park});
      if (code !== OK) {
        alertInfo(message)
        return
      }
      this.setData({
        columns: data?.length ? data?.map(item => item?.name) : [],
        list: data,
      })
    },
    async onSearchBuilding() {
      const { park } = this.data;
      const { code, message, buildings: data = [] } = await getParkBuildingList(park);
      if (code !== OK) {
        alertInfo(message)
        return
      }
      this.setData({
        columns: data?.length ? data?.map(item => item?.name) : [],
        list: data,
      })
    },
    async onSearchMeterBox() {
      const { park, building } = this.data;
      const { code, message, data = [] } = await getParkBoxList({park, building});
      if (code !== OK) {
        alertInfo(message)
        return
      }
      this.setData({
        columns: data?.length ? data?.map(item => item?.address) : [],
        list: data,
      })
    },
    async onSearchCard() {
      const { park } = this.data;
      const { code, message, data = [] } = await getCardList({park});
      if (code !== OK) {
        alertInfo(message)
        return
      }
      this.setData({
        columns: data?.length ? data?.map(item => item?.sim_number) : [],
        list: data,
      })
    }
  }
})

