<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>
      <el-select class="dm-select w160" v-model="listParams.destoryMode" clearable placeholder="所有销毁方式" @change="refresh">
        <el-option v-for="(item, index) in destoryType" :key="index" :label="item.label" :value="item.value"></el-option>
      </el-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="danger" @click="showDialog('add', {})">卡券销毁</el-button>
    </div>
    <el-table tooltipEffect="light" :data="tableList" style="width: 100%">
      <el-table-column :show-overflow-tooltip="true" min-width="120" align="left" prop="cardName" label="卡券名称"></el-table-column>
      <el-table-column :show-overflow-tooltip="true" min-width="100" 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="orderNumber" label="销毁方式">
        <template slot-scope="scope">
          <div>{{ scope.row.destoryMode ? '部分销毁' : '全部销毁' }}</div>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" min-width="80" align="left" prop="cardName" label="计划销毁数量">
        <template slot-scope="scope">
          <div>{{ scope.row.sucessCount + scope.row.failCount || '--' }}</div>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" min-width="80" align="left" prop="createTime" label="销毁结果">
        <template slot-scope="scope">
          <p>
            成功：<span style="color:#52C41A;">{{ scope.row.sucessCount }}</span>
          </p>
          <p>
            失败：<span style="color:#F5222D;">{{ scope.row.failCount }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="left" width="120px" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" @click="exportExcel(scope.row)">导出结果</el-button>
        </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>
    <destory-dialog :show.sync="dialog.show" v-bind="dialog" @refresh="getTableList" />
    <vue-gic-export-excel :dialogVisible.sync="dialogVisible" :dataArr="tableList" :type="2" :excelUrl="excelUrl" :params="params" :projectName="projectName"></vue-gic-export-excel>
  </section>
</template>
<script>
import { pageCoupDestoryPlan, exportDestoryPlanCode } from '@/service/api/cardApi.js';
import { formatDateTimeByType } from '@/utils/index.js';
import destoryDialog from './partials/destory-dialog';
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,
      dialog: {
        show: false,
        destoryItem: '',
        type: 'add'
      },
      dateTime: [],
      listParams: {
        beginTime: '',
        endTime: '',
        search: '',
        destoryMode: '',
        currentPage: 1,
        pageSize: 20
      },
      destoryType: [{ value: 0, label: '全部销毁' }, { value: 1, label: '部分销毁' }], // eslint-disable-line
      total: 0,
      loading: false,
      tableList: [],
      projectName: 'marketing', // 当前项目名
      dialogVisible: false,
      excelUrl: '', // 下载数据的地址
      params: {} // 传递的参数
    };
  },
  created() {
    this.getTableList();
    this.$store.commit('mutations_breadcrumb', [{ name: '营销管理', path: '' }, { name: '卡券营销', path: '' }, { name: '卡券销毁', path: '' }]); // eslint-disable-line
  },
  components: {
    destoryDialog
  },
  methods: {
    showDialog(type, destoryItem) {
      this.dialog.type = type;
      this.dialog.show = true;
      if (type !== 'add') {
        this.dialog.destoryItem = destoryItem;
      }
    },
    // 加载列表
    async getTableList() {
      this.loading = true;
      this.dataTimeFormat();
      try {
        let res = await pageCoupDestoryPlan(this.listParams);
        this.tableList = res.result.result || [];
        this.total = res.result.totalCount || 0;
      } catch (err) {
        this.$tips({ type: 'error', message: '加载列表失败' });
      }
      this.loading = false;
    },
    // 导出列表
    exportExcel({ destoryPlanId }) {
      this.params = { destoryPlanId };
      this.excelUrl = exportDestoryPlanCode;
      this.dialogVisible = true;
    },
    /**-------辅助方法---------- */
    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>
