<template>
  <section class="dm-wrap" v-loading="loading">
    <div class="pb22 clearfix">
      <el-date-picker class="w400 ml10" v-model="dateTime" type="daterange" range-separator="至" start-placeholder="创建开始时间" end-placeholder="创建结束时间" @change="search"></el-date-picker>
      <el-input v-model="listParams.searchName" class="w200" placeholder="输入计划名称" clearable @change="search"><i slot="prefix" class="el-input__icon el-icon-search"></i></el-input>
      <el-checkbox class="vertical-middle" v-if="$store.state.marketing.isShowSelf" v-model="listParams.showSelfFlag" :true-label="1" :false-label="0" label="仅看本人" border @change="search" />
    </div>
    <el-table tooltipEffect="light" :data="tableList" style="width: 100%" element-loading-text="拼命加载中">
      <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-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 min-width="100" align="left" prop="creatorName" label="创建人" v-if="$store.state.marketing.openFlag"></el-table-column>
      <el-table-column label="操作" align="left" width="130" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" @click="toRecord(scope.row)">记录</el-button>
          <template v-if="scope.row.canEdit !== false">
            <el-button type="text" v-if="scope.row.effectType == 2" @click="$router.push(`/scan/info/${scope.row.ecmPlanId}`)">详情</el-button>
          </template>
          <dm-delete v-if="scope.row.putonStatus !== 2 && scope.row.canEdit !== false" @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 { formatDateTimeByType } from '@/utils/index.js';
import { loadEcmList, deleteEcm } from '@/service/api/ecmApi.js';
export default {
  name: 'scan-list',
  data() {
    return {
      dateTime: [],
      listParams: {
        beginTime: '',
        endTime: '',
        onlineStatus: 2, // onlineStatus字段：不填为有效记录 2：历史记录
        searchName: '',
        currentPage: 1,
        pageSize: 20,
        showSelfFlag: '',
        effectAction: 'scanning' // 新增字段：effectAction 当effectAction=scanning时 按level_sort倒序
      },
      total: 0,
      loading: false,
      tableHeader: [
        { label: '计划名称', prop: 'ecmPlanName', minWidth: '130', align: 'left' },
        {
          label: '创建时间',
          prop: 'createTime',
          minWidth: '140',
          align: 'left',
          formatter(row) {
            const date = formatDateTimeByType(row.createTime, 'yyyy-MM-dd-HH-mm-ss', true);
            return date.y + '\n' + date.h;
          }
        },
        {
          label: '状态',
          minWidth: '100',
          align: 'left',
          formatter(row) {
            return '<div v-else class="dm-status--info">已下线</div>';
          }
        },
        {
          label: '执行时间',
          prop: 'createTime',
          minWidth: '140',
          align: 'left',
          formatter(row) {
            const date = formatDateTimeByType(row.beginDate, 'yyyy-MM-dd-HH-mm-ss') + ' ~ <br />' + formatDateTimeByType(row.endDate, 'yyyy-MM-dd-HH-mm-ss');
            return date;
          }
        },
        { label: '合计营销次数', prop: 'timesForPeople', width: '160', align: 'left' }
      ],
      tableList: []
    };
  },
  created() {
    this.getTableList();
  },
  methods: {
    search() {
      this.listParams.currentPage = 1;
      this.getTableList();
    },
    handleSizeChange(val) {
      this.listParams.pageSize = val;
      this.getTableList();
    },
    handleCurrentChange(val) {
      this.listParams.currentPage = val;
      this.getTableList();
    },
    async getTableList() {
      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 = '';
      }
      const params = { ...this.listParams };
      if (params.effectType === '') {
        params.effectType = -1; // 全部时效的默认值是-1
      }
      this.loading = true;
      try {
        let res = await loadEcmList(params);
        this.tableList = res.result.result || [];
        this.total = res.result.totalCount;
        this.loading = false;
      } catch (error) {
        this.loading = false;
      }
    },
    // 记录
    toRecord(row) {
      this.$router.push({ path: `/scan/currentlist/${row.ecmPlanId}`, query: { name: row.ecmPlanName, isHistory: 1 } });
    },
    // 删除
    async delData(row) {
      try {
        let res = await deleteEcm({ ecmPlanId: row.ecmPlanId });
        if (res.errorCode === 0) {
          this.$tips({ type: 'success', message: '删除成功!' });
          if (this.tableList.length === 1 && this.listParams.currentPage !== 1) {
            this.listParams.currentPage--;
          }
          this.getTableList();
        } else {
          this.$tips({ type: 'error', message: res.message || '删除失败!' });
        }
      } catch (err) {
        this.$tips({ type: 'error', message: '删除失败!' });
      }
    }
  }
};
</script>
