<template>
  <div class="app-container">
    <!--<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">-->
    <!--<el-form-item label="选项E" prop="pid">-->
    <!--<el-input-->
    <!--v-model="queryParams.pid"-->
    <!--placeholder="请输入选项E"-->
    <!--clearable-->
    <!--size="small"-->
    <!--@keyup.enter.native="handleQuery"-->
    <!--/>-->
    <!--</el-form-item>-->
    <!--<el-form-item label="试题题型" prop="qtpye">-->
    <!--<el-input-->
    <!--v-model="queryParams.qtpye"-->
    <!--placeholder="请输入试题题型"-->
    <!--clearable-->
    <!--size="small"-->
    <!--@keyup.enter.native="handleQuery"-->
    <!--/>-->
    <!--</el-form-item>-->
    <!--<el-form-item label="试题题型" prop="diff">-->
    <!--<el-input-->
    <!--v-model="queryParams.diff"-->
    <!--placeholder="请输入试题题型"-->
    <!--clearable-->
    <!--size="small"-->
    <!--@keyup.enter.native="handleQuery"-->
    <!--/>-->
    <!--</el-form-item>-->
    <!--<el-form-item label="学科Id" prop="subjectid">-->
    <!--<el-input-->
    <!--v-model="queryParams.subjectid"-->
    <!--placeholder="请输入学科Id"-->
    <!--clearable-->
    <!--size="small"-->
    <!--@keyup.enter.native="handleQuery"-->
    <!--/>-->
    <!--</el-form-item>-->
    <!--<el-form-item label="年级ID" prop="gradeid">-->
    <!--<el-input-->
    <!--v-model="queryParams.gradeid"-->
    <!--placeholder="请输入年级ID"-->
    <!--clearable-->
    <!--size="small"-->
    <!--@keyup.enter.native="handleQuery"-->
    <!--/>-->
    <!--</el-form-item>-->
    <!--<el-form-item label="知识点名称" prop="knowledges">-->
    <!--<el-input-->
    <!--v-model="queryParams.knowledges"-->
    <!--placeholder="请输入知识点名称"-->
    <!--clearable-->
    <!--size="small"-->
    <!--@keyup.enter.native="handleQuery"-->
    <!--/>-->
    <!--</el-form-item>-->
    <!--<el-form-item label="试题来源(试卷)" prop="source">-->
    <!--<el-input-->
    <!--v-model="queryParams.source"-->
    <!--placeholder="请输入试题来源(试卷)"-->
    <!--clearable-->
    <!--size="small"-->
    <!--@keyup.enter.native="handleQuery"-->
    <!--/>-->
    <!--</el-form-item>-->
    <!--<el-form-item label="试题的tiid，结合fromsite进行同网站试题排重，用于增量更新" prop="tiid">-->
    <!--<el-input-->
    <!--v-model="queryParams.tiid"-->
    <!--placeholder="请输入试题的tiid，结合fromsite进行同网站试题排重，用于增量更新"-->
    <!--clearable-->
    <!--size="small"-->
    <!--@keyup.enter.native="handleQuery"-->
    <!--/>-->
    <!--</el-form-item>-->
    <!--<el-form-item label="试题的tiid，结合fromsite进行同网站试题排重，用于增量更新" prop="ptiid">-->
    <!--<el-input-->
    <!--v-model="queryParams.ptiid"-->
    <!--placeholder="请输入试题的tiid，结合fromsite进行同网站试题排重，用于增量更新"-->
    <!--clearable-->
    <!--size="small"-->
    <!--@keyup.enter.native="handleQuery"-->
    <!--/>-->
    <!--</el-form-item>-->
    <!--<el-form-item label="试题的tiid，结合fromsite进行同网站试题排重，用于增量更新" prop="fromsite">-->
    <!--<el-input-->
    <!--v-model="queryParams.fromsite"-->
    <!--placeholder="请输入试题的tiid，结合fromsite进行同网站试题排重，用于增量更新"-->
    <!--clearable-->
    <!--size="small"-->
    <!--@keyup.enter.native="handleQuery"-->
    <!--/>-->
    <!--</el-form-item>-->
    <!--<el-form-item>-->
    <!--<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
    <!--<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
    <!--</el-form-item>-->
    <!--</el-form>-->

    <el-row :gutter="10" class="mb8">
    <el-col :span="1.5">
    <el-button
    type="primary"
    plain
    icon="el-icon-plus"
    size="mini"
    @click="handleAdd"
    v-hasPermi="['back:subquestions:add']"
    >新增</el-button>
    </el-col>
      <el-col :span="1.5">
    <el-button
    type="primary"
    plain
    icon="el-icon-plus"
    size="mini"
    @click="handleAddText"
    v-hasPermi="['back:subquestions:add']"
    >新增文本</el-button>
    </el-col>
    <el-col :span="1.5">
    <el-button
    type="success"
    plain
    icon="el-icon-edit"
    size="mini"
    :disabled="single"
    @click="handleUpdate"
    v-hasPermi="['back:subquestions:edit']"
    >修改</el-button>
    </el-col>
    <el-col :span="1.5">
    <el-button
    type="danger"
    plain
    icon="el-icon-delete"
    size="mini"
    :disabled="multiple"
    @click="handleDelete"
    v-hasPermi="['back:subquestions:remove']"
    >删除</el-button>
    <!--</el-col>-->
    <!--<el-col :span="1.5">-->
    <!--<el-button-->
    <!--type="warning"-->
    <!--plain-->
    <!--icon="el-icon-download"-->
    <!--size="mini"-->
    <!--:loading="exportLoading"-->
    <!--@click="handleExport"-->
    <!--v-hasPermi="['back:subquestions:export']"-->
    <!--&gt;导出</el-button>-->
    </el-col>
    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="subquestionsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="试题的tiid，结合fromsite进行同网站试题排重，用于增量更新" align="center" prop="id" />-->
      <el-table-column label="题目id" align="center" prop="pid" />
      <el-table-column label="试题-题干" align="center" prop="title" />
      <el-table-column label="选项A" align="center" prop="optionA" />
      <el-table-column label="选项B" align="center" prop="optionB" />
      <el-table-column label="选项C" align="center" prop="optionC" />
      <el-table-column label="选项D" align="center" prop="optionD" />
      <el-table-column label="答案" align="center" prop="answer1" />
      <el-table-column label="试题解析" align="center" prop="parse" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row, 1)"
            v-hasPermi="['back:subquestions:edit']"
          >文本修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row, 2)"
            v-hasPermi="['back:subquestions:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['back:subquestions:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改子试题对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="题目" prop="title">
          <editor v-model="form.title" :min-height="122"/>
        </el-form-item>
        <el-form-item label="选项A" prop="optionA">
          <editor v-model="form.optionA" :min-height="122"/>
        </el-form-item>
        <el-form-item label="选项B" prop="optionB">
          <editor v-model="form.optionB" :min-height="122"/>
        </el-form-item>
        <el-form-item label="选项C" prop="optionC">
          <editor v-model="form.optionC" :min-height="122"/>
        </el-form-item>
        <el-form-item label="选项D" prop="optionD">
          <editor v-model="form.optionD" :min-height="122"/>
        </el-form-item>
        <el-form-item label="答案1" prop="answer1">
          <editor v-model="form.answer1" :min-height="122"/>
        </el-form-item>
        <el-form-item label="答案2" prop="answer2">
          <editor v-model="form.answer2" :min-height="122"/>
        </el-form-item>
        <el-form-item label="试题解析" prop="parse">
          <editor v-model="form.parse" :min-height="122"/>
        </el-form-item>
        <el-form-item label="试题题型" prop="qtpye">
          <el-input v-model="form.qtpye" placeholder="请输入试题题型" autosize/>
        </el-form-item>
        <el-form-item label="试题难度" prop="diff">
          <el-input v-model="form.diff" placeholder="请输入试题题型" autosize/>
        </el-form-item>
        <el-form-item label="知识点名称" prop="knowledges">
          <el-input v-model="form.knowledges" placeholder="请输入知识点名称" autosize/>
        </el-form-item>
        <!--<el-form-item label="试题来源(试卷)" prop="source">-->
        <!--<el-input v-model="form.source" placeholder="请输入试题来源(试卷)" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="试题的tiid，结合fromsite进行同网站试题排重，用于增量更新" prop="tiid">-->
        <!--<el-input v-model="form.tiid" placeholder="请输入试题的tiid，结合fromsite进行同网站试题排重，用于增量更新" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="试题的tiid，结合fromsite进行同网站试题排重，用于增量更新" prop="ptiid">-->
        <!--<el-input v-model="form.ptiid" placeholder="请输入试题的tiid，结合fromsite进行同网站试题排重，用于增量更新" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="试题的tiid，结合fromsite进行同网站试题排重，用于增量更新" prop="fromsite">-->
        <!--<el-input v-model="form.fromsite" placeholder="请输入试题的tiid，结合fromsite进行同网站试题排重，用于增量更新" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="答案" prop="answerJson">-->
        <!--<el-input v-model="form.answerJson" type="textarea" placeholder="请输入内容" />-->
        <!--</el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改子试题对话框 -->
    <el-dialog title="文本修改" :visible.sync="openText" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="题目" prop="title">
          <el-input type="textarea" v-model="form.title" :min-height="122"/>
        </el-form-item>
        <el-form-item label="选项A" prop="optionA">
          <el-input type="textarea" v-model="form.optionA" :min-height="122"/>
        </el-form-item>
        <el-form-item label="选项B" prop="optionB">
          <el-input type="textarea" v-model="form.optionB" :min-height="122"/>
        </el-form-item>
        <el-form-item label="选项C" prop="optionC">
          <el-input type="textarea" v-model="form.optionC" :min-height="122"/>
        </el-form-item>
        <el-form-item label="选项D" prop="optionD">
          <el-input type="textarea" v-model="form.optionD" :min-height="122"/>
        </el-form-item>
        <el-form-item label="答案1" prop="answer1">
          <el-input type="textarea" v-model="form.answer1" :min-height="122"/>
        </el-form-item>
        <el-form-item label="答案2" prop="answer2">
          <el-input type="textarea" v-model="form.answer2" :min-height="122"/>
        </el-form-item>
        <el-form-item label="试题解析" prop="parse">
          <el-input type="textarea" v-model="form.parse" :min-height="122"/>
        </el-form-item>
        <el-form-item label="试题题型" prop="qtpye">
          <el-input v-model="form.qtpye" placeholder="请输入试题题型" autosize/>
        </el-form-item>
        <el-form-item label="试题难度" prop="diff">
          <el-input v-model="form.diff" placeholder="请输入试题题型" autosize/>
        </el-form-item>
        <el-form-item label="知识点名称" prop="knowledges">
          <el-input v-model="form.knowledges" placeholder="请输入知识点名称" autosize/>
        </el-form-item>
        <!--<el-form-item label="试题来源(试卷)" prop="source">-->
        <!--<el-input v-model="form.source" placeholder="请输入试题来源(试卷)" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="试题的tiid，结合fromsite进行同网站试题排重，用于增量更新" prop="tiid">-->
        <!--<el-input v-model="form.tiid" placeholder="请输入试题的tiid，结合fromsite进行同网站试题排重，用于增量更新" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="试题的tiid，结合fromsite进行同网站试题排重，用于增量更新" prop="ptiid">-->
        <!--<el-input v-model="form.ptiid" placeholder="请输入试题的tiid，结合fromsite进行同网站试题排重，用于增量更新" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="试题的tiid，结合fromsite进行同网站试题排重，用于增量更新" prop="fromsite">-->
        <!--<el-input v-model="form.fromsite" placeholder="请输入试题的tiid，结合fromsite进行同网站试题排重，用于增量更新" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="答案" prop="answerJson">-->
        <!--<el-input v-model="form.answerJson" type="textarea" placeholder="请输入内容" />-->
        <!--</el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import { listSubquestions, getSubquestions, delSubquestions, addSubquestions, updateSubquestions, exportSubquestions } from "@/api/back/subquestions";
  import Editor from '@/components/Editor';
  export default {
    name: "Subquestions",
    components: {
      Editor
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        openText: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 子试题表格数据
        subquestionsList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          title: null,
          optionA: null,
          optionB: null,
          optionC: null,
          optionD: null,
          pid: null,
          answer1: null,
          answer2: null,
          parse: null,
          qtpye: null,
          diff: null,
          subjectid: null,
          gradeid: null,
          knowledges: null,
          source: null,
          tiid: null,
          ptiid: null,
          fromsite: null,
          answerJson: null
        },
        // 表单参数
        form: {},
        pid: null,
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.pid = this.queryParams.pid = this.$route.params && this.$route.params.pid;
      this.getList();
    },
    methods: {
      /** 查询子试题列表 */
      getList() {
        this.loading = true;
        listSubquestions(this.queryParams).then(response => {
          this.subquestionsList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          title: null,
          optionA: null,
          optionB: null,
          optionC: null,
          optionD: null,
          pid: null,
          answer1: null,
          answer2: null,
          parse: null,
          qtpye: null,
          diff: null,
          subjectid: null,
          gradeid: null,
          knowledges: null,
          source: null,
          tiid: null,
          ptiid: null,
          fromsite: null,
          answerJson: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加子试题";
      },
      handleAddText() {
        this.reset();
        this.openText = true;
        this.title = "添加子试题";
      },
      /** 修改按钮操作 */
      handleUpdate(row, type) {
        this.reset();
        const id = row.id || this.ids
        getSubquestions(id).then(response => {
          this.form = response.data;
          if(type == 1) {
            this.openText = true;
          }else{
            this.open = true;
            this.title = "修改子试题";
          }

        });
      },
      /** 提交按钮 */
      submitForm() {
        this.form.pid = this.pid;
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateSubquestions(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.openText = false;
                this.getList();
              });
            } else {
              addSubquestions(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.openText = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除子试题编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSubquestions(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有子试题数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportSubquestions(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        })
      }
    }
  };
</script>

