<template>
  <div class="app-container role-manage">
    <!-- 搜索 -->
    <el-form :model="state.queryParams" ref="queryRef" :inline="true">
      <el-form-item label="角色名称" prop="title">
        <el-input v-model="state.queryParams.title" placeholder="请输入角色名称" clearable @keyup.enter="handleQuery({initPageIndex: true})" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery({initPageIndex: true})">搜索</el-button>
        <el-button type="primary" icon="Plus" plain @click="roleEditRef.openDialog()">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table border v-loading="state.tableLoading" :data="state.tableData" row-key="id" style="flex: 1">
      <el-table-column prop="title" label="角色名称" min-width="100"></el-table-column>
      <el-table-column prop="details" label="角色描述" min-width="120"></el-table-column>
      <el-table-column prop="group_createdate" label="创建时间" min-width="120"></el-table-column>
      <el-table-column prop="available" label="状态" min-width="100" align="center">
        <template #default="scope">
          <span>{{ Number(scope.row[scope.column.property]) === 0 ? '正常' : '停用' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" :width="fitWidth" class-name="small-padding fixed-width">
        <template #default="scope">
          <div class="operation-box">
            <el-text class="operate-btn-item" type="primary" @click="roleAuthRef.openDialog(scope.row)">权限分配</el-text>
            <el-text class="operate-btn-item" type="primary" @click="roleEditRef.openDialog(scope.row)">编辑</el-text>
            <el-text class="operate-btn-item" type="primary" @click="handleRoleDel(scope)">删除</el-text>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="paginaionConfig.query.pageindex"
        :page-sizes="paginaionConfig.sizeOptions"
        :page-size="paginaionConfig.query.rows"
        layout="total, sizes, prev, pager, next, jumper"
        :total="paginaionConfig.total"
      >
      </el-pagination>
    </div>

    <!-- 角色添加&编辑 -->
    <Suspense>
      <RoleEdit width="70%" ref="roleEditRef" @refresh="handleQuery" />
    </Suspense>

    <!-- 权限分配 -->
    <Suspense>
      <RoleAuth width="70%" ref="roleAuthRef" @refresh="handleQuery" />
    </Suspense>
  </div>
</template>

<script setup name="Role">
import useSystemManageStore from '@store/modules/systemManage'
import RoleEdit from './components/RoleEdit/index.vue'
import RoleAuth from './components/RoleAuth/index.vue'
import _ from 'lodash'
import usePagination from '@/hooks/usePagination'
import useFitColumn from '@/hooks/useFitColumn'

const {proxy} = getCurrentInstance()
const systemManageStore = useSystemManageStore()
const {paginaionConfig, handleSizeChange, handleCurrentChange} = usePagination(handleQuery)
const {fitWidth} = useFitColumn()

const state = reactive({
  tableLoading: false,
  tableData: [],
  queryParams: {
    title: ''
  }
})

const roleEditRef = ref(null)
const roleAuthRef = ref(null)

/** 查询菜单列表 */
const getList = async (query) => {
  state.tableLoading = true
  const {Total, Data} = await systemManageStore.sysusergroupLoad(query)
  paginaionConfig.total = Total
  state.tableData = Data

  state.tableLoading = false
}

function handleQuery(config) {
  if (config?.initPageIndex) paginaionConfig.query.pageindex = 1
  getList(Object.assign(_.cloneDeep(paginaionConfig.query), state.queryParams))
}

const handleRoleDel = async (scope) => {
  try {
    await proxy.$modal.confirm('请问确认删除吗？')
    await systemManageStore.sysusergroupDel({id: scope.row.id})
    proxy.$modal.msgSuccess('删除成功！')
    handleQuery()
  } catch (error) {}
}

onMounted(() => {
  handleQuery({initPageIndex: true})
})
</script>
