<!--
 * @Descripttion : 当前组件信息
 * @Author       : 无尘
 * @Date         : 2020-02-13 14:02:40
 * @LastEditors: 无尘
 * @LastEditTime: 2020-06-01 17:43:36
 * @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 }}</span>
      </div>
    </div>
    <div class="m-t-20">
      <div class="depart-condition-wrap flex flex-space-between ">
        <div>
          <el-input placeholder="请输入姓名/手机号/code" 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></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">
          <el-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"> </el-pagination>
        </div>
      </div>
    </div>
    <add-member v-if="memberVisible" :defaultDepart="defaultDepart" :memberObj="memberObj" @refreshData="refreshData"></add-member>
    <mod-depart v-if="modVisible" :selectObj="multipleSelection" @modDepartData="modDepartData"></mod-depart>
    <related-table v-if="relateShow" :relateRow="relateRow" @closeTable="closeTable"></related-table>
  </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';
export default {
  name: 'depart-list',
  components: {
    addMember,
    modDepart,
    relatedTable
  },
  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: {}
    };
  },
  methods: {
    /**
     * 关联详情
     */
    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;
    },

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

    /**
     * 分页---当前页变化
     */
    handleCurrentChange(val) {
      const that = this;
      that.currentPage = val;
      that.getTableList();
    },
    /**
     * 获取列表数据
     */
    getTableList(val) {
      const that = this;
      that.loading = true;
      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;
}
</style>
