<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2020-02-05 10:50:57
 * @LastEditors  : 无尘
 * @LastEditTime : 2020-02-18 13:51:11
 -->
<!--
  设置成员所在部门（回收站）
  <set-department v-if="setVisible" @closeSet="closeSet"></set-department>
  import setDepartment from '@/components/set-department.vue';
  -->
<template>
  <el-dialog width="600px" title="设置成员所在部门" :visible.sync="setVisible" append-to-body :before-close="handleClose">
    <div class="select-search">
      <!-- 树形也自带有过滤搜索的方法 -->
      <el-input placeholder="请输入内容" v-model="searchSelect" style="width: 296px;" @keyup.native="value => toInput(value, searchSelect)" @clear="clearSearch"> <i slot="prefix" class="el-input__icon el-icon-search"></i> </el-input>
    </div>
    <div class="select-tree-wrap m-t-20">
      <el-tree :data="treeData" node-key="groupId" ref="setTree" default-expand-all :props="defaultProps" :expand-on-click-node="false" @node-click="handleNodeClick">
        <span class="custom-tree-node" slot-scope="{ node, data }">
          <span :class="['font-14 p-r-10 iconfont', node.isStore == 1 ? 'icondianpu-kuai' : 'iconqiye-tianchong', node.bindFlag == 1 ? 'color-2f54eb' : 'color-dedfe6']"></span><span class="font-14 color-606266">{{ node.label }}</span>
        </span>
      </el-tree>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="submitForm">确定</el-button>
    </div>
  </el-dialog>
</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';
export default {
  props: {
    applicationId: {
      type: String,
      default() {
        return '';
      }
    }
  },
  data() {
    return {
      setVisible: true,
      treeData: [],
      searchSelect: '', // 搜索字段
      selectId: ''
    };
  },
  computed: {},

  mounted() {
    this.getData();
  },
  methods: {
    /**
     * 关闭弹窗
     */
    handleClose(done) {
      const that = this;
      that.$refs['form'].resetFields();
      done();
      that.$emit('closeSet');
    },
    cancel() {
      const that = this;
      that.$refs['form'].resetFields();
      that.$emit('closeSet');
    },
    /**
     * 输入
     */
    toInput: _debounce(function(e, value) {
      const that = this;
      if (!that.storesCopy.length) {
        return false;
      }
      if (value == '') {
        that.stores = JSON.parse(JSON.stringify(that.storesCopy));
      }
      let filterData = JSON.parse(JSON.stringify(that.storesCopy)).filter(ele => {
        return ele.storeName.includes(value);
      });
      that.treeData = filterData;
    }, 500),
    /**
     * 清空
     */
    clearSearch() {
      const that = this;
      that.treeData = JSON.parse(JSON.stringify(that.storesCopy));
    },
    /**
     * 点击 tree 数据
     */
    handleNodeClick(data) {
      const that = this;
      console.log(data);
      that.selectId = data.id;
    },
    /**
     * 获取tree 数据
     */
    getData() {
      const that = this;
      let params = {};
      getRequest('/haoban-manage-web/dept/deptList', params)
        .then(res => {
          if (res.data.code === '0000') {
            that.treeData = res.data.result || [];
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },

    /**
     * 确定授权
     */
    submitForm() {
      const that = this;
      if (!that.selectId) {
        showMsg.showmsg('请选择部门', 'warning');
        return false;
      }
      let params = {
        departmentId: that.selectId
      };
      postRequest('/haoban-manage3-web/department-repair', params)
        .then(res => {
          if (res.data.code === '0000') {
            that.$emit('closeSet', that.selectId);
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    }
  },
  components: {}
};
</script>
<style scoped>
.m-t-5 {
  margin-top: 5px;
}

.dialog-top {
  margin-bottom: 20px;
}

.dialog-top img {
  width: 40px;
  height: 40px;
  border: 1px solid #e4e7ed;
  border-radius: 50%;
  vertical-align: middle;
  margin-right: 10px;
}

.dialog-top span {
  font-size: 14px;
  font-weight: 600;
  color: #303133;
}

.select-info {
  display: inline-block;
  vertical-align: middle;
}

.dialog-operate {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.pagination-content {
  text-align: right;
}

.dialog-form {
  width: 430px;
  padding-top: 20px;
}

.w-215 {
  width: 215px;
}

.w-115 {
  width: 115px;
}

.select-tree-wrap {
  height: 445px;
  overflow-y: auto;
  border: 1px solid #e4e7ed;
}
</style>
