<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch" :inline="true" label-width="120px">
      <el-form-item :label="$t('关键词')">
        <el-input style="width: 300px" v-model.trim="queryParams.key" :placeholder="$t('请输入会员名称/会员编号/手机号')" clearable @keyup.enter.native="handleQuery" onkeyup="this.value=this.value.replace(/(^\s*)|(\s*$)/g,'')" />
      </el-form-item>
      <el-form-item :label="$t('积分来源')">
        <dict-selector clearable :type="DICT_TYPE.MEMBER_SCORE_SOURCE" v-model="queryParams.sourceType" @change="handleQuery"> </dict-selector>
      </el-form-item>
      <el-form-item :label="$t('标题')">
        <el-input style="width: 300px" v-model.trim="queryParams.ruleTitle" :placeholder="$t('请输入关键词查找')" clearable @keyup.enter.native="handleQuery" onkeyup="this.value=this.value.replace(/(^\s*)|(\s*$)/g,'')" />
      </el-form-item>
      <el-form-item :label="$t('规则说明')">
        <el-input style="width: 300px" v-model.trim="queryParams.ruleDesc" :placeholder="$t('请输入关键词查找')" clearable @keyup.enter.native="handleQuery" onkeyup="this.value=this.value.replace(/(^\s*)|(\s*$)/g,'')" />
      </el-form-item>

      <el-form-item :label="$t('当前积分')">
        <el-row :gutter="10" style="width: 300px">
          <el-col :span="10">
            <dict-selector clearable :type="DICT_TYPE.QUANTITATIVE_RELATION_SYMBOL" v-model="queryParams.scoreCountOperate" @change="handleQuery"> </dict-selector>
          </el-col>
          <el-col :span="14">
            <el-input v-model.trim="queryParams.scoreCount" :placeholder="$t('请输入数字')" clearable @keyup.enter.native="handleQuery" onkeyup="this.value=this.value.replace(/(^\s*)|(\s*$)/g,'')" />
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item :label="$t('时间')">
        <el-date-picker type="datetimerange" clearable placement="bottom-start" v-model="dateRangeCreateTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" range-separator="-" :start-placeholder="$t('开始日期')" :end-placeholder="$t('结束日期')" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>
    <el-row class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="handleQueryPagination"></right-toolbar>
    </el-row>
    <el-table ref="multipleTable" v-loading="loading" :data="memberList">
      <el-table-column width="140" :label="$t('积分记录ID')" align="center" prop="id"></el-table-column>
      <el-table-column :label="$t('会员名称')" align="center">
        <template v-slot="{ row }">
          <el-button type="text" @click="$router.push('/member/member/member-details/' + row.memberId)">{{ isChinese ? row.memberNameZh : row.memberNameEn }}</el-button>
        </template>
      </el-table-column>

      <el-table-column width="140" :label="$t('封面图片')" align="center" prop="mobile">
        <template slot-scope="scope">
          <el-image :src="isChinese ? scope.row.coverImageZh : scope.row.coverImageEn" style="width: 50px; height: 50px">{{ $t("无") }}</el-image>
        </template>
      </el-table-column>
      <el-table-column width="140" align="center" :label="$t('分值')" prop="scoreCount"></el-table-column>
      <el-table-column :label="$t('类别')" align="center">
        <template slot-scope="scope">
          {{ isChinese ? handleholdScoreStatus(scope.row.operateType).label : handleholdScoreStatus(scope.row.operateType).labelEn }}
        </template>
      </el-table-column>

      <el-table-column :label="$t('积分来源')" align="center">
        <template slot-scope="scope">
          {{ isChinese ? handleSourceType(scope.row.sourceType).label : handleSourceType(scope.row.sourceType).labelEn }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('规则标题')" align="center" :prop="isChinese ? 'ruleTitleZh' : 'ruleTitleEn'"> </el-table-column>
      <el-table-column :label="$t('规则说明')" align="center" :prop="isChinese ? 'ruleDescZh' : 'ruleDescEn'"> </el-table-column>

      <el-table-column :label="$t('操作积分时间')" align="center">
        <template v-slot="{ row }"> {{ parseTime(row.createTime) || "/" }}</template>
      </el-table-column>
      <el-table-column :label="$t('积分到期时间')" align="center">
        <template v-slot="{ row }"> {{ parseTime(row.expireTime) || "/" }}</template>
      </el-table-column>

      <el-table-column width="300" :label="$t('详细信息')" align="center">
        <template v-slot="{ row }">
          <!--  人工操作-->
          <span v-if="row.sourceType == '1'">{{ row.extParamJson.comment || "-" }}</span>
          <!-- 兑换礼品2  兑换礼品撤销4  跳转兑换记录详情-->
          <el-button
            v-if="row.sourceType == '2' || row.sourceType == '4'"
            type="text"
            @click="
              $router.push({
                path: '/member/memberManagement/exchangeRecordOperation',
                query: {
                  pageStatus: 'view',
                  exchangeRewardID: row.extParamJson.redeemId
                }
              })
            "
            >{{ `${$t("记录ID")}:${row.extParamJson.redemptionNumber}` }}</el-button
          >
          <!-- 系统过期  sourceType = 3 不展示 -->
          <span v-if="row.sourceType == '3'"> -</span>
          <!-- 订单V值:  sourceType = 5 , extParamJson中取 orderId 跳转订单详情 -->
          <el-button
            v-if="row.sourceType == '5'"
            type="text"
            @click="
              $router.push({
                path: '/order/detail',
                query: {
                  orderId: row.extParamJson.orderId
                }
              })
            "
            >{{ `${$t("订单ID")}:${row.extParamJson.orderId}` }}</el-button
          >
          <!-- 注册sourceType = 6 , extParamJson中取 userId，userNameZh,userNameZh,phone 展示会员名+手机号（手机号脱敏处理）
推荐 :  sourceType = 7 , extParamJson中取 userId，userNameZh,userNameZh,phone 展示会员名+手机号（手机号脱敏处理） -->
          <span v-if="row.sourceType == '6' || row.sourceType == '7'"> {{ `${isChinese ? row.extParamJson.userNameZh : row.extParamJson.userNameEn}(${row.extParamJson.phone})` }}</span>
          <!--  sourceType = 8 , extParamJson中取 scoreRuleId 跳转活动详情   -->
          <el-button
            v-if="row.sourceType == '8'"
            type="text"
            @click="
              $router.push({
                path: '/member/memberManagement/addIntegrationRuleOperation',
                query: {
                  id: row.extParamJson.scoreRuleId,
                  pageStatus: 'view'
                }
              })
            "
            >{{ `${$t("活动ID")}:${row.extParamJson.scoreRuleId}` }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- //分页列表 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize" @pagination="handleQueryPagination" />
  </div>
</template>
<script>
import { getDictDatas, DICT_TYPE } from "@/utils/dict"
import { queryMemberScoreRecord } from "@/api/ecw/memberManagement"

export default {
  name: "EcwMembermanagementIntegralrecord",
  props: {
    memberCode: [String, Number]
  },
  data() {
    return {
      dateRangeCreateTime: [],
      total: 0,
      loading: true,
      // 显示搜索条件
      showSearch: true,
      memberList: [],
      queryParams: {
        ruleTitle: "",
        ruleDesc: "",
        key: "",
        sourceType: "",
        scoreCount: "",
        scoreCountOperate: "",
        endTime: "",
        pageNo: 1,
        pageSize: 10,
        startTime: ""
      }
    }
  },
  computed: {
    isChinese() {
      return this.$i18n.locale === "zh_CN"
    }
  },
  watch: {
    memberCode: {
      handler: function (newVal, oldVal) {
        if (newVal) {
          this.queryParams.memberId = newVal
        }
      },
      immediate: true
    },
    $route: {
      handler: function (route) {
        const query = route.query
        if (query.memberCode) {
          this.queryParams.key = this.$route.query.memberCode
        }
      },
      immediate: true
    }
  },
  created() {
    this.handleQuery()
  },
  activated() {
    this.handleQuery()
  },
  methods: {
    //积分来源
    handleSourceType(id) {
      return this.getDictDatas(DICT_TYPE.MEMBER_SCORE_SOURCE).filter((item) => item.value == id)[0]
    },
    handleholdScoreStatus(id) {
      return this.getDictDatas(DICT_TYPE.MEMBER_SCORE_OPERATE_TYPE).filter((item) => item.value == id)[0]
    },
    handleQuery() {
      this.queryParams.pageNo = 1
      let params = { ...this.queryParams }

      if (this.dateRangeCreateTime) {
        params.startTime = this.dateRangeCreateTime[0]
        params.endTime = this.dateRangeCreateTime[1]
      }
      queryMemberScoreRecord(params).then((res) => {
        this.loading = false
        this.memberList = res.data.list
        this.total = res.data.total
      })
    },
    handleQueryPagination() {
      let params = { ...this.queryParams }
      if (this.dateRangeCreateTime) {
        params.startTime = this.dateRangeCreateTime[0]
        params.endTime = this.dateRangeCreateTime[1]
      }
      queryMemberScoreRecord(params).then((res) => {
        this.loading = false
        this.memberList = res.data.list
        this.total = res.data.total
      })
    },
    resetQuery() {
      this.loading = true
      this.dateRangeCreateTime = []
      this.queryParams = {
        ruleTitle: "",
        ruleDesc: "",
        key: "",
        sourceType: "",
        scoreCount: "",
        scoreCountOperate: "",
        endTime: "",
        pageNo: 1,
        pageSize: 10,
        startTime: ""
      }
      this.handleQuery()
    }
  }
}
</script>
