<template>
  <div class="question-container">
    <el-tabs v-model="activeName" type="border-card">
      <el-tab-pane label="问题选项" name="1">
        <el-table stripe ref="table" :height="tableHeight" v-loading="tableLoading"
                  :data="questionList"
                  border row-key="id">
          <el-table-column show-overflow-tooltip prop="id" label="序号" align="center" width="50"/>
          <el-table-column min-width="80" show-overflow-tooltip prop="category_text" label="问题分类" align="center"/>
          <el-table-column min-width="80" show-overflow-tooltip prop="type_text" label="问题类型" align="center"/>
          <el-table-column min-width="80" show-overflow-tooltip prop="content" label="问题选项" align="center"/>
          <el-table-column min-width="80"  show-overflow-tooltip prop="note" label="备注" align="center"/>
          <el-table-column width="80" show-overflow-tooltip prop="base_score" label="基础分值" align="center"/>
          <el-table-column width="110" show-overflow-tooltip prop="exam_base_score" label="国考基础分值" align="center"/>
          <el-table-column width="120" show-overflow-tooltip prop="increase_num" label="每增加问题数量" align="center"/>
          <el-table-column width="60" show-overflow-tooltip prop="deduction" label="扣分" align="center"/>
          <el-table-column width="80" show-overflow-tooltip prop="sort" label="显示顺序" align="center"/>
          <el-table-column width="80" show-overflow-tooltip prop="format_create_user" label="添加人" align="center"/>
          <el-table-column width="160" show-overflow-tooltip prop="format_create_time" label="添加时间" align="center"/>
          <el-table-column width="100" show-overflow-tooltip prop="endPoint" label="状态" align="center">
            <template slot-scope="{row}">
              <span v-show="row.status ==1" style="color: #666">显示</span>
              <span v-show="row.status ==2" style="color: #666">隐藏</span>
              <el-switch v-if="isAdmin||btnRule.some(btn=>btn=='status')" v-model="row.status" :active-value="1"
                         :inactive-value="2" size="small"
                         @change="changeStatus(row)"/>
            </template>
          </el-table-column>
          <el-table-column v-if="isAdmin||btnRule.some(btn=>btn=='edit')" width="60" label="操作">
            <template slot-scope="scoped">
              <el-tooltip v-if="isAdmin||btnRule.some(btn=>btn=='edit')" class="item" effect="dark" content="编辑"
                          placement="bottom">
                <el-button @click="handleEdit(scoped.row)" type="primary" size="mini" icon="el-icon-edit"/>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="page-current">
          <el-pagination
            background
            :current-page="currentPage"
            :page-size="pageSize"
            prev-text="上一页"
            next-text="下一页"
            layout="total, prev, pager, next, jumper"
            style="display: inline-block;"
            @current-change="handleCurrentChange"
            :total="count"/>
        </div>
      </el-tab-pane>
      <el-tab-pane v-if="isAdmin||btnRule.some(btn=>btn=='add')" label="添加选项" name="2">
        <questionForm :ruleForm="ruleForm" :typeList="typeList" :categoryList="categoryList"
                    style="width: 50%" v-if="activeName==2" @submit="submit"></questionForm>
      </el-tab-pane>
    </el-tabs>
    <el-dialog :close-on-press-escape="false" :close-on-click-modal="false" center :visible.sync="addDialogVisible" title="编辑问题选项">
      <questionForm ref="dialogForm" :ruleForm="ruleForm" :typeList="typeList" :categoryList="categoryList"
                  style="width: 100%" @submit="submit" @onBack="onBack"></questionForm>
    </el-dialog>
  </div>
</template>

