<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-11-13 16:37:02
 * @LastEditors: 无尘
 * @LastEditTime: 2019-12-12 10:29:35
 -->
<!--
<in-progress-active></in-progress-active>

import inProgressActive from '@/components/active/in-progress-active.vue';
-->

<template>
  <div class="in-progress-active border-box">
    <ul>
      <template v-for="(item, index) in inProgressData">
        <li
          :class="['border-box', selectItem == item.activityId && index != 0 ? 'li-border' : '']"
          :key="index + 'data'"
        >
          <div
            class="in-progress-active-title flex flex-space-between border-box"
            @click.stop.prevent="selectLi(item)"
          >
            <div class="active-title-left">
              <div class="active-title-name font-18 color-303133 font-w-700">{{ item.activityName }}</div>
              <div class="active-title-date font-14 color-909399 m-t-7">{{ item.startDate | formatTimeYmdHms }} - {{ item.endDate | formatTimeYmdHms }}</div>
            </div>
            <div class="active-title-right">
              <el-button
                type="primary"
                plain
                @click.stop="toEdit(index, item)"
              >活动配置</el-button>
              <el-button
                data-clipboard-action="copy"
                :data-clipboard-text="item.activityUrl"
                class="copy-tag"
                type="primary"
                plain
                @click.stop="handleCopy(index, item)"
              >复制路径</el-button>
              <!--  -->

              <el-button
                type="primary"
                plain
                @click.stop="toDataList(index, item)"
              >数据明细</el-button>
            </div>
          </div>
          <div
            v-if="selectItem==item.activityId"
            class="in-progress-active-body"
          >
            <out-date-active
              :barChartData="barChartData"
              :funnelData="funnelData"
            ></out-date-active>
          </div>
        </li>
      </template>
    </ul>
  </div>
</template>
<script>
import outDateActive from '@/components/active/out-date-active.vue';
import Clipboard from 'clipboard';
import { _debounce } from '@/common/js/public';
import showMsg from '@/common/js/showmsg';
import errMsg from '@/common/js/error';
import { getRequest } from '@/api/api';
export default {
  name: 'in-progress-active',
  components: {
    outDateActive
  },
  data() {
    return {
      funnelData: {},
      barChartData: {
        columns: ['日期', '新增参与人数', '新增受邀人数'],
        rows: [{ 日期: '1', 新增参与人数: 1393, 新增受邀人数: 1093 }, { 日期: '2', 新增参与人数: 3530, 新增受邀人数: 3230}, { 日期: '3', 新增参与人数: 2923, 新增受邀人数: 2623}, { 日期: '4', 新增参与人数: 1723, 新增受邀人数: 1423}, { 日期: '5', 新增参与人数: 3192, 新增受邀人数: 3192}, { 日期: '6', 新增参与人数: 3593, 新增受邀人数: 3293}]
      },
      selectItem: '',
      inProgressData: [
        {
          activityId: 1,
          activityName: '',
          startDate: '',
          endDate: '',
          activityUrl: ''
        }
      ]
    };
  },
  methods: {
    /**
     * 选择 li
     */
    selectLi(item) {
      const that = this;
      that.selectItem = item.activityId;
      that.getActivityData(item.activityId);
    },
    /**
     * 获取单个活动数据信息
     * /sharing-core-web/get-activity-data-summary
     */
    getActivityData(activityId) {
      const that = this;
      let para = {
        activityId: activityId
      };
      getRequest('/sharing-core-web/get-activity-data-summary', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            that.funnelData = resData.result || {};
            if (!!resData.result.trend && !!resData.result.trend.length) {
              resData.result.trend.forEach(ele => {
                ele['日期'] = ele.date.split('-')[2];
                ele['新增参与人数'] = ele.inviteCount;
                ele['新增受邀人数'] = ele.invitedCount;
              })
            }
            that.barChartData =  {
              columns: ['日期', '新增参与人数', '新增受邀人数'],
              rows: resData.result.trend || []
            }
            return false;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     * 活动配置
     */
    toEdit(index, row) {
      const that = this;
      that.$router.push(`/createActive?activityId=${row.activityId}`)
    },
    /**
     * 数据明细
     */
    toDataList(index, row) {
      const that = this;
      that.$router.push(`/dataDetail?activityId=${row.activityId}`)
    },
    /**
     * 复制路径
     */
    /* eslint-disable */
    handleCopy: _debounce(function(index, row) {
      const that = this;
      const fakeElement = document.createElement('button')
      let clipboard = new Clipboard(fakeElement, {
        text: function() {
          return row.activityUrl || '您复制的数据是空的！';
        }
      });
      clipboard.on('success', e => {
        showMsg.showmsg('复制成功!', 'success');
        // 释放内存
        clipboard.destroy();
      });
      clipboard.on('error', e => {
        // 不支持复制
        showMsg.showmsg('该浏览器不支持自动复制!', 'warning');
        // 释放内存
        clipboard.destroy();
      });
      document.body.appendChild(fakeElement)
      fakeElement.click();
      document.body.removeChild(fakeElement)
    },300),

    /**
     * 获取列表数据
     */
    getTableList() {
      const that = this;
      let para = {type: 1};
      getRequest('/sharing-core-web/page-activity', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            /*const nowDate = new Date().getTime();
             let arr = [];
            if (resData.result.result && resData.result.result.length) {
              resData.result.result.forEach(ele => {
                if (ele.endDate > nowDate) {
                  arr.push(ele);
                }
              });
            } */
            that.inProgressData = resData.result.result || [];
            if (resData.result.result.length) {
              that.getActivityData(resData.result.result[0].activityId);
            }
            return false;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    }
  },
  mounted() {
    const that = this;
    that.getTableList();
  }
};
</script>
<style lang="less" scoped>
.m-t-7 {
  margin-top: 7px;
}
.in-progress-active {
  width: 100%;
  padding: 0 20px 48px 20px;
  background: #fff;
  li {
    // height: 90px;
    padding: 0 20px;
    cursor: pointer;
    &.li-border {
      padding: 0 20px;
      box-shadow: 0px 0px 8px 3px rgba(154, 170, 201, 0.11);
      border-radius: 4px;
      border: 1px solid rgba(220, 223, 230, 1);
      margin-top: -1px;
    }
    .in-progress-active-title {
      padding: 20px 0;
      border-bottom: 1px solid #dcdfe6;
    }
    .in-progress-active-body {
      padding: 20px 0;
    }
  }
}
</style>
