<template>
  <section class="dm-wrap" v-loading="loading">
    <div class="pb22 clearfix">
      <span>创建时间</span>
      <el-date-picker class="w400 ml10" v-model="dateTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" @change="refresh"></el-date-picker>
      <span class="ml20">发送时间</span>
      <el-date-picker class="w400 ml10" v-model="dateTime2" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" @change="refresh"></el-date-picker>
      <el-button class="fr" type="primary" v-if="$getButtonLimit($buttonCode.marketingMessageImport)" :limit-code="$buttonCode.marketingMessageImport" @click="$router.push('/message/import-send/add')">导入群发</el-button>
    </div>
    <div class="pb22 clearfix">
      <el-select class="dm-select w160" v-model="listParams.sendStatus" clearable placeholder="所有状态" @change="refresh">
        <el-option v-for="(item, index) in status" :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>
    </div>
    <el-table tooltipEffect="light" :data="tableList" style="width: 100%">
      <el-table-column :show-overflow-tooltip="true" min-width="120" align="left" prop="smsTitle" label="模板名称"></el-table-column>
      <el-table-column :show-overflow-tooltip="true" 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" width="100" align="left" prop="createTime" label="发送时间">
        <template slot-scope="scope">
          <p class="cell-time">
            {{ formatDateTimeByType(scope.row.sendTime, 'yyyy-MM-dd-HH-mm', true).y }}<br />
            <span>{{ formatDateTimeByType(scope.row.sendTime, 'yyyy-MM-dd-HH-mm', true).h }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" min-width="100" align="left" prop="creatorName" label="创建人"></el-table-column>
      <el-table-column :show-overflow-tooltip="true" min-width="170" align="left" prop="smsContent" label="模板内容"></el-table-column>
      <el-table-column min-width="120" align="left" prop="importCount" label="计划人数"></el-table-column>
      <el-table-column min-width="120" align="left" prop="sendCount" label="送达人数"></el-table-column>
      <el-table-column min-width="120" align="left" prop="totalFeeNum" label="计费条数"></el-table-column>
      <el-table-column min-width="120" align="left" label="状态">
        <template slot-scope="scope">
          <span v-if="scope.row.sendStatus === 5"><span class="dm-status--primary--flash"></span>执行中</span>
          <span v-else-if="filterStatus(scope.row.sendStatus)" :class="'dm-status--' + filterStatus(scope.row.sendStatus).type">
            {{ filterStatus(scope.row.sendStatus).label }}
            <el-tooltip v-if="scope.row.sendStatus == 2 && scope.row.failReason" :content="scope.row.failReason" placement="top">
              <i class="el-icon-question" style="color:#c0c4cc;cursor:pointer;margin-left:3px;font-size:14px"></i>
            </el-tooltip>
          </span>
          <time-counts v-if="scope.row.sendStatus == 0" class="primary-color fz12" :defaultVal="scope.row.sendTime" :autoStart="true"></time-counts>
        </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(`/message/import-send/info/${scope.row.smsImportPlanId}`)">详情</el-button>
          <dm-delete v-if="scope.row.sendStatus == 0" @confirm="invalidData(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>
    <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 { pageSmsImportPlan, stopSmsImportPlanTimer } from '@/service/api/messageApi.js';
import { formatDateTimeByType } from '@/utils/index.js';
import talbeMethods from '@/mixins/tableMethods.js';
import filterAvater from '@/mixins/filterAvater.js';
import timeCounts from '@/components/timeCount/index.vue';

export default {
  name: 'destory-list',
  mixins: [talbeMethods, filterAvater],
  data() {
    return {
      formatDateTimeByType,
      dateTime: '',
      dateTime2: '',
      listParams: {
        createBeginTime: '',
        createEndTime: '',
        sendBeginTime: '',
        sendEndTime: '',
        search: '',
        sendStatus: '',
        currentPage: 1,
        pageSize: 20
      },
      // 发送状态（-1: 已失效; 0：定时执行，1：执行完毕，2：执行失败,3:无可用模板;4.执行中）
      status: [{ value: 0, label: '定时执行', type: 'warning' }, { value: 1, label: '已执行', type: 'success' }, { value: 2, label: '执行失败', type: 'error' }, { value: 5, label: '执行中', type: 'primary--flash' }, { value: -1, label: '已失效', type: 'error' }], // 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: {
    timeCounts
  },
  methods: {
    invalidData(row) {
      stopSmsImportPlanTimer({ smsImportPlanId: row.smsImportPlanId })
        .then(res => {
          this.$tips({ type: 'success', message: '禁用成功!' });
          this.getTableList();
        })
        .catch(err => {
          this.$tips({ type: 'error', message: '禁用失败!' });
        });
    },
    // 列表状态过滤器
    // -1: 已失效; 0：定时执行，1：执行完毕，2：执行失败,3:无可用模板;4.执行中
    filterStatus(val) {
      let result = null;
      this.status.forEach(v => {
        if (val === v.value) result = v;
      });
      return result;
    },
    // 加载列表
    async getTableList() {
      this.loading = true;
      this.dataTimeFormat();
      try {
        let res = await pageSmsImportPlan(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.createBeginTime = formatDateTimeByType(this.dateTime[0], 'yyyy-MM-dd');
        this.listParams.createEndTime = formatDateTimeByType(this.dateTime[1], 'yyyy-MM-dd');
      } else {
        this.listParams.createBeginTime = '';
        this.listParams.createEndTime = '';
      }
      if (this.dateTime2) {
        this.listParams.sendBeginTime = formatDateTimeByType(this.dateTime2[0], 'yyyy-MM-dd');
        this.listParams.sendEndTime = formatDateTimeByType(this.dateTime2[1], 'yyyy-MM-dd');
      } else {
        this.listParams.sendBeginTime = '';
        this.listParams.sendEndTime = '';
      }
    }
  }
};
</script>
