<!--区域列表-->
<template>
  <div class="app-container">
    <!--筛选条件-->
    <div class="search-div">
      <div class="search-left">
        <el-form ref="selectForm" :inline="true" :model="listQuery" class="form-container">
          <el-row>
            <el-col :span="5">
              <el-form-item class="selectForm-container-item">
                <el-input v-model.trim="listQuery.areaName" placeholder="防区名称" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item class="selectForm-container-item">
                <el-select v-model="listQuery.areaStatus" placeholder="请选择防卫状态" clearable>
                  <el-option v-for="item in areaStatusList" :key="item.value" :label="item.name" :value="item.value"/>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="6" class="search-right">
              <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
    <!--查询结果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 class="edit_btn" size="small" @click="batchExport" :disabled="list.length===0">导出</el-button>-->
        </el-col>
      </el-row>
      <el-table v-loading="listLoading" :data="list" class="table" row-class-name="small-row-class" border>
        <!--序号列-->
        <el-table-column :index="indexMethod" align="center" type="index" label="#" width="55"/>
        <!--内容列-->
        <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 type="text" @click="detail(scope.row)">详情</el-button>
            <el-button v-show="scope.row.status === 0" type="success" plain size="mini" @click="enableDefence(scope.row)">布防</el-button>
            <el-button v-show="scope.row.status === 1" type="warning" plain size="mini" @click="disableDefence(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>

    <defence-detail ref="detail" />
  </div>
</template>

<script>
import { getRegion, updateDefence, getAreaTypeBySubSystem } from '@/api/area'
import { getAreaStatus, getAreaType } from '@/api/allDict'
import DefenceDetail from '@/views/area/defenceDetail'

export default {
  name: 'AreaList',
  components: { DefenceDetail },
  data() {
    return {
      listQuery: {
        areaName: '',
        areaStatus: '',
        areaType: '',
        offset: 1,
        limit: 20,
        sort: '',
        order: ''
      }, // 筛选条件
      columns: [
        {
          text: '区域名称',
          value: 'name',
          align: 'center'
        },
        {
          text: '区域类别',
          value: 'areaTypeName',
          align: 'center'
        },
        {
          text: '防卫状态',
          value: 'areaStatusName',
          align: 'center'
        }
      ], // 显示列
      list: [], // 列表数据
      total: 0, // 数据总数
      areaStatusList: [],
      areaTypeList: [],
      listLoading: false, // 加载动画
      fullscreenLoading: false, // 全屏加载动画
      dialogFormVisible: false
    }
  },
  created() {
    this.initAreaType()
    this.fetchAreaStatusData()
  },
  activated() {
    this.fetchData() // 获取数据
  },
  methods: {
    initAreaType() {
      this.listQuery.areaType = getAreaTypeBySubSystem(this.$store.getters.currentSystem.code)
    },
    // 编辑设备信息
    detail(row) {
      this.dialogFormVisible = true
      this.$refs.detail.initDialog('detail', this.dialogFormVisible, row)
    },
    // 查询数据
    search() {
      this.fetchData(false)
    },
    fetchAreaStatusData() {
      getAreaStatus().then(response => {
        this.areaStatusList = response.data
      })

      getAreaType().then(response => {
        this.areaTypeList = response.data
      })
    },
    // 获取列表
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      getRegion(this.listQuery).then(response => {
        if (response.code === 200) {
          this.list = response.data.rows
          this.total = parseInt(response.data.total)
        } else {
          this.$message.error(response.message)
        }
        this.listLoading = false
      })
    },
    enableDefence(row) {
      const params = {
        id: row.id,
        status: '1'
      }
      updateDefence(params).then(response => {
        if (response.code === 200) {
          this.$message.success('布防操作已成功')
          this.fetchData(true)
        } else {
          this.$message.error('布防操作失败')
        }
      })
    },
    disableDefence(row) {
      const params = {
        id: row.id,
        status: '0'
      }
      this.$confirm(
        '确定要撤防 ' + row.name + ' 吗？',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        updateDefence(params).then(response => {
          if (response.code === 200) {
            this.$message.success('撤防操作已成功')
            this.fetchData(true)
          } else {
            this.$message.error('撤防操作失败')
          }
        })
      })
    },
    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>
  $tableTitleHeight:46px;
  .app-container{
    margin-bottom:20px
  }
  .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>
