<template>
  <LjSelectorSelect
    ref="selectStationRef"
    v-model:value="props.value"
    value-key="outrepcode"
    :loading="loading"
    :remote-method="remoteMethod"
    @select="handleSelect"
    @open-modal="openModal"
  >
    <template #header>
      <div class="table-header lj-select__table">
        <div class="table-tr">
          <div class="table-td pr-4" style="width: 60px">{{ $t("table.u_outrepdef.outrepcode") }}</div>
          <div class="table-td pr-4" style="width: 60px">{{ $t("table.u_outrepdef.outrepname") }}</div>
          <div class="table-td pr-4" style="width: 60px">{{ $t("table.u_outrepdef.engname") }}</div>
          <div class="table-td pr-4" style="width: 50px">{{ $t("table.u_outrepdef.upname") }}</div>
          <div class="table-td pr-4" style="width: 40px">{{ $t("table.u_outrepdef.maxamt") }}</div>
          <div class="table-td pr-4" style="width: 60px">{{ $t("table.u_outrepdef.outrepdscrp") }}</div>
          <div class="table-td pr-4" style="width: 30px">{{ $t("table.u_outrepdef.isuse") }}</div>
        </div>
      </div>
    </template>
    <el-option
      v-for="item in options"
      :key="item.outrepcode"
      :label="item.outrepcode"
      :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: 60px" class="table-td pr-4" :title="item.outrepcode">{{ item.outrepcode }}</div>
          <div style="width: 60px" class="table-td pr-4 text-ellipsis-one" :title="item.outrepname">{{ item.outrepname }}</div>
          <div style="width: 60px" class="table-td pr-4 text-ellipsis-one">{{ item.engname }}</div>
          <div style="width: 50px" class="table-td pr-4 text-ellipsis-one">{{ item.upname }}</div>
          <div style="width: 40px" class="table-td pr-4 text-ellipsis-one">{{ item.maxamt }}</div>
          <div style="width: 60px" class="table-td pr-4 text-ellipsis-one">{{ item.outrepdscrp }}</div>
          <div style="width: 30px" class="table-td pr-4 text-ellipsis-one">
            <el-checkbox :checked="Boolean(Number(item.isuse))" disabled></el-checkbox>
          </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="SelectorOutrepSelect">
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 isuse = ref("有效");

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

  let newParams: any = {};
  newParams.pageindex = 1;
  newParams.pagesize = 5;
  let _isuse = 1;
  if (isuse.value === "全部") {
    _isuse = -1;
  } else if (isuse.value === "有效") {
    _isuse = 1;
  } else {
    _isuse = 0;
  }
  newParams.queryParams = {
    isuse: _isuse,
    keyword: queryString
  };
  newParams.dsname = "web_outrep_index";
  let result = await CommonDynamicSelect(newParams, DwnameEnum.outrep);

  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>
