<template>
  <div>
    <div>
      <el-form :inline="true" class="orderform" size="mini">
        <el-row>
          <el-form-item>
            <el-input v-model="tag" style="width: 250px" placeholder="请输入..." class="search_tag" clearable/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="btn_search()"><i class="el-icon-search"/> 查询</el-button>
            <el-button @click="btn_clear()"><i class="el-icon-remove-outline"/> 重置</el-button>
          </el-form-item>
        </el-row>
      </el-form>
      <el-table
        ref="multipleTable"
        :data="tableData"
        :highlight-current-row="true"
        border>
        <el-table-column
          prop="id"
          label="编号"/>
        <el-table-column
          prop="nickName"
          label="答题人"/>
        <el-table-column
          prop="create_date"
          label="答题时间"/>
        <el-table-column
          fixed="right"
          label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="viewQuestionAnswer(scope.row)">答题详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="mydiv">
        <el-pagination
          :current-page.sync="pageNo"
          :page-sizes="[10, 50, 100, 300,1000]"
          :page-size="pageSize"
          :total="totalDataNumber"
          layout="sizes,total, prev, pager, next"
          class="table-pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>

    </div>

    <!-- 点击删除按钮，删除确认框-->
    <el-dialog
      :visible.sync="dialogFormVisiable"
      title="提示"
      width="30%">
      <div
        v-for="(domain, index) in domains"
        :label="'题目选项' + (index+1) "
        :key="domain.key"
      >
        <span>题号: {{ domain.id }}</span>
        <span> | 答案: {{ domain.choice }}</span>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisiable = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisiable = false">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'Index',
  data() {
    return {
      rules: {
        quesionIndex: [{ required: true, message: '请输入题目显示序号', trigger: 'blur' }],
        title: [{ required: true, message: '请输入题目标题', trigger: 'blur' }],
        type: [{ required: true, message: '请选择题目类型', trigger: 'blur' }]
      },
      tag: '',
      allData: [], // 所有前端分页数据
      tableData: [], // 表格数据 仅展示当前页的数据
      pageNo: 1, // 当前页数
      totalDataNumber: 0, // 总记录数
      pageSize: 10,
      formLabelWidth: '100px', // 弹出框form宽
      dialogFormVisiable: false,
      types: [
        {
          value: 1,
          label: '单选'
        },
        {
          value: 2,
          label: '多选'
        },
        {
          value: 3,
          label: '问答输入'
        }
      ],
      options: [
        {
          value: 1,
          label: '是'
        },
        {
          value: 0,
          label: '否'
        }
      ],
      addForm: {
        quesionIndex: '',
        title: '',
        type: '',
        requiredAnswer: '',
        questionOptions: ''
      },
      modifyForm: {
        id: '',
        quesionIndex: 0,
        title: '',
        type: '',
        requiredAnswer: '',
        questionOptions: ''
      },
      domains: []
    }
  },
  created() {
    this.btn_search()
  },
  mounted() {
  },
  methods: {
    formatStatus(ob) {
      if (ob.requiredAnswer === undefined) {
        return '否'
      } else if (ob.requiredAnswer === 0) {
        return '否'
      } else if (ob.requiredAnswer === 1) {
        return '是'
      } else {
        return '否'
      }
    },
    btn_search() {
      const vm = this
      axios.get(process.env.BASE_API + '/question/listanswer', {
        params: {
          tag: this.tag
        }
      }).then(function(response) {
        var obj = response.data
        if (obj.code !== 10000) {
          vm.$message.error(obj.message)
          vm.totalDataNumber = 0
          vm.allData = []
          vm.tableData = []
        } else {
          vm.totalDataNumber = parseInt(obj.total)
          if (vm.totalDataNumber > 0) {
            vm.allData = obj.data
            vm.tableData = obj.data.slice(0, vm.pageSize)
          } else {
            vm.allData = []
            vm.tableData = []
          }
        }
      }).catch(function(error) {
        alert(error)
      })
    },
    btn_clear() {
      this.tag = ''
      this.btn_search()
    },
    handleCurrentChange() {
      this.tableData = this.allData.slice((this.pageNo - 1) * this.pageSize, this.pageNo * this.pageSize)
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.tableData = this.allData.slice((this.pageNo - 1) * this.pageSize, this.pageNo * this.pageSize)
    },
    addQuestion() {
      this.domains = []
      this.dialogFormVisiable = true
    },
    addQuestionCancel() {
      this.dialogFormVisiable = false
      this.clearForm()
    },
    clearForm() {
    },
    viewQuestionAnswer(row) {
      this.dialogFormVisiable = true
      this.modifyForm = row
      this.domains = JSON.parse(row.answer)
      if (this.domains === null) {
        this.domains = []
      }
    },
    viewQuestionAnswerCancel() {
      this.dialogModifyVisiable = false
    },
    formatType(ob) {
      if (ob.type === undefined) {
        return ''
      } else if (ob.type === 1) {
        return '单选题'
      } else if (ob.type === 2) {
        return '多选题'
      } else if (ob.type === 3) {
        return '输入题'
      } else {
        return '未知'
      }
    },
    submitForm(items) {
      for (var i = 0; i < this.domains.length; i++) {
        console.log(this.domains[i].info)
      }
    },
    removeDomain(item) {
      var index = this.domains.indexOf(item)
      if (index !== -1) {
        this.domains.splice(index, 1)
      }
    },
    addDomain() {
      this.domains.push({
        info: ''
      })
    }
  }
}
</script>

<style scoped>
  .orderform {
    padding-top: 20px;
    padding-left: 10px;
    padding-right: 10px;
  }

  .mydiv {
    float: right;
    margin-right: 10px;
  }
</style>
