import {ref} from "vue";
import {maZuiFangShi} from "@/api/public-api";
import store from '@/store';
import {listIsBlank, stringIsBlank} from "@/utils/blank-utils";
import {getFundPayType, getMedChrgitmType} from "@/api/medical-insurance/si-setl-upload";
import {getFormatDatetime} from "@/utils/date";

const getRoles = store.state.user.info.roles

export function shenHeQuanXian() {
    return getRoles.indexOf(1) > -1 || getRoles.indexOf(8) > -1
}

export function auditQuanXian() {
    return getRoles.indexOf(1) > -1 || getRoles.indexOf(49) >-1 ||getRoles.indexOf(50)  > -1 || getRoles.indexOf(8) > -1
}
//编码组长
export function auditBmzzQuanXian() {
    return getRoles.indexOf(50)  > -1 || getRoles.indexOf(1) > -1 || getRoles.indexOf(8) > -1
}
//审核申请权限
export function auditSendQuaXian() {
    return getRoles.indexOf(48)>-1   || getRoles.indexOf(1) > -1
}

//编码员权限
export function bmyQuaXian() {
    return getRoles.indexOf(49)>-1   || getRoles.indexOf(1) > -1
}

/**
 * 获取医保诊断
 * @param val 诊断编码
 * @returns {string} 返回名称
 */
export function getsiDiagTypeName(val) {
    switch (val) {
        case "1":
            return "西医主要诊断"
        case "2":
            return "西医其他诊断"
        case "3":
            return "中医主要诊断"
        case "4":
            return "中医其他诊断"
        default:
            return "没有选择"
    }
}
/**
 * 获取质控规则等级
 * @param val 编码
 * @returns {string} 返回名称
 */
export function getRuleLevelName(val) {
    switch (val) {
        case "1":
            return "可疑"
        case "2":
            return "违规"
        default:
            return ""
    }
}
/**
 * 获取质控规则类型
 * @param val 编码
 * @returns {string} 返回名称
 */
export function getRuleCategoryName(val) {
    switch (val) {
        case "1":
            return "基础质控"
        case "2":
            return "编码问题"
        default:
            return ""
    }
}
/**
 * 获取是否影响入组
 * @param val 编码
 * @returns {string} 返回名称
 */
export function getAffectGroupName(val) {
    switch (val) {
        case "1":
            return "影响"
        case "2":
            return "不影响"
        default:
            return ""
    }
}

/**
 * 判断这个人是否有申请了
 * @param val
 * @returns {string}
 */
export function auditName(val) {
    switch (val.auditFlag) {
        case 0:
            return `<span style="color: #909399">${val.psnName}</span>`
        case 1:
            return `<span style="color: #67C23A">${val.psnName}</span>`
        case 2:
            return `<span style="color: #F56C6C">${val.psnName}</span>`
        case 3:
            return `<span style="color: #E6A23C">${val.psnName}</span>`
        default:
            return `${val.psnName}`
    }
}

export const qks = ref([
    {code: '0/0', name: '0/0'},
    {code: '1/3', name: 'Ⅰ/丙'},
    {code: '1/1', name: 'Ⅰ/甲'},
    {code: '1/2', name: 'Ⅰ/乙'},
    {code: '2/3', name: 'Ⅱ/丙'},
    {code: '2/1', name: 'Ⅱ/甲'},
    {code: '2/2', name: 'Ⅱ/乙'},
    {code: '3/3', name: 'Ⅲ/丙'},
    {code: '3/1', name: 'Ⅲ/甲'},
    {code: '3/2', name: 'Ⅲ/乙'}
])

export const auditFlags = ref([
    {code: 5, name: '未申请审核'},
    {code: 0, name: '待审核'},
    {code: 1, name: '审核通过'},
    {code: 2, name: '驳回'},
    {code: 4, name: '全部'},
])

/**
 *
 * @param qkjb 切口级别
 * @param yhqk 愈合级别
 * @returns {string} 返回信息
 */
export function qieKouName(qkjb, yhqk) {
    let code = qkjb + '/' + yhqk
    let name = ''
    qks.value.forEach(item => {
        if (item.code === code) {
            return name = item.name
        }
    })
    return name
}

/**
 *
 * @type {ToRef<*[]>} 麻醉数组
 */
export const maZuiFangShis = ref([])

/**
 * 远程获取麻醉方式
 */
maZuiFangShi().then((res) => {
    maZuiFangShis.value = res
})

/**
 * 麻醉的名字
 * @param val code
 * @returns {string} 名称
 */
export function maZuiName(val) {
    let name = ''
    maZuiFangShis.value.forEach(item => {
        if (item.code === val) {
            return name = item.name
        }
    })
    return name
}

/**
 * 医生默认可以默认选择的信息
 * @param arr 数组
 * @param notNode 不需要的 code
 * @returns {*[]} 返回科室
 */
export function huoQuZhongKongChaXunTiaoJian(arr, notNode) {
    let data = []
    arr.forEach(item => {
        if (notNode.indexOf(item.code) === -1) {
            data.push(item.code)
        }
    })
    return data
}


