<template>
  <div class="update-courseware">
    <el-form
      ref="visibleForm"
      :model="visibleForm"
      class="visibleForm"
      label-width="100px"
    >
      <el-form-item label="课件名称">{{ detail.name }}</el-form-item>
      <el-form-item
        label="声部"
        :rules="[{ required: true, message: '请选择声部', trigger: 'change' }]"
        prop="subjectId"
      >
        <el-select
          clearable
          v-model="visibleForm.subjectId"
          placeholder="请选择声部"
          style="width: 100% !important"
        >
          <el-option
            v-for="item in selects.subjects"
            :value="item.id"
            :label="item.name"
            :key="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('close')">取 消</el-button>
      <el-button @click="onSubmit" type="primary">确 定</el-button>
    </span>
  </div>
</template>

<script>
import { updateSubject } from "./api";
export default {
  name: "update-courseware",
  props: ["detail"],
  data() {
    return {
      visibleForm: {
        subjectId: null
      }
    };
  },
  async mounted() {
    this.$store.dispatch("setSubjects");
    this.visibleForm.subjectId = this.detail.subjectId || null;
  },
  methods: {
    async onSubmit() {
      //
      this.$refs["visibleForm"].validate(async flag => {
        if (!flag) {
          return;
        }

        try {
          await updateSubject({
            id: this.detail.id,
            subjectId: this.visibleForm.subjectId
          });
          this.$message.success("修改成功");
          this.$emit("close");
          this.$emit("getList");
        } catch {
          //
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.dialog-footer {
  display: block;
  text-align: right;
}
</style>
