<template>
  <app-container>
    <normal-table :data="list" :total="total" :columns="tableColumns" :query="listQuery" :list-loading="listLoading" @change="changePage" @selection-change="handleSelectionChange">
      <template slot="btns">
        <el-button v-if="hasPerm('/areaPersonliable/add')" class="edit_btn" size="small" @click="add">新增</el-button>
      </template>
      <template slot="columns">
        <el-table-column v-if="hasPerm('/areaPersonliable/update')||hasPerm('/areaPersonliable/delete')" label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button v-if="hasPerm('/areaPersonliable/update')" size="small" class="table-button" type="text" @click="edit(scope.row)">编辑</el-button>
            <el-button v-if="hasPerm('/areaPersonliable/delete')" size="small" class="table-button" type="text" @click="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </template>
    </normal-table>
    <edit-area-personliable ref="editAreaPersonliable" @watchChild="fetchData"/>
  </app-container>
</template>

<script>
import { getAreaPersonliableList, deleteAreaPersonliable } from '@/api/busAdmin/areaPersonliable'
import EditAreaPersonliable from './editAreaPersonliable'

export default {
  name: 'AreaPersonliable',
  components: { EditAreaPersonliable },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: false,
      listQuery: {
        limit: 10,
        offset: 1,
        sort: 'id',
        order: 'asc'
      },
      tableColumns: [
        {
          text: '网格名称',
          value: 'areaName'
        },
        {
          text: '责任人姓名',
          value: 'userName'
        },
        {
          text: '所属区域',
          value: 'pAreaName'
        }
      ]
    }
  },
  created() {
    this.fetchData()
  },
  activated() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getAreaPersonliableList(this.listQuery).then(res => {
        if (res.code === 200) {
          this.list = res.data.rows
          this.total = parseInt(res.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()
    },
    add() {
      this.$refs.editAreaPersonliable.initDialog(true, 'create')
    },
    edit(row) {
      this.$refs.editAreaPersonliable.initDialog(true, 'update', row)
    },
    del(row) {
      this.$confirm(
        '确定要删除吗？',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        deleteAreaPersonliable(row.id).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          }
        })
      })
    }
  }
}
</script>

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