<template>
  <div class="consume-record-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <!--          <el-form-item>-->
          <!--            <el-select-->
          <!--              v-model.trim="queryForm.user_id"-->
          <!--              clearable-->
          <!--              filterable-->
          <!--              :loading="loading"-->
          <!--              :placeholder="__('请输入用户昵称')"-->
          <!--              remote-->
          <!--              :remote-method="remoteMethod"-->
          <!--              reserve-keyword-->
          <!--            >-->
          <!--              <el-option-->
          <!--                v-for="item in accountOptions"-->
          <!--                :key="item.user_id"-->
          <!--                :label="item.user_nickname"-->
          <!--                :value="item.user_id"-->
          <!--              />-->
          <!--            </el-select>-->
          <!--          </el-form-item>-->
          <el-form-item>
            <el-select
              v-model="queryForm.trade_type_id"
              clearable
              :placeholder="__('请选择')"
            >
              <el-option
                v-for="item in tradeType"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
            <el-button icon="el-icon-refresh" type="primary" @click="queryData">
              {{ __('刷新') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column
        align="center"
        fixed="left"
        :label="__('支付流水号')"
        prop="consume_record_id"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('商户订单编号')"
        prop="order_id"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('所属用户编号')"
        prop="user_id"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('用户昵称')"
        prop="user_nickname"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('金额')"
        prop="record_money"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('平台佣金')"
        prop="record_commission_fee"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('年-月-日')"
        prop="record_date"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('标题')"
        prop="record_title"
        show-overflow-tooltip
        width="240"
      />
      <el-table-column
        align="center"
        :formatter="formatRecordTime"
        :label="__('支付时间')"
        prop="record_time"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :formatter="formatTrade"
        :label="__('交易类型')"
        prop="trade_type_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatPayType"
        :label="__('支付方式')"
        prop="payment_type_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatChannel"
        :label="__('支付渠道')"
        prop="payment_channel_id"
        show-overflow-tooltip
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getList } from '@/api/consume/record'
  import { renderTime, timeToYMD } from '@/utils/date'
  import { getAccounts } from '@/api/user/base'

  export default {
    name: 'ConsumeRecord',

    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        accountOptions: [],
        list: [],
        loading: true,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
        tradeType: [
          {
            label: this.__('购物'),
            value: 1201,
          },
          {
            label: this.__('转账'),
            value: 1202,
          },
          {
            label: this.__('充值'),
            value: 1203,
          },
          {
            label: this.__('提现'),
            value: 1204,
          },
          {
            label: this.__('销售'),
            value: 1205,
          },
          {
            label: this.__('佣金'),
            value: 1206,
          },
          {
            label: this.__('退货付款'),
            value: 1207,
          },
          {
            label: this.__('退货收款'),
            value: 1208,
          },
          {
            label: this.__('转账收款'),
            value: 1209,
          },
        ],
        paymentType: [
          {
            label: this.__('货到付款'),
            value: 1301,
          },
          {
            label: this.__('其他'),
            value: 0,
          },
          {
            label: this.__('在线支付'),
            value: 1302,
          },
          {
            label: this.__('白条支付'),
            value: 1303,
          },
          {
            label: this.__('现金支付'),
            value: 1304,
          },
          {
            label: this.__('线下支付'),
            value: 1305,
          },
        ],
        paymentChannel: [
          {
            label: this.__('其他'),
            value: 0,
          },
          {
            label: this.__('支付宝'),
            value: 1401,
          },
          {
            label: this.__('微信支付'),
            value: 1403,
          },
          {
            label: this.__('余额支付'),
            value: 1406,
          },
          {
            label: this.__('支付宝_入驻'),
            value: 1410,
          },
          {
            label: this.__('微信支付_入驻'),
            value: 1411,
          },
          {
            label: this.__('积分支付'),
            value: 1413,
          },
          {
            label: 'payu',
            value: 1414,
          },
          {
            label: 'ccav',
            value: 1415,
          },
          {
            label: this.__('Paypal支付'),
            value: 1420,
          },
          {
            label: this.__('银联'),
            value: 1421,
          },
          {
            label: this.__('线下支付'),
            value: 1422,
          },
          {
            label: 'redsys',
            value: 1431,
          },
        ],
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      formatTrade(row) {
        const tradeTypeId = row.trade_type_id
        const obj = this.tradeType.find((item) => item.value === tradeTypeId)
        if (obj) return obj.label
        return ''
      },
      formatPayType(row) {
        const paymentTypeId = row.payment_type_id
        const obj = this.paymentType.find(
          (item) => item.value === paymentTypeId
        )
        if (obj) return obj.label
        return ''
      },
      formatChannel(row) {
        const paymentChannelId = row.payment_channel_id
        const obj = this.paymentChannel.find(
          (item) => item.value === paymentChannelId
        )
        if (obj) return obj.label
        return ''
      },
      formatTime(row) {
        const recordDate = row.record_date
        return recordDate ? timeToYMD(recordDate) : ''
      },
      formatRecordTime(row) {
        const recordTime = row.record_time
        if (!recordTime) return ''
        return renderTime(recordTime)
      },
      __,
      async remoteMethod(user_nickname) {
        if (user_nickname.trim() !== '') {
          this.loading = true
          const { data } = await getAccounts({ user_nickname })
          this.accountOptions = data
          this.loading = false
        } else {
          this.accountOptions = []
        }
      },

      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
