<template>
  <div id="setQuestion">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="24" class="top">
          <el-button type="primary" size="mini" @click="add">添加题目</el-button>
          <el-button type="primary" size="mini" @click="back">返回</el-button>
        </el-col>
        <el-col :span="24" class="down">
          <el-col :span="24" class="title">
            {{ info.title }}
          </el-col>
          <el-col :span="24" class="form">
            <el-col :span="24" class="list" v-for="(item, index) in info.questions" :key="index">
              <el-col :span="24" class="name"> {{ index + 1 }}-{{ item.title }} </el-col>
              <el-col :span="24" class="radios" v-if="item.type == '0'">
                <el-radio v-model="info.radio" :label="item.num" v-for="(item, index) in item.selects" :key="index">{{ item.num }}-{{ item.name }}</el-radio>
              </el-col>
              <el-col :span="24" class="checkbox" v-else-if="item.type == '1'">
                <el-checkbox v-for="(item, index) in item.selects" :key="index">{{ item.num }}-{{ item.name }}</el-checkbox>
              </el-col>
              <el-col :span="24" class="content" v-else-if="item.type == '2'">
                <el-input v-model="item.content" type="textarea" :autosize="{ minRows: 4, maxRows: 6 }" placeholder="请输入简答" disabled> </el-input>
              </el-col>
            </el-col>
          </el-col>
        </el-col>
      </el-col>
    </el-row>
    <el-dialog title="题目管理" width="40%" :visible.sync="dialog" @closed="handleClose" :destroy-on-close="true">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="选择题目">
          <el-select v-model="form.questions" multiple filterable placeholder="请选择">
            <el-option v-for="item in questionsList" :key="item.id" :label="item.title" :value="item.id">
              <span style="float: left">{{ item.title }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.type == '0' ? '单选' : item.type == '1' ? '多选' : '简答' }}</span>
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" size="mini" @click="onSubmit">保存选项</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: questionnaire } = createNamespacedHelpers('questionnaire');
const { mapActions: question } = createNamespacedHelpers('question');
export default {
  name: 'setQuestion',
  props: {},
  components: {},
  data: function() {
    return {
      info: {},
      // 选择题目
      dialog: false,
      form: {},
      // 题目列表
      questionsList: [],
    };
  },
  async created() {
    await this.search();
  },
  methods: {
    ...questionnaire(['query', 'fetch', 'create', 'update', 'delete']),
    ...question({ queQuery: 'query' }),
    async search() {
      if (this.id) {
        let res = await this.fetch(this.id);
        if (this.$checkRes(res)) {
          this.$set(this, `info`, res.data);
        }
      }
    },
    // 添加题目
    async add() {
      let res = await this.queQuery();
      if (this.$checkRes(res)) {
        this.$set(this, `questionsList`, res.data);
      }
      this.dialog = true;
    },
    // 保存选题
    async onSubmit() {
      let data = this.form;
      data.id = this.id;
      let res = await this.update(data);
      if (this.$checkRes(res)) {
        this.$message({
          message: '添加题目成功',
          type: 'success',
        });
        this.handleClose();
        this.search();
      }
    },
    // 取消添加
    handleClose() {
      this.form = {};
      this.dialog = false;
    },
    back() {
      this.$router.push({ path: '/question' });
    },
  },
  computed: {
    ...mapState(['user']),
    id() {
      return this.$route.query.id;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  .top {
    text-align: right;
    margin: 0 0 15px 0;
  }
  .down {
    padding: 0 10%;
    .title {
      text-align: center;
      font-size: 20px;
      font-weight: bold;
      margin: 0 0 20px 0;
    }
    .form {
      padding: 10px 20px;
      border: 1px solid #ccc;
      .list {
        border-bottom: 1px dashed #f1f1f1;
        padding: 10px 0;
        .name {
          font-weight: bold;
          font-size: 18px;
          margin: 0 0 10px 0;
        }
        .content {
          padding: 0;
        }
      }
    }
  }
}
/deep/.el-select {
  width: 100%;
}
</style>
