<!-- 资源编目 -->
<template>
  <div style="height: 100%; width: 100%; padding: 0; margin: 0;">
  <div class="mains">
    <!-- 左边 -->
    <div class="left">
    <a-card class="box-card" body-style="{height:'100%'}">
      <a-input-search v-model:value="searchValue" style="margin-bottom: 8px" placeholder="搜索关键字" />
      <a-tree
       :expandedKeys="expandedKeys"
       :auto-expand-parent="autoExpandParent"
       :tree-data="gData"
       @expand="onExpand"
       @select="select"
      >
      <template #title="{ title }">
        <span v-if="title.indexOf(searchValue) > -1">
          {{ title.substr(0, title.indexOf(searchValue)) }}
          <span style="color: #f50">{{ searchValue }}</span>
          {{ title.substr(title.indexOf(searchValue) + searchValue.length) }}
        </span>
        <span v-else>{{ title }}</span>
      </template>
      </a-tree>
     </a-card>
    </div>
    <!-- 右边 -->
    <div class="right">
      <div class="right-top">
       <a-card class="box-card" body-style="{height:'100%'}">
        <!-- <div>可用资源</div> 
        <div style="float: left; margin-left: 150px;">类型：</div> -->
        <a-row>
          <a-col :span="10">可用资源</a-col>
          <a-col :span="14">
           <a-row>
             <a-col :span="10">
              类型：<a-checkbox-group v-model:value="checkedList" :options="plainOptions" />
            </a-col>
            <a-col :span="5">
              <a-select
                ref="select"
                v-model:value="value1"
                style="width: 120px"
                @focus="focus"
                @change="handleChange"
                size="small"
              >
               <a-select-option value="jack">无</a-select-option>
              </a-select>
            </a-col>
            <a-col :span="5">
             <a-input-search
               v-model:value="value"
               placeholder="支持名称搜索"
               style="width: 150px"
               size="small"
               @search="onSearch"
             />
            </a-col>
            <a-col :span="4">
              <a-button type="primary" size="small">查询</a-button>
            </a-col>
           </a-row>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24">
            <div class="right-top-item">
             <a-checkbox-group v-model:value="value">
              <a-row>
               <a-col :span="8">
                <a-checkbox value="A">A</a-checkbox>
               </a-col>
               <a-col :span="8">
                <a-checkbox value="B">B</a-checkbox>
               </a-col>
               <a-col :span="8">
                <a-checkbox value="C">C</a-checkbox>
               </a-col>
               <a-col :span="8">
                <a-checkbox value="D">D</a-checkbox>
               </a-col>
               <a-col :span="8">
                <a-checkbox value="E">E</a-checkbox>
               </a-col>
             </a-row>
           </a-checkbox-group>
           </div>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="20">
            <a-pagination v-model:current="current" :total="50" show-less-items />
          </a-col>
        </a-row>
       </a-card>
      </div>

      <div class="right-bottom">
       <a-card class="box-card" body-style="{height:'100%'}">
        <a-row>
          <a-col :span="10">已关联资源</a-col>
          <a-col :span="14">
           <a-row>
             <a-col :span="10">
              类型：<a-checkbox-group v-model:value="checkedList" :options="plainOptions" />
            </a-col>
            <a-col :span="5">
              <a-select
                ref="select"
                v-model:value="value1"
                style="width: 120px"
                @focus="focus"
                @change="handleChange"
                size="small"
              >
               <a-select-option value="jack">无</a-select-option>
              </a-select>
            </a-col>
            <a-col :span="5">
             <a-input-search
               v-model:value="value"
               placeholder="支持名称搜索"
               style="width: 150px"
               size="small"
               @search="onSearch"
             />
            </a-col>
            <a-col :span="4">
              <a-button type="primary" size="small">查询</a-button>
            </a-col>
           </a-row>
          </a-col>
        </a-row>
        <div class="right-bottom-item">
          <a-checkbox-group v-model:value="value">
            <a-row>
              <a-col :span="8">
                <a-checkbox value="A">A</a-checkbox>
              </a-col>
              <a-col :span="8">
                <a-checkbox value="B">B</a-checkbox>
              </a-col>
              <a-col :span="8">
                <a-checkbox value="C">C</a-checkbox>
              </a-col>
              <a-col :span="8">
                <a-checkbox value="D">D</a-checkbox>
              </a-col>
              <a-col :span="8">
                <a-checkbox value="E">E</a-checkbox>
              </a-col>
            </a-row>
          </a-checkbox-group>
        </div>

       </a-card>
      </div>
    </div>
  </div>
