<!--员工管理-->
<template>
  <div class="app-container">
    <!--查询结果Table显示-->
    <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('/strategy/add')" class="edit_btn" size="small" @click="add">新增</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="listLoading" :data="list" class="table" border>
        <el-table-column :index="indexMethod" align="center" type="index" />
        <el-table-column v-for="column in columns" :key="column.value" :label="column.text" :width="column.width" :align="column.align" show-overflow-tooltip>
          <template slot-scope="scope">
            <span :class="column.class">{{ scope.row[column.value] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="160">
          <template slot-scope="scope">
            <el-button v-if="hasPerm('/strategy/update')" type="text" @click="edit(scope.row)">编辑</el-button>
            <el-button v-if="hasPerm('/strategy/delete')" type="text" @click="deleteStrategy(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页-->
    <div class="pagination-container">
      <el-pagination
        v-show="total>listQuery.limit"
        :current-page="listQuery.offset"
        :page-sizes="[20,30,50]"
        :page-size="listQuery.limit"
        :total="total"
        align="center"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>
    <edit-strategy ref="editstrategy" @watchChild="fetchData"/>
  </div>
</template>

<script>
import { getStrategyList, delStrategy } from '@/api/strategy'
import EditStrategy from './editStrategy'

export default {
  name: 'ListStrategy',
  components: { EditStrategy },
  data() {
    return {
      listQuery: {
        offset: 1,
        limit: 20,
        sort: '',
        order: ''
      }, // 筛选条件
      columns: [
        {
          text: '策略名称',
          value: 'name',
          align: 'center'
        },
        {
          text: '策略类型名称',
          value: 'typeName',
          align: 'center'
        },
        {
          text: '描述',
          value: 'description',
          align: 'center'
        }
      ], // 显示列
      multipleSelection: [], // 多选选中项
      list: [], // 列表数据
      total: 0, // 数据总数
      listLoading: true, // 加载动画
      editShow: false // 是否显示编辑框
    }
  },
  mounted() {
    this.fetchData()// 获取数据
  },
  methods: {
    // 新增策略
    add() {
      // this.editShow = true
      this.$refs.editstrategy.initDialog('create')
    },
    // 编辑策略信息
    edit(row) {
      // this.editShow = true
      this.$refs.editstrategy.initDialog('update', row)
    },
    // 删除策略
    deleteStrategy(row) {
      this.$confirm(
        '删除后将影响授权，确定要删除该策略吗？',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delStrategy(row.id).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          }
        })
      })
    },
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 获取集中器数据
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      // TODO: 调取策略列表接口
      getStrategyList(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = parseInt(response.data.total)
        this.listLoading = false
      })
      // 假数据
      // this.list = [
      //   { name: '永久', description: '' },
      //   { name: '当天', description: '' },
      //   { name: '工作日', description: '' }
      // ]
      // this.total = 3
      // this.listLoading = false
      // 假数据结束
    },
    indexMethod(index) {
      return this.listQuery.limit * (this.listQuery.offset - 1) + index + 1
    },
    // 改变页容量
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.fetchData()
    },
    // 改变当前页
    handleCurrentChange(val) {
      this.listQuery.offset = val
      this.fetchData()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-container{
    margin-bottom:20px
  }
  $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
    }
  }
</style>
