<template>
  <el-dialog
    :title="'修改话题 - ' + rows.videoTitle"
    width="500px"
    :visible.sync="dialogVisible"
  >
    <el-form :model="form" :rules="rules" ref="topicForm" label-width="80px">
      <el-form-item label="选择话题">
        <el-select
          v-model="form.topic"
          filterable
          allow-create
          default-first-option
          placeholder="请选择或输入话题"
        >
          <el-option
            v-for="item in topicOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: "ChangeTopicDialog",
  props: {
    rows: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      form: {
        topic: ""
      },
      dialogVisible: false,
      topicOptions: [],
      loading: false,
      rules: {
        topic: [
          { required: true, message: "请选择或输入话题", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    show() {
      this.dialogVisible = !this.dialogVisible;
    },
    async fetchTopics() {
      try {
        this.loading = true;
        const res = await this.$api.video.getTopics(); // 假设接口路径为/video/topics
        this.topicOptions = res.data.map(item => ({
          value: item,
          label: item
        }));
      } finally {
        this.loading = false;
      }
    },
    handleConfirm() {
      this.$refs.topicForm.validate(valid => {
        if (valid) {
          this.$emit("confirm", this.form.topic);
        } else {
          return false;
        }
      });
      this.dialogVisible = false;
    }
  }
};
</script>

<style scoped>
.el-select {
  width: 100%;
}
</style>
