<template>
  <section class="dm-wrap">
    <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" @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%" v-loading="loading">
      <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="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-html="filterStatus(scope.row)"></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>
        </template>
      </el-table-column>
    </el-table>
    <el-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"></el-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 } 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: -1, label: '已失效' }, { value: 0, label: '定时执行' }, { value: 1, label: '执行完毕' }, { value: 2, label: '执行失败' }, { value: 3, label: '无可用模板' }, { value: 5, 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: {
    timeCounts
  },
  methods: {
    // 列表状态过滤器
    // -1: 已失效; 0：定时执行，1：执行完毕，2：执行失败,3:无可用模板;4.执行中
    filterStatus(row) {
      let _content = '';
      if (row.sendStatus == -1) {
        _content = `<div class="dm-status--info">已失效</div>`;
      } else if (row.sendStatus == 0) {
        _content = `<div class="dm-status--warning">定时执行</div>`;
      } else if (row.sendStatus == 1) {
        _content = `<div class="dm-status--success">执行完毕</div>`;
      } else if (row.sendStatus == 2) {
        _content = `<div class="dm-status--default">执行失败</div>`;
      } else if (row.sendStatus == 3) {
        _content = `<div class="dm-status--default">无可用模板</div>`;
      } else if (row.sendStatus == 4) {
        _content = `<div class="dm-status--primary">执行中</div>`;
      }
      return _content;
    },
    // 加载列表
    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>
