<template>
  <div class="custom-tree-container">
    <div class="block">
      <el-tree
        :data="datamsg"
        node-key="compId"
        default-expand-all
        :expand-on-click-node="false"
        :render-content="renderContent"
        current-node-key="1"
        @node-click="nodeClick"
      />
    </div>
    <el-dialog title="添加公司" :visible.sync="addDialogVisible" width="600px" center :close-on-click-modal="false"
               @close="cancel">
      <el-form ref="compChildInfo" :model="compChildInfo" :rules="compChildRules">
        <el-form-item label="公司全称" prop="compName">
          <el-input v-model="compChildInfo.compName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="公司简称" prop="compSimpleName">
          <el-input v-model="compChildInfo.compSimpleName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="公司英文名称">
          <el-input v-model="compChildInfo.compNameEn" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <span class="red">*</span><span class="more-text">更多设置请在 【公司信息】中完善</span>
        <el-button type="primary" @click="addDeptName">保 存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import dropdownChildren from '../../views/companyInfo/dropdownChange';
import dropdownA from '../../views/companyInfo/dropdown';
import {
  compAndChildrenInfo , getCreate , getDelete
} from '../../api/companyInfo';


export default {
  props: ['show'],
  watch: {
    show() {
      this.addDialogVisible = true;
    }
  },
  data() {
    return {
      compChildInfo: {},
      compChildRules: {
        compName: [{ required: true, trigger: 'blur' }],
        compSimpleName: [{ required: true, trigger: 'blur' }],
      },
      addDialogVisible: false,
      addSection: {
        compId: '',
        deptName: '',
        deptName_en: '',
        parentDeptId: ''
      },
      compId: localStorage.getItem('enterprise-front-companyId'),
      datamsg: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 取消添加子公司
    cancel(){
      this.addDialogVisible = false;
      this.$refs.compChildInfo.resetFields();
      this.compChildInfo = {};
    },
    // 获取部门列表
    getList() {
      compAndChildrenInfo().then((response) => {
        this.datamsg = response;
        this.$emit('treeMsg', this.datamsg);
      }).catch(() => {
        this.$emit('treeMsg', false);
      });
    },
    // 添加子节点
    handleAdd(s, d, n) {
      this.addSection.parentDeptId = d.id;
      this.addDialogVisible = true;
      this.$emit('treeAdd', s, d, n);
    },
    // 删除节点
    handleDel(s, d, n) {
      this.$confirm('是否删除该子公司？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        getDelete(
          this.$qs.stringify({
            compId: d.compId
          })
        ).then(() => {
          this.getList();
          this.$message({
            message: '删除成功！',
            type: 'success',
          });
        })
      }).catch(() => {
        return false;
      });
    },
    // 树节点的内容区的渲染
    renderContent(h, {
      node,
      data,
      store
    }) {
      const that = this;
      if (data.compId === this.compId) {
        return h(dropdownA, {
          props: {
            NODE: node,
            DATA: data,
            STORE: store
          },
          on: {
            nodeAdd: (n, d, s) => {
              that.handleAdd(n, d, s)
            }
          }
        });
      }
      if (data.compId !== this.compId) {
        return h(dropdownChildren, {
          props: {
            NODE: node,
            DATA: data,
            STORE: store
          },
          on: {
            nodeDel: (n, d, s) => {
              that.handleDel(n, d, s)
            }
          }
        });
      }
    },
    // 点击树
    nodeClick(data) {
      this.$emit('treeClick', data);
    },
    /* 添加子部门 */
    addDeptName(formName) {
      this.compChildInfo.parentCompId = this.compId;
      this.$refs.compChildInfo.validate((valid) => {
        if (valid) {
          getCreate(this.compChildInfo)
            .then(() => {
              this.$message({
                message: '保存成功',
                type: 'success',
              });
              this.getList();
              this.cancel();
            }).catch(() => { this.cancel(); });
        } else {
          this.$message({
            message: '带*号的为必填项！',
            type: 'warning',
          });
          return false;
        }
      });
    }
  }
};

</script>
<style lang="scss">
  .el-tree-node__content {
    height: 40px;
  }
  /deep/ .el-dropdown-menu {
    margin-top: 8px !important;
    margin-left: 20px !important;
  }
.custom-tree-container {
  height: calc(86vh - 10px);
  overflow-y: auto;
  overflow-x: hidden;
  padding-top: 10px;
  width: 280px !important;
  .el-tree-node__content {
    border-left: 10px solid rgba(0, 0, 0, 0);
  }
  .el-tree-node__content:hover {
    background-color: #dde9fc;
    border-left: 10px solid #1d6ced;
  }
  .el-tree .is-current {
    .el-tree-node__content {
      background-color: #dde9fc;
      border-left: 10px solid #1d6ced;
    }
    .el-tree-node__children .el-tree-node__content {
      background-color: #fff;
      border-left: 10px solid rgba(0, 0, 0, 0);
    }
    .el-tree-node__children .el-tree-node__content:hover {
      background-color: #dde9fc;
      border-left: 10px solid #1d6ced;
    }
  }
}
.el-tree-node__content > .el-tree-node__expand-icon {
  padding: 0;
  width: 18px;
}
  .more-text {
    font-size: 12px;
    padding-right: 10px;
    color: #666;
  }
</style>
