<template>
  <div :class="'app-container'">
    <div class="filter-container">
      <el-select
        v-model="listQuery.sysOrigin"
        :placeholder="$t('pages.userSalaryDiamond.filter.system')"
        style="width: 120px"
        class="filter-item"
        @change="handleSearch"
      >
        <el-option
          v-for="item in permissionsSysOriginPlatforms"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
          <span style="float: left;"> <sys-origin-icon :icon="item.value" :desc="item.value" /></span>
          <span style="float: left;margin-left:10px">{{ item.label }}</span>
        </el-option>
      </el-select>
      <div class="filter-item">
        <account-input v-model="listQuery.userId" :sys-origin="listQuery.sysOrigin" :placeholder="$t('pages.userSalaryDiamond.filter.userId')" />
      </div>
      <el-select
        v-model="listQuery.type"
        :placeholder="$t('pages.userSalaryDiamond.filter.type')"
        style="width: 120px"
        class="filter-item"
        clearable
        @change="handleSearch"
      >
        <el-option :label="$t('pages.userSalaryDiamond.type.income')" :value="0" />
        <el-option :label="$t('pages.userSalaryDiamond.type.expense')" :value="1" />
      </el-select>
      <el-select
        v-model="listQuery.event"
        :placeholder="$t('pages.userSalaryDiamond.filter.event')"
        style="width: 120px"
        class="filter-item"
        clearable
        @change="handleSearch"
      >
        <el-option
          v-for="(item, index) in userSalaryDiamondWaterEvent"
          :key="index"
          :label="getEventLabel(item.value)"
          :value="item.value"
        />
      </el-select>

      <div class="filter-item">
        <el-date-picker
          v-model="rangeDate"
          value-format="timestamp"
          type="datetimerange"
          :picker-options="pickerOptions"
          :range-separator="$t('pages.userSalaryDiamond.date.rangeSeparator')"
          :start-placeholder="$t('pages.userSalaryDiamond.date.start')"
          :end-placeholder="$t('pages.userSalaryDiamond.date.end')"
        />
      </div>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        :disabled="searchDisabled"
        @click="handleSearch"
      >
        {{ $t('pages.userSalaryDiamond.action.search') }}
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      fit
      highlight-current-row
    >
      <el-table-column prop="sysOrigin" :label="$t('pages.userSalaryDiamond.table.system')" align="center" width="80">
        <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" min-width="80" />
      <el-table-column prop="tempBalance" :label="$t('pages.userSalaryDiamond.table.tempBalance')" align="center" min-width="80" />

      <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="160" />
      <el-table-column prop="createUserOriginName" :label="$t('pages.userSalaryDiamond.table.createOrigin')" align="center" />
      <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>
</template>

<script>
import { pageRunningWater } from '@/api/app-user-salary-diamond-balance'
import { pickerOptions } from '@/constant/el-const'
import { mapGetters } from 'vuex'
import { userSalaryDiamondWaterEvent } from '@/constant/user'
import Pagination from '@/components/Pagination'
export default {
  name: 'UserSalaryDiamondRunningWater',
  components: { Pagination },
  data() {
    return {
      userSalaryDiamondWaterEvent,
      rangeDate: [],
      thatSelectedUserId: '',
      thatSelectedAcceptUserId: '',
      searchDisabled: false,
      pickerOptions,
      isCalculateTotalLoading: false,
      isCalculateTotalAmount: false,
      calculateTotalValue: '0',
      listQuery: {
        cursor: 1,
        limit: 20,
        userId: '',
        type: '',
        event: '',
        sysOrigin: 'HALAR',
        startTime: '',
        endTime: ''
      },
      listLoading: false,
      total: 0,
      list: []
    }
  },
  computed: {
    ...mapGetters(['permissionsSysOriginPlatforms'])
  },
  watch: {
    rangeDate: {
      immediate: true,
      deep: true,
      handler(newVal) {
        if (newVal && newVal.length > 0) {
          this.listQuery.startTime = newVal[0]
          this.listQuery.endTime = newVal[1]
          return
        }
        this.listQuery.startTime = ''
        this.listQuery.endTime = ''
      }
    }
  },
  created() {
    const that = this
    const querySystem = this.permissionsSysOriginPlatforms[0]
    if (!querySystem) {
      return
    }
    that.listQuery.sysOrigin = querySystem.value
    that.renderData()
  },
  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
    },
    handleSearch() {
      this.renderData(true)
    },
    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">
  .user-css {
    display: flex;
    align-items: center;
    color: #909399;
    font-size: .15rem;
    line-height: .3rem;
    padding: 0.02rem;
    border-radius: 0.04rem;
  }
  .load-more {
    padding: 20px;
    text-align: center;
  }
</style>
