<template>
  <LjSelectorSelect
    ref="selectStationRef"
    v-model:value="props.value"
    value-key="bednetid"
    :loading="loading"
    v-bind="$attrs"
    :remote-method="remoteMethod"
    popper-class="lj-select-mtrldef__popper"
    @select="handleSelect"
    @open-modal="openModal"
  >
    <template #label v-if="$slots?.label">
      <slot name="label"></slot>
    </template>
    <template #header>
      <div class="table-header lj-select__table">
        <div class="table-tr">
          <div class="table-td pr-4" style="width: 200px; max-width: 200px; min-width: 200px">
            {{ $t("table.u_bednet_define.bednetcode") }}
          </div>
          <div class="table-td pr-4" style="width: 200px; max-width: 200px; min-width: 200px">
            {{ $t("table.u_bednet_define.bednetname") }}
          </div>
          <div class="table-td pr-4" style="width: 80px; max-width: 80px; min-width: 80px">
            {{ $t("table.u_bednet_define.typename") }}
          </div>
        </div>
      </div>
    </template>
    <el-option
      v-for="item in options"
      :key="item.bednetid"
      :label="item.bednetname"
      :value="item"
      class="lj-select__table pl-10 pr-10"
      :class="{
        'w-full': item.label
      }"
    >
      <template v-if="item.label">
        <div class="table-tr w-full text-center">{{ item.label }}</div>
      </template>
      <template v-else>
        <div class="table-tr">
          <div
            style="width: 200px; max-width: 200px; min-width: 200px"
            class="table-td pr-4 text-ellipsis-one"
            :title="item.bednetcode"
          >
            {{ item.bednetcode }}
          </div>
          <div
            style="width: 200px; max-width: 200px; min-width: 200px"
            class="table-td pr-4 text-ellipsis-one"
            :title="item.bednetname"
          >
            {{ item.bednetname }}
          </div>
          <div style="width: 80px; max-width: 80px; min-width: 80px" class="table-td pr-4 text-ellipsis-one">
            {{ item.typename }}
          </div>
        </div>
      </template>
    </el-option>
    <!-- <template #footer>
      <el-radio-group v-model="isuse" size="small" @change="remoteMethod">
        <el-radio-button label="全部" value="全部" />
        <el-radio-button label="有效" value="有效" />
        <el-radio-button label="无效" value="无效" />
      </el-radio-group>
    </template> -->
  </LjSelectorSelect>
</template>

<script lang="ts" setup name="SelectorBednetSelect">
import { ref, computed } from "vue";
import { CommonDynamicSelect } from "@/api/modules/common";
import { DwnameEnum } from "@/enums/dwnameEnum";
import LjSelectorSelect from "@/components/LjSelector/select.vue";
import { useUserStore } from "@/stores/modules/user";

interface ProTableProps {
  value: any;
  [key: string]: any;
}

// 默认值
const props = withDefaults(defineProps<ProTableProps>(), {});
const { userInfo } = useUserStore();
const emit = defineEmits(["select", "update:value", "openModal"]);
const loading = ref(false);

const options = ref<any[]>([]);

const pricePower = computed(() => {
  return userInfo.rsltFunids.includes(96);
});

const remoteMethod = async (queryString: string) => {
  console.log("remoteMethod props :>> ", props, queryString);
  loading.value = true;

  let newParams: any = {};
  newParams.pageindex = 1;
  newParams.pagesize = 5;
  newParams.queryParams = {
    ...props,
    arg_search: queryString
  };
  newParams.dsname = "web_bednet_choose";
  let result = await CommonDynamicSelect(newParams, DwnameEnum.bednetSpringChoose);

  console.log("result.datatable :>> ", result.datatable);
  if (result.datatable.length) {
    // options.value = result.datatable.map(item => {
    //   item.label = item.cuscode;
    //   return item;
    // });
    options.value = result.datatable;
  } else {
    options.value = [
      {
        label: "暂无数据",
        disabled: true
      }
    ];
  }

  loading.value = false;
};

const handleSelect = (item: any) => {
  console.log("handleSelect item :>> ", item);
  emit("select", item);
};

const openModal = (e: any) => {
  emit("openModal", e);
};

const selectStationRef = ref();
const toFocus = () => {
  selectStationRef.value.elment.focus();
};

defineExpose({
  toFocus
});
</script>
