/*
 * @Author: code4eat awesomedema@gmail.com
 * @Date: 2024-12-25 16:00:00
 * @LastEditors: code4eat awesomedema@gmail.com
 * @LastEditTime: 2025-08-06 10:20:51
 * @FilePath: /KC-MiddlePlatform/src/pages/certificateMana/service.ts
 * @Description: 证书管理服务接口
 */

import { request } from '@umijs/max';

// 模拟科室数据
const departmentData = [
    {
        name: '全部',
        code: 'all',
        children: []
    },
    {
        name: '神经内科病区',
        code: 'neurology',
        children: []
    },
    {
        name: '心内科病区',
        code: 'cardiology',
        children: [
            {
                name: '心内科一病区',
                code: 'cardiology_1',
                children: []
            },
            {
                name: '心内科二病区',
                code: 'cardiology_2',
                children: []
            },
            {
                name: '心内科三病区',
                code: 'cardiology_3',
                children: []
            },
            {
                name: '心内科四病区',
                code: 'cardiology_4',
                children: []
            }
        ]
    },
    {
        name: '呼吸内科病区',
        code: 'respiratory',
        children: []
    },
    {
        name: '肾内科病区',
        code: 'nephrology',
        children: []
    },
    {
        name: '血液科病区',
        code: 'hematology',
        children: []
    },
    {
        name: '内分泌科病区',
        code: 'endocrinology',
        children: []
    },
    {
        name: '放射科病区',
        code: 'radiology',
        children: []
    },
    {
        name: '临床医生',
        code: 'clinical_doctor',
        children: []
    },
    {
        name: '医技辅助医生',
        code: 'auxiliary_doctor',
        children: []
    }
];

// 模拟证书数据
const certificateData = [
    {
        id: '1',
        certificateType: '本科毕业证',
        doctorName: '杨崇文',
        doctorId: '000029',
        startDate: '2023-11-01',
        endDate: '2024-11-01',
        attachmentDescription: '这里是大学本科学位证的说明',
        departmentCode: 'cardiology_2',
        departmentName: '心内科二病区'
    },
    {
        id: '2',
        certificateType: '本科毕业证',
        doctorName: '杨崇文',
        doctorId: '000029',
        startDate: '2023-11-01',
        endDate: '2024-11-01',
        attachmentDescription: '这里是大学本科学位证的说明',
        departmentCode: 'cardiology_2',
        departmentName: '心内科二病区'
    },
    {
        id: '3',
        certificateType: '本科毕业证',
        doctorName: '杨崇文',
        doctorId: '000029',
        startDate: '2023-11-01',
        endDate: '2024-11-01',
        attachmentDescription: '这里是大学本科学位证的说明',
        departmentCode: 'cardiology_2',
        departmentName: '心内科二病区'
    },
    {
        id: '4',
        certificateType: '本科毕业证',
        doctorName: '杨崇文',
        doctorId: '000029',
        startDate: '2023-11-01',
        endDate: '2024-11-01',
        attachmentDescription: '这里是大学本科学位证的说明',
        departmentCode: 'cardiology_2',
        departmentName: '心内科二病区'
    },
    {
        id: '5',
        certificateType: '本科毕业证',
        doctorName: '杨崇文',
        doctorId: '000029',
        startDate: '2023-11-01',
        endDate: '2024-11-01',
        attachmentDescription: '这里是大学本科学位证的说明',
        departmentCode: 'cardiology_2',
        departmentName: '心内科二病区'
    },
    {
        id: '6',
        certificateType: '本科毕业证',
        doctorName: '杨崇文',
        doctorId: '000029',
        startDate: '2023-11-01',
        endDate: '2024-11-01',
        attachmentDescription: '这里是大学本科学位证的说明',
        departmentCode: 'cardiology_2',
        departmentName: '心内科二病区'
    },
    {
        id: '7',
        certificateType: '本科毕业证',
        doctorName: '杨崇文',
        doctorId: '000029',
        startDate: '2023-11-01',
        endDate: '2024-11-01',
        attachmentDescription: '这里是大学本科学位证的说明',
        departmentCode: 'cardiology_2',
        departmentName: '心内科二病区'
    },
    {
        id: '8',
        certificateType: '本科毕业证',
        doctorName: '杨崇文',
        doctorId: '000029',
        startDate: '2023-11-01',
        endDate: '2024-11-01',
        attachmentDescription: '这里是大学本科学位证的说明',
        departmentCode: 'cardiology_2',
        departmentName: '心内科二病区'
    },
    {
        id: '9',
        certificateType: '本科毕业证',
        doctorName: '杨崇文',
        doctorId: '000029',
        startDate: '2023-11-01',
        endDate: '2024-11-01',
        attachmentDescription: '这里是大学本科学位证的说明',
        departmentCode: 'cardiology_2',
        departmentName: '心内科二病区'
    },
    {
        id: '10',
        certificateType: '本科毕业证',
        doctorName: '杨崇文',
        doctorId: '000029',
        startDate: '2023-11-01',
        endDate: '2024-11-01',
        attachmentDescription: '这里是大学本科学位证的说明',
        departmentCode: 'cardiology_2',
        departmentName: '心内科二病区'
    }
];

