<template>
  <section class="dm-wrap" v-loading="loading">
    <div class="clearfix pb22">
      <el-date-picker class="w400" v-model="dateTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" @change="refresh"></el-date-picker>
      <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>
    </div>
    <el-table tooltipEffect="light" :data="tableList" style="width: 100%">
      <el-table-column v-for="(v, i) in tableHeader" :show-overflow-tooltip="v.tooltip" :width="v.width" :min-width="v.minWidth" :align="v.align" :key="i" :prop="v.prop" :label="v.label" :formatter="v.formatter">
        <template slot-scope="scope">
          <span v-if="v.formatter" v-html="v.formatter(scope.row)"></span>
          <span v-else>{{ scope.row[v.prop] || '--' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="left" width="190px" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" @click="$router.push(`/activity/data-view/${scope.row.marketingActivityId}`)">历史数据概览</el-button>
          <!-- <el-button type="text" @click="$router.push(`/activity/edit/${scope.row.marketingActivityId}`)">编辑</el-button> -->
          <dm-delete @confirm="delData(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 talbeMethods from '@/mixins/tableMethods.js';
import { activityList, delActivity } from '@/service/api/activityApi.js';
import { formatDateTimeByType } from '@/utils/index.js';
// import { enableAccessControl, confirmInfo } from '@/utils/auth.js';
export default {
  name: 'activity-list',
  data() {
    return {
      loading: false,
      tableList: [],
      dateTime: '',
      tableHeader: [
        { label: '活动名称', prop: 'name', minWidth: '150', tooltip: true, align: 'left' },
        {
          label: '创建时间',
          minWidth: '100',
          align: 'left',
          formatter: function(row) {
            return `<p class="cell-time">
                      ${formatDateTimeByType(row.createTime, 'yyyy-MM-dd-HH-mm-ss', true).y}<br />
                      <span>${formatDateTimeByType(row.createTime, 'yyyy-MM-dd-HH-mm-ss', true).h}</span>
                    </p>`;
          }
        },
        {
          label: '活动时间',
          minWidth: '160',
          align: 'left',
          formatter: function(row) {
            return `<p class="cell-time">
                      ${formatDateTimeByType(row.beginDate, 'yyyy-MM-dd-HH-mm-ss')}<br />
                      <span>${formatDateTimeByType(row.endDate, 'yyyy-MM-dd-HH-mm-ss')}</span>
                    </p>`;
          }
        },
        { label: '创建人', prop: 'creatorName', minWidth: '120', align: 'left' },
        { label: '状态', prop: 'stateDesc', minWidth: '120', align: 'left' },
        { label: '合计营销人次', prop: 'activityMbrCntTd', minWidth: '120', tooltip: true, align: 'left' }
      ],
      listParams: {
        expireType: 1, // -1: 进行中/未开始 1: 已过期 0: 使用中
        beginTime: '',
        endTime: '',
        search: '', //否	String	输入发送人搜索
        currentPage: 1, //是	Number	当前页面
        pageSize: 20 //是	Number	每页显示条数
      },
      total: 0
    };
  },
  mixins: [talbeMethods],
  created() {
    this.$store.commit('aside_handler', false);
    this.$store.commit('mutations_breadcrumb', [{ name: '营销活动列表', path: '/activity/list' }, { name: '历史营销活动列表' }]); // eslint-disable-line
    this.getTableList();
  },
  methods: {
    async getTableList() {
      try {
        this.loading = true;
        this.dataTimeFormat();
        let res = await activityList(this.listParams);
        if (res.errorCode === 0 && res.result) {
          this.tableList = res.result.result || [];
          this.total = res.result.totalCount;
        } else {
          this.tableList = [];
          this.total = 0;
        }
      } catch (err) {}
      this.loading = false;
    },
    delData({ marketingActivityId }) {
      this.loading = true;
      delActivity({ marketingActivityId })
        .then(res => {
          this.loading = false;
          this.$tips({ type: 'success', message: '删除成功!' });
          this.getTableList();
        })
        .catch(err => {
          this.loading = false;
          this.$tips({ type: 'error', message: '删除失败!' });
        });
    },
    /**-------辅助方法---------- */
    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>
