<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-03-20 14:36:37
 * @LastEditors: 无尘
 * @LastEditTime: 2020-11-30 11:31:18
 -->
<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 - 86px)">
        <div class="apps-content flex" style="min-height: calc(100vh - 86px)">
          <div class="apps-content-right">
            <el-tabs v-model="activeName" @tab-click="handleClick">
              <el-tab-pane label="绑定变更日志" name="first">
                <div v-loading="loadingBind">
                  <div class="flex flex-space-between">
                    <div class="">
                      <el-input placeholder="请输入导购手机号或代码" prefix-icon="el-icon-search" v-model="searchInput" class="w-293" @keyup.native="value => toInput(value, searchInput)" clearable @clear="clearInput"> </el-input>
                      <el-select v-model="optType" placeholder="请选择" @change="changeBindList" class="w-168 m-l-10">
                        <el-option label="全部状态" value=""></el-option>
                        <el-option label="绑定" value="0"></el-option>
                        <el-option label="解绑" value="1"></el-option>
                      </el-select>
                    </div>
                    <common-alert-tip :width="'284px'" :tip-content="tipContent"></common-alert-tip>
                  </div>
                  <div class="m-t-20">
                    <el-table class="select-table" ref="multipleTable" :data="tableBindData" tooltip-effect="dark" :style="{ width: '100%' }">
                      <el-table-column prop="" label="类型" show-overflow-tooltip>
                        <template slot-scope="scope">{{ scope.row.optType == 0 ? '绑定' : '解绑' }}</template>
                      </el-table-column>
                      <el-table-column label="导购账号" >
                        <template slot-scope="scope">
                          <el-popover
                            placement="top"
                            width="242"
                            trigger="hover">
                            <div>
                              <div class="flex flex-space-between line-28">
                                <span class="font-14 color-606266">姓名</span><span class="font-14 color-303133">{{scope.row.clerkName || '--'}}（{{scope.row.sex == 1 ? '男' : scope.row.sex == 2 ? '女' : '--'}} ）</span>
                              </div>
                              <div class="flex flex-space-between line-28">
                                <span class="font-14 color-606266">手机号</span><span class="font-14 color-303133">{{scope.row.clerkNationCode || '--'}}-{{scope.row.clerkPhoneNumber || '--'}}</span>
                              </div>
                              <div class="flex flex-space-between line-28">
                                <span class="font-14 color-606266  min-w-90">所在门店</span><span class="font-14 color-303133">{{scope.row.storeName || '--'}}</span>
                              </div>
                            </div>
                          <span class="common-bottom-border" slot="reference">{{scope.row.clerkName || '--' }}（{{scope.row.clerkCode || '--' }}）</span>
                          </el-popover>
                        </template>
                      </el-table-column>
                      <el-table-column label="企微账号" show-overflow-tooltip>
                        <template slot-scope="scope">{{ scope.row.wxUserId || '--' }}</template>
                      </el-table-column>
                      <el-table-column label="手机号" show-overflow-tooltip>
                        <template slot-scope="scope">{{ scope.row.phoneNumber || '--' }}</template>
                      </el-table-column>
                      <el-table-column prop="" label="更新时间" show-overflow-tooltip>
                        <template slot-scope="scope">
                          <div class="line-h-18">{{ scope.row.updateTime | timeStampToYmd }} {{ !scope.row.updateTime ? '--' : '' }}</div>
                          <div class="line-h-18">{{ scope.row.updateTime | timeStampToHms }} {{ !scope.row.updateTime ? '--' : '' }}</div>
                        </template>
                      </el-table-column>
                      <el-table-column label="操作人" show-overflow-tooltip>
                        <template slot-scope="scope">{{ scope.row.optStaffName || '--' }}</template>
                      </el-table-column>
                      <el-table-column prop="" label="原因" show-overflow-tooltip>
                        <template slot-scope="scope">
                          <span v-if="scope.row.channelCode == 1001" type="text">后台管理员绑定</span>
                          <span v-if="scope.row.channelCode == 1002" type="text">好办客户端绑定</span>
                          <span v-if="scope.row.channelCode == 1003" type="text">审核绑定</span>
                          <span v-if="scope.row.channelCode == 1004" type="text">GIC 同步绑定</span>
                          <span v-if="scope.row.channelCode == 1005" type="text">企业同步绑定</span>
                          <span v-if="scope.row.channelCode == 2001" type="text">后台管理员解绑</span>
                          <span v-if="scope.row.channelCode == 2002" type="text">好办客户端解绑</span>
                          <span v-if="scope.row.channelCode == 2003" type="text">GIC 同步解绑</span>
                          <span v-if="scope.row.channelCode == 2004" type="text">企业同步解绑</span>
                          <span v-if="scope.row.channelCode == 2005" type="text">授权范围变更解绑</span>
                        </template>
                      </el-table-column>
                    </el-table>
                    <div class="block common-wrap__page text-right m-t-24" v-if="tableBindData.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>
              </el-tab-pane>
              <el-tab-pane label="通讯录刷新日志" name="second">
                <div v-loading="loading">
                  <div class="flex flex-space-between">
                    <common-alert-tip :width="'284px'" :tip-content="'仅保存近一年的日志'"></common-alert-tip>
                  </div>
                  <div class="m-t-20">
                    <el-table class="select-table" ref="multipleTable" :data="tableData" tooltip-effect="dark" :style="{ width: '100%', minHeight: tableH }">
                      <el-table-column label="提交人" show-overflow-tooltip>
                        <template slot-scope="scope">{{ scope.row.optName || '--' }}</template>
                      </el-table-column>
                      <el-table-column prop="" label="同步时间" show-overflow-tooltip>
                        <template slot-scope="scope">
                          <div class="line-18">{{ scope.row.createTime | timeStampToYmd }}{{ !scope.row.createTime ? '--': '' }}</div>
                          <div class="line-18">{{ scope.row.createTime | timeStampToHms }}{{ !scope.row.createTime ? '--': '' }}</div>
                        </template>
                      </el-table-column>
                      <el-table-column prop="" label="完成时间" show-overflow-tooltip>
                        <template slot-scope="scope">
                          <div class="line-18">{{ scope.row.updateTime | timeStampToYmd }}{{ !scope.row.updateTime ? '--': '' }}</div>
                          <div class="line-18">{{ scope.row.updateTime | timeStampToHms }}{{ !scope.row.updateTime ? '--': '' }}</div>
                        </template>
                      </el-table-column>
                      <el-table-column prop="" label="同步内容" show-overflow-tooltip>
                        <template slot-scope="scope">{{ scope.row.taskDesc | '--' }}</template>
                      </el-table-column>
                      <el-table-column prop="" label="状态" show-overflow-tooltip>
                        <template slot-scope="scope">
                          <span :class="['span-dot', scope.row.statusFlag == 4 ? 'bg-33af4a ' : 'bg-f5222d']"></span><span v-if="scope.row.statusFlag == 4" class="font-14 color-606266">{{ scope.row.statusFlag == 4 ? '成功' : '' }}</span>
                          <span v-if="scope.row.statusFlag == 0"  class="font-14 color-606266">初始化</span>
                          <span v-if="scope.row.statusFlag == 1 || scope.row.statusFlag == 2 || scope.row.statusFlag == 3"  class="font-14 color-606266">分组/门店/成员同步中</span>
                          <el-popover v-if="scope.row.statusFlag == 5 || scope.row.statusFlag == 6" trigger="hover" class="item" :content="scope.row.statusFlag == 5?'完成有错误':'有错误关闭'" placement="top">
                            <span slot="reference" class="common-bottom-border">失败</span>
                          </el-popover>
                        </template>
                      </el-table-column>
                      <!-- <el-table-column prop="" label="操作" show-overflow-tooltip>
                        <template slot-scope="scope">
                          <el-button v-if="scope.row.statusFlag == 5 || scope.row.statusFlag == 6" type="text" @click="toShowLog(scope.$index, scope.row)">查看失败日志</el-button>
                        </template>
                      </el-table-column> -->
                    </el-table>
                    <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>
              </el-tab-pane>
            </el-tabs>
          </div>
        </div>
      </div>
    </div>
    <!-- <vue-gic-footer></vue-gic-footer> -->
    <fail-log v-if="logShow" :task-id="taskId" @closeLog="closeLog"></fail-log>
  </div>
