<template>
  <div class="recycle-bin">
    <p class="r-b-top-header">
      <a class="a-href title" @click="goBack">返回</a>
      <el-button @click="transClerk('group')" :disabled="selectedList.length == 0">批量转移</el-button>
      <el-button @click="transClerk('all')" type="primary">全部转移</el-button>
    </p>
    <el-table :data="store.clerks" @selection-change="selectMember" ref="clerkTable" style="width: 100%">
      <el-table-column type="selection" width="42"></el-table-column>
      <el-table-column label="姓名" prop="name"> </el-table-column>
      <el-table-column label="手机号码" prop="phoneNumber"> </el-table-column>
      <el-table-column label="操作" width="80" prop="clerkCount">
        <template slot-scope="scope">
          <a class="a-href" @click="transClerk('single', scope.row)"><i class="el-icon-sort"></i></a>
          <a class="a-href" @click="delClerk(scope.row)"><i class="el-icon-delete"></i></a>
        </template>
      </el-table-column>
    </el-table>
    <vue-select-store ref="storeSelector" @handleSelectedList="handleSelectedList" :treeSet="treeSet" :selectType="selectType"></vue-select-store>
  </div>
</template>
<script>
import vueSelectStore from 'components/common/vueSelectStore';
import { getRequest } from '@/api/api';
export default {
  name: 'clerkTbale',
  components: {
    vueSelectStore
  },
  props: {
    store: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      treeSet: {
        isSelectPerson: true,
        dialogVisible: false,
        isSingle: true // 是否单选
      },
      selectType: 'store',
      transArr: [],
      selectedList: []
    };
  },
  methods: {
    goBack() {
      window.location.reload();
    },
    // 将店员转移到其他们门店
    transClerk(type, clerk) {
      if (type == 'single') {
        this.transArr = [clerk];
      } else if (type == 'all') {
        this.transArr = this.store.clerks;
      } else {
        this.transArr = this.selectedList;
      }
      this.treeSet.dialogVisible = true;
    },
    delClerk(clerk) {
      this.$emit('delClerk', clerk);
    },
    selectMember(arr) {
      this.selectedList = arr;
    },
    handleSelectedList(store) {
      let arr = [];
      this.transArr.forEach(clerk => {
        arr.push(clerk.employeeClerkId);
      });
      let params = {
        ids: arr.join(','),
        storeId: store[0].id
      };
      let that = this;
      getRequest('/haoban-manage-web/emp/batchTransfer', params)
        .then(res => {
          if (res.data.errorCode == 1) {
            that.$message.success({
              message: '操作成功'
            });
            that.store.clerks.forEach(origin => {
              let idx = arr.indexOf(origin.employeeClerkId);
              if (idx > -1) {
                let index = that.store.clerks.indexOf(origin);
                that.store.clerks.splice(index, 1);
              }
            });
          } else {
            that.$message.error({
              message: res.data.message
            });
          }
        })
        .catch(e => {
          that.$message.error({
            message: e.message
          });
        });
    }
  }
};
</script>
<style lang="scss"></style>
