<template>
  <div class="app-container adjustment-container data-list">
    <div class="filter-container">
      <el-input :maxLength="$inputMax" v-model="listQuery.Reviewer" style="width:220px" size="small" placeholder="Reviewer ID" class="mr" />
      <el-date-picker
        v-model="listQuery.BeginMonth"
        size="small"
        placeholder="Beginning date"
        type="month"
        value-format="yyyy-MM"
        disabled
        class="mr"
      />
      <el-button size="small" type="text" @click="handleReset">Reset</el-button>
      <el-button type="primary" size="small" @click="handleSearch">Search</el-button>
      <el-button style="margin-left:auto" type="primary" size="small" @click="handleNew">New</el-button>
    </div>
    <div class="data-table">
      <el-table
        v-loading="listLoading"
        stripe
        :data="list"
        height="100%"
        size="small"
        @sort-change="sortByColumn"
      >
        <el-table-column type="index" width="50" />
        <el-table-column
          prop="LastName"
          label="Name"
          min-width="100"
          sortable="custom"
          show-overflow-tooltip
        >
          <template slot-scope="scope">{{ scope.row.LastName + ' / ' + scope.row.FirstName }}</template>
        </el-table-column>
        <el-table-column
          prop="ChineseName"
          label="Name CN"
          min-width="120"
          sortable="custom"
          show-overflow-tooltip
        />
        <el-table-column
          prop="ReviewerCode"
          label="Reviewer ID"
          min-width="110"
          sortable="custom"
          show-overflow-tooltip
        />
        <el-table-column
          prop="AdjustPaymentUSD"
          label="Amount($)"
          min-width="120"
          sortable="custom"
          show-overflow-tooltip
        >
          <template slot-scope="scope">{{ scope.row.AdjustPaymentUSD | rounding }}</template>
        </el-table-column>
        <el-table-column
          prop="AdjustPaymentCNY"
          label="Amount(￥)"
          min-width="120"
          sortable="custom"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <span>{{ scope.row.AdjustPaymentCNY | rounding }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="Note" label="Note" min-width="120" show-overflow-tooltip />
        <el-table-column
          prop="YearMonth"
          label="Adjust Month"
          min-width="120"
          sortable="custom"
          show-overflow-tooltip
        />
        <el-table-column label="Action">
          <template slot-scope="scope">
            <el-button size="small" type="text" :disabled="scope.row.IsLock" @click="handleEdit(scope.row)">Edit</el-button>
            <el-button
              size="small"
              type="text"
              :disabled="scope.row.IsLock"
              @click="handleDelete(scope.row)"
            >Delete</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination">
      <pagination :total="total" :page.sync="listQuery.PageIndex" :limit.sync="listQuery.PageSize" @pagination="getList" />
    </div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="600px"
    >
      <adjustment-form
        :key="timer"
        :row="row"
        :date="date"
        :exchange-rate="exchangeRate"
        @closeDialog="closeDialog"
        @getList="getList"
      />
    </el-dialog>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination'
import AdjustmentForm from './components/AdjustmentForm'
import { getPaymentAdjustmentList, deletePaymentAdjustment } from '@/api/financials'
import { FormatTime } from '@/utils/formatter'
export default {
  filters: {
    rounding(value) {
      return value ? Number(value).toFixed(2) : value
    }
  },
  components: { Pagination, AdjustmentForm },
  data() {
    return {
      listQuery: {
        TrialCode: '',
        Reviewer: '',
        BeginMonth: '',
        EndMonth: '',
        PageIndex: 1,
        PageSize: 20,
        Asc: false,
        SortField: ''
      },
      listLoading: false,
      list: [],
      total: 0,
      exchangeRate: '',
      date: '',
      timer: '',
      dialogVisible: false,
      title: '',
      row: ''
    }
  },
  mounted() {
    this.date = this.$router.currentRoute.query.date
    this.listQuery.BeginMonth = this.date
    this.listQuery.EndMonth = this.date
    // this.exchangeRate = this.$router.currentRoute.query.exchangeRate ? parseInt(this.$router.currentRoute.query.exchangeRate) : 0
    this.exchangeRate = this.$router.currentRoute.query.exchangeRate ? this.$router.currentRoute.query.exchangeRate : 0
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getPaymentAdjustmentList(this.listQuery).then(res => {
        this.listLoading = false
        this.list = res.Result.CurrentPageData
        this.total = res.Result.TotalCount
      })
    },
    handleNew() {
      this.title = 'New'
      this.row = {}
      this.timer = new Date().getTime()
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.title = 'Edit'
      this.row = JSON.parse(JSON.stringify(row))
      this.timer = new Date().getTime()
      this.dialogVisible = true
    },
    handleDelete(row) {
      this.$confirm('Confirm to delete?', {
        type: 'warning',
        distinguishCancelAndClose: true,
        confirmButtonText: 'Ok',
        cancelButtonText: 'Cancel'
      })
        .then(() => {
          deletePaymentAdjustment(row.Id)
            .then(res => {
              if (res.IsSuccess) {
                this.list.splice(this.list.indexOf(row), 1)
                this.total = this.total - 1
                this.$message.success('Deleted successfully !')
              }
            })
        })
    },
    handleSearch() {
      this.listQuery.PageIndex = 1
      this.getList()
    },
    handleReset() {
      this.listQuery.PageIndex = 1
      this.listQuery.Reviewer = ''
      this.getList()
    },
    sortByColumn(column) {
      if (column.order === 'ascending') {
        this.listQuery.Asc = true
      } else {
        this.listQuery.Asc = false
      }
      this.listQuery.SortField = column.prop
      this.listQuery.PageIndex = 1
      this.getList()
    },
    closeDialog() {
      this.dialogVisible = false
    },
    adjustMonthFormatter(row) {
      if (row.AdjustedYearMonth) {
        const date = new Date(row.AdjustedYearMonth)
        return FormatTime(date, 'yyyy-MM')
      } else {
        return ''
      }
    }
  }
}
</script>
<style lang="scss">
.adjustment-container{

  .filter-container{
    display: flex;
    align-items: center;
    .mr{
      margin-right:5px;
    }
  }
  .list-container{
    height: calc(100% - 80px);
  }
}
</style>

