<template>
  <div class="distribution-user-order-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
              :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 :label="__('有效时间')" prop="uo_search_time">
            <el-date-picker
              v-model="uo_search_time"
              :end-placeholder="__('活动结束日期')"
              format="yyyy-MM-dd HH:mm:ss"
              :start-placeholder="__('活动开始日期')"
              :style="{ width: '100%' }"
              type="datetimerange"
              value-format="yyyy-MM-dd HH:mm:ss"
            />
          </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"
        :label="__('编号')"
        prop="uo_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('收益用户编号')"
        prop="user_id"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :label="__('收益用户昵称')"
        prop="user_nickname"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('订单用户编号')"
        prop="buyer_user_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('订单用户昵称')"
        prop="buyer_user_name"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :label="__('推广员佣金')"
        prop="uo_buy_commission"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('销售员佣金')"
        prop="uo_directseller_commission"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('买家编号')"
        prop="buyer_user_id"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :label="__('分佣等级')"
        prop="uo_level"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :formatter="formatActive"
        :label="__('是否有效')"
        prop="uo_active"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('支付时间')"
        prop="uo_paytime"
        show-overflow-tooltip
        width="160"
      />
    </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/distribution/user/order'
  import { timestampToTime } from '@/utils/date'
  import { getAccounts } from '@/api/user/base'

  export default {
    name: 'DistributionUserOrder',

    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        uo_search_time: [],
        list: [],
        accountOptions: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          title: '',
          user_id: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      formatTime(row) {
        if (row.uo_paytime > 0) {
          return timestampToTime(row.uo_paytime)
        }
      },
      // formatTime(row) {
      //   const cardTime = row.shopOrderBase.order_time
      //   return cardTime ? renderTime(cardTime) : ''
      // },
      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 = []
        }
      },
      formatActive(row) {
        return row.uo_active ? this.__('有效') : this.__('未生效')
      },

      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        let uoSearchTime = this.uo_search_time
        if (uoSearchTime && uoSearchTime.length > 0) {
          if (uoSearchTime[0]) this.queryForm.start_time = uoSearchTime[0]
          if (uoSearchTime[1]) this.queryForm.end_time = uoSearchTime[1]
        } else {
          this.queryForm.start_time = ''
          this.queryForm.end_time = ''
        }
        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>