</template>
<script>
import failLog from '@/components/company/fail-log.vue';
import navCrumb from '@/components/nav/nav.vue';
import commonAlertTip from '@/components/common/common-alert-tip.vue';
import fetch from '@/api/contact.js';
const { getBindLog, getRefreshLog, getWxEnterprise } = fetch;
import { _debounce } from '@/common/js/public';
export default {
  name: 'SyncLog',
  components: {
    navCrumb,
    failLog,
    commonAlertTip
  },
  data() {
    return {
      tipContent: '仅保存近一年绑定解绑的日志',
      // 面包屑参数
      navpath: [
        /* {
          name: '首页',
          path: '/index'
        }, */
        {
          name: '通讯录',
          path: '/contactsList'
        },
        {
          name: '日志详情',
          path: ''
        }
      ],
      wxEnterpriseId: localStorage.getItem('haoBanUser') ? JSON.parse(localStorage.getItem('haoBanUser')).wxEnterpriseId : '',
      activeName: 'first',
      searchInput: '',
      // 分页参数
      currentPage: 1,
      pageSize: 20,
      total: 0,
      tableData: [],
      tableBindData: [],
      logShow: false,
      taskId: '',
      // 解绑日志
      optType: '',
      enterpriseId: '',
      brandOptions: [],
      loading: false,
      loadingBind: false
    };
  },
  mounted() {
    const that = this;
    that.getBrandData();
    that.getBindList();
    document.documentElement.style.backgroundColor = '#f0f2f5';
  },
  destroyed() {
    document.documentElement.style.backgroundColor = '#fff';
  },
  methods: {
    /**
     * @description: 搜索标签清空
     * @author: 无尘
     */
    clearInput() {
      const that = this;
      that.currentPage = 1;
      that.getBindList();
    },
    changeBindList() {
      const that = this;
      that.currentPage = 1;
      that.getBindList();
    },

    /**
     * @description: 搜索标签
     * @param {String} e
     * @author: 无尘
     */
    toInput: _debounce(function(e) {
      const that = this;
      that.currentPage = 1;
      that.getBindList();
    }, 200),
    /**
     * @description: 切换tab
     * @param {Object} tab
     * @author: 无尘
     */
    handleClick(tab) {
      const that = this;
      that.currentPage = 1;
      that.pageSize = 20;
      that.total = 0;
      that.enterpriseId = '';
      if (tab.name == 'second') {
        that.getTableList();
      } else {
        that.getBrandData();
        that.getBindList();
      }
    },
    /**
     * @description: 查看失败日志
     * @param {Number} index
     * @param {Object} item
     * @author: 无尘
     */
    toShowLog(index, item) {
      const that = this;
      that.taskId = item.taskId;
      that.logShow = true;
    },
    closeLog() {
      const that = this;
      that.taskId = '';
      that.logShow = false;
    },
    /**
     * @description: 路由跳转
     * @param {String} path
     * @author: 无尘
     */
    changeRoute(path) {
      this.$router.push(path);
    },

    /**
     * @description: 分页---页码变化
     * @param {Number} val
     * @author: 无尘
     */
    handleSizeChange(val) {
      const that = this;
      that.currentPage = 1;
      that.pageSize = val;
      if (that.activeName == 'second') {
        that.getTableList();
      } else {
        that.getBindList();
      }
    },

    /**
     * @description: 分页---当前页变化
     * @param {Number} val
     * @author: 无尘
     */
    handleCurrentChange(val) {
      const that = this;
      that.currentPage = val;
      if (that.activeName == 'second') {
        that.getTableList();
      } else {
        that.getBindList();
      }
    },
    /**
     * @description: 获取商户
     * @author: 无尘
     */
    getBrandData() {
      const that = this;
      getWxEnterprise({ wxEnterpriseId: that.wxEnterpriseId })
        .then(res => {
          if (!!res.result && !!res.result.length) {
            that.brandOptions = res.result;
          }
        })
        .catch(function(error) {

        });
    },
    /**
     * @description: 获取绑定数据
     * @author: 无尘
     */
    getBindList() {
      const that = this;
      that.loadingBind = true;
      let para = {
        search: that.searchInput || '', // 搜索字段
        enterpriseId: that.enterpriseId,
        wxEnterpriseId: that.wxEnterpriseId,
        optType: that.optType,
        pageNum: that.currentPage, // 当前页
        pageSize: that.pageSize // 一页显示个数
      };
      getBindLog(para)
        .then(res => {
          that.loadingBind = false;
          that.tableBindData = res.result.result || [];
          that.total = res.result.totalCount;
        })
        .catch(function(error) {
          that.loadingBind = false;
        });
    },
    /**
     * @description: 获取刷新数据
     * @author: 无尘
     */
    getTableList() {
      const that = this;
      that.loading = true;
      let para = {
        enterpriseId: that.enterpriseId,
        wxEnterpriseId: that.wxEnterpriseId,
        pageNum: that.currentPage, // 当前页
        pageSize: that.pageSize // 一页显示个数
      };
      getRefreshLog(para)
        .then(res => {
          that.loading = false;
          that.tableData = res.result.result || [];
          that.total = res.result.totalCount;
        })
        .catch(function(error) {
          that.loading = false;
        });
    }
  }
};
</script>
<style type="text/less" lang="less" scoped>
.span-dot {
  display: inline-block;
  vertical-align: middle;
  width: 6px;
  height: 6px;
  margin-right: 6px;
  border-radius: 3px;
  &.bg-33af4a  {
    background: #33af4a ;
  }
  &.bg-f5222d {
    background: #f5222d;
  }
}
.bg-82C5FF {
  background: #82c5ff;
}
.color-508CEE {
  color: #508cee;
}
.color-FF585C {
  color: #ff585c;
}
.line-h-18 {
  line-height: 18px;
}
.line-28 {
  line-height: 28px;
}
.min-w-90 {
  min-width: 90px;
}
.tooltip-text {
  width: 100%;
  white-space: pre-wrap;
  word-break: break-all;
}
.w-293 {
  width: 293px;
}
.w-191 {
  width: 191px;
}
.w-168 {
  width: 168px;
}
.common-bottom-border {
  cursor: pointer;
  padding-bottom: 2px;
  border-bottom: 1px dashed #2f54eb;
}
.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;
  }
}
.el-button {
  &.cursor-unset {
    cursor: unset;
  }
}
.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;
        }
      }
    }
  }
}
</style>
