<template>
  <div>
    <n-card>
      <headSearch :info="info" @search="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>
  <!-- 新增、编辑弹窗 -->
  <dept-modal
    v-if="modalShow"
    v-model:visible="modalShow"
    :row="rowData"
    :dept-list="deptList"
    @done="handleSearch"
  />
</template>

<script>
import headSearch from '@/components/Search/index.vue'
import dataTable from '@/components/DataTable/index.vue'
import TableAction from '@/components/DataTable/tools/Action.vue'
import {
  getDeptList,
  getDeptAll,
  deleteDept
} from '@/api/system/dept/index.js'
import { h, unref, toRefs, ref, reactive, onMounted } from 'vue'
import DeptModal from './components/DeptModal.vue'
import info from './info.js'
import table from './table.js'
export default {
  name: 'MenuPage',
  components: { dataTable, DeptModal, headSearch },
  setup() {
    const data = reactive({
      columns: [
        ...table.columns,
        {
          title: '操作',
          align: 'center',
          width: 150,
          fixed: 'right',
          render(row) {
            return h(TableAction, {
              actions: [
                {
                  label: '添加',
                  type: 'button',
                  props: {
                    type: 'primary',
                    onClick: add.bind(null, row)
                  },
                  auth: 'basic_list'
                },
                {
                  label: '修改',
                  type: 'button',
                  props: {
                    type: 'primary',
                    onClick: play.bind(null, row)
                  },
                  auth: 'basic_list'
                },
                {
                  label: '删除',
                  type: 'popconfirm',
                  auth: 'basic_list',
                  tip: '确定删除这条数据吗?',
                  props: {
                    negativeText: '取消',
                    positiveText: '确认',
                    onPositiveClick: deleteSingle.bind(null, row.id)
                  }
                }
              ],
              align: 'center'
            })
          }
        }
      ],
      info: ref(info),
      modalShow: false,
      rowData: {
        status: 1,
        type: 0,
        hide: 0
      },
      deptList: []
    })

    // 获取全部菜单信息
    async function getDept() {
      const list = await getDeptAll()
      data.deptList = list.data
    }
    onMounted(() => {
      getDept()
    })
    /**
     * @description: 获取部门数据并做树形结构处理
     * @return {*}
     */
    const params = ref({})

    const tableRef = ref()

    function handleSearch(data) {
      params.value = {
        ...data
      }
      tableRef.value.reFetch({ ...unref(params) })
    }

    const loadDataTable = async(res) => {
      const _params = {
        ...unref(params),
        ...res
      }
      return await getDeptList(_params)
    }

    // 新增
    function handleModal() {
      data.modalShow = true
      data.rowData = {}
    }
    // 行内新增
    function add(row) {
      data.modalShow = true
      data.rowData = {}
      data.rowData.pid = row.id
    }

    // 编辑
    function play(row) {
      data.rowData = row
      data.modalShow = true
    }

    // 单个删除数据
    function deleteSingle(id) {
      deleteData(id)
    }

    // 删除方法
    function deleteData(id) {
      deleteDept(id)
        .then((res) => {
          if (res.code === 0) {
            handleSearch({})
            $message.success(res.msg)
          } else {
            $message.error(res.msg)
          }
        })
        .catch((e) => {
          console.log(e)
        })
    }

    return {
      ...toRefs(data),
      tableRef,
      loadDataTable,
      handleSearch,
      handleModal,
      deleteSingle
    }
  }
}
</script>
<style scoped lang='scss'>
.n-button + .n-button {
  margin-left: 10px;
}
</style>
