import request from '@/utils/request.js'

// 获取省 数据
export function getProver() {
    return request({
        url: '/area/getProver',
        method: 'get'
    })
};

// 获取市 区数据

export function getArea(data) {
    return request({
        url: `/area/getChildren/${data}`,
        method: 'get'
    })
};

// 获取 选择器类型信息
export function getSelectType(data) {
    return request({
        url: `/basis/parameterconfigMgr/customerQuery/${data}`,
        method: 'post'
    })
};

/**
 * 获取药房详细信息
 * @deprecated {@link @/api/business.js#getPharmacyMsg}
 */
export function getPharmacyMsg(data) {
    return request({
        url: `/basis/pharmacyMgr/${data}`,
        method: 'get',
    })
};

// 医共体选择器数据
export function doctorBodySelect() {
    return request({
        url: '/test/outpatient/organizationCustomerQuery',
        method: 'post'
    })
};

// 选择医疗机构
export function medicalInstitution(data) {
    return request({
        url: '/test/outpatient/stitutionsCustomerQuery?organizationId=' + data.organizationId,
        method: 'post',
        data
    })
};

// 开方医生选择器
export function getDoctorSelect(data) {
    return request({
        url: '/test/outpatient/userCustomerQuery?stitutionsId=' + data.stitutionsId,
        method: 'post',
        data
    })
};

// 获取患者地址信息
export function getPatAddress(data) {
    return request({
        url: '/test/outpatient/historyDistribution?patId=' + data.patId,
        method: 'get',
        data
    })
};

let _hospitalCategoryAndDepartment_ = null;
async function getHospitalCategoryAndDepartment() {
    if (_hospitalCategoryAndDepartment_) { return _hospitalCategoryAndDepartment_; }
    try {
        const res = await request({
            url: '/basis/prescriptionMgr/getPreSectInfo',
            method: 'get',
            data: {},
        });
        _hospitalCategoryAndDepartment_ = res && res.ResultCode === 0 ? res.Data : {};
    } catch (error) { _hospitalCategoryAndDepartment_ = {}; }
    return _hospitalCategoryAndDepartment_;
}

export async function getHospitalCategory() {
    _hospitalCategoryAndDepartment_ = null;
    const data = await getHospitalCategoryAndDepartment();
    return Object.keys(data).filter(Boolean).map(name => ({id: name, name}));
}

export async function getHospitalDepartment(params) {
    const data = (await getHospitalCategoryAndDepartment())[params.hospital || ''] || []
    return data.filter(Boolean).map(name => ({id: name, name}));
}