<template>
  <el-select
    v-model="innerValue"
    filterable
    :loading="loading"
    placeholder="请选择检验清单"
    clearable
    @change="handleChange"
    style="width: 100%"
  >
    <el-option
      v-for="item in options"
      :key="item.checkId"
      :label="`${item.checkNo || '无编号'} - ${item.partName || ''}`"
      :value="item.checkId"
    />
  </el-select>
</template>

<script>
import { listEquipmentChecklist } from "@/api/mes/eqp/equipmentChecklist";

export default {
  name: "ChecklistSelect",
  props: {
    value: {
      type: [String, Number],
      default: ""
    }
  },
  data() {
    return {
      loading: false,
      options: [],
      innerValue: this.value
    };
  },
  watch: {
    value(val) {
      this.innerValue = val;
    }
  },
  mounted() {
    this.loadAll();
  },
  methods: {
    async loadAll() {
      this.loading = true;
      try {
        const res = await listEquipmentChecklist({ pageNum: 1, pageSize: 9999 });
        if (res.code === 200) {
          this.options = res.rows || [];
        }
      } catch (e) {
        console.error("获取检验清单列表失败", e);
      } finally {
        this.loading = false;
      }
    },
    handleChange(val) {
      if (!val) {
        this.$emit('input', "");
        this.$emit('change', "", null);
      } else {
        this.$emit('input', val);
        const row = this.options.find(o => o.checkId === val);
        this.$emit('change', val, row || null);
      }
    }
  }
};
</script>