// 获取证书列表
export async function getCertificateList(params: any) {
    // 模拟API调用延迟
    await new Promise(resolve => setTimeout(resolve, 500));
    
    const { current = 1, pageSize = 10, departmentCode, doctorName, expirationDate } = params;
    
    let filteredData = [...certificateData];
    
    // 根据科室筛选
    if (departmentCode && departmentCode !== 'all') {
        filteredData = filteredData.filter(item => item.departmentCode === departmentCode);
    }
    
    // 根据医生姓名筛选
    if (doctorName) {
        filteredData = filteredData.filter(item => 
            item.doctorName.includes(doctorName) || item.doctorId.includes(doctorName)
        );
    }
    
    // 根据到期时间筛选
    if (expirationDate && expirationDate.length === 2) {
        const [startDate, endDate] = expirationDate;
        filteredData = filteredData.filter(item => {
            const itemEndDate = new Date(item.endDate);
            const filterStartDate = new Date(startDate);
            const filterEndDate = new Date(endDate);
            return itemEndDate >= filterStartDate && itemEndDate <= filterEndDate;
        });
    }
    
    const startIndex = (current - 1) * pageSize;
    const endIndex = startIndex + pageSize;
    const list = filteredData.slice(startIndex, endIndex);
    
    return {
        success: true,
        list,
        total: filteredData.length,
        current,
        pageSize
    };
}

// 获取科室列表（改为与医生授权管理一致的后端接口）
export async function getDepartmentList(params?: any) {
    return request<any>('/medical/qualificationManage/listAllDepartment', {
        method: 'GET',
        params: { ...params }
    });
}

// 获取医生附件（证书）列表
export async function getDoctorAttachmentList(params?: any) {
    return request<any>('/medical/qualificationManage/listAllDoctorAttachments', {
        method: 'GET',
        params: { ...params }
    });
}

// 删除证书（复用个人中心的删除附件接口）
export async function deleteCertificate(id: number) {
    return request<any>('/medical/qualificationManage/deleteDoctorAttachment', {
        method: 'POST',
        params: { id }
    });
}

// 下载证书
export async function downloadCertificate(id: string) {
    // 模拟API调用延迟
    await new Promise(resolve => setTimeout(resolve, 1000));
    
    // 模拟下载文件
    const blob = new Blob(['模拟的证书文件内容'], { type: 'application/pdf' });
    const url = window.URL.createObjectURL(blob);
    const link = document.createElement('a');
    link.href = url;
    link.download = `certificate_${id}.pdf`;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    window.URL.revokeObjectURL(url);
    
    return {
        success: true,
        message: '下载成功'
    };
}

// 新增证书（复用个人中心的保存附件接口）
export async function addCertificate(data: any) {
    return request<any>('/medical/qualificationManage/saveDoctorAttachment', {
        method: 'POST',
        data
    });
}

// 编辑证书（复用个人中心的编辑附件接口）
export async function editCertificate(data: any) {
    return request<any>('/medical/qualificationManage/editDoctorAttachment', {
        method: 'POST',
        data
    });
}

// 更新证书
export async function updateCertificate(data: any) {
    // 模拟API调用延迟
    await new Promise(resolve => setTimeout(resolve, 500));
    
    return {
        success: true,
        message: '更新成功'
    };
}

// 获取医生下拉列表（复用医生授权管理接口）
export async function getDoctorListForSelect(params?: any) {
    return request<any>('/medical/qualificationManage/listByDoctor', {
        method: 'GET',
        params: { ...params }
    });
} 