<template>
  <div>
    <!--查询结果Table显示-->
    <div>
      <el-table v-loading="listLoading" :data="list" class="table" size="mini" border @selection-change="handleSelectionChange">
        <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>{{ scope.row[column.value] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="del(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="[5,10]"
        :page-size="listQuery.limit"
        :total="total"
        align="center"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>

<script>
import { getGroupWells, delWellFromGroup } from '@/api/group'

export default {
  name: 'GroupListWell',
  props: {
    id: {
      type: String,
      required: true
    }// 小组id
  },
  data() {
    return {
      listQuery: {
        offset: 1,
        limit: 10,
        sort: '',
        order: ''
      }, // 筛选条件
      columns: [
        {
          text: '井编号',
          value: 'wellCode',
          align: 'center'
        },
        {
          text: '位置',
          value: 'position',
          align: 'center'
        },
        {
          text: '井类型',
          value: 'wellTypeName',
          align: 'center'
        },
        {
          text: '所属派出所',
          value: 'deptName',
          align: 'center'
        }
      ], // 显示列
      list: [], // 列表数据
      total: 0, // 数据总数
      listLoading: true, // 加载动画
      fullscreenLoading: false // 全屏加载动画
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 获取组列表
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      const listQuery = { id: this.id, ...this.listQuery }
      getGroupWells(listQuery).then(response => {
        if (response.code === 200) {
          // response.data.rows = [
          //   { id: '1', wellCode: 'N57F141', position: '北京市通州区AA路AA号', wellTypeName: '雨水井', deptName: '永定路派出所' },
          //   { id: '1', wellCode: 'N57F141', position: '北京市通州区AA路AA号', wellTypeName: '雨水井', deptName: '永定路派出所' },
          //   { id: '1', wellCode: 'N57F141', position: '北京市通州区AA路AA号', wellTypeName: '雨水井', deptName: '永定路派出所' },
          //   { id: '1', wellCode: 'N57F141', position: '北京市通州区AA路AA号', wellTypeName: '雨水井', deptName: '永定路派出所' },
          //   { id: '1', wellCode: 'N57F141', position: '北京市通州区AA路AA号', wellTypeName: '雨水井', deptName: '永定路派出所' },
          //   { id: '1', wellCode: 'N57F141', position: '北京市通州区AA路AA号', wellTypeName: '雨水井', deptName: '永定路派出所' }
          // ]
          if (response.data.rows.length === 0 && this.listQuery.offset > 1) {
            this.listQuery.offset -= 1
            this.fetchData()
          } else {
            this.list = response.data.rows
            this.total = parseInt(response.data.total)
            this.listLoading = false
          }
        }
      })
    },
    // 删除井
    del(row) {
      this.$confirm(
        '确定要从该组中移除该井吗？',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        const params = {
          id: this.id,
          wellId: row.id
        }
        delWellFromGroup(params).then(response => {
          if (response.code === 200) {
            this.$message.success('移除成功')
            this.fetchData()
          }
        })
      })
    },
    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()
    },
    // 多选触发方法
    handleSelectionChange(val) {
      this.multipleSelection = val
    }
  }
}
</script>

<style scoped>
  .table{
    margin-bottom: 20px;
  }
  .pagination-container{
    margin-bottom: 10px;
  }
</style>
