<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-03-20 14:36:37
 * @LastEditors: 无尘
 * @LastEditTime: 2020-08-17 16:19:09
 -->
<template>
  <div class="my-customer-wrap common-set-wrap">
    <nav-crumb :navpath="navpath"> </nav-crumb>
    <div class="right-content">
      <div class="right-box" style="min-height: calc(100vh - 104px);">
        <div class="apps-content flex" style="min-height: calc(100vh - 104px);">
          <div class="apps-content-right">
            <div class="flex flex-space-between">
              <div class="font-16 color-303133">
                <div class="flex">
                  <div :class="['font-14  font-w-600 cursor-pointer', tabActive == 1 ? 'color-303133' : 'color-909399']" @click="changeTab('1')">日访问趋势</div>
                  <div :class="['font-14 font-w-600 day-tend cursor-pointer', tabActive == 2 ? 'color-303133' : 'color-909399']" @click="changeTab('2')">日时段访问趋势</div>
                </div>
              </div>
              <div role="alert" class="el-alert el-alert-tip el-alert--info flex flex-align-start w-426">
                <i class="el-alert__icon el-icon-info font-12 color-2f54eb"></i>
                <div class="el-alert__content">
                  <span class="el-alert__title color-606266 font-14">数据更新截至于昨日。今日数据需要隔天上午8点后进行查看。</span>
                </div>
              </div>
            </div>
            <div v-if="tabActive == 2" class="date-div m-t-20">
              <el-date-picker v-model="selectTendDay" :value-format="'yyyy-MM-dd'" type="date" placeholder="选择日期" :clearable="false" :picker-options="pickerOptions" @change="changeDate"> </el-date-picker>
            </div>
            <div class="chart-body m-t-20">
              <div class="flex flex-space-between">
                <day-login-chart v-if="charData.length" class="flex-1" :charData="charData" id="day-login-chart"></day-login-chart>
                <div v-if="!charData.length" class="no-data-char">
                  <img src="../../assets/data/bar.png" alt="" />
                  <p class="font-12 color-c0c4cc m-t-20">- 暂无数据 -</p>
                </div>
                <time-login-chart v-if="charData.length" class="flex-1" :charData="charData" id="time-login-chart"></time-login-chart>
                <div v-if="!charData.length" class="no-data-char">
                  <img src="../../assets/data/bar.png" alt="" />
                  <p class="font-12 color-c0c4cc m-t-20">- 暂无数据 -</p>
                </div>
              </div>
            </div>
            <div class="m-t-20">
              <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
                <el-tab-pane label="登录人员" name="first"></el-tab-pane>
                <el-tab-pane label="未登录人员" name="second"></el-tab-pane>
              </el-tabs>
            </div>
            <div class="table-search">
              <div class="flex flex-space-between">
                <div>
                  <el-date-picker v-model="selectEndDay" :value-format="'yyyy-MM-dd'" type="date" placeholder="选择日期" :clearable="false" :picker-options="pickerOptions" @change="changeTableDate"> </el-date-picker><gic-select-group-mult class="m-l-10" :selectData="groupIds" @checkGroupIds="checkGroupIds"> </gic-select-group-mult><span class="font-14 color-606266 p-l-10">筛选条件下，{{ activeName == 'second' ? '未' : '' }}登录人员有 {{ total }} 人，名单如下：</span>
                </div>
                <el-button type="primary" :loading="loadingBtn" @click="exportExcel"><i class="iconfont icon-icon_yunxiazai p-r-6"></i>导出</el-button>
              </div>
            </div>
            <div class="m-t-20">
              <div v-if="activeName == 'first'">
                <el-table class="select-table" ref="multipleTable" :data="tableData" @sort-change="sortChangeLogin" tooltip-effect="dark" :style="{ width: '100%' }" @selection-change="handleSelectionChange">
                  <!-- <el-table-column type="selection" width="45"> </el-table-column>
                  <el-table-column width="25">
                    <template slot="header" slot-scope="scope">
                      <el-dropdown style="line-height: 10px; padding: 0; margin-left: -15px; transform: translateY(4px); -webkit-transform: translateY(4px);" @command="handleCommand" placement="bottom-start">
                        <span class="el-dropdown-link"><i class="iconfont icongengduo"></i> </span>
                        <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item command="0"><span :style="{ color: selectRadio == 0 ? '#2f54eb' : '#606266' }">选择当页</span></el-dropdown-item>
                          <el-dropdown-item command="1"><span :style="{ color: selectRadio == 1 ? '#2f54eb' : '#606266' }">选择全部</span></el-dropdown-item>
                        </el-dropdown-menu>
                      </el-dropdown>
                    </template>
                    <template slot-scope="scope"> </template>
                  </el-table-column> -->
                  <el-table-column prop="" label="员工信息" show-overflow-tooltip>
                    <template slot-scope="scope">
                      <div class="ul-li-cell">
                        <div class="cell-info">
                          <div class="cell-info-name font-14 color-303133 flex flex-column">
                            <span class="gic-name"
                              ><span>{{ scope.row.clerkName || '--' }}</span
                              ><el-tag v-if="scope.row.clerkType == 1" class="m-l-10" size="small" type="warning">店长</el-tag></span
                            ><span class="gic-version">{{ scope.row.clerkCode || '--' }}</span>
                          </div>
                        </div>
                      </div>
                    </template>
                  </el-table-column>
                  <el-table-column prop="" label="归属门店" show-overflow-tooltip>
                    <template slot-scope="scope">{{ scope.row.storeName || '--' }}</template>
                  </el-table-column>
                  <el-table-column prop="lastLoginTime" label="最后登录时间" sortable="custom" show-overflow-tooltip>
                    <template slot-scope="scope">
                      <div class="line-18">{{ scope.row.lastLoginTime | timeStampToYmd }} {{ !scope.row.lastLoginTime ? '--' : '' }}</div>
                      <div class="line-18">{{ scope.row.lastLoginTime | timeStampToHms }} {{ !scope.row.lastLoginTime ? '--' : '' }}</div>
                    </template>
                  </el-table-column>
                  <el-table-column prop="loginTimes" label="登录次数" sortable="custom" show-overflow-tooltip>
                    <template slot-scope="scope">
                      {{ scope.row.loginTimes || '0' }}
                    </template>
                  </el-table-column>
                  <el-table-column prop="avgVisitTime" label="平均访问时长" sortable="custom" show-overflow-tooltip>
                    <template slot-scope="scope"
                      >{{ scope.row.avgVisitTime | formatTime }}
                    </template>
                  </el-table-column>
                </el-table>
              </div>
              <!-- 未登录 -->
              <div v-else>
                <el-table class="select-table" ref="multipleTable" :data="tableData" @sort-change="sortChange" tooltip-effect="dark" :style="{ width: '100%' }" @selection-change="handleSelectionChange">
                  <!-- <el-table-column type="selection" width="45"> </el-table-column>
                  <el-table-column width="25">
                    <template slot="header" slot-scope="scope">
                      <el-dropdown style="line-height: 10px; padding: 0; margin-left: -15px; transform: translateY(4px); -webkit-transform: translateY(4px);" @command="handleCommand" placement="bottom-start">
                        <span class="el-dropdown-link"><i class="iconfont icongengduo"></i> </span>
                        <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item command="0"><span :style="{ color: selectRadio == 0 ? '#2f54eb' : '#606266' }">选择当页</span></el-dropdown-item>
                          <el-dropdown-item command="1"><span :style="{ color: selectRadio == 1 ? '#2f54eb' : '#606266' }">选择全部</span></el-dropdown-item>
                        </el-dropdown-menu>
                      </el-dropdown>
                    </template>
                    <template slot-scope="scope"> </template>
                  </el-table-column> -->
                  <el-table-column prop="" label="员工信息" show-overflow-tooltip>
                    <template slot-scope="scope">
                      <div class="ul-li-cell">
                        <div class="cell-info">
                          <div class="cell-info-name font-14 color-303133 flex flex-column">
                            <span class="gic-name"
                              ><span>{{ scope.row.clerkName || '--' }}</span
                              ><el-tag v-if="scope.row.clerkType == 1" class="m-l-10" size="small" type="warning">店长</el-tag></span
                            ><span class="gic-version">{{ scope.row.clerkCode || '--' }}</span>
                          </div>
                        </div>
                      </div>
                    </template>
                  </el-table-column>
                  <el-table-column prop="" label="归属门店" show-overflow-tooltip>
                    <template slot-scope="scope">{{ scope.row.storeName || '--' }}</template>
                  </el-table-column>
                  <el-table-column prop="lastLoginTime" label="上次登录时间" sortable="custom" show-overflow-tooltip>
                    <template slot-scope="scope">
                      <div class="line-18">{{ scope.row.lastLoginTime | timeStampToYmd }}{{ !scope.row.lastLoginTime ? '--' : '' }}</div>
                      <div class="line-18">{{ scope.row.lastLoginTime | timeStampToHms }}{{ !scope.row.lastLoginTime ? '--' : '' }}</div>
                    </template>
                  </el-table-column>
                </el-table>
              </div>
              <div class="block common-wrap__page text-right m-t-24" v-if="tableData.length != 0">
                <dm-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[20, 40, 60, 80]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </dm-pagination>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- <vue-gic-footer></vue-gic-footer> -->
  </div>
