<!--
 * @Descripttion : 当前组件信息
 * @Author       : 无尘
 * @Date         : 2020-02-13 14:02:40
 * @LastEditors: 无尘
 * @LastEditTime: 2020-08-26 09:59:15
 * @FilePath     : \Hapi-demoj:\公司\haoban-3\src\components\company\depart-member.vue
 -->
<!--
  <depart-member :departObj="departObj" @resetData="resetData"></depart-member>
  import departMember from '@/components/company/depart-member.vue';
 -->
<template>
  <div class="depart-list-body">
    <div class="depart-children-list m-t-20">
      <div class="depart-children-title">
        <span class="iconfont-hb3 iconchengyuan color-303133"></span><span class="font-16 color-303133 font-w-600  p-l-5">部门成员</span>
        <span class="font-12 color-909399 p-l-10">提示：邀请成员当天对该成员最多发送一次邀请申请</span>
      </div>
      <div class="depart-condition-wrap flex flex-space-between m-t-20">
        <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>
          <el-button type="primary" plain @click="modifyDepart">调整所在部门</el-button>
          <el-button type="primary" plain @click="toInvite('')">邀请</el-button>
          <el-button type="danger" plain @click="toDel">删除</el-button>
          <el-button type="primary" @click="addMember">新建成员</el-button>
        </div>
      </div>
      <div class="m-t-20">
        <el-table class="select-table" ref="multipleDepartTable" :data="tableData" tooltip-effect="dark" :style="{ width: '100%', minHeight: tableH }" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"> </el-table-column>
          <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="操作" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-button type="text" @click="showDetail(scope.row)">详情</el-button><el-button v-if="scope.row.activeFlag == 0" type="text" @click="toInvite(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-size="pageSize" layout="total,  prev, pager, next, jumper" :total="total"> </dm-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>
  </div>
</template>
<script>
/* eslint-disable */
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';
export default {
  name: 'depart-list',
  components: {
    addMember,
    modDepart
  },
  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: 20,
      total: 0,
      tableData: [],
      // 新增
      memberVisible: false,
      memberObj: {},
      defaultDepart: [],
      multipleSelection: [], //勾选数据
      // 调整部门
      modVisible: false
    };
  },
  methods: {
    /**
     * 获取同步状态
     */
    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; //可勾选
      }
    },
    /**
     * 输入
     */
    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.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.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) {
      const that = this;
      that.multipleSelection = val;
      // 如果需要过滤就需要,下面重新勾选过滤后的数据
      //rows.forEach(row => {
      //  this.$refs.multipleDepartTable.toggleRowSelection(row);
      //});
    },

    /**
     * 分页---页码变化
     * @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;
      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;
          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.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    }
  },
  watch: {
    departObj(newData, oldData) {
      const that = this;
      if (Object.keys(newData)) {
        that.currentPage = 1;
        that.getTableList();
      }
    }
  },
  mounted() {
    const that = this;
    if (Object.keys(that.departObj)) {
      that.currentPage = 1;
      that.getTableList();
    }
  }
};
</script>
<style lang="scss" scoped>
.w-274 {
  width: 274px;
}

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

.depart-list-body {
  .depart-children-list {
    padding: 16px 10px;
    background: #f5f7fa;
  }
}
</style>
