<template>
  <section class="dm-wrap" v-loading="loading">
    <div class="pb22 clearfix">
      <el-date-picker class="w400" v-model="dateTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" @change="refresh"></el-date-picker>
      <activity-select class="vertical-middle w150 mr5" :actId.sync="listParams.marketingActivityId" onlySelect @actUpdate="refresh"></activity-select>
      <el-input v-model="listParams.search" class="w300" placeholder="请输入卡券包名称" clearable @change="refresh"><i slot="prefix" class="el-input__icon el-icon-search"></i></el-input>
      <el-button class="fr" type="primary" @click="$router.push('/card/merge/add')">新建卡券包</el-button>
    </div>
    <el-table tooltipEffect="light" :data="tableList" style="width: 100%">
      <el-table-column :show-overflow-tooltip="true" min-width="140" align="left" prop="name" label="卡券包名称"></el-table-column>
      <el-table-column :show-overflow-tooltip="true" min-width="140" align="left" prop="createTime" label="有效期">
        <template slot-scope="scope">
          <span v-if="scope.row.effectiveMode">
            {{ formatDateTimeByType(scope.row.beginDate, 'yyyy-MM-dd') }}
            ~<br />
            {{ formatDateTimeByType(scope.row.endDate, 'yyyy-MM-dd') }}
          </span>
          <span v-else>不限</span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" min-width="140" align="left" prop="cardSize" label="卡券数量"></el-table-column>
      <el-table-column :show-overflow-tooltip="true" min-width="140" align="left" prop="createTime" label="创建时间">
        <template slot-scope="scope">
          <p class="cell-time">
            {{ formatDateTimeByType(scope.row.createTime, 'yyyy-MM-dd-HH-mm', true).y }}<br />
            <span>{{ formatDateTimeByType(scope.row.createTime, 'yyyy-MM-dd-HH-mm', true).h }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" min-width="120" align="left" prop="marketingActivityName" label="营销活动">
        <template slot-scope="scope">{{ scope.row.marketingActivityName || '--' }}</template>
      </el-table-column>
      <el-table-column label="操作" align="left" width="120px" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" @click="$router.push(`/card/merge/edit/${scope.row.cardPackageId}`)">修改</el-button>
          <dm-delete @confirm="del(scope.row)" tips="是否删除该卡券包？">
            <el-button type="text">删除</el-button>
          </dm-delete>
        </template>
      </el-table-column>
    </el-table>
    <dm-pagination v-show="tableList.length" background class="dm-pagination" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listParams.currentPage" :page-sizes="[20, 40, 60, 80]" :page-size="listParams.pageSize" layout="total, sizes, prev, pager, next" :total="total"></dm-pagination>
  </section>
</template>
<script>
import activitySelect from '@/components/activity-select';
import { pageCoupPackage, deleteCoupPackage } from '@/service/api/cardApi.js';
import { formatDateTimeByType } from '@/utils/index.js';
import talbeMethods from '@/mixins/tableMethods.js';
import filterAvater from '@/mixins/filterAvater.js';

export default {
  name: 'destory-list',
  mixins: [talbeMethods, filterAvater],
  data() {
    return {
      formatDateTimeByType,
      visible: false,
      dateTime: [],
      listParams: {
        marketingActivityId: '',
        beginTime: '',
        endTime: '',
        search: '',
        currentPage: 1,
        pageSize: 20
      },
      total: 0,
      loading: false,
      tableList: [],
      projectName: 'marketing', // 当前项目名
      dialogVisible: false,
      excelUrl: '', // 下载数据的地址
      params: {} // 传递的参数
    };
  },
  components: {
    activitySelect
  },
  created() {
    this.getTableList();
    this.$store.commit('mutations_breadcrumb', [{ name: '营销管理', path: '' }, { name: '卡券营销', path: '' }, { name: '卡券包', path: '' }, { name: '卡券包列表', path: '' }]); // eslint-disable-line
  },
  methods: {
    del({ cardPackageId }) {
      this.loading = true;
      deleteCoupPackage({ cardPackageId })
        .then(() => {
          this.loading = false;
          this.$tips({ type: 'success', message: '删除成功' });
          this.getTableList();
        })
        .catch(err => {
          this.$tips({ type: 'error', message: err.message || '操作失败' });
          this.loading = false;
        });
    },
    // 加载列表
    async getTableList() {
      this.loading = true;
      this.dataTimeFormat();
      try {
        let res = await pageCoupPackage(this.listParams);
        this.tableList = res.result.result || [];
        this.total = res.result.totalCount || 0;
      } catch (err) {
        this.$tips({ type: 'error', message: '加载列表失败' });
      }
      this.loading = false;
    },
    /**-------辅助方法---------- */
    dataTimeFormat() {
      if (this.dateTime) {
        this.listParams.beginTime = formatDateTimeByType(this.dateTime[0], 'yyyy-MM-dd');
        this.listParams.endTime = formatDateTimeByType(this.dateTime[1], 'yyyy-MM-dd');
      } else {
        this.listParams.beginTime = '';
        this.listParams.endTime = '';
      }
    }
  }
};
</script>
