<template>
  <LjSelectorSelect
    ref="selectStationRef"
    v-model:value="props.value"
    value-key="springid"
    :loading="loading"
    v-bind="$attrs"
    :remote-method="remoteMethod"
    popper-class="lj-select-spring__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: 270px">{{ $t("table.u_mtrl_price.name") }}</div>
        </div>
      </div>
    </template>
    <el-option
      v-for="item in options"
      :key="item.springid"
      :label="item.springname"
      :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: 270px" class="table-td pr-4 text-ellipsis-one" :title="item.springname">{{ item.springname }}</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="SelectorSpringSelect">
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;
  argHeight?: any;
  argBednettypeid?: 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 = {
    arg_search: queryString,
    arg_newspring: 1
  };
  props?.argHeight && (newParams.queryParams.arg_height = props?.argHeight);
  if (props?.argBednettypeid) {
    let _arg_bedtypeid = -1;
    let _arg_newspring = 0;
    switch (props?.argBednettypeid) {
      case 11: // 袋装网
        _arg_bedtypeid = 0;
        _arg_newspring = 1;
        break;
      case 12: // 圆网
        _arg_bedtypeid = 2;
        break;
      case 13: // 拉丝网
        _arg_bedtypeid = 1;
        break;
    }
    newParams.queryParams.arg_bedtypeid = _arg_bedtypeid;
    newParams.queryParams.arg_newspring = _arg_newspring;
  }
  newParams.dsname = "web_spring_define_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);
};
</script>

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