<script lang="ts" setup>
import { ElMessageBox } from 'element-plus'
import type Node from 'element-plus/es/components/tree/src/model/node'
import DetailForm from './components/DetailForm/index.vue'

// import api from '@/api'

const menuRef = ref()
const menu = ref([])
const form = ref({
  visible: false,
  id: '',
  parentId: '',
})

const getMenuList = () => {
  // api.get('pages_example/menu/list', {
  //   baseURL: '/mock/',
  // }).then((res: { data: never[] }) => {
  //   menu.value = res.data
  // })
}
onMounted(() => {
  getMenuList()
})

const menuAdd = (data: any) => {
  form.value = {
    visible: false,
    id: '',
    parentId: '',
  }
  nextTick(() => {
    form.value = {
      visible: true,
      id: '',
      parentId: data.id,
    }
  })
}
const menuEdit = (data?: any) => {
  form.value = {
    visible: false,
    id: '',
    parentId: '',
  }
  nextTick(() => {
    form.value = {
      visible: true,
      id: data?.id ?? '',
      parentId: data?.parentId ?? '',
    }
  })
}
const menuDelete = (node: Node, data: any) => {
  ElMessageBox.confirm(`确认删除「${data.label}」吗？`, '确认信息').then(() => {

  })
}
const menuSubmit = () => {
  form.value = {
    visible: false,
    id: '',
    parentId: '',
  }
  getMenuList()
}
</script>

<template>
  <div class="absolute-container">
    <page-header title="导航管理" content="页面数据为 Mock 示例数据，非真实数据。" />
    <div class="page-main">
      <LayoutContainer left-side-width="300px" hide-left-side-toggle>
        <template #leftSide>
          <el-button-group class="btns">
            <el-button type="primary" class="add" @click="menuEdit()">
              新增主导航
            </el-button>
            <el-button @click="getMenuList">
              <template #icon>
                <el-icon>
                  <svg-icon name="i-ep:refresh" />
                </el-icon>
              </template>
            </el-button>
          </el-button-group>
          <el-scrollbar class="tree">
            <ElTree ref="menuRef" :data="menu" default-expand-all>
              <template #default="{ node, data }">
                <div class="custom-tree-node">
                  <div class="label" :title="data.meta.title">
                    <el-icon v-if="data.meta.icon" size="16px" style="margin-right: 5px;">
                      <svg-icon :name="data.meta.icon" />
                    </el-icon>
                    <div class="text">
                      {{ data.meta.title }}
                    </div>
                  </div>
                  <div class="actions">
                    <el-button-group>
                      <el-button type="primary" plain size="small" @click.stop="menuAdd(data)">
                        <template #icon>
                          <el-icon>
                            <svg-icon name="i-ep:plus" />
                          </el-icon>
                        </template>
                      </el-button>
                      <el-button type="info" plain size="small" @click.stop="menuEdit(data)">
                        <template #icon>
                          <el-icon>
                            <svg-icon name="i-ep:edit" />
                          </el-icon>
                        </template>
                      </el-button>
                      <el-button type="danger" plain size="small" @click.stop="menuDelete(node, data)">
                        <template #icon>
                          <el-icon>
                            <svg-icon name="i-ep:delete" />
                          </el-icon>
                        </template>
                      </el-button>
                    </el-button-group>
                  </div>
                </div>
              </template>
            </ElTree>
          </el-scrollbar>
        </template>
        <!-- <DetailForm v-if="form.visible" :id="form.id" :parent-id="form.parentId" @on-submit="menuSubmit" />
        <div v-else class="empty">
          请在左侧新增或编辑一个导航
        </div> -->
      </LayoutContainer>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.absolute-container {
  position: absolute;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;

  .page-header {
    margin-bottom: 0;
  }

  .page-main {
    flex: 1;
    overflow: auto;
    display: flex;
    flex-direction: column;

    .flex-container {
      position: static;
    }
  }
}

.flex-container {
  :deep(.left-side) {
    display: flex;
    flex-direction: column;
    height: 100%;

    .btns {
      display: inline-flex;
      width: 100%;
      margin-bottom: var(--container-padding);

      .add {
        width: 100%;
      }
    }

    .tree {
      flex: 1;
      overflow-y: auto;

      .el-tree {
        .el-tree-node__content {
          height: 40px;
        }

        .is-current > .el-tree-node__content {
          background-color: var(--el-color-primary-light-9);
        }

        .custom-tree-node {
          flex: 1;
          position: relative;
          width: 0;
          height: 100%;
          display: flex;
          flex-direction: column;
          justify-content: center;

          .label {
            display: inline-flex;
            align-items: center;
            width: calc(100% - 10px);
            color: var(--el-text-color-primary);

            .text {
              @include text-overflow;
            }
          }

          &:hover {
            .actions {
              display: block;
            }
          }

          .actions {
            display: none;
            position: absolute;
            right: 10px;
            top: 50%;
            transform: translateY(-50%);

            .el-button {
              padding: 5px 8px;
            }
          }
        }
      }
    }
  }

  :deep(.main) {
    .main-container {
      display: flex;
      flex-direction: column;
      justify-content: center;
      width: 100%;
      height: 100%;
      padding: 0;
    }
  }

  .empty {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 32px;
    color: var(--el-text-color-placeholder);
  }
}
</style>
