<template>
  <div class="list">
    <van-list
      v-model="loading"
      :finished="finished"
      @load="onLoad"
      v-if="list.length"
    >
      <my-activity-item
        v-for="item in list"
        :data="item"
        :isCheck="true"
        :key="item.id"
        @check="onCheck"
      ></my-activity-item>
    </van-list>
    <template v-else>
      <slot name="empty">
        <div class="empty">暂时没有内容哦，去别处看看吧～</div>
      </slot>
    </template>
    <!-- 审核 -->
    <van-action-sheet
      v-model="check.show"
      cancel-text="取消"
      :actions="check.actions"
      @select="onSelect"
    />
  </div>
</template>

<script>
import * as api from '@/api/index'
import MyActivityItem from '@/components/MyActivityItem'
export default {
  components: { MyActivityItem },
  props: {
    params: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      requestParams: { offset: 0, limit: 20 },
      // 审核
      check: {
        show: false,
        data: null,
        actions: [
          { name: '通过', value: '1' },
          { name: '拒绝', value: '0' }
        ]
      }
    }
  },
  watch: {
    params: {
      immediate: true,
      handler(value) {
        this.requestParams = Object.assign({}, this.requestParams, value)
      }
    }
  },
  methods: {
    getList() {
      this.loading = true
      api
        .getMyApproves(this.requestParams)
        .then(response => {
          if (response.length) {
            this.list = this.list.concat(response)
          }
          this.loading = false
          this.finished = response.length < this.requestParams.limit
        })
        .catch(() => {
          this.loading = false
          this.finished = true
        })
    },
    onLoad() {
      this.requestParams.offset =
        this.requestParams.offset + this.requestParams.limit
      this.getList()
    },
    // 刷新
    refresh() {
      this.requestParams.offset = 0
      this.list = []
      this.getList()
    },
    // 显示审核
    onCheck(data) {
      this.check.show = true
      this.check.data = data
    },
    // 审核
    onSelect(data) {
      const activityId = this.check.data.id
      api
        .updateActivityStatus(activityId, { status: data.value })
        .then(response => {
          this.$dialog
            .alert({
              confirmButtonText: '确定',
              message: response.message
            })
            .then(() => {
              // 刷新列表
              this.refresh()
            })
        })
        .finally(() => {
          this.check.show = false
        })
    }
  },
  beforeMount() {
    this.getList()
  }
}
</script>
