<template>
  <div>
    <n-card>
      <headSearch :info="search" @search="handleSearch" @reset="handleSearch" />
      <data-table
        ref="tableRef"
        :columns="columns"
        :pagination="false"
        data-type="tree"
        :request="loadDataTable"
        :row-key="(row) => row.id"
      >
        <template #tableTitle>
          <n-button type="primary" @click="handleModal"> 添加菜单 </n-button>
        </template>
      </data-table>
    </n-card>
  </div>

  <MenuModal v-if="modalShow" v-model:visible="modalShow" :data="rowData" :type="modalType" @reload="handleSearch" />
</template>

<script>
import search from './tools/search.js'
import table from './tools/table.js'
import headSearch from '@/components/Search/index.vue'
import dataTable from '@/components/DataTable/index.vue'
import MenuModal from './components/MenuModal.vue'
import { getMenuList } from '@/api/system/menu/index.js'
import { unref, reactive, toRefs, onUnmounted } from 'vue'

export default {
  name: 'MenuPage',
  components: { dataTable, MenuModal, headSearch },
  setup() {
    const data = reactive({
      ...toRefs(table),
      search
    })

    const loadDataTable = async(res) => {
      const _params = {
        ...unref(data.searchParams),
        ...res
      }
      return await getMenuList(_params)
    }

    // 打开新增弹框
    function handleModal() {
      data.rowData = null
      data.modalType = 'create'
      data.modalShow = true
    }

    onUnmounted(() => {
      data.searchParams = null
    })

    return {
      ...toRefs(data),
      loadDataTable,
      handleModal
    }
  }
}
</script>
<style scoped lang='scss'>
</style>
