<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-view">
        <el-input
          class="filter-input"
          v-model="filters.pageVo.criteria"
          placeholder="Search by Label Name"
          prefix-icon="el-icon-search"
          clearable/>
        <div>
          <el-button
            @click="onClickSearch"
            icon="el-icon-search"
            type="primary">
            Search
          </el-button>
        </div>
        <div
          class="filter-flex-button"
          v-if="!$route.meta.onlyView">
          <el-button
            icon="el-icon-plus"
            type="primary"
            @click="onClickAdd">
            Add Label
          </el-button>
        </div>
      </div>
    </div>
    <el-table
      v-loading="loading"
      :data="table.list">
      <el-table-column
        align="center"
        label="Label ID"
        prop="userPk"
        min-width="120">
        <template slot-scope="{row}" >
          <span
            class="link-type"
            @click="onClickEdit(row)"
            v-if="!$route.meta.onlyView">
            {{ row.siteLabelId }}
          </span>
          <span v-else>{{ row.siteLabelId }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="Label Name"
        prop="siteLabelName"
        min-width="120">
        <template slot-scope="{row}">
          <div class="site-label-name" :style='"background-color:" + row.siteLabelBackground + ";"'>{{row.siteLabelName}}</div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="No. of Sites"
        prop="assignedSiteCount"
        min-width="120"/>
      <el-table-column
        align="center"
        label="Status"
        prop="dataStatus"
        min-width="120"/>
      <el-table-column
        align="center"
        label="Update Time"
        prop="updateTime"
        min-width="120"/>
      <el-table-column
        align="center"
        label="Action"
        min-width="70"
        v-if="!$route.meta.onlyView">
        <template v-slot="{ row }">
          <el-dropdown
            class="action-dropdown"
            @command="(v) => handleCommand(v, row)"
            v-if="row.dataStatus != 'Inactive'">
            <i class="el-icon-more icon-action"></i>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="assignSites">
                Assign Sites
              </el-dropdown-item>
              <el-dropdown-item
                command="onClickEdit">
                Edit
              </el-dropdown-item>
              <el-dropdown-item
                command="onClickDelete">
                Delete
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <div class="right">
      <pagination
        v-show="table.total > 0"
        :total="table.total"
        :page.sync="filters.pageNo"
        :limit.sync="filters.pageSize"
        @pagination="getTableData" />
    </div>
    <DialogDetail
      v-bind="dialogAction"
      @hide="hideActionDialog"/>
    <DialogAssignment
      v-bind="dialogAssign"
      @hide="hideAssignDialog"/>
  </div>
</template>

<script>
import TableAction from '@/components/TableAction.vue'
import Pagination from '@/components/Pagination'
import DialogDetail from './detail.vue'
import DialogAssignment from './assignment.vue'
import api from '../../http/api/site.js'
export default {
  data() {
    return {
      loading: false,
      filters: {
        pageNo: 1,
        pageSize: 10,
        pageVo: {
          criteria: ""
        }
      },
      table: {
        list: [],
        total: 0
      },
      dialogAssign: {
        item: {},
        visible: false
      },
      dialogAction: {
        id: "",
        isEdit: false,
        visible: false
      }
    };
  },
  components: { Pagination, TableAction,DialogDetail,DialogAssignment },
  created() {
    this.onClickSearch()
  },
  methods: {
    onClickSearch() {
      this.filters.pageNo = 1
      this.getTableData()
    },
    getTableData() {
      this.loading = true;
      api.getLabelPages(this.filters).then(res => {
        if (res.data && res.total) {
          this.table.total = res.total;
          this.table.list = res.data;
        } else {
          this.table.total = 0;
          this.table.list = [];
        }
      }).catch(err => {
        this.$message({
          type: 'error',
          message: err
        })
        this.table.total = 0;
        this.table.list = [];
      }).finally(() => {
        this.loading = false;
      })
    },
    handleCommand(cb, item) {
      this[cb](item)
    },
    onClickAdd() {
      this.dialogAction.id = "";
      this.dialogAction.isEdit = false;
      this.dialogAction.visible = true;
    },
    onClickEdit(row) {
      this.dialogAction.id = row.siteLabelId;
      this.dialogAction.isEdit = true;
      this.dialogAction.visible = true;
    },
    onClickDelete(row) {
      this.$confirm('Are you sure you want to delete this label?', 'Delete', {
        confirmButtonText: 'OK',
        cancelButtonText: 'Cancel',
        type: 'warning'
      }).then(res => {
        this.deleteSiteLabel(row.siteLabelId);
      })
    },
    deleteSiteLabel(id) {
      this.loading = true;
      api.deleteSiteLabel(id).then(res => {
        this.$message({
          type: 'success',
          message: "Delete success."
        })
        this.getTableData()
      }).catch(err => {
        this.$message({
          type: 'error',
          message: err
        })
        this.loading = false;
      })
    },
    assignSites(row) {
      if (row) {
        this.dialogAssign.item = row;
        this.dialogAssign.visible = true;
      }
    },
    hideActionDialog(e) {
      this.dialogAction.id = "";
      this.dialogAction.visible = false;
      if (e) {
        this.getTableData();
      }
    },
    hideAssignDialog(e) {
      this.dialogAssign.item = {};
      this.dialogAssign.visible = false;
      if (e) {
        this.getTableData();
      }
    }
  }
}
</script>

<style scoped lang="scss">
@import '../../styles/element-ui.scss';
.filter-input {
  min-width: 100px;
  max-width: 300px;
}
.site-label-name {
  color: #fff;
  display: inline-block;
  font-size: 14px;
  padding: 4px 10px;
  border-radius: 4px;
  background: $--color-primary;
}
</style>