<template>
  <div class="menu-container">
    <div class="search-box">
      <input v-model="searchState.searchText" class="input" @input="changeSearchText" @keyup.enter="changeSearchText" />
      <div v-show="searchState.showList" class="search-content">
        <div class="search-title">全局搜索</div>
        <div class="search-sub-content">
          <div class="search-header">
            <div class="header-left">
              <i class="icon-position1" />
            </div>
          </div>
          <div class="search-list">
            <div class="scroll">
              <div v-for="(item, index) in searchState.resultList" :key="index" class="list-item" @click="selectSearchMarker(item)">
                <i class="icon1" />
                <div class="text1" :title="item.name">{{ item.name }}</div>
                <!--              <div class="text2">{{ item.address }}</div>-->
              </div>
              <div v-show="searchState.showList && searchState.resultList.length === 0" style="text-align: center">无数据</div>
            </div>
          </div>
        </div>
        <div class="search-sub-content">
          <div class="search-header">
            <div class="header-left">
              <i class="icon-layer" />
              <div class="text1">图层</div>
            </div>
          </div>
          <div class="search-list" style="height: 160px">
            <div class="scroll" style="height: 150px">
              <div
                v-for="(item, index) in searchState.layerList"
                :key="index"
                class="list-item"
                style="margin-top: 5px"
                @click="selectSearchMarker(item)"
              >
                <div class="tag">{{ item.dict_label }}</div>
                <div class="text1" :title="item.name">{{ item.name }}</div>
              </div>
              <div v-show="searchState.showList && searchState.layerList.length === 0" style="text-align: center">无数据</div>
            </div>
          </div>
        </div>
        <div class="search-sub-content">
          <div class="search-header">
            <div class="header-left">
              <i class="icon-video" />
              <div class="text1">视频</div>
            </div>
          </div>
          <div class="search-list" style="height: 160px">
            <div class="scroll" style="height: 150px">
              <div
                v-for="(item, index) in searchState.videoList"
                :key="index"
                class="list-item2"
                style="margin-top: 5px"
              >
                <div class="text1" :title="item.name" @click="handleShowDialog(item)">{{ item.name }}</div>
                <div class="tags">
                  <div v-for="(item2, index2) in item.tags" :key="index2" class="tag">{{ item2.name }}</div>
                </div>
              </div>
              <div v-show="searchState.showList && searchState.videoList.length === 0" style="text-align: center">无数据</div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="menu-box">
      <div class="menu-btn" @click="changExpand">
        <div class="gradient-text">{{ menuState.isExpand ? '收起' : '展开' }}</div>
      </div>
      <Transition>
        <div v-show="menuState.isExpand" class="menu-box2">
          <div class="menu-header">
            <div
              v-for="(item, index) in menuData"
              :key="index"
              :class="menuState.activeIndex === index ? 'menu-item active' : 'menu-item'"
              @click="changeActive(index)"
            >
              <div class="gradient-text text">{{ item.name }}</div>
            </div>
          </div>
          <div class="menu-content">
            <div v-for="(item, index) in menuData[menuState.activeIndex]?.children" :key="index" class="content-box">
              <div class="box-header">
                <div v-if="item.name === '防风防汛'" class="icon1" style="width: 24px; height: 26px"></div>
                <div v-if="item.name === '危化品安全监测' || item.name === '危险化工品'" class="icon2" style="width: 23px; height: 26px"></div>
                <div v-if="item.name === '其他'" class="icon3" style="width: 26px; height: 26px"></div>
                <div v-if="item.name === '水情监测'" class="icon4" style="width: 29px; height: 26px"></div>
                <div v-if="item.name === '自然灾害监测'" class="icon5" style="width: 25px; height: 26px"></div>
                <div>{{ item.name }}</div>
                <div :class="item.show ? 'expand-icon down-icon' : 'expand-icon up-icon'" @click="changeBoxShow(item)"></div>
              </div>
              <Transition>
                <div v-show="item.show" class="box-content">
                  <div
                    v-for="(item2, index2) in item.children"
                    :key="index2"
                    :class="item2.checked ? 'box-item box-item-active' : 'box-item'"
                    @click="handleClick(item2)"
                  >
                    <div :class="item2.checked ? 'checked-box checked' : 'checked-box'"></div>
                    {{ item2.name }}
                  </div>
                </div>
              </Transition>
            </div>
          </div>
        </div>
      </Transition>
    </div>
    <Dialog
      v-if="showDialog"
      v-model="showDialog"
      v-model:tags="videoMonitorData.tags"
      type="lg"
      header-type="header2"
      title="视频"
      :get-tag-id="videoMonitorData.video_code"
      hide-footer
    >
      <div style="width: 100%; height: 100%; display: flex; justify-content: center; align-items: center">
        <HKVideo :dot_data="videoMonitorData" />
      </div>
    </Dialog>
  </div>
</template>

<script lang="ts" setup>
import { getPointInfoComprehensiveSearch } from '@/api/globalMap';
import { listMenu2 } from '@/api/system/menu';
import { deepClone } from '@/utils';

