<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form
        ref="filter-form"
        :model="filter"
        v-bind:inline="true"
        label-position="left"
        label-width="70px"
        style="width: 100%;">
        <div class="filter-view">
          <div class="flex1" style="min-width: 200px; max-width: 350px;">
            <el-input
              v-model="filter.criteria"
              placeholder="Search by Site Name or Address"
              @keyup.enter.native="handleFilter" />
          </div>
          <div>
            <el-button
              v-waves
              type="primary"
              icon="el-icon-search"
              @click="handleFilter">
              Search
            </el-button>
          </div>
          <div class="filter-flex-button">
            <el-button
              v-if="visible.addButton"
              type="primary"
              icon="el-icon-plus"
              @click="handleClickAddSiteButton">
                Add Site
            </el-button>
          </div>
        </div>
      </el-form>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      style="width: 100%;">
      <!--el-table-column
        label="Site ID"
        prop="sitePk"
        align="center"
        width="80">
          <template slot-scope="{row}">
            <span>{{ row.sitePk }}</span>
          </template>
      </el-table-column-->
      <el-table-column
        label="Site Name"
        prop="siteName"
        align="center"
        min-width="260">
          <template slot-scope="{row}">
            <span>{{ row.siteName }}</span>
          </template>
      </el-table-column>
      <el-table-column
        label="Address"
        prop="siteAddress"
        align="center"
        min-width="280">
        <template slot-scope="{row}">
          <span>{{ row.siteAddress }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="Service Provider"
        prop="siteName"
        align="center"
        min-width="180">
        <template slot-scope="{row}">
          <div
            v-for="item in row.serviceProviders"
            :key="item">
            {{item}}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="Rates"
        prop="siteName"
        align="center"
        min-width="200">
        <template slot-scope="{row}" v-if="row.dynamicRates">
          <div
            v-for="(item,index) in row.dynamicRates"
            :key="index">
            {{item}}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="Total Stations"
        prop="stationNo"
        align="center"
        min-width="100">
          <template slot="header" slot-scope="scope">
            <div>Stations</div>
            <div style="font-size: 0.8em; margin-top: -5px;">(In Use/Total)</div>
          </template>
          <template slot-scope="{row}">
            <span>{{ row.stationInUse || "0" }}/{{ row.stationNo || "0" }}</span>
          </template>
      </el-table-column>
      <!--el-table-column
        label="Stations in Use"
        prop="stationNo"
        align="center">
          <template slot-scope="{row}">
            <span>{{ row.stationInUse || "0" }}</span>
          </template>
      </el-table-column-->
      <el-table-column
        label="Total Connectors"
        prop="connectorNo"
        align="center"
        min-width="100"
        class-name="fixed-width">
          <template slot="header" slot-scope="scope">
            <div>Connectors</div>
            <div style="font-size: 0.8em; margin-top: -5px;">(In Use/Total)</div>
          </template>
          <template slot-scope="{row}">
            <span>{{ row.connectorInUse || "0" }}/{{ row.connectorNo || "0"}}</span>
          </template>
      </el-table-column>
      <!--el-table-column
        label="Connectors in Use"
        prop="connectorNo"
        align="center"
        class-name="fixed-width">
          <template slot-scope="{row}">
            <span>{{ row.connectorInUse || "0" }}</span>
          </template>
      </el-table-column-->
      <el-table-column
        v-if="visible.actions"
        label="Action"
        align="center"
        min-width="140">
          <template slot-scope="{row, $index}">
            <TableAction
              :showDel="visible.actionDelete"
              @edit="handleUpdateSite(row, $index)"
              @delete="handleDeleteSite(row, $index)"/>
          </template>
      </el-table-column>
    </el-table>
    <div class="right">
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="handlePageChange" />
    </div>
  </div>
</template>

<script>

// waves directive
import waves from '@/directive/waves'
// secondary package based on el-pagination
import Pagination from '@/components/Pagination'
import TableAction from '@/components/TableAction.vue'

import site from '../../http/api/site'
import { getRoleName } from '@/utils/auth'

const {
  deleteSite,
  getSiteList
} = site;

export default {
  name: 'SiteManagement',
  components: { Pagination, TableAction },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      filter: {
        criteria: '',
      },
      listQuery: {
        page: 1,
        limit: 10,
      },
      visible: {
        actions: true,
        actionDelete: true,
        addButton: true,
      },
    }
  },
  created() {
    this.getList()
    this.setVisible()
  },
  methods: {
    setVisible() {
      const role = getRoleName()
      if (role === "MCST") {
        this.visible.actionDelete = false
        this.visible.addButton = false
      } else if (this.$route.meta.onlyView) {
        this.visible.actions = false
        this.visible.addButton = false
      }
    },
    getList() {
      this.listLoading = true
      getSiteList({
        pageSize: this.listQuery.limit,
        pageNo: this.listQuery.page,
        pageVo: {
          criteria: this.filter.criteria,
        }
      }).then((response) => {
        this.list = response.data
        this.total = response.total
      }).catch(err => {
        this.$message({
          message: err,
          type: 'error'
        })
      }).finally(() => {
        this.listLoading = false
      })
    },
    handlePageChange(value) {
      const {limit, page} = value;
      this.listQuery.limit = limit;
      this.listQuery.page = page;
      this.getList();
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleClickAddSiteButton() {
      //this.$router.push({ path: '/site-management/add-site' })
      this.$router.push({ path: '/site-management/add' })
    },
    handleUpdateSite(row, index) {
      /*this.$store.commit('site/SET_SELECTED_SITE', row)
      this.$router.push({ path: '/site-management/update-site' })*/
      this.$router.push({ path: '/site-management/edit/' + row.sitePk })
    },
    handleDeleteSite(row, index) {
      this.$confirm(
        'Are you sure you want to delete this site ?',
        'Delete',
        {
          confirmButtonText: 'OK',
          cancelButtonText: 'Cancel',
          type: 'warning'
        },
      ).then(res => {
        deleteSite({ sitePk: row.sitePk }).then(() => {
          this.$message({
            message: `delete site success ${row.siteName}`,
            type: "success",
            duration: 3000,
          })
          this.list.splice(index, 1)
        }).catch(error => {
          this.$message({
            message: error,
            type: "error",
            duration: 3000,
          })
        })
      })
    },
  }
}
</script>