</template>
<script>
import navCrumb from '@/components/nav/nav.vue';
import gicSelectGroupMult from '@/components/login-data/gic-select-group-mult.vue';
import dayLoginChart from '@/components/login-data/day-login-chart.vue';
import timeLoginChart from '@/components/login-data/time-login-chart.vue';
import { getRequest, postRequest, postExcel } from '@/api/api';
import errMsg from '@/common/js/error';
// import { _debounce } from '@/common/js/public';
import dateFormat from '@/filters/index.js';
// import showMsg from '@/common/js/showmsg';
export default {
  name: 'activeDetail',
  data() {
    return {
      // 面包屑参数
      navpath: [
        {
          name: '好办使用详情',
          path: '/enterpriseUseData?brandId=' + this.$route.query.brandId
        },
        {
          name: '登录人员明细',
          path: ''
        }
      ],
      selectTendDay: '',
      activeName: 'first',
      selectStartDay: '',
      endDay: '',
      selectEndDay: '', // 默认当天之前七天时间
      pickerOptions: {
        disabledDate(time) {
          const one = 8 * 24 * 3600 * 1000;
          const seven = Date.now() - one;
          return time.getTime() >= Date.now() - 8.64e7 || time.getTime() < seven;
        }
      },
      groupIds: [],
      charData: [], //[{ bizDates: '10:10', call: 4, loginClerkCnt: 2, avgLoginTimes: 1, avgVisitTime: 2 }, { bizDates: '10:15', call: 2, loginClerkCnt: 6, avgLoginTimes: 15, avgVisitTime: 3 }, { bizDates: '10:20', call: 13, loginClerkCnt: 2, avgLoginTimes: 10, avgVisitTime: 5 }, { bizDates: '10:25', call: 9, loginClerkCnt: 9, avgLoginTimes: 6, avgVisitTime: 1 }, { bizDates: '10:30', call: 5, loginClerkCnt: 2, avgLoginTimes: 16, avgVisitTime: 3 }, { bizDates: '10:35', call: 8, loginClerkCnt: 2, avgLoginTimes: 6, avgVisitTime: 1 }, { bizDates: '10:40', call: 13, loginClerkCnt: 1, avgLoginTimes: 19, avgVisitTime: 2 }],

      // 分页参数
      currentPage: 1,
      pageSize: 20,
      total: 0,
      tableData: [],
      multipleSelection: [],
      sortColumn: '',
      sortType: '',
      addShow: false,
      loadingBtn: false,
      selectRadio: 0,
      // tab
      tabActive: '1',
      storeId: '' // 查看人员名单用到
    };
  },
  computed: {},
  methods: {
    /**
     * 切换列表日
     */
    changeTableDate() {
      const that = this;
      // 重新获取数据
      that.getTableList();
    },
    /**
     * 切换日
     */
    changeDate() {
      const that = this;
      // 重新获取数据
      that.getTendData();
    },
    /**
     * 切换tab
     */
    changeTab(val) {
      const that = this;
      that.tabActive = val;
      // 重新获取数据
      that.getTendData();
    },
    /**
     * 选择分组
     */
    checkGroupIds: function(nodes) {
      const that = this;
      that.groupIds = nodes;
      that.storeId = '';
      that.getTableList();
    },
    /**
     * tab
     */
    handleClick(tab, event) {
      const that = this;
      that.currentPage = 1;
      that.pageSize = 20;
      that.total = 0;
      that.selectRadio = 0;
      that.tableData = [];
      that.getTableList();
    },
    /**
     * 排序
     */
    sortChangeLogin(column, prop, order) {
      const that = this;
      that.sortColumn = column.prop;
      that.sortType = column.order == 'ascending' ? 'asc' : 'desc';
      that.getTableList();
    },
    sortChange(column, prop, order) {
      const that = this;
      that.sortColumn = column.prop;
      that.sortType = column.order == 'ascending' ? 'asc' : 'desc';
      that.getTableList();
    },
    /**
     * 显示选择本页/全部
     */
    handleCommand(command) {
      this.selectRadio = command;
    },
    /**
     * 表格选择
     */
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    /**
     * 导出
     */
    exportExcel() {
      const that = this;
      /* if (!that.multipleSelection.length) {
        showMsg.showmsg('请选择人员', 'warning');
        return false;
      } */
      that.loadingBtn = true;
      that.postExcelData();
    },
    postExcelData() {
      const that = this;
      let para = {
        enterpriseId: that.$route.query.brandId,
        startDate: that.selectEndDay,
        endDate: that.selectEndDay,
        groupIdList: that.groupIds.map(ele => ele.storeGroupId).join(','),
        storeIdList: that.storeId,
        loginStatus: that.activeName == 'first' ? 1 : '0',
        selectType: that.selectRadio
        //taskIds: that.selectRadio == 1 ? '' : that.multipleSelection.map(ele => ele.taskId).join(',')
      };
      postExcel('/haoban-manage3-web/web/export-login-detail-list', para)
        .then(res => {
          that.loadingBtn = false;
          if (!res.data) {
            errMsg.errorMsg('暂无数据');
            return false;
          }
          // let resData = res.data;
          const blob = new Blob([res.data], {
            type: 'application/vnd.ms-excel'
          });
          const fileName = `${that.activeName == 'first' ? '登录明细' : '未登录明细'}报表.csv`;
          const elink = document.createElement('a');
          elink.download = fileName;
          elink.style.display = 'none';
          elink.href = URL.createObjectURL(blob);
          document.body.appendChild(elink);
          elink.click();
          URL.revokeObjectURL(elink.href); // 释放URL 对象
          document.body.removeChild(elink);
          // errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.loadingBtn = false;
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     * 路由跳转
     *  @param {String} path
     */
    changeRoute(path) {
      this.$router.push(path);
    },

    /**
     * 分页---页码变化
     *  @param {Number} val
     */
    handleSizeChange(val) {
      const that = this;
      that.currentPage = 1;
      that.pageSize = val;
      that.getTableList();
    },

    /**
     * 分页---当前页变化
     *  @param {Number} val
     */
    handleCurrentChange(val) {
      const that = this;
      that.currentPage = val;
      that.getTableList();
    },
    /**
     * 获取访问趋势数据
     */
    getTendData(val) {
      const that = this;
      let para = {
        enterpriseId: that.$route.query.brandId,
        datesType: that.tabActive,
        startDate: that.tabActive == 1 ? that.selectStartDay : that.selectTendDay,
        endDate: that.tabActive == 1 ? that.endDay : that.selectTendDay
      };
      getRequest('/haoban-manage3-web/web/total-login-data', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            if (resData.result.result) {
              resData.result.result.forEach(ele => {
                ele.bizDates = that.tabActive == 1 ? ele.bizDates.split('-')[1] + '-' + ele.bizDates.split('-')[2] : ele.bizDates;
                ele.avgLoginTimes = parseInt(ele.avgLoginTimes);
                ele.avgVisitTime = parseInt(ele.avgVisitTime);
              });
            }
            that.charData = resData.result.result || [];
            return false;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     * 获取列表数据
     */
    getTableList(val) {
      const that = this;
      let para = {
        enterpriseId: that.$route.query.brandId,
        startDate: that.selectEndDay,
        endDate: that.selectEndDay,
        groupIdList: that.groupIds.map(ele => ele.storeGroupId).join(','),
        storeIdList: that.storeId,
        loginStatus: that.activeName == 'first' ? 1 : '0',
        sortColumn: that.sortColumn,
        sortType: that.sortType,
        pageSize: that.pageSize,
        currentPage: that.currentPage
      };
      postRequest('/haoban-manage3-web/web/login-detail-list', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            that.tableData = resData.result.result.list || [];
            that.total = resData.result.result.total || 0;
            return false;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    }
  },
  watch: {
    activeBrand: function(newData, oldData) {
      const that = this;
      that.activeBrand = newData;
    },
    activeGroup: function(newData, oldData) {
      const that = this;
      that.activeGroup = newData;
    }
  },
  mounted() {
    const that = this;
    that.selectStartDay = dateFormat.timeStampToYmd(new Date().getTime() - 7 * 24 * 3600 * 1000);
    that.endDay = dateFormat.timeStampToYmd(new Date().getTime() - 24 * 3600 * 1000);
    that.selectEndDay = !!that.$route.query.selectTableDay ? that.$route.query.selectTableDay : dateFormat.timeStampToYmd(new Date().getTime() - 24 * 3600 * 1000);
    that.selectTendDay = dateFormat.timeStampToYmd(new Date().getTime() - 24 * 3600 * 1000);
    that.storeId = !!that.$route.query.storeId ? that.$route.query.storeId : '';
    that.$nextTick(() => {
      that.getTableList();
      that.getTendData();
    });
    document.documentElement.style.backgroundColor = '#f0f2f5';
  },
  destroyed() {
    document.documentElement.style.backgroundColor = '#fff';
  },
  components: {
    navCrumb,
    gicSelectGroupMult,
    dayLoginChart,
    timeLoginChart
  }
};
</script>
<style type="text/scss" lang="scss" scoped>
.m-wh-40 {
  max-width: 40px;
  max-height: 40px;
}
.span-dot {
  display: inline-block;
  vertical-align: middle;
  width: 6px;
  height: 6px;
  margin-right: 6px;
  border-radius: 3px;
  &.bg-52c41a {
    background: #52c41a;
  }
  &.bg-f5222d {
    background: #f5222d;
  }
}
.bg-82C5FF {
  background: #82c5ff;
}
.color-508CEE {
  color: #508cee;
}
.color-FF585C {
  color: #ff585c;
}
.line-h-18 {
  line-height: 18px;
}
.tooltip-text {
  width: 100%;
  white-space: pre-wrap;
  word-break: break-all;
}
.w-293 {
  width: 293px;
}
.w-191 {
  width: 191px;
}
.el-alert--info {
  background: rgba(240, 245, 255, 1);
  border-radius: 2px;
  border: 1px solid rgba(133, 165, 255, 1);
  -webkit-box-align: flex-start;
  -webkit-align-items: flex-start;
  -ms-flex-align: flex-start;
  align-items: flex-start;
  .el-alert__icon {
    font-size: 12px;
    padding-top: 3px;
  }
}
.my-customer-wrap {
  height: 100%;
}
.right-content {
  /*width: 100%;*/
  padding: 0 20px 20px 20px;
  min-height: calc(100% - 160px);
  .right-box {
    background: #fff;
    min-height: 500px;
    padding: 0px;
    .apps-content {
      .apps-content-right {
        width: 100%;
        padding: 20px;
        background: #fff;
        .overstore-body {
          background: #fff;
        }
        .daily-set-wrap {
          height: 100%;
          background: #fff;
        }
        .app-detail-wrap {
          height: 100%;
          background: #fff;
        }
        .common-set-wrap {
          height: 100%;
          background: #fff;
        }
      }
    }
  }
}
.day-tend {
  margin-left: 12px;
  padding-left: 12px;
  position: relative;
  &::before {
    content: '';
    position: absolute;
    left: 0;
    top: 2px;
    height: 13px;
    width: 1px;
    background: #dcdfe6;
  }
}
.no-data-char {
  margin: 100px 0 90px 0;
}
</style>
