<template>
  <div class="app-container coupon-list">
    <el-form v-show="showSearch" :model="queryParams" ref="queryForm" :inline="true" size="small">
      <el-form-item label="活动名称">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入活动名称"
          clearable @clear="queryParams.pageNum = 1;getList()"
          @keyup.enter.native="queryParams.pageNum = 1;getList()"
        />
      </el-form-item>
      <el-form-item label="活动状态">
        <el-select v-model="queryParams.isOn" placeholder="请选择活动状态" clearable @change="queryParams.pageNum = 1;getList()">
          <el-option label="全部" value="" />
          <el-option v-for="(item,index) in marketingStatusList"  :label="item.dictLabel" :value="Number(item.dictValue)" :key="index"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="queryParams.pageNum = 1;getList()">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="getList(true)">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="10">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="$router.push({ name: 'MarketingAdd' })"
          v-hasPermi="['business:marketing:add']"
        >创建活动</el-button>
        <el-button
          type="infor"
          plain
          icon="el-icon-download"
          size="small"
          @click="handleExportDraw"
          v-hasPermi="['order:userTicket:export']"
        >导出中奖数据</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="活动编号" prop="id" width="80" />
      <el-table-column label="活动名称" prop="title" />
      <el-table-column label="参与人数" prop="realNum"/>
      <el-table-column label="开始时间" prop="startTime" >
        <template slot-scope="{row}">
          {{ parseTime(row.startTime) }}
        </template>
      </el-table-column>
      <el-table-column label="结束时间" prop="endTime" >
        <template slot-scope="{row}">
          {{ parseTime(row.endTime) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status">
        <template slot-scope="{row}">
          <el-tag :type="JSON.parse(row.status).value === 4 || JSON.parse(row.status).value === -1 ? 'info' : 'success'">{{ JSON.parse(row.status).desc }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" align="right" label="操作" width="240">
        <template slot-scope="{row}">
          <el-button v-hasPermi="['business:marketing:query']" type="text" @click="$router.push({ name: 'MarketingQuery', query: { id: row.id } })">查看</el-button>
          <el-button v-if=" JSON.parse(row.status).value === 3 || JSON.parse(row.status).value === 0 || JSON.parse(row.status).value === 2 " v-hasPermi="['business:marketing:edit']" type="text" @click="$router.push({ name: 'MarketingEdit', query: { id: row.id } })">编辑</el-button>
          <el-button v-if=" JSON.parse(row.status).value === 3 || JSON.parse(row.status).value === 4 || JSON.parse(row.status).value === -1 " v-hasPermi="['business:marketing:edit']" type="text"  @click="activityTableVisible = true, ids = { id:row.id, title:row.title, realNum:row.realNum }">活动数据</el-button>
          <el-button v-if=" JSON.parse(row.status).value === 0 " v-hasPermi="['business:coupon:on']" type="text" @click="setStatus(row, 'on')">开启</el-button>
          <el-button v-if=" JSON.parse(row.status).value === 3 || JSON.parse(row.status).value === 2 " v-hasPermi="['business:coupon:off']" type="text" @click="setStatus(row, 'off')">关闭</el-button>
          <el-button v-if=" JSON.parse(row.status).value === 0 " v-hasPermi="['business:coupon:remove']" class="del" type="text" @click="del(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList()" />
    <activity-table :dialog-visible="activityTableVisible" :ids="ids" @close="activityTableVisible = false" v-if="activityTableVisible"></activity-table>
  </div>
</template>
<script>
import { getMarketingList, delMarketing, setMarketingStatus, setMarketingStatusOff, } from '@/api/business/marketing'
import ActivityTable from "./components/ActivityTable";
import { accDiv } from '@/utils/util'
export default {
  name: 'CouponList',
  components:{
    ActivityTable,
  },
  data() {
    return {
      loading: false,
      showSearch: true,
      tableData: [],
      queryParams: {},
      total: 0,
      ids: {},
      activityTableVisible: false,
      marketingStatusList: []
    }
  },
  created() {
    this.getList(true)
    this.getMarketingStatus()
  },
  methods: {
    getList(reset) {
      if (this.loading) {
        return
      }
      this.loading = true
      if (reset) {
        this.queryParams = { pageNum: 1, pageSize: 20 }
      }
      getMarketingList('pageNum='+this.queryParams.pageNum + '&pageSize='+this.queryParams.pageSize+'&', this.queryParams).then(res => {
        this.loading = false
        if (res.code === 0) {
          this.tableData = res.rows
          this.total = res.total
        }
      }).catch(() => {
        this.loading = false
      })
    },
    getMarketingStatus(){
      this.getDicts('marketing_status').then(res=>{
        this.marketingStatusList = res.data
      })
    },
    setStatus(item, status) {
      this.$confirm(`确认${status === 'on' ? '开启' : '关闭'}活动 “${item.title}” 吗？`, `${status === 'on' ? '开启' : '关闭'}活动`, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (status === 'on'){
          setMarketingStatus(item.id).then(res => {
            if (res.code === 0) {
              this.$message.success('开启已完成！')
              this.getList()
            }
          })
        }else{
          setMarketingStatusOff(item.id).then(res => {
            if (res.code === 0) {
              this.$message.success('关闭已完成！')
              this.getList()
            }
          })
        }
      })
    },
    del(item) {
      this.$confirm(`确认删除活动 “${item.title}” 吗？`, '删除活动', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delMarketing(item.id).then(res => {
          if (res.code === 0) {
            this.$message.success('操作已完成！')
            this.getList()
          }
        })
      })
    },

    // 导出订单
    handleExportDraw() {
      this.$confirm("是否确认导出活动数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.vloading = this.$loading({
            lock: true,
            text: "正在导出活动数据.....",
            background: "rgba(0, 0, 0, 0.7)",
          });
          return ticketOrderExport(this.queryParams);
        })
        .then((response) => {
          this.vloading.close();
          this.download(response.msg);
        })
        .catch(() => {
          this.vloading.close();
        });
    },
  }
}
</script>