/* 打印结算单的一些工具类 */
export function brdyName(val) {
    if (stringIsBlank(val)) {
        return ""
    }
    let name = val.split("-")
    return `${name[0]}年${name[1]}月${name[2]}日`
}

/* 入院时间拆分 */
export function getDateName(val) {
    if (stringIsBlank(val)) {
        return ""
    }
    let date = val.split(" ")[0].split("-")
    let times = val.split(" ")[1].split(":")
    return `${date[0]}年${date[1]}月${date[2]}日 ${times[0]}时`
}

// 手术时间拆分
export function getShouShuShiJian(val) {
    let {oprnOprtBegntime, oprnOprtEndtime} = val
    let format = 'YYYY年MM月DD日 HH时mm分ss秒'
    return `${getFormatDatetime(oprnOprtBegntime, format)} 至 ${getFormatDatetime(oprnOprtEndtime, format)}`
}

// 麻醉时间
export function getMaZuiShiJian(val) {
    let {anstBegntime, anstEndtime} = val
    let format = 'YYYY年MM月DD日 HH时mm分ss秒'
    return `${getFormatDatetime(anstBegntime, format)} 至 ${getFormatDatetime(anstEndtime, format)}`
}

// 颅内时间拆分
export function luNeiShiJian(val) {
    if (stringIsBlank(val)) {
        return ""
    }
    let data = val.split("/")
    if (data.length === 3) {
        return ` ${data[0]}天 ${data[1]}小时 ${data[2]}分钟`;
    }
    return ""
}

//结算时间计算
export function nianYueRi(val) {
    if (stringIsBlank(val)) {
        return ' 年 月 日'
    }
    let date = val.split("-")
    return `${date[0]}年${date[1]}月${date[2]}日`
}

export function getAge(val) {
    if (stringIsBlank(val)) {
        return ""
    }
    return val.split('.')[0];
}

export function getAdmissCond(val) {
    switch (val) {
        case 1:
            return "有"
        case 2:
            return "临床未确定"
        case 3:
            return "情况不明"
        case 4:
            return "无"
        default:
            return ""

    }
}

export function getXiYiZhenDuan(val) {
    if (listIsBlank(val)) {
        return []
    }
    let diag = []
    val.forEach(item => {
        if (item.siDiagType === '1' || item.siDiagType === '2') {
            diag.push(item)
        }
    })
    if (listIsBlank(diag)) {
        return []
    }
    let zhuYao = []
    let ciYao = []
    diag.forEach(item => {
        if (item.siDiagType === '1' && zhuYao.length === 0) {
            zhuYao.push(item)
        } else {
            ciYao.push(item)
        }
    })
    return zhuYao.concat(ciYao)
}

export function getZhongYiZhenDuan(val) {
    if (listIsBlank(val)) {
        return []
    }
    let diag = []
    val.forEach(item => {
        if (item.siDiagType === '3' || item.siDiagType === '4') {
            diag.push(item)
        }
    })
    if (listIsBlank(diag)) {
        return []
    }
    let zhuYao = []
    let ciYao = []
    diag.forEach(item => {
        if (item.siDiagType === '3' && zhuYao.length === 0) {
            zhuYao.push(item)
        } else {
            ciYao.push(item)
        }
    })
    return zhuYao.concat(ciYao)
}

export function zhenDuanData(val) {
    let xiYiData = []
    let zhongYiData = []
    let obj = {disDiagComment: '', disDiag: '', admissCond: ''}
    // 只有当诊断全部为空的 时候才创建 9 个 空的数据
    if (listIsBlank(val)) {
        for (let i = 0; i < 9; i++) {
            xiYiData.push(obj)
            zhongYiData.push(obj)
        }
    }
    xiYiData = getXiYiZhenDuan(val)
    zhongYiData = getZhongYiZhenDuan(val)
    let maxLength = xiYiData.length > zhongYiData.length ? xiYiData.length : zhongYiData.length
    let newXiYiData = []
    let newZhongYiData = []
    if (maxLength < 2) {
        maxLength = 9
    }
    for (let i = 0; i < maxLength; i++) {
        if (i >= xiYiData.length) {
            newXiYiData.push(obj)
        } else {
            newXiYiData.push(xiYiData[i])
        }
        if (i >= zhongYiData.length) {
            newZhongYiData.push(obj)
        } else {
            newZhongYiData.push(zhongYiData[i])
        }
    }
    return zhenDuanHeBing(newXiYiData, newZhongYiData)
}

export function zhenDuanHeBing(xiYiZhenDuan, ZhongYiZhenDuan) {
    // 因为我在上面 保证了 两个诊断的长度完全一致 所以怎么循环都可以
    let data = []
    for (let i = 0; i < xiYiZhenDuan.length; i++) {
        data.push({
            xiYiDisDiag: xiYiZhenDuan[i].disDiag,
            xiYiDisDiagComment: xiYiZhenDuan[i].disDiagComment,
            xiYiAdmissCond: xiYiZhenDuan[i].admissCond,
            ZhongYiDisDiag: ZhongYiZhenDuan[i].disDiag,
            ZhongYiDiagComment: ZhongYiZhenDuan[i].disDiagComment,
            ZhongYiAdmissCond: ZhongYiZhenDuan[i].admissCond,
        })
    }
    return data
}


