<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.title"
        style="width:200px;margin-right:10px"
        class="filter-item"
        placeholder="请输入标题"
        clearable
      />
      <!-- <el-select v-model="listQuery.source" clearable class="filter-item" placeholder="请选择积分来源">
        <el-option v-for="item of sourceOptions" :key="item.value" v-bind="item" />
      </el-select> -->
      <el-button
        type="primary"
        style="margin-left:10px"
        @click="handleSearch"
      >搜索</el-button>
    </div>
    <el-button
      type="primary"
      size="mini"
      style="margin-bottom:20px"
      @click="handleAdd"
    >新增活动</el-button>
    <el-table v-loading="loading" :data="list" border style="width: 100%">
      <el-table-column label="标题" prop="title" />
      <el-table-column label="简介" prop="introduction" />
      <el-table-column label="图片" prop="images">
        <template v-if="scope.row.images && scope.row.images.length" slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.images[0]"
            :preview-src-list="scope.row.images"
          />
        </template>
      </el-table-column>
      <el-table-column label="创建时间">
        <template slot-scope="scope">
          <span>{{
            scope.row.createDate | parseTime("{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" width="280px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            round
            style="font-size:14px;padding:10px 20px"
            @click="handleDetail(scope.row)"
          >查看详情</el-button>
          <el-button
            size="mini"
            type="primary"
            round
            style="font-size:14px;padding:10px 20px"
            @click="handleUpdate(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            round
            style="font-size:14px;padding:10px 20px"
            @click="handleDel(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-if="total > 0"
      :total="total"
      :page.sync="listQuery.lastPageIndex"
      :limit.sync="listQuery.size"
      @pagination="getList"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { activityList, activityDel } from '@/api/fingertipIntegral'
export default {
  components: { Pagination },
  filters: {
    optionsFilter(value, options = []) {
      const findItem = options.find((option) => option.value === value)
      if (findItem) {
        return findItem.label
      }
      return ''
    }
  },
  data() {
    return {
      loading: false,
      total: 0,
      listQuery: {
        lastPageIndex: 1,
        size: 10,
        title: undefined
      },
      list: undefined,

      sourceOptions: [
        {
          value: 'REAL_NAME_AUTHENTICATION',
          label: '实名认证'
        },
        {
          value: 'HANDLING',
          label: '处理办件'
        },
        {
          value: 'APPLY',
          label: '申请办件'
        },
        {
          value: 'TIMEOUT',
          label: '超期办件扣分'
        },
        {
          value: 'VERY_SATISFIED',
          label: '非常满意得分'
        },
        {
          value: 'NEGATIVE_COMMENT',
          label: '差评扣分'
        },
        {
          value: 'READ',
          label: '阅读'
        },
        {
          value: 'SEND_A_CIRCLE_OF_FRIENDS',
          label: '发朋友圈'
        },
        {
          value: 'CONSUMPTION',
          label: '消费'
        },
        {
          value: 'MERCHANT_SETTLEMENT',
          label: '商户结算'
        },
        {
          value: 'POINT_REFUND',
          label: '积分回退'
        },
        {
          value: 'REGULATORY_PUNISHMENT_AND_REWARD',
          label: '监管惩罚'
        },
        {
          value: 'SPECIAL_WORK_TYPE',
          label: '特殊工单类型奖励积分(医保卫生)'
        }
      ]
    }
  },
  watch: {
    'listQuery.lastPageIndex': {
      handler(val) {
        this.getList()
      },
      deep: true // true 深度监听
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      activityList(this.listQuery).then(res => {
        this.list = res.d.records
        this.total = res.d.total
        // this.pageSize = res.d.pageSize
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    handleDel(item) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        activityDel({ id: item.id }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        })
      })
    },
    handleAdd() {
      this.$router.push({
        path: '/fingertipIntegral/activityCreate'
      })
    },
    handleUpdate(item) {
      this.$router.push({
        path: '/fingertipIntegral/activityChange',
        query: {
          id: item.id
        }
      })
    },
    handleDetail(item) {
      this.$router.push({
        path: '/fingertipIntegral/activityDetail',
        query: {
          type: 'detail',
          id: item.id
        }
      })
    },
    handleSearch() {
      const { listQuery } = this
      this.listQuery.lastPageIndex = 1
      Object.keys(listQuery).forEach(key => {
        if (listQuery[key] === '') listQuery[key] = undefined
      })
      this.getList()
    }
  }
}
</script>

<style></style>
