<template>
  <div>
    <n-card>
      <headSearch :info="search" @search="handleSearch" @reset="handleSearch" />
      <data-table
        ref="tableRef"
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row) => row.id"
        size="large"
      >
        <template #tableTitle>
          <n-button v-if="permissionList.includes('user:menber:create')" type="primary" @click="handleModal"> {{ getPermissionLabel('user:menber:create') }} </n-button>
        </template>
      </data-table>
    </n-card>
  </div>
  <!-- 新增、编辑弹窗 -->
  <UserModal v-if="modalShow" v-model:visible="modalShow" :data="rowData" :type="modalType" @reload="handleSearch" />
</template>

<script>
import search from './tools/search.js'
import { tableData, getRoleOptions } 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 { unref, toRefs, reactive, onUnmounted } from 'vue'
import { userPage } from '@/api/system/user.js'
import { usePermissionStore } from '@/store/modules/permission'

export default {
  name: 'UserManage',
  components: { dataTable, UserModal, headSearch },
  setup() {
    const permissionStore = usePermissionStore()// 按钮权限
    const data = reactive({
      ...toRefs(tableData),
      search,
      permissionList: permissionStore.accessPermissionCodes

    })
    const getPermissionLabel = (code) => {
      return permissionStore.accessPermissions.find((item) => {
        return item.code === code
      })?.name || ''
    }
    getRoleOptions()

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

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

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

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