<template>
  <LjSelectorSelect
    ref="selectStationRef"
    v-model:value="props.value"
    :value-key="valueKey"
    :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: 120px; max-width: 120px; min-width: 120px">
            {{ $t("table.u_mtrldef.mtrlcode") }}
          </div>
          <div class="table-td pr-4" style="width: 120px; max-width: 120px; min-width: 120px">
            {{ $t("table.u_mtrldef.mtrlname") }}
          </div>
          <div class="table-td pr-4" style="width: 60px; max-width: 60px; min-width: 60px">
            {{ $t("table.u_mtrldef.mtrltype") }}
          </div>
          <div class="table-td pr-4" style="width: 60px; max-width: 60px; min-width: 60px">
            {{ $t("table.u_mtrldef.mtrlmode") }}
          </div>
          <div class="table-td pr-4" style="width: 120px; max-width: 120px; min-width: 120px">
            {{ $t("table.u_mtrldef.mtrlsectype") }}
          </div>
          <div class="table-td pr-4" style="width: 120px; max-width: 120px; min-width: 120px">
            {{ $t("table.u_mtrldef.zxmtrlmode") }}
          </div>
        </div>
      </div>
    </template>
    <el-option
      v-for="item in options"
      :key="item.mtrlid"
      :label="item.mtrlname"
      :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: 120px; max-width: 120px; min-width: 120px"
            class="table-td pr-4 text-ellipsis-two break-word"
            :title="item.mtrlcode"
          >
            {{ item.mtrlcode }}
          </div>
          <div
            style="width: 120px; max-width: 120px; min-width: 120px"
            class="table-td pr-4 text-ellipsis-two break-word"
            :title="item.mtrlname"
          >
            {{ item.mtrlname }}
          </div>
          <div
            style="width: 60px; max-width: 60px; min-width: 60px"
            :title="item.mtrltype"
            class="table-td pr-4 text-ellipsis-two break-word"
          >
            {{ item.mtrltype }}
          </div>
          <div
            style="width: 60px; max-width: 60px; min-width: 60px"
            :title="item.mtrlmode"
            class="table-td pr-4 text-ellipsis-two break-word"
          >
            {{ item.mtrlmode }}
          </div>
          <div
            style="width: 120px; max-width: 120px; min-width: 120px"
            class="table-td pr-4 text-ellipsis-two break-word"
            :title="item.mtrlsectype"
          >
            {{ item.mtrlsectype }}
          </div>
          <div
            style="width: 120px; max-width: 120px; min-width: 120px"
            class="table-td pr-4 text-ellipsis-two break-word"
            :title="item.zxmtrlmode"
          >
            {{ item.zxmtrlmode }}
          </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="SelectorMtrldefSelect">
import { ref, computed, nextTick } from "vue";
import { GetERPMtrldefList } from "@/api/modules/common";
import { DwnameEnum } from "@/enums/dwnameEnum";
import LjSelectorSelect from "@/components/LjSelector/select.vue";
import { useUserStore } from "@/stores/modules/user";
import { isFilterPrice } from "@/utils/index";

interface ProTableProps {
  value: any;
  valueKey?: string;
  // mtrltype?: any;
  // pricelistid?: any;
  // pricelistidfn?: any;
  [key: string]: any;
  searchKey?: any;
}

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

const options = ref<any[]>([]);
// const isuse = ref("有效");
const selectStationRef = ref();

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

  // if (!_pid) {
  //   return false;
  // }

  let newParams: any = {};
  newParams.pageindex = 1;
  newParams.pagesize = 5;
  // newParams.mtrlid = props.mtrlid;
  if (props.searchKey) {
    newParams[props.searchKey] = queryString;
  } else {
    newParams.keyword = queryString;
  }
  console.log("newParams :>> ", newParams);
  let result = await GetERPMtrldefList(newParams);

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

  loading.value = false;
};

const handleSelect = (item: any) => {
  console.log("handleSelect 手动选择 :>> ", item);
  emit("select", item);
  // emit("select", item, () => {
  //   console.log("handleSelect 手动选择 :>>endsss :>> ", selectStationRef.value);
  //   // nextTick(() => {
  //   selectStationRef.value.element.blur();
  //   // });
  // });
};

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

<style lang="scss">
.lj-select-mtrldef__popper {
  max-width: 600px !important;
  min-width: 600px !important;
}
</style>
