<template>
  <div class="basic-info-container">
    <div class="basic-info-title">
      <img src="@/assets/enterpriseLibrary/basic-info.png" alt="基本信息">
      <span>基本信息</span>
    </div>

    <el-form
      :model="formData"
      :rules="rules"
      ref="basicInfoForm"
      label-width="120px"
      label-position="top"
      class="basic-form"
    >
      <el-row :gutter="24">
        <el-col :span="8">
      <el-form-item label="模板名称" prop="templateName">
        <el-input
          v-model="formData.templateName"
          placeholder="请输入"
          show-word-limit
          maxlength="64"
        ></el-input>
      </el-form-item>
        </el-col>

        <el-col :span="8">
      <el-form-item label="行业类型" prop="industryType">
        <el-select
          v-model="formData.industryType"
          placeholder="请选择行业类型"
          clearable
        >
          <el-option
            v-for="item in industryTypeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="24">
        <el-col :span="24">
      <el-form-item label="描述" prop="templateDesc">
        <el-input
          v-model="formData.templateDesc"
          placeholder="请输入"
          type="textarea"
          rows="4"
          maxlength="500"
        ></el-input>
      </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'BasicInfo',
  props: {
    type: {
      type: String,
      default: 'add'
    }
  },
  data() {
    return {
      formData: {
        templateName: '',
        industryType: '',
        templateDesc: ''
      },
      rules: {
        templateName: [
          { required: true, message: '请输入模板名称', trigger: 'blur' },
          { max: 128, message: '模板名称不能超过64个字符', trigger: 'blur' }
        ],
        industryType: [
          { required: true, message: '请选择行业类型', trigger: 'change' }
        ]
      },
      industryTypeOptions:[],
    }
  },

  created(){
    this.loadIndustryTypeDict()
  },

  methods: {

    loadIndustryTypeDict() {
      this.getDicts('industry_type').then(response => {
        this.industryTypeOptions = response.data.map(item => ({
          label: item.dictLabel,
          value: item.dictLabel
        }))
      }).catch(error => {
        console.error('加载行业类型字典失败：', error)
      })
    },

    // 回显数据
    setFormData(data) {
      this.formData = {
        templateName: data.templateName || '',
        industryType: data.industryType || '',
        templateDesc: data.templateDesc || ''
      }
    },

    // 表单验证
    validate() {
      return new Promise((resolve, reject) => {
        this.$refs.basicInfoForm.validate((valid) => {
          if (valid) {
            resolve({ ...this.formData })
          } else {
            reject(new Error('基本信息填写不完整'))
          }
        })
      })
    },

    // 重置表单
    resetForm() {
      this.$refs.basicInfoForm.resetFields()
    }
  }
}
</script>

<style scoped lang="scss">
.basic-info-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;
  }
}

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

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