<!--
 * @Descripttion : 当前组件信息
 * @Author       : 无尘
 * @Date         : 2020-02-13 14:02:40
 * @LastEditors: 无尘
 * @LastEditTime: 2020-08-26 09:59:22
 * @FilePath     : \Hapi-demoj:\公司\haoban-3\src\components\company\new-depart-member.vue
 -->
<!--

  <new-depart-member :departObj="departObj"></new-depart-member>
  import newDepartMember from '@/components/company/new-depart-member.vue';
 -->
<template>
  <div class="depart-list-body">
    <div class="depart-name-wrap">
      <div class="depart-name">
        <span class="font-16 p-r-10 iconfont iconqiye-tianchong color-303133"></span><span class="font-16 color-303133 font-w-600">{{ departObj.departmentName }} （{{ total }}人）</span>
      </div>
    </div>
    <div class="m-t-20">
      <div class="depart-condition-wrap flex flex-space-between ">
        <div>
          <el-input placeholder="请输入姓名/手机号" maxlength="50" v-model="searchInput" class="w-274" @keyup.native="value => toInput(value, searchInput)" clearable @clear="clearInput"></el-input>
          <el-select v-model="activeFlag" placeholder="所有状态" class="m-l-6" @change="changeStatus">
            <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
          </el-select>
        </div>
        <div class="flex">
          <el-tooltip content="通讯录日志" placement="top">
            <div class="log-icon flex flex-align-center flex-pack-center cursor-pointer " @click="toLog"><span class="font-14 iconfont icontongxunlu color-909399"></span></div>
          </el-tooltip>
          <div class="to-log-btn font-12 color-2f54eb cursor-pointer" @click="toShowTip">查看帮助提示</div>
        </div>
      </div>
      <div class="m-t-20">
        <el-table class="select-table" ref="multipleTable" v-loading="loading" :data="tableData" tooltip-effect="dark" :style="{ width: '100%', minHeight: tableH }">
          <!-- <el-table-column type="selection" width="55"> </el-table-column> @selection-change="handleSelectionChange" -->
          <el-table-column label="姓名" show-overflow-tooltip>
            <template slot-scope="scope">{{ scope.row.staffName || '--' }}</template>
          </el-table-column>
          <el-table-column prop="" label="职务" show-overflow-tooltip>
            <template slot-scope="scope">{{ scope.row.postion || '--' }}</template>
          </el-table-column>
          <el-table-column prop="" label="手机号码" show-overflow-tooltip>
            <template slot-scope="scope"
              >{{ scope.row.nationCode }}-{{ scope.row.phoneNumber }}</template
            >
          </el-table-column>
          <el-table-column prop="" label="导购ID">
            <template slot="header" slot-scope="scope">
              <el-tooltip class="item" effect="light" placement="right">
                <div slot="content">
                  是否使用好办3.0小程序
                </div>
                <span style="cursor: pointer;padding-bottom: 2px; border-bottom: 1px dashed #2F54EB;">使用状态</span>
              </el-tooltip>
            </template>
            <template slot-scope="scope">
              <span :class="['inline-block iconfont iconhuaban', scope.row.activeFlag == 1 ? 'color-2f54eb' : 'color-c0c4cc']"></span>
            </template>
          </el-table-column>
          <el-table-column prop="" label="关联详情" show-overflow-tooltip>
            <template slot="header" slot-scope="scope">
              <el-tooltip class="item" effect="light" placement="right">
                <div slot="content">员工关联导购详情，因只能识别企业微信用户usrid且门店与部门不相关联，<br />所以虽然在一个部门下，但是可能出现一个人关联多个导购code。<br />即一个用户对应多个导购code</div>
                <span style="cursor: pointer;padding-bottom: 2px; border-bottom: 1px dashed #2F54EB;">关联详情</span>
              </el-tooltip>
            </template>
            <template slot-scope="scope">
              <el-button type="text" @click="showRelateDetail(scope.row)">查看详情</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="" label="操作" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-button type="text" @click="showDetail(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="[10, 20, 30]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </dm-pagination>
        </div>
      </div>
    </div>
    <add-member v-if="memberVisible" :defaultDepart="defaultDepart" :memberObj="memberObj" @refreshData="refreshData" @changeObj="changeObj"></add-member>
    <mod-depart v-if="modVisible" :selectObj="multipleSelection" @modDepartData="modDepartData"></mod-depart>
    <related-table v-if="relateShow" :relateRow="relateRow" @closeTable="closeTable"></related-table>
    <help-tip v-if="tipShow" @toCloseTip="toCloseTip"></help-tip>
  </div>
