<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-view">
        <el-select
          class="filter-input"
          placeholder="Account Type"
          clearable
          @change="onClickSearch"
          v-model="filters.pageVo.accountType">
          <el-option
            v-for="(item,index) in options.types"
            :key="index"
            :label="item"
            :value="item"/>
        </el-select>
        <el-input
          v-model="filters.pageVo.criteria"
          placeholder="Entity Name, POC Name, POC Email"
          prefix-icon="el-icon-search"
          style="max-width: 300px;"
          @change="onClickSearch"
          @keyup.enter.native="onClickSearch"
          clearable/>
        <div class="filter-flex-button">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="onClickAdd">
            Create
          </el-button>
        </div>
      </div>
    </div>
    <el-table
      v-loading="table.loading"
      :data="table.list">
      <el-table-column
        align="center"
        label="Entity Name"
        prop="entityName"
        min-width="150"/>
      <el-table-column
        align="center"
        label="Account Type"
        prop="accountType"
        min-width="150"/>
      <el-table-column
        align="center"
        label="POC Name"
        prop="pocName"
        min-width="150"/>
      <el-table-column
        align="center"
        label="POC Email"
        prop="pocEmail"
        min-width="150"/>
      <el-table-column
        align="center"
        label="Effective Date"
        prop="effectiveDateRange"
        min-width="150">
        <template slot-scope="{row}">
          <div
            v-for="(item,index) in row.effectiveDateRange"
            :key="index">
            <span v-if="index==0">Start:</span>
            <span v-else>&nbsp;End:</span>
            {{item}}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="Billing Date"
        prop="billingDate"
        min-width="150"/>
      <el-table-column
        align="center"
        label="Update Date Time"
        prop="updateDateTime"
        min-width="150"/>
      <el-table-column
        align="center"
        label="Billing Type"
        prop="billingType"
        min-width="120"/>
      <el-table-column
        align="center"
        label="Status"
        prop="status"
        min-width="100"/>
      <el-table-column
        align="center"
        label="Action"
        min-width="80"
        v-if="!$route.meta.onlyView">
        <template v-slot="{ row }">
          <el-dropdown
            class="action-dropdown"
            @command="(v) => handleCommand(v, row)"
            v-if="row.status != 'Inactive'">
            <i class="el-icon-more icon-action"></i>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="onClickEdit">
                Edit
              </el-dropdown-item>
              <el-dropdown-item
                command="assignGroups"
                v-if="row.accountType == 'Corporate'">
                Assign Groups
              </el-dropdown-item>
              <el-dropdown-item
                command="assignSites"
                v-else>
                Assign Sites
              </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>
    <Assignment
      v-bind="assign"
      @hide="hideAssiment"/>
  </div>
</template>

<script>
import financial from '@/http/api/financial';
import Assignment from './assignment.vue'
import Pagination from '@/components/Pagination'
export default {
  data() {
    return {
      filters: {
        pageNo: 1,
        pageSize: 10,
        pageVo: {
          criteria: "",
          accountType: ""
        }
      },
      table: {
        list: [],
        total: 0,
        loading: false
      },
      options: {
        types: []
      },
      assign: {
        item: {},
        visible: false,
        isGroup: false
      }
    };
  },
  components: { Pagination, Assignment },
  created() {
    this.getAccountTypeOptions();
    this.onClickSearch();
  },
  methods: {
    onClickSearch() {
      this.filters.pageNo = 1
      this.getTableData()
    },
    getAccountTypeOptions() {
      financial.getBillingAccountTypes().then(res => {
        if (res.data) {
          this.options.types = res.data
        } else {
          this.options.types = []
        }
      }).catch(err => {
        this.$message({
          message: error,
          type: 'error'
        })
        this.options.types = []
      })
    },
    getTableData() {
      this.table.loading = true;
      financial.getBillingAccountPages(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.table.loading = false;
      })
    },
    handleCommand(cb, item) {
      this[cb](item)
    },
    onClickAdd() {
      this.$router.push({
        path: "/financial-management/billing-account-add"
      })
    },
    onClickEdit(row) {
      this.$router.push({
        path: "/financial-management/billing-account-edit/" + row.entityId
      })
    },
    onClickDelete(row) {
      this.$confirm('Are you sure you want to delete this item?', 'Set Inactive', {
        confirmButtonText: 'OK',
        cancelButtonText: 'Cancel',
        type: 'warning'
      }).then(res => {
        this.deleteEntity(row.entityId);
      })
    },
    assignSites(row) {
      this.assign.item = row;
      this.assign.isGroup = false;
      this.assign.visible = true;
    },
    assignGroups(row) {
      this.assign.item = row;
      this.assign.isGroup = true;
      this.assign.visible = true;
    },
    hideAssiment(e) {
      this.assign.item = {};
      this.assign.visible = false;
      this.assign.isGroup = false;
      if (e) {
        this.getTableData();
      }
    },
    deleteEntity(id) {
      this.loading = true;
      financial.deleteBillingAccount(id).then(res => {
        this.$message({
          type: 'success',
          message: "Set inactive success."
        })
        this.getTableData()
      }).catch(err => {
        this.$message({
          type: 'error',
          message: err
        })
        this.loading = false;
      })
    }
  }
}
</script>

<style scoped>

</style>