<template>
  <Card
    ref="resourceSearch"
    class="resource-check ems-components-new"
    :style="resCheckStyle"
  >
    <div slot="title">
      <Input
        v-model.trim="searchForm.valueName"
        size="small"
        @input="search()"
        placeholder="请输入"
      ></Input>
      <!-- <Button
        ref="search"
        size="small"
        type="warning"
        icon="md-search"
        style="margin-left: 10px; height: 24px;"
        :loading="loading.search"
        @click.native="search()"
      >
        {{ $t('查询') }}
      </Button> -->
    </div>

    <div class="resource-check-content">
      <div
        class="resource-tree-wrp"
        :style="resourceTreeStyle"
      >
        <DispatchDimensionTree
          ref="resourceTree"
          :treeData="resourceTree"
          :showCheckbox="showCheckbox"
          @select="select"
        ></DispatchDimensionTree>
      </div>
    </div>
  </Card>
</template>
<script>
import { dispatchDimensionTree } from '../server.js';
import Three from '../../util/getDispatchDimensionTree.js';
import DispatchDimensionTree from './DispatchDimensionTree.vue';
export default {
  components: {
    DispatchDimensionTree
  },
  props: {
    showCheckbox: {
      type: Boolean,
      default: false
    },
    queryOpts: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      loading: {
        search: false
      },
      searchForm: {
        dimensionId: '',
        valueName: '',
        enabled: '1',
        isEffectTime: 1,
        excludeId: ''
      },
      resourceTreeStyle: '',
      resCheckStyle: '',
      defaultTree: [],
      resourceTree: [],
      treeData: null
    };
  },
  created() {
    this.searchForm = Object.assign({}, this.searchForm, this.queryOpts);
    this.resourceTree = JSON.parse(JSON.stringify(this.defaultTree));
    this.search();
  },
  mounted() {
    try {
      this.resourceTreeStyle = 'top: 10px';
      this.resCheckStyle = `height: ${window.innerHeight - this.$refs.resourceSearch.$el.offsetTop - 55}px`;
    } catch (err) {}
  },
  methods: {
    select(data) {
      this.$emit('select', data);
    },
    // 新增
    add() {
      this.$emit('add');
    },

    // 查看对象中是否存在空值
    isEmptyObject(obj) {
      const flag = true;
      for (var key in obj) {
        if (obj[key]) return false;
      }
      return flag;
    },

    /*
    * 树资源初始化
    * currentNodeId | 当前需要展开的节点path
    * isSearch | 是否搜索
    */
    search(currentNodeId = [], thisNode = '') {
      this.loading.search = true;
      dispatchDimensionTree(this.searchForm).then(res => {
        const { code = '', msg = '', data = [] } = res;
        if (code === 'S000A000') {
          this.loading.search = false;
          const tree = new Three(data, {
            sort: true,
            currentNodeId,
            isSearch: this.searchForm.valueName != '',
            thisNode
          });
          this.resourceTree = tree.json;
        } else {
          this.$Message.error(msg || '网络错误！');
        }
      }).catch(() => {
        this.loading.search = false;
        this.$Message.error('网络错误！');
      });
    },
    // 刷新树
    reload(currentNodeId = '', thisNode = '') {
      const nodeId = currentNodeId.split('/');
      nodeId.pop();
      this.search(nodeId, thisNode);
    },
    // 获取选中的树节点
    getSelectedNodes() {
      return this.$refs.resourceTree.getSelectedNodes();
    }
  }
};

</script>
<style lang="less">
.resource-check {
  width: 100%;
  float: left;
  background: #fff;
  .resource-check-content {
    position: absolute;
    top: 40px;
    bottom: 0;
    left: 5px;
    right: 0;
    .resource-tree-wrp {
      position: absolute;
      bottom: 0;
      right: 0;
      left: 0;
      overflow: auto;
    }
    .ivu-form-item {
      width: 100%;
    }
  }
  .ivu-form-item {
    margin-bottom: 0;
  }
  .ivu-form-item-error {
    margin-bottom: 0;
  }
  .ivu-tree-empty {
    padding-top: 15px;
    text-align: center;
  }
}
.ivu-card-bordered {
  border: 0 !important;
}
.core-modal-body:hover,.ivu-card-bordered:hover {
  box-shadow: 0 0 0 #fff !important;
}
.ivu-card-head{
  border-bottom: 0 !important;
}
</style>
