<template>
  <div>
    <n-card>
      <headSearch :info="search" @search="handleSearch" @reset="handleSearch" />
      <data-table
        ref="tableRef"
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row) => row.id"
        :pagination="false"
        data-type="tree"
        size="large"
      >
        <template #tableTitle>
          <n-button type="primary" @click="handleModal"> 新建 </n-button>
        </template>
      </data-table>
    </n-card>
  </div>
  <!-- 新增、编辑弹窗 -->
  <DepartmentModal 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 DepartmentModal from './components/DepartmentModal.vue'
import { getDeptList } from '@/api/system/dept/index.js'
import { unref, toRefs, reactive, onUnmounted } from 'vue'

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

    const loadDataTable = async(res) => {
      const _params = {
        ...unref(data.searchParams),
        ...res
      }
      return await getDeptList(_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'>
.n-button + .n-button {
  margin-left: 10px;
}
</style>
