<template>
  <div class="app-container">
    <div class="common-card">
      <!-- 新建类别 -->
      <div class="engineeringWork-create">
        <div class="engineeringWork-create-title">新建类别</div>
        <div class="engineeringWork-create-edit">
          <div class="engineeringWork-create-none"></div>
          <div class="engineeringWork-create-input">
            <span>类别名称：</span>
            <a-input
              v-model.trim="questionTypeName"
              placeholder="请输入题目类别"
            />
          </div>
          <a-button
            type="primary"
            :disabled="!questionTypeName.length"
            @click="engineeringWorkCreateFun"
            >新建</a-button
          >
        </div>
      </div>
    </div>
    <div class="common-card a-card-margin-top">
      <!-- 已有类别 -->
      <div class="engineeringWork-list">
        <div class="engineeringWork-list-title">已有类别</div>
        <a-table
          :columns="columns"
          :row-key="(record) => record.id"
          :data-source="tableData"
          :pagination="false"
        >
          <a
            slot="action"
            slot-scope="text, record"
            @click="engineeringWorkDelete(record)"
            >删除</a
          >
        </a-table>
      </div>
    </div>
  </div>
</template>

<script>
import { formatePathParams } from '@/filters';
export default {
  name: 'examQuestionType',
  props: {},
  components: {},
  data() {
    return {
      questionTypeName: '', // 新建题目类别的名称
      columns: [], // 表单配置
      tableData: [], // 列表数据
    };
  },
  created() {
    this.initDataFun(); //初始化数据
  },
  mounted() {},
  beforeDestroy() {},
  watch: {},
  computed: {},
  methods: {
    //初始化数据
    initDataFun() {
      // 表单配置
      this.columns = [
        {
          title: '题目类别',
          dataIndex: 'name',
          key: 'name',
        },
        {
          title: '操作',
          key: 'action',
          scopedSlots: { customRender: 'action' },
          width: 150,
        },
      ];
      this.getTableListFun(); // 查询：题目列表
    },
    // 查询：题目类别列表
    getTableListFun() {
      this.loading = true;
      this.$_http
        .get(this.$_API.INTERFACE_GET_EXAMS_QUESTION_TYPE)
        .then((res) => {
          this.tableData = res.data;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    // 操作：新建题目类别
    engineeringWorkCreateFun() {
      this.loading = true;
      let params = {
        name: this.questionTypeName,
        parentId: null,
      };
      this.$_http
        .post(this.$_API.INTERFACE_POST_EXAMS_QUESTION_TYPE_CREATE, params)
        .then(() => {
          this.questionTypeName = '';
          this.loading = false;
          this.$message.success('新建题目类别成功');
          this.getTableListFun(); // 查询：题目类别列表
        })
        .catch(() => {
          this.loading = false;
        });
    },
    // 操作：删除题目类别
    engineeringWorkDelete(record) {
      this.loading = true;
      let params = {
        categoryId: record.id,
      };
      this.$_http
        .delete(
          formatePathParams(
            this.$_API.INTERFACE_POST_EXAMS_QUESTION_TYPE_DELETE,
            params
          )
        )
        .then(() => {
          this.loading = false;
          this.$message.success('删除题目类别成功');
          this.getTableListFun(); // 查询：题目类别列表
        })
        .catch(() => {
          this.loading = false;
        });
    },
  },
};
</script>

<style lang="less"></style>

<style lang="less" scoped>
@import '~@/styles/common/variable.less';
.app-container {
  overflow-y: auto;
  .engineeringWork-create {
    width: 100%;
    .engineeringWork-create-title {
      font-size: 18px;
      font-weight: bold;
      color: @mainColorBlack85;
    }
    .engineeringWork-create-edit {
      display: flex;
      .engineeringWork-create-none {
        width: 100px;
      }
      .engineeringWork-create-input {
        margin-right: 50px;
        display: flex;
        align-items: center;
        > span {
          font-size: 14px;
          flex-wrap: nowrap;
          white-space: nowrap;
        }
      }
    }
  }
  .engineeringWork-list {
    .engineeringWork-list-title {
      margin-bottom: @paddingMarginVal;
      font-size: 18px;
      font-weight: bold;
      color: @mainColorBlack85;
    }
  }
}
</style>