<script>
  import questionApi from '@/api/basicData/question'
  import questionForm from './questionForm'

  export default {
    name: "question",
    data() {
      return {
        activeName: '1',
        btnRule: [],
        isAdmin: false,
        addDialogVisible: false,
        ruleForm: {
          content:'',
          category:'',
          type:'',
          note:'',
          baseScore:'',
          increaseNum:'',
          examBaseScore:'',
          deduction:'',
          sort:'1'
        },
        typeList:{},
        categoryList:{},
        currentPage: 1,
        pageSize: 10,
        count: 0,
        questionList: [],
        tableLoading: false,
        tableHeight: document.body.clientHeight - 245,
      }
    },
    components: {questionForm},
    created() {
      const {btnRule, isAdmin} = this.$store.state.user
      this.btnRule = btnRule
      this.isAdmin = isAdmin
      this.pageSize = parseInt((document.body.clientHeight - 284) / 50)
    },
    mounted() {
      this.tableLoading = true
      this.getAllList()
      this.getBaseData()
    },
    watch: {
      activeName() {
        this.initRuleForm()
      }
    },
    methods: {
      getBaseData() {
        questionApi.getBaseData().then(res => {
          const {type_list={},category_list={}}=res.data
          this.typeList=type_list
          this.categoryList=category_list
        }).catch(e => {
          this.typeList={}
          this.categoryList={}
        })
      },
      changeStatus(data) {
        var content = ''
        if (data.status === 1) {
          content = '确定要启用吗？'
        } else {
          content = '确定要禁用吗？'
        }
        this.$confirm(content, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let params = {
            status: data.status == 1 ? '1' : '2',
            id: data.id
          }
          this.tableLoading = true
          questionApi.setStatus(params).then(res => {
            this.$message({
              type: 'success',
              message: '操作成功！'
            })
            this.getAllList()
          }).catch(e => {
            let questionList = JSON.parse(JSON.stringify(this.questionList))
            for (let i = 0; i < questionList.length; i++) {
              if (questionList[i].id == data.id) {
                questionList[i].status = data.status == 1 ? 2 : 1
              }
            }
            this.questionList = Object.assign([], questionList)
            this.tableLoading = false
          })
        }).catch(() => {
          let questionList = JSON.parse(JSON.stringify(this.questionList))
          for (let i = 0; i < questionList.length; i++) {
            if (questionList[i].id == data.id) {
              questionList[i].status = data.status == 1 ? 2 : 1
            }
          }
          this.questionList = Object.assign([], questionList)
          this.$message({
            type: 'info',
            message: '已取消该操作！'
          })
        })
      },
      handleEdit(data, type) {
        this.initRuleForm()
        this.addDialogVisible = true
        this.$nextTick(function () {
          try {
            let child = JSON.parse(JSON.stringify(data))
            child.baseScore=child.base_score
            child.increaseNum=child.increase_num
            child.examBaseScore=child.exam_base_score
            this.ruleForm = Object.assign({}, child)
            this.$refs.dialogForm.$refs['ruleForm'].resetFields()
          } catch (e) {
          }
        })
      },
      initRuleForm() {
        this.ruleForm = {
          content:'',
          category:'',
          type:'',
          note:'',
          baseScore:'',
          increaseNum:'',
          deduction:'',
          examBaseScore:'',
          sort:'1'
        }
      },
      getAllList() {
        let searchParam={
          page : this.currentPage,
          pageSize : this.pageSize
        }
        questionApi.getIndex(searchParam).then(res => {
          this.questionList = res.data.records
          this.count = res.data.total
          this.tableLoading = false
        }).catch(err => {
          this.questionList = []
          this.tableLoading = false
        })
      },
      handleCurrentChange(val) {
        this.currentPage = val
        this.tableLoading = true
        this.$refs.table.bodyWrapper.scrollTop = 0;
        this.getAllList()
      },
      submit(type) {
        if (type == 1) {
          this.activeName='1'
          this.currentPage = 1
          this.tableLoading = true
          this.$refs.table.bodyWrapper.scrollTop = 0;
          this.getAllList()
        } else {
          this.tableLoading = true
          this.$refs.table.bodyWrapper.scrollTop = 0;
          this.getAllList()
          this.addDialogVisible = false
        }
      },
      onBack() {
        this.initRuleForm()
        this.addDialogVisible = false
      }
    }
  }
</script>

<style lang="less">
  .question-container {
    width: 100%;

    .el-input.is-disabled .el-input__inner {
      background-color: #fff !important;
      color: #333 !important;
    }
  }
</style>
