import { UserListParm } from '@/api/user/UserModel'
import {reactive} from 'vue'
import { getUserListApi } from '@/api/user/user'
import { ActionOptions } from '@/components/DropDownButtons/type'
import { Delete, Edit, Setting } from "@element-plus/icons-vue";
import { UsersType } from '@/type/BaseEnum';
export default function useUserTable(){
    //定义表格绑定的数据
    const tableData = reactive({
        list:[]
    })

    //表格查询的参数
    const listParm = reactive<UserListParm>({
        deptId:'',
        pageSize:10,
        pageNum:1,
        total:0,
        loginName:''
    })

    //查询表格
    const getUserList = async() =>{
        let res = await getUserListApi(listParm)
        if(res && res.code == 200){
            //把返回的数据设置到表格绑定的数据里面
            tableData.list = res.data.records
            //设置分页总条数
            listParm.total = res.data.total;
        }
    }
    //树组件调用，查询用户列表
    const treeClick = async(deptId:number) =>{
        //设置部门Id
        listParm.deptId = deptId
        //加载表格
        getUserList();
    }

    //页容量改变时触发
    const sizeChange = (size:number) =>{
        listParm.pageSize = size;
        //重新加载表格
        getUserList();
    }

    //页数改变时触发
    const currentChange = (page:number) =>{
        listParm.pageNum = page;
        //重新加载表格
        getUserList();
    }
    //搜索
    const searchBtn = () =>{
        getUserList();
    }
    //重置
    const resetBtn = () =>{
        listParm.loginName = '';
        getUserList();
    }
    // 操作类别 
    const usersActionOptions: ActionOptions[] = [
        {
            value: UsersType.RESET_PASSWORD,
            label: "重置密码",
            type: 'info',
            icon: Edit,
            permissions: ['sys:user:edit']
        },
        {
            value: UsersType.EDIT,
            label: "编辑",
            icon: Edit,
            color: "#02ABE8",
            permissions: ['sys:user:edit']
        },
        {
            value: UsersType.DATA_PERMISSION,
            label: "数据权限",
            icon: Setting,
            type: 'success',
            permissions: ['sys:userDataPermission:save']
        },
        {
            value: UsersType.ASSIGN_ROLE,
            label: "分配角色",
            icon: Edit,
            color: "#409EFF",
            permissions: ['sys:user:assign']
        },
        {
            value: UsersType.DELETE,
            label: "删除",
            type: "danger",
            icon: Delete,
            permissions: ['sys:user:delete']
        },
    ]

    return {
        tableData,
        listParm,
        getUserList,
        treeClick,
        sizeChange,
        currentChange,
        searchBtn,
        resetBtn,
        usersActionOptions
    }
}