<!--
 * @Descripttion : 当前组件信息
 * @Author       : 无尘
 * @Date         : 2020-02-13 14:02:40
 * @LastEditors  : 无尘
 * @LastEditTime : 2020-02-21 16:33:55
 * @FilePath     : \Hapi-demoj:\公司\haoban-3\src\components\company\store-member.vue
 -->
<!--

  <store-member :departObj="departObj"></store-member>
  import storeMember from '@/components/company/store-member.vue';
 -->
<template>
  <div class="depart-list-body">
    <div class="depart-name-wrap">
      <div class="depart-name">
        <span class="font-16 color-303133 font-w-600">{{ departObj.departmentName }}</span>
      </div>
      <div class="depart-id font-12 color-909399">门店code：{{ departObj.departmentId }}</div>
    </div>
    <div class="depart-children-list m-t-20">
      <div class="depart-children-title"><span class="iconfont iconchengyuan color-303133"></span><span class="font-16 color-303133 font-w-600 p-l-5">门店成员</span></div>
      <div class="depart-condition-wrap flex flex-space-between m-t-20">
        <div>
          <el-input placeholder="请输入姓名/手机号" 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="multipleTable" :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="导购ID">
            <template slot-scope="scope">
              <el-select v-model="scope.row.clerkId" placeholder="请选择" @change="changeClerk($event, scope.row)" @focus="getClerkList($event, scope.row)">
                <el-option v-for="item in clerkOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
              </el-select>
            </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="toInvaite(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="[20, 40, 60, 80]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
        </div>
      </div>
    </div>
    <add-member v-if="memberVisible" :memberObj="memberObj" @refreshData="refreshData"></add-member>
    <mod-depart v-if="modVisible" :selectObj="multipleSelection" @modDepartData="modDepartData"></mod-depart>
  </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';
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: 10,
      total: 0,
      tableData: [],
      clerkOptions: [],
      // 新增
      memberVisible: false,
      memberObj: {},
      multipleSelection: [], //勾选数据
      // 调整部门
      modVisible: false
    };
  },
  methods: {
    /**
     * 选择导购
     */
    changeClerk(e, item) {
      const that = this;
      that
        .$confirm(`是否确认为【${item.staffName}】绑定导购ID【${e}】`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {})
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
    },
    postChange(obj) {
      const that = this;
      let params = {
        clerkId: obj.clerkId
      };
      postRequest('/haoban-manage3-web/staff-bind', params)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            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: item.departmentId
      };
      getRequest('/haoban-manage3-web/staff-no-bind-list', params)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            that.clerkOptions = resData.result.list;
            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();
    },
    /**
     * 调整部门
     */
    modifyDepart() {
      const that = this;
      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 = [];
    },
    /**
     * 邀请
     */
    toInvite(item) {
      const that = this;
      if (item) {
        that.postInvite(item);
      }
      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
          });
        });
    },

    /**
     * 删除
     */
    toDel() {
      const that = this;
      if (!that.multipleSelection.length) {
        showMsg.showmsg('请先选择成员！', 'warning');
        return false;
      }
      that.postDel();
    },
    postDel() {
      const that = this;
      let params = {
        staffDepartmentRelatedIds: that.multipleSelection.map(ele => ele.staffDepartmentRelatedIds).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
          });
        });
    },

    /**
     * 显示详情弹窗
     */
    showDetail(item) {
      const that = this;
      that.memberObj = item;
      that.memberVisible = true;
    },

    /**
     * 新建成员
     */
    addMember() {
      const that = this;
      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;
      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="less" scoped>
.w-274 {
  width: 274px;
}

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

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