<template>
  <section class="dm-wrap" v-loading="loading">
    <div class="page-tip">历史数据限制：2018年11月15日之前的历史营销计划，没有详细数据</div>
    <div class="pb22 clearfix">
      <el-date-picker class="w250" v-model="dateTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" @change="search"></el-date-picker>
      <el-select class="dm-select" clearable v-model="listParams.type" placeholder="选择上线状态" @change="search">
        <el-option v-for="(v, i) in showTypeOptions" :key="i" :label="v.label" :value="v.value"></el-option>
      </el-select>
      <el-input v-model="listParams.title" class="w200" placeholder="输入营销内容标题" clearable @change="search"><i slot="prefix" class="el-input__icon el-icon-search"></i></el-input>
    </div>
    <el-table tooltipEffect="light" :data="tableList" style="width: 100%" element-loading-text="拼命加载中">
      <el-table-column min-width="120" label="营销内容">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" open-delay="300" placement="top-start">
            <div style="max-width:400px;" slot="content">
              <pre>{{ scope.row.title }}</pre>
            </div>
            <div class="ellipsis">{{ scope.row.title }}</div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column v-for="(v, i) in tableHeader" :fixed="v.fixed" :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="header" slot-scope="scope">
          <span>
            <span>{{ v.label }}</span>
            <el-tooltip v-if="v.prop == 'successCount'" placement="top" :open-delay="200">
              <i class="iconfont icon-xinxixianshi" style="color:#909399;font-size:14px"></i>
              <div slot="content" style="line-height: 18px">
                <div style="line-height: 18px">
                  指的是GIC系统成功提交针对这些用户的营销行为，并不是指的实际触达成功，如：<br />
                  成功通过服务号接口进行消息群发；<br />
                  成功向短信运营商提交需要发送短信的用户名单；<br />
                  成功通过企微群发接口下发群发任务；<br />
                  成功通过话务任务接口下发话务任务；<br />
                </div>
              </div>
            </el-tooltip>
          </span>
        </template>
        <template slot-scope="scope">
          <span v-if="v.formatter" v-html="v.formatter(scope.row)"></span>
          <component v-else-if="v.component" :is="v.component"></component>
          <span v-else>{{ scope.row[v.prop] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="批次时间" align="left" min-width="120" prop="execTime">
        <template slot-scope="scope">
          <p class="cell-time">
            {{ formatDateTimeByType(scope.row.execTime, 'yyyy-MM-dd-HH-mm-ss', true).y }}<br />
            <span>{{ formatDateTimeByType(scope.row.execTime, 'yyyy-MM-dd-HH-mm-ss', true).h }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="left" min-width="120" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" @click="toDialog(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>
    <batch-dialog :show.sync="dialogShow" :id="dialogId" :execTime="dialogExecTime" :ecmPlanName="dialogEcmPlanName"></batch-dialog>
  </section>
</template>
<script>
import { ecmBatchSendInfos, getUseStoredFalg, getXsxsFalg } from '@/service/api/ecmApi.js';
import { formatDateTimeByType } from '@/utils/index.js';
import batchDialog from './batch-dialog';
import { ecmTypes } from '@/config/ecm.js';

export default {
  name: 'ecm-batch-list',
  components: {
    'batch-dialog': batchDialog
  },
  data() {
    let that = this;
    return {
      dateTime: [Date.now() - 30 * 24 * 60 * 60 * 1000, Date.now()],
      typeOptions: [{ value: '', label: '所有营销方式', visible: true }, ...JSON.parse(JSON.stringify(ecmTypes))], // eslint-disable-line
      listParams: {
        ecmPlanId: this.$route.params.id,
        beginTime: '',
        endTime: '',
        type: '',
        title: '',
        currentPage: 1,
        pageSize: 20
      },
      total: 0,
      loading: false,
      tableHeader: [
        // {
        //   label: '批次时间',
        //   prop: 'execTime',
        //   minWidth: '150',
        //   align: 'left',
        //   fixed: 'left',
        //   formatter(row) {
        //     const date = formatDateTimeByType(row.execTime, 'yyyy-MM-dd-HH-mm', true);
        //     return date.y + '\n' + date.h;
        //     // return formatDateTimeByType(row.execTime, 'yyyy-MM-dd-HH-mm');
        //   }
        // },
        // {
        //   label: '营销内容',
        //   prop: 'title',
        //   minWidth: '120',
        //   align: 'left',
        //   formatter(row) {
        //     return row.title || '--';
        //   }
        // },
        {
          label: '营销方式',
          prop: 'marketingType',
          minWidth: '140',
          align: 'left',
          formatter(row) {
            let result = '--';
            that.typeOptions.map(v => {
              if (v.value === row.marketingType) {
                result = v.label;
              }
            });
            return result;
          }
        },
        // { label: '批次人次', prop: 'totalCount', width: '160', align: 'left' },
        { label: '应发人次', prop: 'filterCount', minWidth: '160', align: 'left' },
        { label: '提交成功人次', prop: 'successCount', minWidth: '160', align: 'left' }
      ],
      tableList: [],
      dialogShow: false,
      dialogId: '',
      dialogExecTime: '',
      dialogEcmPlanName: ''
    };
  },
  created() {
    getXsxsFalg().then(res => {
      if (res.result) {
        // this.typeOptions.find(el => el.value == 'qywx').visible = true;
        this.typeOptions.find(el => el.value == 'qfxx').visible = true;
        this.typeOptions.find(el => el.value == 'teltask-fail').visible = true;
        this.typeOptions.find(el => el.value == 'message-fail').visible = true;
      }
    });
    getUseStoredFalg().then(res => {
      if (res.result.useStoredFlag) {
        this.typeOptions.find(el => el.value == 'grade').visible = true;
      }
    });
    this.ecmBatchSendInfos();
  },
  computed: {
    showTypeOptions() {
      return this.typeOptions.filter(el => el.visible == true);
    }
  },
  methods: {
    formatDateTimeByType,
    search() {
      this.listParams.currentPage = 1;
      this.ecmBatchSendInfos();
    },
    handleSizeChange(val) {
      this.listParams.pageSize = val;
      this.ecmBatchSendInfos();
    },
    handleCurrentChange(val) {
      this.listParams.currentPage = val;
      this.ecmBatchSendInfos();
    },
    async ecmBatchSendInfos() {
      this.loading = true;
      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 = '';
      }
      let res = await ecmBatchSendInfos(this.listParams);
      console.log(res);
      this.tableList = res.result.result || [];
      this.total = res.result.totalCount;
      this.loading = false;
    },
    //批次人数
    toDialog(row) {
      this.dialogShow = true;
      this.dialogId = row.ecmPlanQuartzLogId;
      this.dialogEcmPlanName = this.$route.query.name;
      this.dialogExecTime = formatDateTimeByType(row.execTime, 'yyyy-MM-dd-HH-mm-ss');
      console.log(this.dialogId);
    }
  }
};
</script>
