<template>
  <Page class="page">
    <ElForm v-show="showSearchForm" inline @submit.prevent="listAll">
      <ElFormItem label="菜单名称">
        <ElInput v-model="searchForm.title" placeholder="菜单名称"/>
      </ElFormItem>
      <ElFormItem label="路由名称">
        <ElInput v-model="searchForm.routeName" placeholder="路由名称"/>
      </ElFormItem>
      <ElFormItem>
        <ElButton :icon="elIcons.Search" :loading="searching" native-type="submit" type="primary">搜索</ElButton>
        <ElButton :icon="elIcons.Refresh" :loading="searching" @click="reset">重置</ElButton>
      </ElFormItem>
    </ElForm>

    <div class="tool-bar">
      <ElButton :icon="elIcons.Plus" type="primary" @click="addHandler">新建</ElButton>
      <ElButton :icon="elIcons.Filter" type="default" @click="showSearchForm = !showSearchForm"/>
    </div>

    <ElTable v-loading="searching" :data="tableData"
      :load="treeLoad" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      cell-class-name="table-cell"
      class="table-list"
      empty-text="暂无数据"
      header-row-class-name="table-header"
      lazy
      row-key="id">
      <ElTableColumn label="图标" prop="icon"/>
      <ElTableColumn label="菜单名称" prop="title"/>
      <ElTableColumn label="类型" prop="menuCategoryTxt"/>
      <ElTableColumn label="路径" prop="breadcrumb"/>
      <ElTableColumn label="路由名称" prop="routeName"/>
      <ElTableColumn label="路由地址" prop="routePath"/>
      <ElTableColumn label="排序" prop="sort"/>
      <ElTableColumn label="操作">
        <template #default="scope">
          <ElButton text type="danger" @click="delHandler(scope)">删除</ElButton>
          <ElButton text type="primary" @click="modifyHandler(scope)">修改</ElButton>
        </template>
      </ElTableColumn>
    </ElTable>

    <MenuForm ref="menuForm"/>

  </Page>
</template>

<script lang="ts" setup>
import MenuApi from '@/pages/sys/menus/menu-api.ts'
import Page from '@/components/page/Page.vue'
import { onMounted } from 'vue'
import { elIcons } from '@/common/element/element.ts'
import MenuForm from '@/pages/sys/menus/menu-form/MenuForm.vue'

const tableData = ref<MenuTypes.SysMenu[]>([])
const searchForm = reactive<MenuTypes.SearchForm>({})
const searching = ref(false)
const showSearchForm = ref(true)
const menuFormIns = useTemplateRef<InstanceType<typeof MenuForm>>('menuForm')

function showDialog(data?: MenuTypes.MenuForm) {
  menuFormIns.value?.setValue(data)
}

function delHandler({row}: { row: MenuTypes.MenuForm, }) {
  MenuApi.del([ row.id! ])
}

function modifyHandler({row}: { row: MenuTypes.MenuForm, }) {
  showDialog(row)
}

function addHandler() {
  showDialog()
}

function reset() {
  Object.assign(searchForm, {})
  listAll()
}

function listAll() {
  searching.value = true
  MenuApi.listAll({...searchForm, pid: '0'})
    .then(res => {
      tableData.value = res.data?.map(it => {
        it.hasChildren = true
        return it
      }) ?? []
    })
    .finally(() => {
      searching.value = false
    })
}

function treeLoad(row: MenuTypes.SysMenu, _: any, resolve: (data: MenuTypes.SysMenu[]) => void) {
  searching.value = true
  MenuApi.listAll({...searchForm, pid: row.id})
    .then(res => {
      resolve(res.data?.map(it => {
        it.hasChildren = true
        return it
      }) ?? [])
    })
    .finally(() => {
      searching.value = false
    })
}

onMounted(() => {
  listAll()
})

</script>

<style lang="stylus" scoped>
.page {
  display flex
  flex-direction column
}

.table-list {
  flex 1;

  :deep(.table-header) {
    color black

    th {
      background-color #E1E5EB
      font-weight 500
      position relative

      & > div {
        display flex
        gap 5px
        align-items center
      }

      &:not(:first-child) > div::before {
        position: absolute;
        top: 50%;
        left: 1px;
        width: 1px;
        background-color: #A6AFB5;
        transform: translateY(-50%);
        content: "";
        height 50%
      }
    }
  }

  :deep(.table-cell) {
    color #2F3540
  }
}

.tool-bar {
  display flex
  justify-content space-between
  margin 0 0 20px 0
}
</style>
