<template>
  <div class="content-container">
    <el-row class="tool-bar">
      <el-col :span="12" class="left">
        <div class="content-title">
          {{ title }}
        </div>
      </el-col>
      <el-col :span="12" class="right">
        <el-input v-model="conditions.keywords" class="search-input m-right-15" placeholder="请输入内容">
          <el-button slot="append" icon="el-icon-search" @click="getData()" />
        </el-input>
        <el-button type="primary" @click="handleAdd">添加</el-button>
      </el-col>
    </el-row>

    <el-row class="m-top-15">
      <el-table v-loading="listLoading" class="page-table" border :data="dataList" height="calc(100vh - 180px)">
        <el-table-column type="index" label="序号" align="center" header-align="center" width="80" />
        <el-table-column label="地图名称" align="center" header-align="center" width="200">
          <template v-slot="{row}">
            <span>{{ row.mapTitle }}</span>
          </template>
        </el-table-column>

        <el-table-column label="说明" align="left" header-align="left">
          <template v-slot="{row}">
            <span>{{ row.mapDesc }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
          <template v-slot="{row}">
            <el-button size="mini" type="primary" @click="handleEdit(row)">修改</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <ent-map ref="AddMap" title="新增地图" @formSuccess="getData" />
    <ent-map ref="EditMap" title="编辑地图" @formSuccess="getData" />
  </div>
</template>

<script>
import EntMap from './components/Map'
import { getMapByPage, deleteMapById } from '@/api/system/mapApi'

export default {
  name: 'Index',
  components: {
    EntMap
  },

  data() {
    return {
      title: '地图配置',
      dataList: [],
      listLoading: false,
      total: 0,
      conditions: {
        page: 1,
        limit: 10,
        keywords: ''
      }
    }
  },

  mounted() {
    this.getData()
  },

  methods: {
    // Fetch Data
    getData() {
      this.listLoading = true
      getMapByPage(this.conditions).then((resp) => {
        const { data, total } = resp
        this.listLoading = false
        this.total = total
        this.dataList = data
      }).catch((error) => {
        console.log(error)
      })
    },

    // 查询
    handleFilter() {
      this.conditions.page = 1
      this.getData()
    },

    // 添加
    handleAdd() {
      this.$refs['AddMap'].showAddModel()
    },

    // 修改
    handleEdit(data) {
      const { mapId } = data
      this.$refs['EditMap'].showEditModel(mapId)
    },

    // 删除
    handleDelete(data) {
      const { mapId } = data

      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteMapById(mapId).then((resp) => {
          const { msg } = resp
          this.getData()
          this.$message({
            type: 'success',
            message: msg
          })
        }).catch((error) => {
          console.log(error)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