</template>
<script>
import { _debounce } from '@/common/js/public';
import { getRequest, postRequest } from '@/api/api';
import errMsg from '@/common/js/error';
import showMsg from '@/common/js/showmsg';
import addMember from '@/components/company/add-member.vue';
import modDepart from '@/components/company/mod-depart.vue';
import relatedTable from '@/components/company/related-table.vue';
import helpTip from '@/components/company/help-tip.vue';
export default {
  name: 'depart-list',
  components: {
    addMember,
    modDepart,
    relatedTable,
    helpTip
  },
  props: {
    departObj: {
      type: Object,
      default() {
        return {
          departmentId: '',
          departmentName: '',
          wxDepartmentId: '',
          parentId: '',
          isStore: '',
          staffCount: '',
          bindFlag: ''
        };
      }
    }
  },
  data() {
    return {
      searchInput: '',
      activeFlag: '',
      statusOptions: [
        {
          value: '',
          label: '所有状态'
        },
        {
          value: '0',
          label: '未激活'
        },
        {
          value: '1',
          label: '已激活'
        }
      ],
      // 分页参数
      currentPage: 1,
      pageSize: 10,
      total: 0,
      loading: false,
      tableData: [],
      clerkOptions: [],
      // 新增
      memberVisible: false,
      memberObj: {},
      defaultDepart: [],
      multipleSelection: [], //勾选数据
      // 调整部门
      modVisible: false,
      navData: [],
      relateShow: false,
      relateRow: {},
      tipShow: false
    };
  },
  methods: {
    toLog() {
      window.open('/haoban-3/#/syncLog');
    },
    /**
     * 修改选择部门
     */
    changeObj(obj) {
      const that = this;
      that.memberVisible = false;
      that.$emit('changeDepart', obj);
    },
    /**
     * 显示提示
     */
    toShowTip() {
      const that = this;
      that.tipShow = true;
    },
    toCloseTip() {
      const that = this;
      that.tipShow = false;
    },

    /**
     * 关联详情
     */
    showRelateDetail(row) {
      const that = this;
      that.relateShow = true;
      that.relateRow = row;
    },
    closeTable() {
      const that = this;
      that.relateShow = false;
      that.relateRow = {};
    },
    changeDepart(item, index) {
      const that = this;
      if (index == that.navData.length - 1) {
        return false;
      }
      that.$emit('changeDepart', item);
    },
    /**
     * 获取同步状态
     */
    getStatus() {
      const that = this;
      return new Promise((resolve, reject) => {
        let para = {};
        getRequest('/haoban-manage3-web/department-sync-check', para)
          .then(res => {
            let resData = res.data;
            if (resData.errorCode == 1) {
              resolve(resData.result);
              return false;
            }
            resolve(false);
            errMsg.errorMsg(resData);
          })
          .catch(function(error) {
            resolve(false);
            that.$message.error({
              duration: 1000,
              message: error.message
            });
          });
      });
    },
    /**
     * 可选设置
     **/
    selectInit(row, index) {
      if (row.activeFlag == 1) {
        return false; //不可勾选
      } else {
        return true; //可勾选
      }
    },
    /**
     * 选择导购
     */
    changeClerk(e, item) {
      const that = this;
      that
        .$confirm(`是否确认为【${item.staffName}】绑定导购ID【${e}】`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          that.postChange(item);
        })
        .catch(() => {
          that.getTableList();
        });
    },
    postChange(obj) {
      const that = this;
      let params = {
        fromStaffDepartmentRelatedId: obj.staffDepartmentRelatedId,
        toClerkCode: obj.clerkCode
      };
      postRequest('/haoban-manage3-web/staff-bind', params)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            showMsg.showmsg('绑定成功！', 'success');
            that.currentPage = 1;
            that.getTableList();
            // 是否需要更新左侧 tree
            return false;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     * 获取未绑定导购
     */
    getClerkList(e, item) {
      const that = this;
      let params = {
        departmentId: that.departObj.departmentId,
        storeId: that.departObj.relatedId
      };
      getRequest('/haoban-manage3-web/clerk-code-list', params)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            that.clerkOptions = resData.result;
            return false;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     * 输入
     */
    toInput: _debounce(function(e, value) {
      const that = this;
      that.currentPage = 1;
      that.getTableList();
    }, 500),
    // 搜索清除
    clearInput() {
      const that = this;
      that.currentPage = 1;
      that.getTableList();
    },
    // 选择状态
    changeStatus() {
      const that = this;
      that.currentPage = 1;
      that.getTableList();
    },
    /**
     * 调整部门
     */
    async modifyDepart() {
      const that = this;
      const tag = await that.getStatus();
      if (!!tag && (tag.syncStatus == 0 || tag.syncStatus == 1 || tag.syncStatus == 2 || tag.syncStatus == 3 || tag.syncStatus == 5)) {
        showMsg.showmsg('通讯录同步中，暂无法操作', 'warning');
        return false;
      }
      if (!that.multipleSelection.length) {
        showMsg.showmsg('请先选择成员！', 'warning');
        return false;
      }
      that.modVisible = true;
    },
    modDepartData(res) {
      const that = this;
      that.modVisible = false;
      if (res == 'close') {
        return false;
      }
      that.currentPage = 1;
      that.getTableList();
      that.multipleSelection = [];
    },
    /**
     * 邀请
     */
    async toInvite(item) {
      const that = this;
      const tag = await that.getStatus();
      if (!!tag && (tag.syncStatus == 0 || tag.syncStatus == 1 || tag.syncStatus == 2 || tag.syncStatus == 3 || tag.syncStatus == 5)) {
        showMsg.showmsg('通讯录同步中，暂无法操作', 'warning');
        return false;
      }
      if (item) {
        that.postInvite(item);
        return false;
      }
      if (!that.multipleSelection.length) {
        showMsg.showmsg('请先选择成员！', 'warning');
        return false;
      }
      that.postInvite();
    },
    postInvite(item) {
      const that = this;
      let params = {
        staffIds: item ? item.staffId : that.multipleSelection.map(ele => ele.staffId).join(',')
      };
      postRequest('/haoban-manage3-web/staff-invite', params)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            showMsg.showmsg('邀请成功', 'success');
            that.multipleSelection = [];
            that.currentPage = 1;
            that.getTableList();
            // 是否需要更新左侧 tree
            return false;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },

    /**
     * 删除
     */
    async toDel() {
      const that = this;
      const tag = await that.getStatus();
      if (!!tag && (tag.syncStatus == 0 || tag.syncStatus == 1 || tag.syncStatus == 2 || tag.syncStatus == 3 || tag.syncStatus == 5)) {
        showMsg.showmsg('通讯录同步中，暂无法操作', 'warning');
        return false;
      }
      if (!that.multipleSelection.length) {
        showMsg.showmsg('请先选择成员！', 'warning');
        return false;
      }
      that.postDel();
    },
    postDel() {
      const that = this;
      let params = {
        staffDepartmentRelatedIds: that.multipleSelection.map(ele => ele.staffDepartmentRelatedId).join(',')
      };
      postRequest('/haoban-manage3-web/staff-batch-del', params)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            showMsg.showmsg('删除成功', 'success');
            that.multipleSelection = [];
            that.currentPage = 1;
            that.getTableList();
            return false;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },

    /**
     * 显示详情弹窗
     */
    async showDetail(item) {
      const that = this;
      /* const tag = await that.getStatus();
      if (!!tag && (tag.syncStatus == 0 || tag.syncStatus == 1 || tag.syncStatus == 2 || tag.syncStatus == 3 || tag.syncStatus == 5)) {
        showMsg.showmsg('通讯录同步中，暂无法操作', 'warning');
        return false;
      } */
      that.memberObj = item;
      that.memberVisible = true;
    },

    /**
     * 新建成员
     */
    async addMember() {
      const that = this;
      const tag = await that.getStatus();
      if (!!tag && (tag.syncStatus == 0 || tag.syncStatus == 1 || tag.syncStatus == 2 || tag.syncStatus == 3 || tag.syncStatus == 5)) {
        showMsg.showmsg('通讯录同步中，暂无法操作', 'warning');
        return false;
      }
      // 默认当前部门
      that.defaultDepart = [that.departObj];
      that.memberVisible = true;
      that.memberObj = {};
    },
    /**
     * 确认新建后刷新
     */
    refreshData(res) {
      const that = this;
      that.memberVisible = false;
      that.memberObj = {};
      if (res == 'close') {
        return false;
      }

      that.currentPage = 1;
      that.getTableList();
    },

    /**
     * 勾选
     */
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    /**
     * 分页---页码变化
     * @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();
    },
    /**
     * 获取列表数据
     */
    getTableList(val) {
      const that = this;
      that.loading = true;
      if (!that.departObj.departmentId) {
        that.loading = false;
        return false;
      }
      let para = {
        departmentId: that.departObj.departmentId,
        activeFlag: that.activeFlag,
        keyWord: that.searchInput || '', // 搜索字段
        pageNum: that.currentPage, // 当前页
        pageSize: that.pageSize // 一页显示个数
      };
      getRequest('/haoban-manage3-web/staff-list', para)
        .then(res => {
          let resData = res.data;
          that.loading = false;
          if (resData.errorCode == 1) {
            that.tableData = resData.result.result || resData.result.list || [];
            that.total = resData.result.totalCount || resData.result.total;
            return false;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.loading = false;
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     * 获取面包屑
     */
    toGetNavData() {
      const that = this;
      let params = {
        departmentId: that.departObj.departmentId
      };
      postRequest('/haoban-manage3-web/department-chain', params)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            that.navData = resData.result || [];
            return false;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    }
  },
  watch: {
    departObj(newData, oldData) {
      const that = this;
      if (Object.keys(newData)) {
        that.currentPage = 1;
        that.getTableList();
        // that.toGetNavData();
      }
    }
  },
  mounted() {
    const that = this;
    if (Object.keys(that.departObj)) {
      that.currentPage = 1;
      that.getTableList();
      // that.toGetNavData();
    }
  }
};
</script>
<style lang="less" scoped>
.m-b-10 {
  margin-bottom: 10px;
}
.el-breadcrumb__inner.is-link:hover,
.el-breadcrumb__inner a:hover {
  color: #2f54eb;
}
.w-274 {
  width: 274px;
}

.m-l-6 {
  margin-left: 6px;
}

.depart-list-body {
  .depart-id {
    margin-top: 5px;
  }
  .depart-children-list {
    padding: 16px 10px;
    background: #f5f7fa;
  }
}
.iconhuaban {
  font-size: 24px;
  vertical-align: bottom;
}
.to-log-btn {
  width: 102px;
  height: 28px;
  line-height: 28px;
  text-align: center;
  background: rgba(245, 247, 250, 1);
  &:hover {
    color: #597ef7;
  }
}
.log-icon {
  width: 28px;
  height: 28px;
  border-radius: 2px;
  margin-right: 15px;
  span {
    color: #c0c4cc;
  }
  &:hover {
    background: rgba(240, 245, 255, 1);
    span {
      color: #909399;
    }
  }
}
</style>