const emits = defineEmits(['switchMap', 'clickMenu', 'selectSearchMarker']);
// 左侧菜单
let menuData = ref([]);

const searchState = reactive({
  searchText: '',
  showList: false,
  resultList: [],
  layerList: [],
  videoList: []
});
let showDialog = ref(false);
let videoMonitorData = ref({});
const changeSearchText = () => {
  if (!searchState.searchText) {
    searchState.showList = false;
    return;
  } else {
    console.log('搜索值：', searchState.searchText);
    getPointInfoComprehensiveSearch({
      keyword: searchState.searchText
    }).then((res) => {
      searchState.showList = true;
      searchState.resultList = res.data.add_data;
      searchState.layerList = res.data.point_data;
      searchState.videoList = res.data.video_data;
    });
  }
};

// 点击搜索结果，添加标注
const selectSearchMarker = (item) => {
  searchState.showList = false;
  emits('selectSearchMarker', item);
};

const menuState = reactive({
  // 是否展开菜单
  isExpand: true,
  activeIndex: 0
});
// 展示收起整个菜单
const changExpand = () => {
  menuState.isExpand = !menuState.isExpand;
};

// 切换tab
const changeActive = (index) => {
  menuState.activeIndex = index;
};

// 菜单内容展开收缩
const changeBoxShow = (item) => {
  item.show = !item.show;
};

// 处理菜单点击事件
const handleClick = (item) => {
  item.checked = !item.checked;
  emits('clickMenu', item, menuData.value);
};

const initData = () => {
  listMenu2().then((res: any) => {
    const data = res.data ? res.data[0]?.children : [];
    data.forEach((item) => {
      item.show = true;
      item.name = item.meta?.title;
      item.children?.forEach((item2) => {
        item2.show = true;
        item2.name = item2.meta?.title;
        item2.children?.forEach((item3) => {
          item3.name = item3.meta?.title;
          if (item3.component === '2' && !!item3.path) {
            item3.checked = false;
          }
        });
      });
    });
    menuData.value = data;
    console.log(menuData.value);
  });
};

const setMenuChange = (item, flag) => {
  for (let i = 0; i < menuData.value.length; i++) {
    if (item.meta.title === menuData.value[i].meta.title) {
      menuData.value[i].checked = flag;
      break;
    } else if (!!menuData.value[i].children) {
      const res = setChild(menuData.value[i].children, item, flag);
      if (!!res) {
        break;
      }
    }
  }
};
const setChild = (data, item, flag) => {
  let res = false;
  for (let i = 0; i < data.length; i++) {
    if (item.meta.title === data[i].meta.title) {
      data[i].checked = flag;
      break;
    } else if (!!data[i].children) {
      res = setChild(data[i].children, item, flag);
      if (!!res) {
        break;
      }
    }
  }
  return res;
};
const handleShowDialog = (row) => {
  showDialog.value = false;
  nextTick(() => {
    const data = deepClone(row);
    data.video_code = data.id;
    videoMonitorData.value = data;
    showDialog.value = true;
  });
};
onMounted(() => {
  initData();
});
defineExpose({ setMenuChange });
</script>

<style lang="scss" scoped>
.search-box {
  width: 260px;
  height: 27px;
  padding: 0 10px;
  background: url('@/assets/images/menu/search.png') no-repeat;
  background-size: 100% 100%;
  position: relative;
  margin: 0 auto;
  .input {
    border: none;
    outline: none;
    color: #fff;
    width: calc(100% - 20px);
    margin-left: 22px;
    height: 22px;
    font-size: 14px;
    background-color: transparent;
  }

  .search-content {
    position: absolute;
    top: 30px;
    left: -12px;
    //width: 260px;
    //height: 613px;
    width: 285px;
    height: 672px;
    background: url('@/assets/images/map/box1.png') no-repeat;
    background-size: 100% 100%;
    color: #ffffff;
    z-index: 9;
    padding: 10px 6px;
    .search-title {
      font-size: 18px;
      line-height: 36px;
      padding: 0 9px;
    }
    .search-sub-content {
      width: 100%;
      .search-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        width: 379px;
        height: 39px;
        background: url('@/assets/images/map/titleBox3.png') no-repeat;
        background-size: 100% 100%;
        padding-left: 10px;
        padding-bottom: 16px;
        .header-left {
          display: flex;
          align-items: center;
          .icon-position1 {
            display: inline-block;
            width: 52px;
            height: 55px;
            background: url('@/assets/images/map/position.png') no-repeat;
            background-size: 100% 100%;
          }
          .icon-layer {
            display: inline-block;
            width: 55px;
            height: 51px;
            background: url('@/assets/images/map/layer.png') no-repeat;
            background-size: 100% 100%;
          }
          .icon-video {
            display: inline-block;
            width: 53px;
            height: 53px;
            background: url('@/assets/images/map/video.png') no-repeat;
            background-size: 100% 100%;
          }
          .text1 {
            margin-left: -12px;
            font-size: 16px;
          }
        }
      }
    }
    .search-list {
      width: 260px;
      height: 190px;
      padding-bottom: 10px;
      background: url('@/assets/images/map/base.png') no-repeat center bottom;
      background-size: 260px 49px;
      .scroll {
        height: 180px;
        overflow-y: auto;
      }
      .list-item,
      .list-item2 {
        display: flex;
        align-items: center;
        cursor: pointer;
        margin-top: -11px;
        .icon1 {
          display: inline-block;
          width: 53px;
          height: 56px;
          background: url('@/assets/images/map/position2.png') no-repeat;
          background-size: 100% 100%;
          flex-shrink: 0;
        }
        .tag {
          width: 126px;
          height: 26px;
          background: url('@/assets/images/map/tag.png') no-repeat;
          background-size: 100% 100%;
          flex-shrink: 0;
          color: #ffffff;
          font-size: 14px;
          display: flex;
          justify-content: center;
          align-items: center;
        }
        .text1 {
          margin-left: 5px;
          font-size: 14px;
          //overflow: hidden;
          //text-overflow: ellipsis;
          //white-space: nowrap;
        }
      }
      .list-item2 {
        flex-direction: column;
        align-items: unset;
        .tags {
          display: flex;
          flex-wrap: wrap;
          .tag {
            margin-top: 5px;
          }
        }
      }
    }
  }
}

