<template>
  <div class="my-layout">
    <el-card class="mt8" shadow="never" :body-style="{ paddingBottom: '0' }">
      <el-form :model="state.filter" :inline="true" @submit.stop.prevent>
        <el-form-item label="应用">
          <el-input v-model="state.filter.app" disabled placeholder="请选择应用" @keyup.enter="onQuery" />
        </el-form-item>
        <el-form-item label="权限名称">
          <el-input v-model="state.filter.name" placeholder="权限名称" @keyup.enter="onQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="ele-Search" @click="onQuery"> 查询 </el-button>
          <el-dropdown
            v-auths="['api:admin:permission:addgroup', 'api:admin:permission:addmenu', 'api:admin:permission:adddot']"
            style="margin-left: 12px"
          >
            <el-button type="primary"
              >新增<el-icon class="el-icon--right"><ele-ArrowDown /></el-icon
            ></el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item v-if="auth('api:admin:permission:addgroup')" @click="onAdd(1)">新增分组</el-dropdown-item>
                <el-dropdown-item v-if="auth('api:admin:permission:addmenu')" @click="onAdd(2)">新增菜单</el-dropdown-item>
                <el-dropdown-item v-if="auth('api:admin:permission:adddot')" @click="onAdd(3)">新增权限点</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="my-fill mt8" shadow="never">
      <el-table
        :data="state.permissionTreeData"
        style="width: 100%"
        v-loading="state.loading"
        row-key="id"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        :expand-row-keys="state.expandRowKeys"
      >
        <el-table-column prop="label" label="权限名称" width="240" show-overflow-tooltip>
          <template #default="{ row }">
            <SvgIcon :name="row.icon" />
            {{ row.label }}
          </template>
        </el-table-column>
        <el-table-column prop="type" label="类型" width="80" show-overflow-tooltip>
          <template #default="{ row }">
            {{ row.type === 1 ? '分组' : row.type === 2 ? '菜单' : row.type === 3 ? '权限点' : '' }}
          </template>
        </el-table-column>
        <el-table-column prop="path" label="权限地址" min-width="240" show-overflow-tooltip>
          <template #default="{ row }">
            <div v-if="row.type === 1 || row.type === 2">
              {{ row.viewPath ? '视图地址：' + row.viewPath : '' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序" width="80" align="center" show-overflow-tooltip />
        <!-- <el-table-column label="状态" width="80" align="center" show-overflow-tooltip>
          <template #default="{ row }">
            <el-tag type="success" v-if="row.enabled">启用</el-tag>
            <el-tag type="danger" v-else>禁用</el-tag>
          </template>
        </el-table-column> -->
        <el-table-column label="操作" width="160" fixed="right" header-align="center" align="center">
          <template #default="{ row }">
            <el-button
              v-if="
                (row.type === 1 && auth('api:admin:permission:updategroup')) ||
                (row.type === 2 && auth('api:admin:permission:updatemenu')) ||
                (row.type === 3 && auth('api:admin:permission:updatedot'))
              "
              icon="ele-EditPen"
              text
              type="primary"
              @click="onEdit(row)"
              >编辑</el-button
            >
            <el-button v-auth="'api:admin:permission:delete'" icon="ele-Delete" text type="danger" @click="onDelete(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <permission-group-form
      ref="permissionGroupFormRef"
      :title="state.permissionFormTitle"
      :permission-tree-data="state.formPermissionGroupTreeData"
    ></permission-group-form>

    <permission-menu-form
      ref="permissionMenuFormRef"
      :title="state.permissionFormTitle"
      :permission-tree-data="state.formPermissionGroupTreeData"
    ></permission-menu-form>

    <permission-dot-form
      ref="permissionDotFormRef"
      :title="state.permissionFormTitle"
      :permission-tree-data="state.formPermissionMenuTreeData"
    ></permission-dot-form>
  </div>
</template>

<script lang="ts" setup name="admin/permission">
import { ref, reactive, onMounted, getCurrentInstance, onBeforeMount, defineAsyncComponent } from 'vue'
import { OauthPermission,OauthApply } from '/@/api/admin/data-contracts'
import { PermissionApi } from '/@/api/admin/Permission'
import { listToTree, treeToList, filterTree } from '/@/utils/tree'
import router from "/@/router";
import { cloneDeep } from 'lodash-es'
import eventBus from '/@/utils/mitt'
import { auth } from '/@/utils/authFunction'
import { it } from 'node:test'

// 引入组件
const PermissionGroupForm = defineAsyncComponent(() => import('./components/permission-group-form.vue'))
const PermissionMenuForm = defineAsyncComponent(() => import('./components/permission-menu-form.vue'))
const PermissionDotForm = defineAsyncComponent(() => import('./components/permission-dot-form.vue'))

const { proxy } = getCurrentInstance() as any

const permissionGroupFormRef = ref()
const permissionMenuFormRef = ref()
const permissionDotFormRef = ref()
const state = reactive({
  loading: false,
  permissionFormTitle: '',
  filter: {
    name: '',
    app:''
  },
  permissionTreeData: [] as Array<OauthPermission>,
  formPermissionGroupTreeData: [] as Array<OauthPermission>,
  formPermissionMenuTreeData: [] as Array<OauthPermission>,
  expandRowKeys: [] as string[]
})

onMounted(async () => {
  await onQuery()
  state.expandRowKeys = treeToList(cloneDeep(state.permissionTreeData))
    .filter((a: OauthPermission) => a.opened === true)
    .map((a: OauthPermission) => a.id + '') as string[]
  eventBus.off('refreshPermission')
  eventBus.on('refreshPermission', async () => {
    onQuery()
  })
})

onBeforeMount(() => {
  eventBus.off('refreshPermission')
})

const onQuery = async () => {
  state.loading = true
  const name = router.currentRoute.value.params.id
  const appData=await new PermissionApi().getApplyInfo({Name:name})
  state.filter.app=appData.data[0].name
  const res = await new PermissionApi().getPermissionInfo({appid:appData.data[0].id,label:state.filter.name}).catch(() => {
    state.loading = false
  })
  res.data=res.data.map((item)=>{
    item.parentId=Number(item.parentId)
    if(item.parentId===0||item.parentId===null){
      //item.parentId=Number('0')
      item.type=1
    }
    // if(item.parentId===0){
    //   //item.parentId=Number(item.parentId)
    //   item.type=2
    // }
    // if(item.parentId!==0){
    //   //item.parentId=Number(item.parentId)
    //   item.type=3
    // }
    return item
  })
  if (res && res.data && res.data.length > 0) {
    state.permissionTreeData = filterTree(listToTree(cloneDeep(res.data)), state.filter.name, {
      filterWhere: (item: any, keyword: string) => {
        return item.label?.toLocaleLowerCase().indexOf(keyword) > -1
      },
    })
    state.formPermissionGroupTreeData = listToTree(cloneDeep(res.data).filter((a) => a.type === 1))
    state.formPermissionMenuTreeData = listToTree(cloneDeep(res.data).filter((a) => a.type === 1 || a.type === 2))
  } else {
    state.permissionTreeData = []
    state.formPermissionGroupTreeData = []
    state.formPermissionMenuTreeData = []
  }
  state.loading = false
}

const onAdd = (type: number) => {
  switch (type) {
    case 1:
      state.permissionFormTitle = '新增分组'
      permissionGroupFormRef.value.open(state.filter.app)
      break
    case 2:
      state.permissionFormTitle = '新增菜单'
      permissionMenuFormRef.value.open(state.filter.app)
      break
    case 3:
      state.permissionFormTitle = '新增权限点'
      permissionDotFormRef.value.open(state.filter.app)
      break
  }
}

const onEdit = (row: OauthPermission) => {
  switch (row.type) {
    case 1:
      state.permissionFormTitle = '编辑分组'
      permissionGroupFormRef.value.open(state.filter.app,row)
      break
    case 2:
      state.permissionFormTitle = '编辑菜单'
      permissionMenuFormRef.value.open(state.filter.app,row)
      break
    case 3:
      state.permissionFormTitle = '编辑权限点'
      permissionDotFormRef.value.open(state.filter.app,row)
      break
  }
}

const onDelete = (row: OauthPermission) => {
  proxy.$modal
    .confirmDelete(`确定要删除${row.type === 1 ? '分组' : row.type === 2 ? '菜单' : row.type === 3 ? '权限点' : ''}【${row.label}】?`)
    .then(async () => {
      await new PermissionApi().permissionDelete({id:row.id}, { loading: true })
      onQuery()
    })
    .catch(() => {})
}
</script>

<style scoped lang="scss"></style>
