<template>
  <div
    :class="{ root: index === 1 }"
    class="tree"
    v-if="getFlatFloders(root).length !== 0"
  >
    <div
      class="solid header"
      :class="{ ['root-header']: index === 1 }"
      :style="{ '--index': index }"
      @click="showChildren = !showChildren"
    >
      <span> {{ root.title }} </span>
      <ui-icon
        :type="`pull-${showChildren ? 'up' : 'down'}`"
        class="icon"
        ctrl
        v-if="floders.length || root.children?.length"
      />
    </div>

    <template v-if="!root.modal && showChildren && (floders.length || children?.length)">
      <div class="items" :class="{ ['root-items']: index === 1 }">
        <template v-if="floders.length">
          <div
            :style="{ '--index': index }"
            v-for="floder in floders"
            :key="floder.filesId"
            class="fun-ctrl solid item"
            @click="$emit('preview', [floder, root])"
          >
            <ui-icon :type="typeIcons[floder.metaType]" v-if="floder.metaType" />
            <p>{{ floder.filesTitle }}</p>
          </div>
        </template>
        <template v-if="children?.length">
          <FloderView
            v-for="item in children"
            @preview="(v: any) => emit('preview', v)"
            :root="item"
            :index="index + 1"
          />
        </template>
      </div>
    </template>
  </div>

  <Modal
    v-if="root.modal"
    width="1200px"
    :title="root.title"
    @cancel="showChildren = false"
    :open="showChildren"
    :footer="null"
  >
    <div class="modal-root-content">
      <ModalFloderView :root="root" @preview="(v) => emit('preview', v)" />
    </div>
  </Modal>
</template>
<script lang="ts" setup>
import { Floder, FloderRoot, getFlatFloders } from "@/store";
import { computed, ref, toRaw, watchEffect } from "vue";
import { MetaType } from "@/utils";
import ModalFloderView from "./modal-floder-view.vue";
import { Modal } from "ant-design-vue";

const props = defineProps<{ root: FloderRoot; index?: number }>();
const emit = defineEmits<{ (e: "preview", v: [Floder, FloderRoot]): void }>();

const index = props.index || 1;
const typeIcons = {
  [MetaType.image]: "pic",
  [MetaType.video]: "a-film",
  [MetaType.other]: "nav-edit",
  [MetaType.audio]: "nav-edit",
  [MetaType.xfile]: "nav-edit",
};

const floders = computed(() => {
  if (props.root.flat) {
    return getFlatFloders(props.root);
  } else {
    return props.root.floders;
  }
});
const children = computed(() => {
  if (props.root.flat || props.root.modal) {
    return [];
  } else {
    return props.root.children;
  }
});
const showChildren = ref(props.root.modal ? false : true);
watchEffect(() => {
  if (showChildren.value) {
    // console.log(toRaw(props.root));
  }
});
</script>

<style lang="scss" scoped>
.tree {
  margin-bottom: 0;
}
.modal-root-content {
  max-height: 700px;
  overflow-y: auto;
}
.root-items {
  background: rgba(0, 0, 0, 0.5);
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0;
  cursor: pointer;
  padding: 20px 0;
  position: relative;
  padding-left: calc(var(--index) * 20px);
  padding-right: 20px;

  span {
    font-weight: normal;
    font-size: 14px;
  }

  &.root-header {
    padding: 20px;
    span {
      font-weight: bold;
      font-size: 16px;
    }
  }

  .icon {
    font-size: 14px;
  }
}

.solid {
  &::after {
    content: "";
    position: absolute;
    left: 20px;
    right: 20px;
    height: 1px;
    background: rgba(255, 255, 255, 0.16);
    bottom: 0;
  }
}

.item {
  margin-right: 20px;
  padding: 20px 0;
  cursor: pointer;
  display: flex;
  align-items: center;
  position: relative;
  padding-left: calc(var(--index) * 20px);
  padding-right: 0;

  &.solid::after {
    right: 0;
  }

  p {
    margin-left: 10px;
    font-size: 12px;
    color: currentColor;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
  }
}
</style>