.menu-box {
  width: 280px;
  height: 551px;
  margin-top: 10px;
  color: #fff;
  position: relative;

  .menu-box2 {
    width: 280px;
    height: 551px;
    background: url('@/assets/images/menu/menu.png') no-repeat;
    background-size: 100% 100%;
    padding: 10px 20px 10px 24px;
  }

  .menu-btn {
    position: absolute;
    top: -5px;
    left: 50%;
    transform: translateX(-50%);
    letter-spacing: 5px;
    font-size: 16px;
    cursor: pointer;
    width: 125px;
    height: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    background: url('@/assets/images/menu/btn.png') no-repeat;
    background-size: 100% 100%;
  }

  .menu-header {
    display: flex;
    margin-top: 15px;
    height: 28px;

    .menu-item {
      width: 87px;
      height: 20px;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      &:hover {
        position: relative;
        background: url('@/assets/images/menu/menuActive.png') no-repeat bottom center;
        background-size: 100% 100%;
      }
      .text {
        font-size: 14px;
      }
    }

    .active {
      background: url('@/assets/images/menu/menuActive.png') no-repeat bottom center;
      background-size: 100% 100%;
    }
  }

  .menu-content {
    margin: 0 0 0 -6px;
    height: 488px;
    overflow-y: auto;
    overflow-x: hidden;
    .content-box {
      padding: 0 5px;
      width: 250px;
      background: url('@/assets/images/menu/menuContent.png') no-repeat;
      background-size: 100% 100%;

      .box-header {
        height: 36px;
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative;
        font-size: 14px;
        font-weight: 500;
        position: relative;
        &::after {
          position: absolute;
          bottom: 0;
          left: 0;
          content: '';
          width: 100%;
          height: 3px;
          background-color: rgba(142, 189, 241, 0.3);
        }
        .icon1 {
          background-image: url('@/assets/images/menu/icon1.png');
          background-size: 100% 100%;
        }

        .icon2 {
          background-image: url('@/assets/images/menu/icon2.png');
          background-size: 100% 100%;
        }
        .icon3 {
          background-image: url('@/assets/images/menu/icon3.png');
          background-size: 100% 100%;
        }
        .icon4 {
          background-image: url('@/assets/images/menu/icon4.png');
          background-size: 100% 100%;
        }
        .icon5 {
          background-image: url('@/assets/images/menu/icon5.png');
          background-size: 100% 100%;
        }

        .expand-icon {
          position: absolute;
          top: 50%;
          right: 30px;
          transform: translateY(-50%);
          cursor: pointer;
          width: 11px;
          height: 6px;
        }

        .down-icon {
          background: url('@/assets/images/menu/down.png') no-repeat;
          background-size: 100% 100%;
        }

        .up-icon {
          background: url('@/assets/images/menu/up.png') no-repeat;
          background-size: 100% 100%;
        }
      }

      .box-content {
        display: flex;
        align-items: center;
        flex-wrap: wrap;
        padding: 5px 3px;
        overflow: hidden;

        .box-item-active {
          text-shadow: 0px 0px 4px #0766d6;
        }

        .box-item {
          padding: 7px 4px;
          cursor: pointer;
          font-size: 14px;
          font-family: 'PingFang SC', sans-serif;
          display: flex;
          align-items: center;

          &:hover {
            text-shadow: 0px 0px 8px #0766d6;
          }

          .checked-box {
            width: 10px;
            height: 10px;
            background: url('@/assets/images/menu/checked-box.png') no-repeat;
            background-size: 100% 100%;
            margin-right: 4px;
          }

          .checked {
            background: url('@/assets/images/menu/checked-box2.png') no-repeat;
            background-size: 100% 100%;
          }
        }
      }
    }
  }
}

/* 切换动画 */
.v-enter-active,
.v-leave-active {
  transition: opacity 0.5s ease;
}

.v-enter-from,
.v-leave-to {
  opacity: 0;
}
</style>
