<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :inline="true" :model="searchForm" size="mini" class="demo-form-inline">
        <el-form-item label="充电站名称">
          <el-input v-model="searchForm.name" placeholder="充电站名称" clearable style="width: 160px;" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" clearable placeholder="状态" size="mini" style="width: 150px;">
            <el-option label="启用" value="ENABLED" />
            <el-option label="禁用" value="DISABLED" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleCurrentChange()">查询</el-button>
          <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-tooltip content="表格字段设置" effect="dark">
            <img
              class="table-cfg-icon"
              src="https://qiniu.bms16.com/menu_header_config.png"
              @click="loadFieldDialog"
            >
          </el-tooltip>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      v-loading="loading"
      ref="multipleTable"
      :data="tableData"
      border
      style="width: 100%"
      @header-dragend="onHeaderDragend"
    >
      <el-table-column v-for="(item, index) in fieldDefsData" :key="index" v-bind="item" >
        <template slot-scope="scope">
          <div v-if="item.prop === 'status'">
            <el-tag v-if="scope.row.status === 'ENABLED'" type="success">启用</el-tag>
            <el-tag v-if="scope.row.status === 'DISABLED'" type="danger">禁用</el-tag>
          </div>
          <div v-else>{{ scope.row[item.prop] }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="showDetail(scope.row)">详情</el-button>
          <el-button type="text" size="mini" @click="handleUpdate(scope.row)">修改</el-button>
        </template>
      </el-table-column>

    </el-table>
    <el-pagination
      :current-page="searchForm.page"
      :page-size="searchForm.limit"
      :total="records"
      style="margin-top: 10px;"
      background
      layout="total, sizes, prev, pager, next, jumper"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <TableFieldCfg
      ref="TableFieldCfg"
      :default-column-defs="fieldDefs"
      @success="getTableListFieldDefs"
    />
  </div>
</template>

<script>
import { apiQueryChargerGroupList } from '@/api/deviceCfg'
import TableFieldCfg from '@/common/components/TableFieldCfg'
import tableFields from '@/common/components/TableFieldCfg/index.mixin.js'
export default {
  components: {
    TableFieldCfg
  },
  mixins: [tableFields],
  data() {
    return {
      loading: false,
      records: 0,
      searchForm: {},
      tableData: [],
      fieldDefs: [
        { label: '充电站名称', prop: 'group_name', align: 'center' },
        { label: '所在城市', prop: 'cityname', align: 'center' },
        { label: '充电站地址', prop: 'address', align: 'center' },
        { label: '订单量', prop: 'order_count', align: 'center' },
        { label: '联系人', prop: 'contacts', align: 'center' },
        { label: '状态', prop: 'status', align: 'center' }
      ]
    }
  },
  created() {
    this.queryList()
  },
  methods: {
    handleAdd() {
      this.$router.push({ path: '/edit-chargingStation' })
    },
    handleUpdate(row) {
      const data = JSON.stringify(row)
      this.$router.push({ path: `/edit-chargingStation?data=${data}` })
    },
    handleCurrentChange(val = 1) {
      this.searchForm.page = val
      this.queryList()
    },
    handleSizeChange(val) {
      this.searchForm.limit = val
      this.queryList()
    },
    queryList() {
      this.loading = true
      apiQueryChargerGroupList(this.searchForm).then(res => {
        const { data, code } = res.data
        this.loading = false
        if (code === 200) {
          this.tableData = data.list
          this.records = +data.total
        }
      })
    },
    showDetail(row) {
      this.$router.push(`/chargingStationDetail?id=${row.group_id}`)
    }
  }
}
</script>
