<template>
  <el-dialog title="问卷绑定" :visible.sync="dialogVisible" center width="50%" @close="dialogClose">
    <el-form :model="form" ref="ruleForm" label-width="100px">
      <el-form-item label="问卷" prop="quizIds">
        <el-select v-model="form.quizIds" placeholder="请选择问卷" multiple class="w100" :multiple-limit="1">
          <el-option v-for="item in quizList" :key="item.quizId" :label="item.title" :value="item.quizId" />
        </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="submitInfo">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {getAvailQuizList} from '@/api/test';
import {drugQuizBindApi, getDrugQuizApi} from '@/api/wmdadrugentdrugtable';
export default {
  data() {
    return {
      dialogVisible: false,
      quizList: [],
      form: {
        drugId: '',
        quizIds: []
      }
    };
  },
  created() {
    this.getAvailQuizListFn();
  },
  methods: {
    showInfo(id) {
      this.form.drugId = id;
      this.getDrugQuizFn(id);
      this.dialogVisible = true;
    },
    async getDrugQuizFn(id) {
      const res = await getDrugQuizApi({
        drugId: id
      });
      if (res.data.code === 0) {
        this.form.quizIds = res.data.data;
      }
    },
    async getAvailQuizListFn() {
      const res = await getAvailQuizList();
      if (res.data.code === 0) {
        this.quizList = res.data.data;
      }
    },
    submitInfo() {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          const res = await drugQuizBindApi(this.form);
          if (res.data.data) {
            this.$message.success('绑定成功');
          }
          this.$emit('success');
          this.dialogClose();
        }
      });
    },
    dialogClose() {
      this.form = {
        drugId: '',
        quizIds: []
      };
      this.dialogVisible = false;
    }
  }
};
</script>

<style lang="scss" scoped></style>
