<!--阶段时限-->
<template>
  <app-container>
    <normal-table :data="stateLimitList" :total="total" :query="listQuery" :list-loading="listLoading" @change="changePage" @selection-change="handleSelectionChange">
      <template slot="btns">
        <el-button v-if="hasPerm('/timeLimit/delete')" class="edit_btn" size="small" @click="del">删除</el-button>
        <el-button v-if="hasPerm('/timeLimit/add')" class="edit_btn" size="small" @click="add">添加</el-button>
      </template>
      <template slot="preColumns">
        <el-table-column align="center" type="selection" width="50"/>
      </template>
      <template slot="columns">
        <el-table-column align="center" prop="description" label="案卷状态"/>
        <el-table-column align="center" prop="attribute" label="限制时间(未设置默认120分钟)"/>
        <el-table-column label="操作" align="center" width="240">
          <template slot-scope="scope">
            <el-button class="table-button" type="text" size="small" @click="showDetail(scope.row)">查看</el-button>
            <el-button class="table-button" type="text" size="small" @click="edit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </template>
    </normal-table>
    <detail-timeLimit ref="detailTimeLimit"/>
    <edit-timeLimit ref="editTimeLimit" @watchChild="fetchData"/>
  </app-container>
</template>

<script>
import { getStateLimitList, deleteStateLimit } from '@/api/busAdmin/timeLimit'
import detailTimeLimit from '@/views/busAdmin/TimeLimit/detailTimeLimit'
import editTimeLimit from '@/views/busAdmin/TimeLimit/editTimeLimit'

export default {
  name: 'ListTimeLimit',
  components: { detailTimeLimit, editTimeLimit },
  data() {
    return {
      listLoading: false,
      stateLimitList: [],
      listQuery: {
        offset: 1,
        limit: 10,
        sort: 'id',
        order: 'asc'
      },
      total: 0,
      multipleSelection: []
      // detailShow: false,
      // editShow: false
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    indexMethod(index) {
      return this.listQuery.limit * (this.listQuery.offset - 1) + index + 1
    },
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      getStateLimitList(this.listQuery).then(response => {
        this.stateLimitList = response.data.rows
        this.total = parseInt(response.data.total)
        this.listLoading = false
      })
    },
    // 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
    changePage(val) {
      if (val && val.size) {
        this.listQuery.limit = val.size
      }
      if (val && val.page) {
        this.listQuery.offset = val.page
      }
      this.fetchData()
    },
    // 多选触发方法
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 检查选择情况
    checkSelection() {
      if (this.multipleSelection.length === 0) {
        return false
      } else {
        return true
      }
    },
    add() {
      this.$refs.editTimeLimit.initDialog(true, 'create')
    },
    del() {
      if (this.checkSelection()) {
        const stateLimitIds = []
        this.multipleSelection.forEach(function(value, index) {
          stateLimitIds.push(value.id)
        })
        this.$confirm(
          '确定要删除所选类别吗？',
          '确认操作',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).then(() => {
          console.log(stateLimitIds.join(','))
          deleteStateLimit(stateLimitIds.join(',')).then(response => {
            if (response.code === 200) {
              this.$message.success('删除成功')
              this.fetchData()
            }
          })
        })
      } else {
        this.$message.error('至少选中一项')
      }
    },
    showDetail(row) {
      debugger
      this.$refs.detailTimeLimit.initDialog(true, row)
    },
    edit(row) {
      if (this.hasPerm('/timeLimit/update')) {
        event.stopPropagation()
        this.$refs.editTimeLimit.initDialog(true, 'update', row)
      } else {
        event.stopPropagation()
        this.$message.warning('没有权限')
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .table-container{
    border: 0px !important;
  }
</style>
