<template>
  <div class="app-container">
    <!-- 筛选条件 -->
    <div class="screen-container">
      <el-form ref="screenForm" :model="screenForm" label-width="70px" size="small" label-position="left">
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="活动名称" prop="name">
              <el-input v-model="screenForm.name" placeholder="请输入活动名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="12" :lg="6">
            <el-form-item label="状态" prop="status">
              <el-select v-model="screenForm.status" placeholder="请选择状态">
                <el-option label="全部" value=""></el-option>
                <el-option label="进行中" :value="true"></el-option>
                <el-option label="已结束" :value="false"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :lg="12" class="tr">
            <el-form-item label="">
              <el-button size="small" @click="resetScreenForm">清空</el-button>
              <el-button size="small" type="primary" @click="submitScreenForm">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <div class="mymain-container">
      <div class="btn-group clearfix">
        <div class="fl">
          <el-button size="small" type="primary" icon="el-icon-plus" @click="addOrEdit('add')">新增活动</el-button>
        </div>
        <div class="fr">
          <el-button size="small" type="primary" icon="el-icon-download" @click="handleExport">导出</el-button>
        </div>
      </div>

      <div class="table">
        <el-table v-loading="listLoading" :data="dataList" element-loading-text="Loading" border fit highlight-current-row stripe>
          <el-table-column align="center" label="商户" prop="companyWechatName" min-width="120" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="活动名称" prop="name" min-width="120" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="优惠券总数" prop="total" min-width="120"></el-table-column>
          <el-table-column align="center" label="兑换总数" prop="exchangeTotal" min-width="120"></el-table-column>
          <el-table-column align="center" label="剩余兑换数量" prop="surplusExchange" min-width="120"></el-table-column>
          <el-table-column align="center" label="使用总数" prop="useTotal" min-width="120"></el-table-column>
          <el-table-column align="center" label="未使用数量" prop="notUse" min-width="120"></el-table-column>
          <el-table-column align="center" label="状态" prop="status" min-width="120">
            <template slot-scope="scope">
              {{ scope.row.status | statusFilter}}
            </template>
          </el-table-column>
          <el-table-column align="center" label="创建时间" prop="createTime" min-width="160"></el-table-column>
         
          <el-table-column align="center" label="操作" fixed="right" width="250">
            <template slot-scope="scope">
              <el-button type="text" @click="toDetail(scope.row.id)">详情</el-button>
              <el-popconfirm style="margin: 0 10px;" title="确定取消吗？" @onConfirm="changeStatus(scope.row.id, false)" v-if="scope.row.status">
                <el-button slot="reference" type="text" style="color: #F56C6C;">取消</el-button>
              </el-popconfirm>
              <el-popconfirm style="margin: 0 10px;" title="确定取消吗？" @onConfirm="changeStatus(scope.row.id, true)" v-if="!scope.row.status">
                <el-button slot="reference" type="text" style="color: #67C23A;">恢复</el-button>
              </el-popconfirm>
              <el-button type="text" @click="toRecords(scope.row.id)">兑换记录</el-button>
              <el-button type="text" @click="exportCode(scope.row.id)">导出兑换码</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="pagination clearfix">
        <div class="fr">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="listTotal">
          </el-pagination>
        </div>
      </div>
    </div>


  </div>
</template>

<script>
import { getList, changeStatus } from "@/api/draw";
import { downloadFiles } from '@/utils/util'

export default {
  filters: {
    statusFilter(val) {
      const MAP = {
        true: '正常',
        false: '已取消',
      }
      return MAP[val]
    },
  },
  data() {
    return {
      dataList: null, // 列表数据
      listLoading: true, // 列表加载loading
      screenForm: { // 筛选表单数据
        name: '', // 活动名称
        status: '' // 标志
      },
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      
    }
  },
  computed: {
    
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;

      let params = {
        name: this.screenForm.name,
        status: this.screenForm.status,
        pageNum: this.currentPage,
        pageSize: this.pageSize
      }

      getList(params).then(res => {
        this.dataList = res.data.records;
        this.listTotal = res.data.total;
        this.listLoading = false;
      })
    },

    // 提交筛选表单
    submitScreenForm() {
      this.currentPage = 1;
      this.getList();
    },

    // 重置筛选表单
    resetScreenForm() {
      this.$refs.screenForm.resetFields();
      this.currentPage = 1;
      this.getList();
    },

    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.getList();
    },

    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
    },

    // 添加模块
    addOrEdit(type, id) {
      if(type == 'add') {
        this.$router.push({
          path: '/activity/draw_add',
          query: {}
        })
      }else {
        this.$router.push({
          path: '/activity/draw_add',
          query: {
            id
          }
        })
      }
    },

    // 详情
    toDetail(id) {
      this.$router.push({
        path: '/activity/draw_detail',
        query: {
          id
        }
      })
    },

    // 查看兑换记录
    toRecords(id) {
      this.$router.push({
        path: '/activity/draw_records',
        query: {
          promotionLuckDrawId: id
        }
      })
    },

    // 更改状态
    changeStatus(id, val) {
      changeStatus({
        promotionLuckDrawId: id,
        status: val
      }).then(res => {
        this.getList();
        this.$successMsg();
      })
    },

    // 导出
    handleExport() {
      let screenData = {
        name: this.screenForm.name,
        status: this.screenForm.status,
      };
      downloadFiles('promotion/luck/draw/exchange/export', screenData);
    },

    // 导出兑换码
    exportCode(id) {
      downloadFiles('promotion/luck/draw/code/export', {promotionLuckDrawId: id});
    },

    
  }
}
</script>

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