<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-control">
        <span style="width: auto;">活动类型:</span>
        <el-select v-model="listQuery.activeType" style="width:120px;" clearable @change="getList">
          <el-option value="" label="请选择" />
          <el-option :value="1" label="商品列表" />
          <el-option :value="2" label="宣传图(一张)" />
          <el-option :value="3" label="宣传图(多张)" />
        </el-select>
      </div>
      <div class="filter-control">
        <span style="width: auto;">主题:</span>
        <el-input
          v-model="listQuery.topic"
          placeholder="请输入活动主题"
          clearable
          @keyup.enter.native="getList"
          @clear="getList"
        />
      </div>
      <div class="filter-control">
        <span style="width: auto;">状态:</span>
        <el-select v-model="listQuery.enabledStatus" style="width:120px;" clearable @change="getList">
          <el-option value="" label="所有" />
          <el-option :value="1" label="已上线" />
          <el-option :value="0" label="已下线" />
        </el-select>
      </div>
      <div class="filter-control">
        <el-button type="primary" icon="el-icon-search" @click="getList">查询</el-button>
        <el-button type="primary" icon="el-icon-plus" @click="handleToEdit('add')">添加活动</el-button>
      </div>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
    >
      <el-table-column label="活动ID" align="center" prop="id" />
      <el-table-column label="活动类型" align="center" prop="activeType">
        <template slot-scope="{row}">
          <span>{{ row.activeType | activeTypeFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动主题" align="center" prop="topic" />
      <el-table-column class-name="status-col" label="状态" align="center" prop="status">
        <template slot-scope="{row}">
          <el-tag v-if="row.enabledStatus===0" type="danger" size="small">已下线</el-tag>
          <el-tag v-else type="success" size="small">已上线</el-tag>
          <el-button v-if="row.enabledStatus === 0" type="primary" size="mini" @click="handleShangeStatus(row.id,row.enabledStatus)">上线</el-button>
          <el-button v-else type="primary" size="mini" @click="handleShangeStatus(row.id,row.enabledStatus)">下线</el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间" prop="addTime">
        <template slot-scope="{row}">
          <span>{{ row.addTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleToEdit('edit',row.id)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码 -->
    <pagination
      :total="total"
      :page-sizes="[20]"
      :page-size="20"
      :page.sync="listQuery.index"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getActiveList, getActiveStatus, delActive } from '@/api/campaign'

export default {
  filters: {
    activeTypeFilter(value) {
      const map = {
        1: '商品列表',
        2: '宣传图(一张)',
        3: '宣传图(多张)'
      }
      return map[value]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        index: 1,
        pageSize: 20,
        enabledStatus: '',
        organizeId: this.$store.getters.organizeId
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取轮播图列表
    async getList() {
      this.listLoading = true
      try {
        const res = await getActiveList(this.listQuery)
        this.list = res.data.results
        this.total = res.data.totalRecord
        this.listLoading = false
      } catch (error) {
        console.log('error', error)
      }
    },
    // 操作上下线
    handleShangeStatus(id, status) {
      let confirmTxt = ''
      if (status === 1) {
        confirmTxt = '确定下线该活动吗？ '
      } else {
        confirmTxt = '确定上线该活动吗？ '
      }
      this.$confirm(confirmTxt).then(_ => {
        this.getActiveStatus(id)
      }).catch(() => {
        this.getList()
      })
    },
    // 上下线轮播图
    async getActiveStatus(id) {
      try {
        await getActiveStatus(id)
        this.$message({ message: '操作成功', type: 'success', center: true })
        this.getList()
      } catch (error) {
        console.log('error', error)
      }
    },
    // 删除活动
    handleDelete(id) {
      this.$confirm('确定删除该该活动吗？').then(_ => {
        this.delActive(id)
      })
    },
    async delActive(id) {
      try {
        await delActive(id)
        this.$message({ message: '操作成功', type: 'success' })
        this.getList()
      } catch (error) {
        console.log('error', error)
      }
    },
    handleToEdit(type, id) {
      if (type === 'add') {
        this.$router.push({ path: '/campaign/activity-edit', query: { type: type }})
      } else {
        this.$router.push({ path: '/campaign/activity-edit', query: { type: type, id: id }})
      }
    },
    checkedInput(event, type) {
      let pattern = ''
      switch (type) {
        case 1:
          pattern = /[^\d]/g
          break
        case 2:
          pattern = /[^u4E00-u9FA5|d|a-zA-Z|rns,.?!，。？！…—&$=()-+/*{}[]]|s/g
          break
      }
      return event.replace(pattern, '')
    }
  }
}

</script>
<style>
.el-input-number.is-controls-right .el-input__inner {
    padding-left: 5px;
    padding-right: 30px;
}
</style>
<style lang="scss" scoped>
.filter-title{
  float: left;
  font-size: 12px;
  line-height: 18px;
  text-align: left;
  color: #2E3748;
}
.filter-title p{
  margin:10px 0;
}
</style>
