<template>
  <ai-detail>
    <template slot="title">
      <ai-title title="详情" isShowBack isShowBottomBorder @onBackClick="cancel(false)">
      </ai-title>
    </template>
    <template slot="content">
      <ai-card title="基本信息">
        <template #content>
          <ai-wrapper
            label-width="120px">
            <ai-info-item label="姓名" isLine :value="info.userName"></ai-info-item>
            <ai-info-item label="个人积分" isLine :value="info.integral || 0"></ai-info-item>
            <ai-info-item label="已用积分" isLine :value="info.usedIntegral || 0"></ai-info-item>
          </ai-wrapper>
        </template>
      </ai-card>
      <ai-card>
        <ai-title slot="title" title="余额变动明细"/>
        <template #content>
          <ai-search-bar>
            <template #left>
              <ai-select v-model="search.type" placeholder="请选择类型" @change="search.current = 1, getList()" :selectList="dict.getDict('integralType')"/>
            </template>
            <template #right>
              <ai-download
                :instance="instance"
                :url="`/app/appintegraluser/changeIntegralExport?id=${params.id}`"
                :params="search"
                fileName="余额变动明细"
                :disabled="tableData.length == 0">
                <el-button size="small">导出</el-button>
              </ai-download>
            </template>
          </ai-search-bar>
          <ai-table :tableData="tableData" :total="total" :current.sync="search.current" :size.sync="search.size"
                    @getList="getList" :col-configs="colConfigs" :dict="dict">
            <el-table-column slot="changeIntegral" label="变动积分" align="center">
              <template slot-scope="{ row }">
                <span v-if="row.integralType == 3">{{ row.changeIntegral | formatTime }}</span>
                <span v-if="row.integralType == 0 || row.integralType == 2">{{ row.integralCalcType == 0 ? '-' : '+' }}{{ row.changeIntegral }}</span>
              </template>
            </el-table-column>
            <el-table-column slot="integralType" label="类型" align="center">
              <template slot-scope="{ row }">
                <span v-if="row.integralType == 0">积分调整</span>
                <span v-else>{{ row.eventType }}</span>
              </template>
            </el-table-column>
            <el-table-column slot="eventDesc" label='事件' align="center" width="400px" show-overflow-tooltip>
              <template slot-scope="{ row }">
                <span>{{ row.eventDesc || row.eventName }}</span>
              </template>
            </el-table-column>
          </ai-table>
        </template>
      </ai-card>
    </template>
  </ai-detail>
</template>

<script>
  export default {
    name: 'Detail',

    props: {
      instance: Function,
      dict: Object,
      params: Object
    },

    data () {
      return {
        info: {},
        id: '',
        search: {
          current: 1,
          size: 10,
          type: ''
        },
        total: 0,
        tableData: []
      }
    },

    computed: {
      colConfigs() {
        return [
          {prop: "doTime", label: '时间', align: "left", width: "200px"},
          {slot: "integralType", label: '类型', align: "center", width: "240px", dict: "integralType"},
          {slot: "changeIntegral"},
          {prop: "nowIntegral", label: '剩余积分', align: "center", width: "200px"},
          {slot: "eventDesc"},
        ]
      }
    },

    created () {
      console.log(this.params.id)
      this.dict.load('integralType').then(() => {
        this.getInfo()
        this.getList()
      })
    },

    methods: {
      getInfo (id) {
        this.instance.post(`/app/appintegraluser/girdDetail?id=${this.params.id}`).then(res => {
          if (res.code === 0) {
            this.info = res.data
          }
        })
      },

      getList () {
        this.instance.post(`/app/appintegraluser/getChangeDetail`, null, {
          params: {
            ...this.search,
            id: this.params.id
          }
        }).then(res => {
          if (res.code === 0) {
            this.tableData = res.data.records
            this.total = res.data.total
          }
        })
      },

      cancel (isRefresh) {
        this.$emit('change', {
          type: 'List',
          isRefresh: !!isRefresh
        })
      }
    }
  }
</script>

<style scoped lang="scss">
</style>
