<template>
  <el-dialog
    :title="$t('pages.userSalaryDiamond.dialog.runningWaterTitle')"
    :visible="true"
    :modal-append-to-body="true"
    :append-to-body="true"
    :before-close="handleClose"
    :close-on-click-modal="false"
    width="80%"
    top="20px"
  >
    <div class="content">
      <el-table
        v-loading="listLoading"
        :data="list"
        :before-close="handleClose"
        element-loading-text="Loading"
        fit
        highlight-current-row
        max-height="350px"
      >
        <el-table-column prop="sysOrigin" :label="$t('pages.userSalaryDiamond.table.system')" align="center">
          <template slot-scope="scope">
            <sys-origin-icon :icon="scope.row.sysOrigin" :desc="scope.row.sysOrigin" />
          </template>
        </el-table-column>
        <el-table-column :label="$t('pages.userSalaryDiamond.table.user')" align="center" min-width="80">
          <template slot-scope="scope">
            <div><user-table-exhibit :size="scope.row.userProfile ? 'mini' : 'small'" :user-profile="scope.row.userProfile" :query-details="true" /></div>
            <div v-if="scope.row.tmpUserProfile" class="attached">
              <user-table-exhibit size="mini" :user-profile="scope.row.tmpUserProfile" :query-details="true" :tag-name="getTransferDescriptionName(scope.row.event)" />
            </div>
          </template>
        </el-table-column>
        <el-table-column :label="$t('pages.userSalaryDiamond.table.type')" align="center" width="80">
          <template slot-scope="scope">
            {{ getTypeLabel(scope.row.type) }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('pages.userSalaryDiamond.table.amount')" align="center" min-width="80">
          <template slot-scope="scope">
            <div>
              <span v-if="scope.row.type === 0" class="font-danger">+{{ scope.row.amount }}</span>
              <span v-if="scope.row.type === 1">-{{ scope.row.amount }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="balance" :label="$t('pages.userSalaryDiamond.table.balance')" align="center" />
        <el-table-column :label="$t('pages.userSalaryDiamond.table.event')" align="center">
          <template slot-scope="scope">
            <div>{{ getEventDesc(scope.row.salaryEvent) }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="remark" :label="$t('pages.userSalaryDiamond.table.remark')" align="center" min-width="200" />
        <el-table-column prop="createTime" :label="$t('pages.userSalaryDiamond.table.createTime')" align="center" width="160">
          <template slot-scope="scope">
            {{ scope.row.createTime | dateFormat }}
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.cursor"
        :limit.sync="listQuery.limit"
        @pagination="renderData"
      />
    </div>
  </el-dialog>
</template>

<script>
import { pageRunningWater } from '@/api/app-user-salary-diamond-balance'
import Pagination from '@/components/Pagination'
import { userSalaryDiamondWaterEvent } from '@/constant/user'
export default {
  name: 'SalaryDiamondRunningWaterDrawer',
  components: { Pagination },
  props: {
    row: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      userSalaryDiamondWaterEvent,
      listQuery: {
        cursor: 1,
        limit: 20,
        userId: '',
        sysOrigin: ''
      },
      listLoading: false,
      total: 0,
      list: []
    }
  },
  watch: {
    row: {
      handler(newVal) {
        this.listQuery.userId = newVal.userId
        this.listQuery.sysOrigin = newVal.sysOrigin
        this.renderData()
      },
      immediate: true
    }
  },
  methods: {
    renderData(isReset) {
      const that = this
      if (!that.listQuery.sysOrigin) {
        return
      }
      if (isReset === true) {
        that.list = []
        that.listQuery.cursor = 1
      }
      that.listLoading = true
      pageRunningWater(that.listQuery).then(res => {
        const { body } = res
        that.total = body.total || 0
        that.list = body.records
        that.listLoading = false
      }).catch(er => {
        that.listLoading = false
      })
    },
    getTransferDescriptionName(event) {
      if (event === 'TRANSFER') {
        return this.$t('pages.userSalaryDiamond.tag.payee')
      }
      if (event === 'RECEIVE_TRANSFER') {
        return this.$t('pages.userSalaryDiamond.tag.payer')
      }
      return ''
    },
    getTypeLabel(type) {
      return this.$t(type === 0 ? 'pages.userSalaryDiamond.type.income' : 'pages.userSalaryDiamond.type.expense')
    },
    getEventLabel(event) {
      const key = `pages.userSalaryDiamond.events.${event}`
      const translated = this.$t(key)
      return translated === key ? event : translated
    },
    handleClose() {
      this.$emit('close')
    },
    getEventDesc(_key) {
      const that = this
      const result = that.userSalaryDiamondWaterEvent.filter(_event => _event.value === _key)
      if (!result || result.length === 0) {
        return _key
      }
      return that.getEventLabel(result[0].value)
    }
  }
}
</script>
<style scoped lang="scss">
  .load-more {
    padding: 20px;
    text-align: center;
  }
</style>
