<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="queryForm.buyer_id"
              clearable
              filterable
              :loading="loading"
              :placeholder="__('请输入买家用户名称')"
              remote
              :remote-method="remoteFindUserList"
              reserve-keyword
            >
              <el-option
                v-for="item in userList"
                :key="item.user_id"
                :label="item.user_account"
                :value="item.user_id"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="queryForm.seller_id"
              clearable
              filterable
              :loading="loading"
              :placeholder="__('请输入商家用户名称')"
              remote
              :remote-method="remoteFindUserList"
              reserve-keyword
            >
              <el-option
                v-for="item in userList"
                :key="item.user_id"
                :label="item.user_account"
                :value="item.user_id"
              />
            </el-select>
            <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-form-item>
            <el-button icon="el-icon-search" 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 == 3012"
            disabled
            round
            type="primary"
          >
            {{ formatPaid(row) }}
          </el-button>
          <el-button
            v-if="row.trade_is_paid == 3013"
            disabled
            round
            type="success"
          >
            {{ formatPaid(row) }}
          </el-button>
          <el-button
            v-if="row.trade_is_paid == 3011"
            disabled
            round
            type="warning"
          >
            {{ formatPaid(row) }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :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
        align="center"
        :label="__('备注')"
        prop="trade_remark"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :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 { getList as getListAccount } from '@/api/user/base'
  import { timestampToTime } from '@/utils/date'

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

    methods: {
      __,
      formatPaid(row) {
        return row.trade_is_paid === 3010
          ? this.__('未付款')
          : row.trade_is_paid === 3011
          ? this.__('待付款')
          : row.trade_is_paid === 3012
          ? this.__('部分付款')
          : this.__('已付款')
      },
      async remoteFindUserList(user_nickname) {
        if (user_nickname !== '') {
          this.loading = true
          setTimeout(() => {
            this.loading = false
            getListAccount({ user_nickname: user_nickname }).then((res) => {
              this.userList = res.data.items
            })
          }, 200)
        } else {
          this.userList = []
        }
      },
      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>
