<template>
  <div class="consume-trade-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.order_id"
              clearable
              :placeholder="__('请输入商户订单编号')"
            />
          </el-form-item>
          <el-form-item>
            <el-select
              v-model.trim="queryForm.buyer_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.trim="queryForm.seller_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_is_paid"
              clearable
              :placeholder="__('请选择支付状态')"
            >
              <el-option
                v-for="item in options"
                :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_trade_id"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('标题')"
        prop="trade_title"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('商户订单编号')"
        prop="order_id"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('买家编号')"
        prop="buyer_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('买家姓名')"
        prop="buyer_nickname"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('店铺编号')"
        prop="store_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('支付状态')"
        prop="trade_is_paid"
        show-overflow-tooltip
        width="120"
      >
        <template #default="{ row }">
          <el-button
            v-if="row.trade_is_paid === 3010"
            disabled
            round
            type="danger"
          >
            {{ formatPaid(row) }}
          </el-button>
          <el-button
            v-if="row.trade_is_paid !== 3010"
            disabled
            round
            type="primary"
          >
            {{ formatPaid(row) }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :formatter="formatTrade"
        :label="__('交易类型')"
        prop="trade_type_id"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('支付渠道')"
        prop="payment_channel_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('实付金额')"
        prop="trade_payment_amount"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('平台佣金')"
        prop="order_commission_fee"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('货币支付')"
        prop="trade_payment_money"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('积分支付')"
        prop="trade_payment_points"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('折扣优惠')"
        prop="trade_discount"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('备注')"
        prop="trade_remark"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('创建时间')"
        prop="trade_create_time"
        show-overflow-tooltip
        width="180"
      />
    </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/trade'
  import { getAccounts } from '@/api/user/base'
  import { renderTime } from '@/utils/date'

  export default {
    name: 'ConsumeTrade',
    components: {},
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        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,
          },
        ],
        accountOptions: [],
        options: [
          {
            value: 3010,
            label: this.__('未付款'),
          },
          {
            value: 3011,
            label: this.__('待付款'),
          },
          {
            value: 3012,
            label: this.__('部分付款'),
          },
          {
            value: 3013,
            label: this.__('已付款'),
          },
        ],
        value: '',
        list: [],
        loading: true,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
      }
    },
    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 ''
      },

      formatTime(row) {
        return renderTime(row.trade_create_time)
      },
      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 = []
        }
      },
      formatPaid(row) {
        return row.trade_is_paid === 3010
          ? this.__('未付款')
          : row.trade_is_paid === 3011
          ? this.__('待付款')
          : row.trade_is_paid === 3012
          ? this.__('部分付款')
          : this.__('已付款')
      },
      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>
