<template>
  <LjSelectorSelect
    v-model:value="props.value"
    value-key="outrepcode"
    :loading="loading"
    :remote-method="remoteMethod"
    @select="handleSelect"
    @open-modal="openModal"
  >
    <template #header>
      <div class="table-header flx">
        <div class="flx-1 pr-4">{{ $t("table.u_outrepdef.outrepcode") }}</div>
        <div class="flx-1 pr-4">{{ $t("table.u_outrepdef.outrepname") }}</div>
        <div class="flx-1 pr-4">{{ $t("table.u_outrepdef.outrepdscrp") }}</div>
      </div>
    </template>
    <el-option v-for="item in options" :key="item.outrepcode" :label="item.outrepcode" :value="item" class="flx pl-10 pr-10">
      <template v-if="item.label">
        <div class="w-full text-center">{{ item.label }}</div>
      </template>
      <template v-else>
        <div class="flx-1 pr-4" :title="item.outrepcode">{{ item.outrepcode }}</div>
        <div class="flx-1 pr-4 text-ellipsis-one" :title="item.outrepname">{{ item.outrepname }}</div>
        <div class="flx-1 pr-4 text-ellipsis-one">{{ item.outrepdscrp }}</div>
      </template>
    </el-option>
  </LjSelectorSelect>
</template>

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

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

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

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

const remoteMethod = async (queryString: string) => {
  loading.value = true;

  let newParams: any = {};
  newParams.pageindex = 1;
  newParams.pagesize = 5;
  newParams.queryParams = {
    keyword: queryString
  };
  newParams.dsname = "web_outrep_up_ch";
  let result = await CommonDynamicSelect(newParams, DwnameEnum.outrepUp);

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

  loading.value = false;
};

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

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

<style lang="scss"></style>
