<template>
  <!-- 工序选择 -->
  <el-select v-model="valueName" v-if="status1==false" placeholder="请输入工序" :remote-method="remoteMethod" remote-show-suffix remote clearable reserve-keyword filterable :loading="Loading" @change="onSelectChange" @visible-change="onVisibleChange">
    <el-option v-for="item in procedureSelectList" :key="item.id" :label="item.name" :value="item.id" />
  </el-select>
   <el-select v-model="valueName" v-else style="text-decoration: line-through" placeholder="请输入工序" :remote-method="remoteMethod" remote-show-suffix remote clearable reserve-keyword filterable :loading="Loading" @change="onSelectChange" @visible-change="onVisibleChange">
    <el-option v-for="item in procedureSelectList" :key="item.id" :label="item.name" :value="item.id" />
  </el-select>
</template>

<script lang="ts" setup>
import { ref, onMounted, toRefs } from 'vue'
import * as ProcedureApi from '@/api/heli/procedure'
//接收调用组件传递的值
const props = defineProps({
  modelValue: {
    type: [Number, String],
    required: true // 如果是必需的属性，可以设置为true
  }
})

const queryParams = {
  pageNo: 1,
  pageSize: 10,
  code: undefined,
  name: undefined,
  description: undefined,
  status: 1,
  creator: undefined,
  createTime: [],
  updater: undefined,
  updateTime: [],
  deleted: undefined,
  tenantId: undefined,
  isReport: undefined,
  wid: undefined
}

const valueName: any = ref() //选中的值
const valueNameObject: any = ref() // 存储选中的完整用户对象
const emit = defineEmits(['update:newValue'])
const procedureList = ref<ProcedureApi.ProcedureVO[]>([]) // 用户列表
const procedureSelectList = ref<ProcedureApi.ProcedureVO[]>([])
const loading = ref(false)
const SelectLoading = ref(false)
const propsmodelValue = toRefs(props).modelValue // 获取 props 中的 modelValue
const initialValue: any = ref(null) // 存储初始选中的用户 ID
const status1 = ref(false)
const gitlist = async () => {
  const data = await ProcedureApi.getProcedurePage(queryParams)
  procedureList.value = []
  procedureSelectList.value = []
  procedureList.value = [...procedureList.value, ...data.list]
  // 设置初始值
  if (propsmodelValue.value) {
    valueName.value = Number(propsmodelValue.value)
    const initialUser = await ProcedureApi.getProcedure(valueName.value)
      if (initialUser.status == 2) {
      status1.value = true
    } else {
      status1.value = false
    }
    // 查找初始用户是否已经在 procedureList 中
    let foundInitialUserInList = false
    for (const eqyip of procedureList.value) {
      if (eqyip.id === initialUser.id) {
        foundInitialUserInList = true
        break
      }
    }
    // 如果初始用户不在列表中，则将其添加到列表开头
    if (!foundInitialUserInList) {
      procedureList.value.unshift(initialUser)
    }
    // loading.value = false
  }
  procedureSelectList.value = procedureList.value
  loading.value = false
}

const remoteMethod = async (query: any) => {
  loading.value = true
  procedureSelectList.value = []
  try {
    if (query) {
      queryParams.name = query
      const data = await ProcedureApi.getProcedurePage(queryParams)
      procedureList.value = data.list
      procedureSelectList.value = data.list
    } else {
      gitlist()
    }
  } catch (error) {
    console.error(error)
  } finally {
    loading.value = false
  }
}
// 初始化用户列表
onMounted(async () => {
  try {
    await gitlist()
  } catch (error) {
    console.error(error)
  }
})
watch(valueName, (newValue: any) => {
  if (newValue) {
    onSelectChange(newValue)
    procedureList.value = []
    gitlist()
  }
})

const onVisibleChange = (isVisible: boolean) => {
  if (!isVisible) {
    // 下拉框关闭时，重置查询参数和下拉框列表
    queryParams.name = undefined
    procedureList.value = []
    procedureSelectList.value = []
    } else {
    gitlist()
  }
}

const onSelectChange = (newValue: any) => {
  valueNameObject.value = procedureList.value.find((eqyip) => eqyip.id === newValue) // 更新选中的完整用户对象
  emit('update:newValue', valueNameObject.value)
}
</script>
