<template>
  <a-select
    :value="row[field]"
    :options="options"
    :disabled="disabled"
    :open="dropdownOpen"
    allow-clear
    show-search
    :bordered="false"
    size="small"
    class="mixing-step-select"
    popup-class-name="mixing-step-select-dropdown"
    :list-height="listHeight"
    :placeholder="placeholder"
    :get-popup-container="getPopupContainer"
    @update:value="handleChange"
    @dropdown-visible-change="handleDropdownVisibleChange"
    @click.stop
  />
</template>

<script lang="ts" setup>
  import { computed, ref } from 'vue';
  import { useMessage } from '/@/hooks/web/useMessage';

  /** 下拉单项高度（与 ant-design-vue small Select 选项行高一致） */
  const STEP_SELECT_ITEM_HEIGHT = 32;
  /** 下拉最多可见条数 */
  const STEP_SELECT_MAX_VISIBLE = 20;

  const props = defineProps<{
    row: Recordable;
    field: string;
    options: { label: string; value: string }[];
    disabled?: boolean;
    placeholder?: string;
    machineId?: string;
  }>();

  const { createMessage } = useMessage();
  const dropdownOpen = ref(false);

  //update-begin---author:cursor ---date:20260525  for：【XSLMES-20260525-A57】混合步骤动作/组合下拉最多展示20条-----------
  const listHeight = computed(() => {
    const count = props.options?.length ?? 0;
    const visibleCount = count > 0 ? Math.min(count, STEP_SELECT_MAX_VISIBLE) : STEP_SELECT_MAX_VISIBLE;
    return visibleCount * STEP_SELECT_ITEM_HEIGHT;
  });
  //update-end---author:cursor ---date:20260525  for：【XSLMES-20260525-A57】混合步骤动作/组合下拉最多展示20条-----------

  function handleChange(value: string | undefined) {
    props.row[props.field] = value;
  }

  //update-begin---author:cursor ---date:20260522  for：【XSLMES-20260522-A37】未选机台点击动作/组合提示请先选择机台-----------
  function handleDropdownVisibleChange(visible: boolean) {
    if (props.disabled) {
      dropdownOpen.value = false;
      return;
    }
    if (visible && !props.machineId) {
      createMessage.warning('请先选择机台');
      dropdownOpen.value = false;
      return;
    }
    dropdownOpen.value = visible;
  }
  //update-end---author:cursor ---date:20260522  for：【XSLMES-20260522-A37】未选机台点击动作/组合提示请先选择机台-----------

  //update-begin---author:cursor ---date:20260522  for：【XSLMES-20260522-A36】动作/组合下拉挂到body避免被表格裁切-----------
  function getPopupContainer() {
    return document.body;
  }
  //update-end---author:cursor ---date:20260522  for：【XSLMES-20260522-A36】动作/组合下拉挂到body避免被表格裁切-----------
</script>

<style lang="less" scoped>
  .mixing-step-select {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;

    :deep(.ant-select) {
      width: 100%;
      height: 100%;
    }

    :deep(.ant-select-selector) {
      border: none !important;
      box-shadow: none !important;
      background: transparent !important;
      padding: 0 18px 0 2px !important;
      min-height: 24px !important;
      height: 100% !important;
      display: flex !important;
      align-items: center !important;
    }

    :deep(.ant-select-selection-item),
    :deep(.ant-select-selection-placeholder) {
      line-height: 1.2 !important;
      text-align: center;
      padding-inline-end: 0 !important;
    }

    :deep(.ant-select-selection-search-input) {
      height: 100% !important;
    }

    :deep(.ant-select-arrow) {
      right: 2px;
      margin-top: -3px;
      color: #666;
      font-size: 10px;
      pointer-events: none;
    }

    :deep(.ant-select-clear) {
      right: 14px;
    }
  }
</style>