export function getShouShuData(val) {
    if (listIsBlank(val)) {
        let kong = []
        for (let i = 0; i < 3; i++) {
            kong.push({
                ssmc: '',
                ssbm: '',
                mzff: '',
                ssysName: '',
                ssys: '',
                mzysName: '',
                mzys: '',
            })
        }
        return kong
    }
    let zhuYaoShouShu = []
    let ciYaoShouShu = []
    val.forEach(item => {
        if (item.ssjb === 4 && zhuYaoShouShu.length === 0) {
            zhuYaoShouShu.push(item)
        } else {
            ciYaoShouShu.push(item)
        }
    })
    return zhuYaoShouShu.concat(ciYaoShouShu)
}

// 重症监护类型
export function zhongZhenJianHu(val) {
    if (val === null) {
        val = []
    }
    let data = []
    if (listIsBlank(val)) {
        data.push({
            scsCutdWardType: '',
            scsCutdInpoolTime: '',
            scsCutdExitTime: '',
            scsCutdSumDura: '',
        })
        return data
    }
    val.forEach(item => {
        item.scsCutdInpoolTime = nianYueRiShiFen(item.scsCutdInpoolTime)
        item.scsCutdExitTime = nianYueRiShiFen(item.scsCutdExitTime)
        item.scsCutdSumDura = zhongZhenShiFenMiao(item.scsCutdSumDura)
    })
    return val
}

export function nianYueRiShiFen(val) {
    if (stringIsBlank(val)) {
        return ""
    }
    let date = val.split(" ")[0].split("-")
    let times = val.split(" ")[1].split(":")
    return `${date[0]}年${date[1]}月${date[2]}日${times[0]}时${times[1]}分`
}

export function zhongZhenShiFenMiao(val) {
    if (stringIsBlank(val)) {
        return ""
    }
    let times = val.split("/")
    return `${times[0]}天${times[1]}时${times[2]}分`
}

// 获取费用类型
export let medChrgitmType = []
export let medChrgitmTypeMap = {}

getMedChrgitmType().then((res) => {
    medChrgitmType = res
    medChrgitmType.forEach(item => {
        medChrgitmTypeMap[item.code] = {name: item.name}
    })
})

export function getMedChrgitmTypeData(val) {
    let data = []
    if (listIsBlank(val)) {
        medChrgitmType.forEach(item => {
            data.push({
                medChrgitm: item.name,
                amt: '',
                claaSumfee: '',
                clabAmt: '',
                fulamtOwnpayAmt: '',
                othAmt: '',
            })
        })
        data.push({
            medChrgitm: "合计",
            amt: 0,
            claaSumfee: 0,
            clabAmt: 0,
            fulamtOwnpayAmt: 0,
            othAmt: 0,
        })
        return data
    }

    val.sort(function (a, b) {
        return a.medChrgitm - b.medChrgitm
    })
    data = val

    return data
}

// 医保统筹支付
export let fndPayTypeData = []
export let fndPayTypeMap = {}

getFundPayType().then(res => {
    fndPayTypeData = res
    fndPayTypeData.forEach(item => {
        fndPayTypeMap[item.code] = {code: item.code, name: item.name}
    })
})

export function fenLeiZhiFuLeiXing(val) {
    let obj = {}
    let qiTa = []
    let shuShiHua = ['310100', '330100', '390200', '320100', '610100', "370100"]
    for (let i = 0; i < shuShiHua.length; i++) {
        obj[shuShiHua[i]] = {
            fundPayType: shuShiHua[i],
            fundPayamt: "",
        }
    }
    val.forEach(item => {
        let name = fndPayTypeMap.hasOwnProperty(item.fundPayType)
        if (shuShiHua.indexOf(item.fundPayType) > -1) {
            obj[item.fundPayType] = {
                fundPayType: item.fundPayType,
                fundPayamt: item.fundPayamt,
                name: name ? fndPayTypeMap[item.fundPayType].name : ""
            }
        } else {
            qiTa.push({
                fundPayType: item.fundPayType,
                fundPayamt: item.fundPayamt,
                name: name ? fndPayTypeMap[item.fundPayType].name : "其他"
            })
        }
    })
    if (listIsBlank(qiTa)) {
        qiTa.push({
            fundPayType: "",
            fundPayamt: "",
            name: ""
        })
    }
    return {obj, qiTa}
}

export const outpatientChronicDisease = (list) => {
    let data = []
    if (listIsBlank(list)) {
        for (let i = 0; i < 6; i++) {
            data.push({
                diagCode: '',
                diagName: '',
                oprnOprtCode: '',
                oprnOprtName: '',
            })
        }
    } else {
        return list
    }
    return data
}