</div>

</template>
<script>
  import { defineComponent, ref, watch } from 'vue';
  const plainOptions = ['地图', '场景', '文件数据'];
  const x = 3;
  const y = 2;
  const z = 1;
  const genData = [];
  const generateData = (_level, _preKey, _tns) => {
  const preKey = _preKey || '0';
  const tns = _tns || genData;
  const children = [];
  for (let i = 0; i < x; i++) {
    const key = `${preKey}-${i}`;
    tns.push({
      title: key,
      key,
    });
    if (i < y) {
      children.push(key);
    }
  }
  if (_level < 0) {
    return tns;
  }
  const level = _level - 1;
  children.forEach((key, index) => {
    tns[index].children = [];
    return generateData(level, key, tns[index].children);
  });
};
generateData(z);
const dataList = [];
const generateList = data => {
  for (let i = 0; i < data.length; i++) {
    const node = data[i];
    const key = node.key;
    dataList.push({
      key,
      title: key,
    });
    if (node.children) {
      generateList(node.children);
    }
  }
};
generateList(genData);
const getParentKey = (key, tree) => {
  let parentKey;
  for (let i = 0; i < tree.length; i++) {
    const node = tree[i];
    if (node.children) {
      if (node.children.some(item => item.key === key)) {
        parentKey = node.key;
      } else if (getParentKey(key, node.children)) {
        parentKey = getParentKey(key, node.children);
      }
    }
  }
  return parentKey;
};

  export default defineComponent({
    setup() {
    const expandedKeys = ref([]);
    const searchValue = ref('');
    const autoExpandParent = ref(true);
    const gData = ref(genData);
    const onExpand = keys => {
      expandedKeys.value = keys;
      autoExpandParent.value = false;
    };
    watch(searchValue, value => {
      const expanded = dataList
        .map(item => {
          if (item.title.indexOf(value) > -1) {
            return getParentKey(item.key, gData.value);
          }
          return null;
        })
        .filter((item, i, self) => item && self.indexOf(item) === i);
      expandedKeys.value = expanded;
      searchValue.value = value;
      autoExpandParent.value = true;
    });
    // 点击树的节点触发
    const select = () => {
      alert(111)
    }
    return {
      expandedKeys,
      searchValue,
      autoExpandParent,
      gData,
      onExpand,
      value: ref([]),
      plainOptions,
      current: ref(2),
      select
    };
  },
  });
</script>

<style lang="scss" scoped>
.mains{
  .box-card{
        height: calc(100% - 0px);
    }

    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px;
    height: calc(100% - 0px) ;

    .left{
        height: calc(100% - 0px);
        width: 20%;
        }

    .right{
      display: flex;
      flex-direction: column; //设置主轴为y轴
      justify-content: flex-start;//子元素排列方式，
      align-items: center;   //设置侧轴上子元素对齐方式
      padding: 20px;
      padding-top: 0;
      padding-bottom: 0;
      height: calc(100% - 0px) ;
      width: 100%;
        // height: 50%;
        // width: 78%;
        // margin-left: 1%;
        // margin-right: 1%;
        // border: 1px red solid;

        .right-top{
          height: 50%;
          width: 100%;
          margin-left: 1%;
          margin-right: 1%;
          margin-bottom: 2%;

          .right-top-item{
            border-style:solid;
            border-width:1px;
            border-color:#ddd;
            box-shadow:0 0 10px #ddd;
            height: 250px;
            margin-top: 10px;
            padding: 10px;
          }
        }

        .right-bottom{
          height: 50%;
          width: 100%;
          margin-left: 1%;
          margin-right: 1%;

          .right-bottom-item{
            border-style:solid;
            border-width:1px;
            border-color:#ddd;
            box-shadow:0 0 10px #ddd;
            height: 250px;
            margin-top: 10px;
            padding: 10px;
          }
        }
    }

}
</style>
