<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-08-14 16:51:07
 * @LastEditors  : 无尘
 * @LastEditTime : 2020-02-18 14:59:04
 -->
<!--
<wx-tree v-model="itemData" ></wx-tree>

import wxTree from './wx-tree.vue';
-->
<template>
  <ul class="">
    <draggable :list="itemData" class="wx-component" :options="rightOption" @input="emitter" @change="changeData">
      <li v-for="(item, index) in itemData" :key="index + 'gic'">
        <div class="li-cell cursor-pointer" @click="toggleExpand(item)">
          <div class="flex flex-space-between boder-box p-r-20" :style="{ 'padding-left': item.level * 10 + 'px' }">
            <div class="tree-left">
              <span :class="['font-12 color-606266', item.expand ? 'el-icon-caret-bottom' : 'el-icon-caret-right']"></span><span :class="['font-14 p-l-10 p-r-10 iconfont', item.isStore == 1 ? 'icondianpu-kuai' : 'iconqiye-tianchong', item.bindFlag == 1 ? 'color-2f54eb' : 'color-dedfe6']"></span><span class="font-14 color-606266">{{ item.label }}</span>
            </div>
            <div class="tree-right">
              <el-button v-if="item.isStore == 0" type="text" @click.stop="addChild(item)">添加子部门</el-button>
              <el-button type="text" @click.stop="modChild(item)">修改名称</el-button>
              <el-button v-if="item.level > 1" type="text" @click.stop="delChild(item)">删除</el-button>
            </div>
          </div>
        </div>
        <li-row v-if="item.expand" :itemData="item.children"></li-row
        ><!-- v-show="!!item.children.length && item.expand" -->
      </li>
    </draggable>
    <add-depart v-if="addShow" :departObj="departObj" @refreshData="refreshData"></add-depart>
  </ul>
</template>
<script>
import addDepart from '@/components/company/add-depart.vue';
import draggable from 'vuedraggable';
// import showMsg from '@/common/js/showmsg';
import errMsg from '@/common/js/error';
import { postRequest } from '@/api/api';
export default {
  name: 'li-row',
  components: {
    draggable,
    addDepart
  },
  props: {
    value: {
      type: [Object, Array],
      default() {
        return [];
      }
    },
    itemData: {
      type: [Object, Array],
      default() {
        return [];
      }
    }
  },
  data() {
    return {
      rightOption: {
        group: {
          name: 'people',
          pull: true,
          put: true
        },
        sort: true
      },
      addShow: false,
      additem: {},
      departObj: {}
    };
  },
  computed: {},
  methods: {
    /**
     *  改变数组数据
     */
    emitter(value) {
      // console.log(value);
      this.$emit('input', value);
    },
    changeData(value) {
      // console.log(value);
    },
    /**
     * 展开或者关闭下级
     */
    toggleExpand(item) {
      const that = this;
      // console.log(item);
      item.expand = item.expand ? false : true;
      if (!item.children.length) {
        item.expand = true;
      }
      that.$forceUpdate();
    },
    /**
     * 添加子部门
     */
    addChild(item) {
      const that = this;
      // console.log('添加：', item);
      that.addShow = true;
      that.additem = item;
    },
    /**
     * 修改名称
     */
    modChild(item) {
      const that = this;
      // console.log('修改：', item);
      item.label = 2222;
      that.addShow = true;
      that.$forceUpdate();
    },

    refreshData(name) {
      const that = this;
      that.addShow = false;
      if (name == 'close') {
        return false;
      }
      that.additem.children.push({
        id: String(new Date().getTime()),
        expand: true,
        level: that.additem.level + 1,
        isStore: 0,
        label: name,
        children: []
      });

      that.$forceUpdate();
    },

    /**
     * 搜索查询列表
     */
    delPost(item) {
      const that = this;
      let params = {
        departmentId: item.departmentId
      };
      return new Promise((resolve, reject) => {
        postRequest('/haoban-manage3-web/department-del', params)
          .then(res => {
            let resData = res.data;
            if (resData.errorCode == 1) {
              resolve(resData.result);
              return false;
            }
            errMsg.errorMsg(resData);
            resolve(false);
          })
          .catch(function(error) {
            resolve(false);
            that.$message.error({
              duration: 1000,
              message: error.message
            });
          });
      });
    },

    /**
     * 删除
     */
    async delChild(item) {
      const that = this;
      const flag = await that.delPost(item);
      // console.log('删除:', item, that.itemData);
      if (!flag) {
        // showMsg.showmsg('当前节点下有子节点，不可删除', 'warning');
        return false;
      }
      let key = '';
      that.itemData.forEach((ele, index) => {
        if (ele.id == item.id) {
          key = index;
        }
      });
      that.itemData.splice(key, 1);
      that.$forceUpdate();
    }
  },
  watch: {
    value: function(newData, oldData) {
      const that = this;
      that.trData = JSON.parse(JSON.stringify(newData));
    }
  },
  mounted() {
    const that = this;
    that.trData = JSON.parse(JSON.stringify(that.value));
  }
  /*  beforeDestroy() {} */
};
</script>
<style lang="less" scoped>
.li-cell {
  min-height: 26px;
  line-height: 26px;
  .tree-right {
    display: none;
  }
  &:hover {
    background: rgba(47, 84, 235, 0.05);
    .tree-right {
      display: block;
    }
  }
}

.w-110 {
  width: 110px;
}

.w-320 {
  width: 320px;
}

.m-t-14 {
  margin-top: 14px;
}

.child-row {
  padding-left: 10px;
}
</style>
