<!-- 路灯类型列表 -->
<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="4">
              <el-form-item class="selectForm-container-item">
                <el-input v-model.trim="listQuery.typeName" placeholder="类型名称" clearable />
              </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 v-if="hasPerm('/lampType/delete')" class="edit_btn" size="small" @click="del">删除</el-button>
          <el-button class="edit_btn" size="small" @click="add">新增</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="listLoading" :data="list" class="table" border @selection-change="handleSelectionChange">
        <!--拥有删除权限则显示复选框-->
        <el-table-column v-if="hasPerm('/lampType/delete')" align="center" type="selection" width="55"/>
        <!--序号列-->
        <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">
            <div v-if="column.value === 'offlineAlarm'">
              <span v-html="alarmFormatter(scope.row.offlineAlarm, column.value)"/>
            </div>
            <div v-else-if="column.value === 'electricAlarm'">
              <span v-html="alarmFormatter(scope.row.electricAlarm, column.value)"/>
            </div>
            <div v-else-if="column.value === 'voltageAlarm'">
              <span v-html="alarmFormatter(scope.row.voltageAlarm, column.value)"/>
            </div>
            <div v-else>
              <span :class="column.class">{{ scope.row[column.value] }}</span>
            </div>
          </template>
        </el-table-column>
        <!--操作列-->
        <el-table-column label="操作" align="center" width="160">
          <template slot-scope="scope">
            <el-button v-if="hasPerm('/lampType/edit')" type="text" @click="edit(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-lamp-type ref="editLampType" @watchChild="fetchData"/>
  </div>
</template>

<script>
import editLampType from '@/views/base/lampType/editLampType'
import { getLampType, delLampType } from '@/api/system/base'

export default {
  name: 'LampTypeList',
  components: { editLampType },
  data() {
    return {
      listQuery: {
        typeName: '',
        offset: 1,
        limit: 20,
        sort: '',
        order: ''
      }, // 筛选条件
      columns: [
        {
          text: '类型名',
          value: 'typeName',
          align: 'center'
        },
        // {
        //   text: '是否开启离线报警',
        //   value: 'offlineAlarm',
        //   align: 'center'
        // },
        {
          text: '是否开启电流报警',
          value: 'electricAlarm',
          align: 'center'
        },
        {
          text: '是否开启电压报警',
          value: 'voltageAlarm',
          align: 'center'
        }
      ], // 显示列
      multipleSelection: [], // 多选选中项
      list: [], // 列表数据
      total: 0, // 数据总数
      listLoading: false, // 加载动画
      fullscreenLoading: false, // 全屏加载动画
      dialogFormVisible: false
    }
  },
  created() {
    this.fetchData()// 获取数据
  },
  methods: {
    // 检查选择情况
    checkSelection() {
      if (this.multipleSelection.length === 0) {
        return false
      } else {
        return true
      }
    },
    alarmFormatter(row, cellValue) {
      if (row === '0') return '不报警'
      else if (row === '1') return '报警'
    },
    // 新增路灯类型
    add() {
      this.dialogFormVisible = true
      this.$refs.editLampType.initDialog('create', this.dialogFormVisible)
    },
    // 编辑路灯类型
    edit(row) {
      this.dialogFormVisible = true
      this.$refs.editLampType.initDialog('update', this.dialogFormVisible, row)
    },
    // 删除路灯类型
    del() {
      if (this.checkSelection()) {
        const typeIds = []
        this.multipleSelection.forEach(function(value, index) {
          typeIds.push(value.id)
        })
        this.$confirm(
          '确定要删除所选路灯类型吗？',
          '确认操作',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).then(() => {
          delLampType(typeIds).then(response => {
            if (response.code === 200) {
              this.$message.success('删除成功')
              this.fetchData()
            }
          })
        })
      } else {
        this.$message.error('至少选中一项')
      }
    },
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 获取街道列表
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      getLampType(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
      })
    },
    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 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>
