<template>
  <div class="app-container">
    <div>
      <el-row class="table-title">
        <el-col :span="6"><div class="title-header"><i class="el-icon-menu"/>数据列表</div></el-col>
        <el-col :span="12" :offset="6" class="edit_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>
          <!-- <el-button v-if="hasPerm('/marker/delete')" class="edit_btn" size="small" @click="del">删除</el-button> -->
        </el-col>
      </el-row>
      <el-table v-loading="listLoading" :data="stateLimitList" class="table" border @selection-change="handleSelectionChange" @row-click="showDetail">
        <el-table-column align="center" type="selection" width="50"/>
        <el-table-column :index="indexMethod" align="center" type="index" />
        <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" @click="edit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        :current-page="listQuery.offset"
        :page-sizes="[5,10,20,30,50]"
        :page-size="listQuery.limit"
        :total="total"
        align="center"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>

    <detail-timeLimit ref="detailTimeLimit"/>
    <edit-timeLimit ref="editTimeLimit" @watchChild="fetchData"/>
  </div>
</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
      })
    },
    // 改变页容量
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.fetchData()
    },
    // 改变当前页
    handleCurrentChange(val) {
      this.listQuery.offset = val
      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) {
      // this.detailShow = true
      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>
  $tableTitleHeight:46px;
  .table{
    margin-bottom: 20px;
  }
  .pagination-container{
    margin-bottom: 50px;
  }
  .table-title{
    background-color:rgba(243, 243, 243, 1);
    height: $tableTitleHeight;
    .title-header{
      line-height:$tableTitleHeight;
      color: #606266;
      font-size: 15px;
      i{
        margin-left: 5px;
        margin-right: 5px;
      }
    }
  }
  .edit_btns{
    .edit_btn{
      float:right;
      margin:7px 3px;//为了需要居中显示margin-top和bottom要用$tableTitleHeight减去按钮原高度除以2
    }
  }
  .table-button{
    padding: 10px;
  }
</style>
