<template>
  <LjSelectorSelect
    ref="selectStationRef"
    v-model:value="props.value"
    value-key="mtrlid"
    :loading="loading"
    v-bind="$attrs"
    :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_mtrl_price.name") }}</div>
          <div class="table-td pr-4" style="width: 60px">{{ $t("table.u_mtrl_price.thickness") }}</div>
          <div class="table-td pr-4" style="width: 60px">{{ $t("table.u_mtrl_price.priceunit") }}</div>
          <div class="table-td pr-4" style="width: 40px">{{ $t("table.u_mtrl_price.dscrp") }}</div>
        </div>
      </div>
    </template>
    <el-option
      v-for="item in options"
      :key="item.mtrlid"
      :label="item.name"
      :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 text-ellipsis-one" :title="item.mtrlname">{{ item.mtrlname }}</div>
          <div style="width: 60px" class="table-td pr-4 text-ellipsis-one">{{ Number(item.thickness) }}</div>
          <div style="width: 60px" class="table-td pr-4 text-ellipsis-one">{{ item.priceunit }}</div>
          <div style="width: 40px" class="table-td pr-4 text-ellipsis-one">{{ item.dscrp }}</div>
        </div>
      </template>
    </el-option>
  </LjSelectorSelect>
</template>

<script lang="ts" setup name="SelectorMtrldefSelect">
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;
  mtrltype: 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 isuse = ref("有效");

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 = {
    keyword: queryString,
    arg_mtrltype: props.mtrltype,
    arg_pricelistid: props.pricelistid
  };
  newParams.dsname = "web_mtrl_noprice_choose";
  let result = await CommonDynamicSelect(newParams, DwnameEnum.mtrlPrice);

  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) => {
  emit("select", item);
};

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