<template>
  <div class="label-container">
    <div class="basic-info-title">
      <img src="@/assets/enterpriseLibrary/basic-info.png" alt="解析标签组">
      <span>{{ title }}</span>
    </div>

    <el-form
      :model="formData"
      :rules="rules"
      ref="analysisLabelForm"
      label-width="120px"
      label-position="top"
      class="label-form"
    >

      <el-row :gutter="24">
        <el-col :span="8">
      <el-form-item label="解析标签组" prop="analysisLabelId">
        <el-select
          :disabled="currentVersion !== null "
          v-model="formData.analysisLabelId"
          placeholder="请选择解析标签组"
          clearable
          filterable
          :remote-method="handleRemoteSearch"
          :loading="labelLoading"
        >
          <el-option
            v-for="item in labelOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { analysisLabelSelect } from '@/api/template/analysisLabel/analysisLabel'
import {listAnalysisLabel} from "../../../../../api/template/analysisLabel/analysisLabel";

export default {
  name: 'AnalysisLabel',
  props: {
    title: {
      type: String,
      default: '模板组成-解析标签组'
    }
  },
  data() {
    return {
      currentVersion: '',
      labelOptions: [], // 标签组选项
      labelLoading: false, // 远程搜索加载状态
      formData: {
        analysisLabelId: '',
        analysisLabelName: '',
      },
      rules: {
        analysisLabelId: [
          { required: true, message: '请选择解析标签组', trigger: 'change' }
        ]
      }
    }
  },

  created(){
    this.loadAnalysisLabel()
  },

  methods: {

    loadAnalysisLabel(){
      listAnalysisLabel()
        .then(res => {
          this.labelOptions = res.rows.map(item => ({
            label: item.analysisLabelName,
            value: item.analysisLabelId
          }))
        }).catch(error => {
        console.error('加载标签组失败：', error)
      })
    },

    // 远程搜索标签组
    handleRemoteSearch(query) {
      if (!query) {
        this.labelOptions = []
        return
      }
      this.labelLoading = true
      analysisLabelSelect({ keyword: query })
        .then(res => {
          this.labelOptions = res.data || []
        })
        .catch(error => {
          console.error('搜索标签组失败：', error)
          this.$message.error('搜索标签组失败，请重试')
        })
        .finally(() => {
          this.labelLoading = false
        })
    },

    // 回显数据
    setFormData(data) {
      this.currentVersion = data.currentVersion
      this.formData = {
        analysisLabelId: data.analysisLabelId || '',
        analysisLabelName: data.analysisLabelName || '',
        labelDesc: data.labelDesc || ''
      }
    },

    // 表单验证
    validate() {
      return new Promise((resolve, reject) => {
        this.$refs.analysisLabelForm.validate((valid) => {
          if (valid) {
            resolve({ ...this.formData })
          } else {
            reject(new Error('请选择解析标签组'))
          }
        })
      })
    }
  }
}
</script>

<style scoped lang="scss">
.label-container {
  width: 100%;
}

.basic-info-title {
  display: flex;
  align-items: center;
  margin: 10px 0 20px;
  span {
    margin-left: 8px;
    font-size: 18px;
    font-weight: 500;
    color: #1d2129;
  }
  img {
    width: 20px;
    height: 20px;
    object-fit: contain;
  }
}

.label-form {
  padding: 0 10px;
}

::v-deep .el-select {
  width: 100%;
}

::v-deep .el-form-item {
  margin-bottom: 20px;
}
</style>
