<template>
  <div class="user-points-history-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="20">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-select
              v-model.trim="queryForm.user_id"
              clearable
              filterable
              :loading="userLoading"
              :placeholder="__('请输入用户昵称')"
              remote
              :remote-method="getUserInfoList"
              reserve-keyword
            >
              <el-option
                v-for="item in userOptions"
                :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.store_id"
              clearable
              filterable
              :loading="storeLoading"
              :placeholder="__('请输入店铺名称')"
              remote
              :remote-method="getStoreList"
              reserve-keyword
            >
              <el-option
                v-for="item in storeOptions"
                :key="item.store"
                :label="item.store_name"
                :value="item.store_id"
              />
            </el-select>
          </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"
        :formatter="formatPointsKind"
        :label="__('类型')"
        prop="points_kind_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatPintsType"
        :label="__('积分类型')"
        prop="points_type_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('会员编号')"
        prop="user_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('积分变动值')"
        prop="points_log_points"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('当前积分')"
        prop="user_points"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('创建时间')"
        prop="points_log_time"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('描述')"
        prop="points_log_desc"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('所属店铺')"
        prop="store_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('交互会员编号')"
        prop="user_id_other"
        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"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getList as getStoreList } from '@/api/store/base'
  import { getList } from '@/api/user/points/history'
  import Edit from './components/UserPointsHistoryEdit'
  import { renderTime } from '@/utils/date'
  import { getAccounts } from '@/api/user/base'

  export default {
    name: 'UserPointsHistory',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        userLoading: false,
        userOptions: [],
        storeLoading: false,
        storeOptions: [],
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
        pointsTypes: [
          {
            value: 1,
            label: this.__('会员注册'),
          },
          {
            value: 2,
            label: this.__('会员登录'),
          },
          {
            value: 3,
            label: this.__('商品评论'),
          },
          {
            value: 4,
            label: this.__('购买商品'),
          },
          {
            value: 5,
            label: this.__('管理员操作'),
          },
          {
            value: 7,
            label: this.__('积分换购商品'),
          },
          {
            value: 8,
            label: this.__('积分兑换代金券'),
          },
          {
            value: 9,
            label: this.__('积分兑换'),
          },
          {
            value: 10,
            label: this.__('积分转出'),
          },
          {
            value: 11,
            label: this.__('积分转入'),
          },
          {
            value: 12,
            label: this.__('积分退还'),
          },
          {
            value: 13,
            label: this.__('升级服务商'),
          },
          {
            value: 14,
            label: this.__('升级商家'),
          },
          {
            value: 15,
            label: this.__('发展粉丝赠送积分'),
          },
          {
            value: 16,
            label: this.__('余额换积分'),
          },
          {
            value: 17,
            label: this.__('积分换余额'),
          },
          {
            value: 21,
            label: this.__('购买商品抵扣积分'),
          },
        ],
        pointsKinds: [
          {
            value: 1,
            label: this.__('获取积分'),
          },
          {
            value: 2,
            label: this.__('消费积分'),
          },
        ],
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      formatTime(row) {
        const points_log_time = row.points_log_time
        return points_log_time ? renderTime(points_log_time) : ''
      },
      formatDateTime(row) {
        const points_log_date = row.points_log_date
        return points_log_date ? renderTime(points_log_date) : ''
      },
      formatPintsType(row) {
        const points_type_id = row.points_type_id
        const obj = this.pointsTypes.find(
          (item) => item.value === points_type_id
        )
        return obj ? obj.label : ''
      },
      formatPointsKind(row) {
        const points_kind_id = row.points_kind_id
        const obj = this.pointsKinds.find(
          (item) => item.value === points_kind_id
        )
        return obj ? obj.label : ''
      },
      handleEdit(row) {
        if (row.points_log_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      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
      },
      async getUserInfoList(user_nickname) {
        if (user_nickname.trim() !== '') {
          this.userLoading = true
          const { data } = await getAccounts({ user_nickname })
          this.userOptions = data
          this.userLoading = false
        } else {
          this.userOptions = []
        }
      },
      async getStoreList(store_name) {
        if (store_name.trim() !== '') {
          this.storeLoading = true
          const { data } = await getStoreList({ store_name: store_name })
          if (data.items) {
            this.storeOptions = data.items
          }
          this.storeLoading = false
        } else {
          this.storeOptions = []
        }
      },
    },
  }
</script>
