<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="门店">
          <lazy-tree
            ref="addTree"
            input-width="400px"
            style="margin-bottom: 0;display:flex;width:100%"
            @handleSelectGroup="selectGroup"
          />
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="searchForm.is_all_level" :true-label="1" :false-label="0">包含所有门店</el-checkbox>
        </el-form-item>
        <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"
      :key="JSON.stringify(fieldDefsData)"
      border
      style="width: 100%"
      @row-dblclick="handleUpdate"
      @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-if="item.prop === 'contacts'">
            {{ scope.row.contacts }} {{ '/' + scope.row.phone }}
          </div>
          <div v-else>{{ scope.row[item.prop] }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button type="text" size="mini" @click="showDetail(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 LazyTree from '@/components/LazyTree'
import tableFields from '@/common/components/TableFieldCfg/index.mixin.js'
export default {
  name: 'ChargingStation',
  components: {
    TableFieldCfg,
    LazyTree
  },
  mixins: [tableFields],
  data() {
    return {
      loading: false,
      records: 0,
      searchForm: {
        is_all_level: 1,
        page: 1,
        limit: 10
      },
      tableData: [],
      fieldDefs: [
        { label: '充电站名称', prop: 'group_name', align: 'center', minWidth: 150 },
        { label: '所在城市', prop: 'cityname', align: 'center' },
        { label: '充电站地址', prop: 'address', align: 'center', minWidth: 200 },
        { label: '订单量', prop: 'order_count', align: 'center' },
        { label: '联系人', prop: 'contacts', align: 'center' },
        { label: '状态', prop: 'status', align: 'center' }
      ]
    }
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      if (to.meta.refresh) {
        vm.queryList()
      }
    })
  },
  created() {
    const { refresh } = this.$route.meta
    !refresh && this.queryList()
  },
  methods: {
    selectGroup(group) {
      this.searchForm.admin_id = group.shop_id
    },
    handleAdd() {
      this.$router.push({ path: '/add-chargingStation' })
    },
    handleUpdate(row) {
      this.$router.push({ path: `/edit-chargingStation?id=${row.group_id}` })
    },
    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
        }
      }).catch(() => {
        this.loading = false
      })
    },
    showDetail(row) {
      this.$router.push(`/chargingStationDetail?id=${row.group_id}`)
    }
  }
}
</script>
