<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"
            @click="onClickManual">
            Generate
          </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="160"/>
      <el-table-column
        align="center"
        label="Account Type"
        prop="accountType"
        min-width="120"/>
      <el-table-column
        align="center"
        label="Year"
        prop="year"
        min-width="100"/>
      <el-table-column
        align="center"
        label="Month"
        prop="month"
        min-width="100"/>
      <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="Generated Date"
        prop="generatedDate"
        min-width="150"/>
      <!-- <el-table-column
        align="center"
        label="Update Date Time"
        prop="updateDateTime"
        min-width="150"/> -->
      <el-table-column
        align="center"
        label="Amount"
        prop="amount"
        min-width="140"/>
      <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="File Name"
        min-width="180"
        v-if="!$route.meta.onlyView">
        <template v-slot="{ row }">
          <a
            class="link-detail"
            :href="row.fileUri"
            target="_blank"
            title="Download"
            v-if="row.fileUri">
            <i class="el-icon-download"></i>
            {{row.fileName}}
          </a>
          <!-- <TableAction
            :showDel="false"
            :showIcon="false"
            editText="Download"
            @edit="onClickDownload(row)"
            @delete="onClickDelete(row)"
            v-if="row.status != 'Inactive'"/> -->
        </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>
    <ManualGenerate
      :visible="showGenerate"
      @hide="hideGenerate"/>
  </div>
</template>

<script>
import financial from '@/http/api/financial';
import TableAction from '@/components/TableAction.vue';
import Pagination from '@/components/Pagination';
import ManualGenerate from './ManualGenerate.vue';
export default {
  data() {
    return {
      filters: {
        pageNo: 1,
        pageSize: 10,
        pageVo: {
          criteria: "",
          accountType: ""
        }
      },
      table: {
        list: [],
        total: 0,
        loading: false
      },
      options: {
        types: []
      },
      showGenerate: false
    };
  },
  components: { Pagination, TableAction, ManualGenerate },
  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
        }
      }).catch(error => {
        this.$message({
          message: error,
          type: 'error'
        })
      })
    },
    getTableData() {
      this.table.loading = true;
      financial.getAccountSettlementPages(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;
      })
    },
    onClickManual() {
      this.showGenerate = true;
    },
    hideGenerate(e) {
      this.showGenerate = false;
      if (e) {
        this.onClickSearch();
      }
    }
  }
}
</script>

<style scoped>

</style>