<!--
 * @Descripttion : 当前组件信息
 * @Author       : 无尘
 * @Date         : 2020-02-13 14:02:40
 * @LastEditors: 无尘
 * @LastEditTime: 2020-03-19 09:44:16
 * @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><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="请输入姓名/导购ID/手机号" 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" :selectable="selectInit"> </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.clerkCode" placeholder="请选择" @change="changeClerk($event, scope.row)" @focus="getClerkList($event, scope.row)">
                <el-option v-for="item in clerkOptions" :key="item.clerkCode" :label="item.staffName" :value="item.clerkCode"> </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="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">
          <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10, 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" :defaultDepart="defaultDepart" :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: {},
      defaultDepart: [],
      multipleSelection: [], //勾选数据
      // 调整部门
      modVisible: false
    };
  },
  methods: {
    /**
     * 可选设置
     **/
    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();
    },
    /**
     * 调整部门
     */
    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;
      console.log(item);
      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
          });
        });
    },

    /**
     * 删除
     */
    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.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
          });
        });
    },

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

    /**
     * 新建成员
     */
    addMember() {
      const that = this;
      // 默认当前部门
      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;
      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>
