<template>
  <div>
    <n-card>
      <headSearch :info="search" @search="handleSearch" @reset="handleSearch" />
      <data-table
        ref="tableRef"
        :columns="columns"
        :row-key="(row) => row.id"
        :request="loadDataTable"
        size="large"
        @update:checked-row-keys="handleCheck"
      >
        <template #tableTitle>
          <n-button type="primary" @click="handleModal"> 新建 </n-button>
          <n-popconfirm
            negative-text="取消"
            positive-text="确认"
            @positive-click="deleteComplex"
          >
            <template #trigger>
              <n-button type="primary"> 删除 </n-button>
            </template>
            确认要删除选中数据吗?
          </n-popconfirm>
        </template>
      </data-table>
    </n-card>
  </div>

  <!-- 新增、编辑弹窗 -->
  <UserModal v-if="modalShow" v-model:visible="modalShow" :type="modalType" :data="rowData" @reload="handleSearch" />
</template>

<script>
import { search, fetchRolesOption } 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 UserModal from './components/UserModal.vue'
import { getUserList } from '@/api/system/user/index.js'
import { unref, ref, toRefs, reactive, onUnmounted } from 'vue'

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

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

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

    // 选择表格数据
    const selectedIds = ref([])
    function handleCheck(rowKeys) {
      selectedIds.value = rowKeys
    }

    // 批量删除
    function deleteComplex() {
      if (selectedIds.value.length) {
        data.deleteData(selectedIds.value)
      } else {
        $message.warning('请至少选中一条数据')
      }
    }

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

    return {
      ...toRefs(data),
      loadDataTable,
      handleModal,
      selectedIds,
      deleteComplex,
      handleCheck
    }
  },
  methods: {

  }
}
</script>
<style scoped lang='scss'>
.n-button + .n-button {
  margin-left: 10px;
}
</style>
