<template>
  <div class="app-container">
    <!--查询结果Table显示-->
    <el-table v-loading="listLoading" :data="list" class="table" @selection-change="handleSelectionChange">
      <el-table-column label="名称" width="400" align="left">
        <template scope="scope">
          <span>{{ scope.row['name'] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务地址" align="left">
        <template scope="scope">
          <span v-if="!scope.row.isSave">{{ scope.row['url'] }}</span>
          <el-input v-else v-model.trim="scope.row.url" type="text" />
        </template>
      </el-table-column>
      <el-table-column label="编辑" width="250" align="center">
        <template scope="scope">
          <el-button v-if="scope.row.isSave" type="primary" plain size="medium" @click="edit(scope.row)">保存</el-button>
          <el-button v-else type="primary" plain size="medium" @click="edit(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <div class="pagination-container">
      <el-pagination
        v-show="total>listQuery.limit"
        :current-page="listQuery.offset"
        :page-sizes="[15,20,30]"
        :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 { getServerList, serverEdit } from '@/api/baseSource/server'
export default {
  name: 'ListServer',
  data() {
    return {
      deptShow: true,
      fencetypelist: [],
      listQuery: {
        offset: 1,
        limit: 15
      }, // 查询条件
      columns: [
        {
          text: '名称',
          value: 'name',
          align: 'left',
          width: 400
        },
        {
          text: '服务地址',
          value: 'url',
          align: 'left'
        }
      ], // 动态加载的表头
      list: [], // 列表数据
      total: 0, // 数据总数
      listLoading: false, // 加载动画
      fullscreenLoading: false // 全屏加载动画
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    edit(row) {
      if (row.isSave) {
        this.$confirm('该操作可能造成系统运行异常，请谨慎', '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'info'
        }).then(() => { // 提交
          serverEdit(row).then(response => {
            this.fetchData()
          })
        }).catch(() => {
          this.fetchData()
        })
      } else {
        row.isSave = true
      }
    },
    search() {
      this.fetchData(false)
    },
    fetchData(isNowPage = true) {
      console.log('fetchData')
      // var list = [{ id: '1', name: 'GIS地图', url: '192.168.0.202:8080' }, { id: '2', name: '二维地图URL', url: '/arcgis/rest/services/dt/MapServer' }]
      // for (var i = 0; i < list.length; i++) {
      //   list[i].isSave = false
      // }
      // this.list = list
      this.listLoading = true
      if (!isNowPage) {
        this.listQuery.offset = 1
      }
      getServerList(this.listQuery).then(response => {
        var list = response.data.rows
        for (var i = 0; i < list.length; i++) {
          list[i].isSave = false
        }
        this.list = list
        this.total = parseInt(response.data.total)
        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>
  .el-table__header tr,
  .el-table__header th {
    padding: 0;
    height: 40px;
    background-color: #f8f8f8;
  }
  .table{
    margin-bottom: 20px;
  }
  .pagination-container{
    margin-bottom: 100px;
  }
</style>
