import store from '@/store'
import router from "@/router";
import {deletePatientEmrByDocumentId, insertEmrData} from "@/api/zhu-yuan-yi-sheng/emr-patient";

// 患者数据
export function EMRInteractive(data, editorEvent) {

    this.setEditor = (editor, runtime) => {
        this.editor = editor;
        this.runtime = runtime;
        if (editorEvent !== null) {
            for (let key in editorEvent) {
                editor.on(key, function (...args) {
                    editorEvent[key](...args)
                })
            }
        }
    };

    this.getEditor = () => {
        return this.editor
    }

    this.deleteElement = (params) => {
        this.editor.execute('execFn', {
            value: {
                fn: function () {
                    let EMR = this.editor.getScriptRuntime().EMR
                    let emrDoc = EMR.getDocument()
                    let components = emrDoc.getComponentsByCode(params);
                    components.forEach((component) => {
                        let view = component.target;
                        let viewDom = view.el;
                        let viewParagraphDom = viewDom.parentElement;
                        viewDom.remove();
                        if (viewParagraphDom.textContent === "") {
                            viewParagraphDom.remove()
                        }
                    })
                },
                scope: this, params: params
            }
        })
    }

    this.getRoot = () => {
        return this.editor.model.document.getRoot()
    }

    this.callMethod = (name, ...value) => {
        return this.editor[name](...value)
    }

    // 这个不能删除是编辑器需要的东西
    this.getAppContext = () => {
        return {
            endpoints: {
                app: "/bdp/dataservice/api",
                his: import.meta.env.VITE_BASE_URL,
            }, input: {
                user: store.state.user.info.code,
                name: store.state.user.info.name
            }, login: {
                token: store.state.user.info.token,
                user: {
                    id: store.state.user.info.code,
                    name: store.state.user.info.name,
                }
            },
            data
        }
    };

    this.saveDocument = (value, newDate) => {
        let document = this.editor.getDocument();

        document.properties.categoryCode = value.emrCategoryCode
        document.properties.patientId = value.patNo + "_" + value.times;
        document._id = value.emrDocumentId
        value.documentData = document

        if (document.properties.creator) {
            document.properties.modifier = data['编辑者姓名'];
            document.properties.modifierId = data['编辑者编码'];
            document.properties.modifyTime = newDate
        } else {
            document.properties.creator = data['编辑者姓名'];
            document.properties.creatorId = data['编辑者编码'];
            document.properties.createTime = newDate
        }
        return insertEmrData(value)
    };

    this.deleteDocument = (documentId) => {
        return deletePatientEmrByDocumentId(documentId)
    };

}

export const query = ref({
    patNo: '',
    times: 0,
    maxTimes: 1,
    state: 0
})

export function resolveRoute(val) {
    query.value = JSON.parse(window.atob(val))
}

export const emrConfig = ref({
    // 当前是否可以编辑，用来分辨出院只读，和在院患者的
    editor: true,
})


// 默认查询的是全部的患者  state == 1 自己科室的患者
export function getEmrUrl(patNo, times, state = 3) {
    let query = {
        patNo,
        times,
        state: state,
        maxTimes: 1
    }
    let temp = JSON.stringify(query)
    return router.resolve({
        name: 'emrEditor',
        query: {
            pat: window.btoa(temp)
        }
    }).href
}

/**
 * 获取出院患者
 * @param patNo 住院号
 * @param times 住院次数
 * @returns {string}
 */
export function getInEmrUrl(patNo, times) {
    let query = {
        patNo,
        times,
        state: 2,
        maxTimes: 1
    }
    let temp = JSON.stringify(query)
    return router.resolve({
        name: 'emrEditor',
        query: {
            pat: window.btoa(temp)
        }
    }).href
}

let EMR_COPY = 'emrCopy'
export const setEmrCopy = (name, value) => {
    if (localStorage.getItem(EMR_COPY) === null) {
        localStorage.setItem(EMR_COPY, '{}')
    }
    let temp = JSON.parse(localStorage.getItem(EMR_COPY))
    temp[name] = value
    localStorage.setItem(EMR_COPY, JSON.stringify(temp))
}
export const getEmrCopy = (name) => {
    if (localStorage.getItem(EMR_COPY) === null) {
        return null;
    }
    let temp = JSON.parse(localStorage.getItem(EMR_COPY))
    let value = temp[name]
    if (value === null) {
        return null
    }
    return value
}

export const delEmrCopy = (name) => {
    if (localStorage.getItem(EMR_COPY) === null) {
        return
    }
    let temp = JSON.parse(localStorage.getItem(EMR_COPY))
    delete temp[name]
    localStorage.setItem(EMR_COPY, JSON.stringify(temp))
}

export const copyEnum = {
    "会诊": 'huizhenjilv'
}

export const fontSizes = [
    {
        "code": "42pt",
        "name": "初号"
    },
    {
        "code": "36pt",
        "name": "小初"
    },
    {
        "code": "26pt",
        "name": "一号"
    },
    {
        "code": "24pt",
        "name": "小一"
    },
    {
        "code": "22pt",
        "name": "二号"
    },
    {
        "code": "18pt",
        "name": "小二"
    },
    {
        "code": "16pt",
        "name": "三号"
    },
    {
        "code": "15pt",
        "name": "小三"
    },
    {
        "code": "14pt",
        "name": "四号"
    },
    {
        "code": "12pt",
        "name": "小四"
    },
    {
        "code": "10.5pt",
        "name": "五号"
    },
    {
        "code": "9pt",
        "name": "小五"
    },
    {
        "code": "7.5pt",
        "name": "六号"
    },
    {
        "code": "6.5pt",
        "name": "小六"
    },
    {
        "code": "5.5pt",
        "name": "七号"
    },
    {
        "code": "5pt",
        "name": "八号"
    },
    {
        "code": "4pt",
        "name": "4"
    },
    {
        "code": "8pt",
        "name": "8"
    },
    {
        "code": "10pt",
        "name": "10"
    },
    {
        "code": "11pt",
        "name": "11"
    },
    {
        "code": "20pt",
        "name": "20"
    },
    {
        "code": "28pt",
        "name": "28"
    },
    {
        "code": "48pt",
        "name": "48"
    },
    {
        "code": "72pt",
        "name": "72"
    }
]
export const availableFonts = [
    {
        "text": "微软雅黑",
        "font": "\"Microsoft YaHei\""
    },
    {
        "text": "宋体",
        "font": "SimSun"
    },
    {
        "text": "黑体",
        "font": "SimHei"
    },
    {
        "text": "仿宋",
        "font": "FangSong"
    },
    {
        "text": "楷体",
        "font": "KaiTi"
    },
    {
        "text": "新宋体",
        "font": "NSimSun"
    },
    {
        "text": "微软正黑体",
        "font": "\"Microsoft JhengHei\""
    },
    {
        "text": "Arial",
        "font": "Arial"
    },
    {
        "text": "Times New Roman",
        "font": "\"Times New Roman\""
    },
    {
        "text": "Calibri",
        "font": "Calibri"
    },
    {
        "text": "Comic Sans MS",
        "font": "\"Comic Sans MS\""
    },
    {
        "text": "Courier New",
        "font": "\"Courier New\""
    },
    {
        "text": "Georgia",
        "font": "Georgia"
    },
    {
        "text": "Impact",
        "font": "Impact"
    },
    {
        "text": "Trebuchet MS",
        "font": "\"Trebuchet MS\""
    },
    {
        "text": "Verdana",
        "font": "Verdana"
    },
    {
        "text": "Arial Black",
        "font": "\"Arial Black\""
    },
    {
        "text": "Arial Narrow",
        "font": "\"Arial Narrow\""
    },
    {
        "text": "Cambria",
        "font": "Cambria"
    },
    {
        "text": "Cambria Math",
        "font": "\"Cambria Math\""
    },
    {
        "text": "Candara",
        "font": "Candara"
    },
    {
        "text": "Consolas",
        "font": "Consolas"
    },
    {
        "text": "Constantia",
        "font": "Constantia"
    },
    {
        "text": "Corbel",
        "font": "Corbel"
    },
    {
        "text": "Ebrima",
        "font": "Ebrima"
    },
    {
        "text": "Franklin Gothic",
        "font": "\"Franklin Gothic\""
    },
    {
        "text": "Franklin Gothic Medium",
        "font": "\"Franklin Gothic Medium\""
    },
    {
        "text": "Gabriola",
        "font": "Gabriola"
    },
    {
        "text": "Gadugi",
        "font": "Gadugi"
    },
    {
        "text": "Lucida Console",
        "font": "\"Lucida Console\""
    },
    {
        "text": "Lucida Sans Unicode",
        "font": "\"Lucida Sans Unicode\""
    },
    {
        "text": "Malgun Gothic",
        "font": "\"Malgun Gothic\""
    },
    {
        "text": "Marlett",
        "font": "Marlett"
    },
    {
        "text": "Microsoft Himalaya",
        "font": "\"Microsoft Himalaya\""
    },
    {
        "text": "Microsoft JhengHei UI",
        "font": "\"Microsoft JhengHei UI\""
    },
    {
        "text": "Microsoft JhengHei UI Light",
        "font": "\"Microsoft JhengHei UI Light\""
    },
    {
        "text": "Microsoft New Tai Lue",
        "font": "\"Microsoft New Tai Lue\""
    },
    {
        "text": "Microsoft PhagsPa",
        "font": "\"Microsoft PhagsPa\""
    },
    {
        "text": "Microsoft Sans Serif",
        "font": "\"Microsoft Sans Serif\""
    },
    {
        "text": "Microsoft Tai Le",
        "font": "\"Microsoft Tai Le\""
    },
    {
        "text": "Microsoft YaHei UI",
        "font": "\"Microsoft YaHei UI\""
    },
    {
        "text": "Microsoft YaHei UI Light",
        "font": "\"Microsoft YaHei UI Light\""
    },
    {
        "text": "Microsoft Yi Baiti",
        "font": "\"Microsoft Yi Baiti\""
    },
    {
        "text": "MingLiU-ExtB",
        "font": "MingLiU-ExtB"
    },
    {
        "text": "MingLiU_HKSCS-ExtB",
        "font": "MingLiU_HKSCS-ExtB"
    },
    {
        "text": "Mongolian Baiti",
        "font": "\"Mongolian Baiti\""
    },
    {
        "text": "MS Gothic",
        "font": "\"MS Gothic\""
    },
    {
        "text": "MS PGothic",
        "font": "\"MS PGothic\""
    },
    {
        "text": "MS UI Gothic",
        "font": "\"MS UI Gothic\""
    },
    {
        "text": "MT Extra",
        "font": "\"MT Extra\""
    },
    {
        "text": "MV Boli",
        "font": "\"MV Boli\""
    },
    {
        "text": "Myanmar Text",
        "font": "\"Myanmar Text\""
    },
    {
        "text": "Nirmala UI",
        "font": "\"Nirmala UI\""
    },
    {
        "text": "Palatino Linotype",
        "font": "\"Palatino Linotype\""
    },
    {
        "text": "PMingLiU-ExtB",
        "font": "PMingLiU-ExtB"
    },
    {
        "text": "Segoe Print",
        "font": "\"Segoe Print\""
    },
    {
        "text": "Segoe Script",
        "font": "\"Segoe Script\""
    },
    {
        "text": "Segoe UI Symbol",
        "font": "\"Segoe UI Symbol\""
    },
    {
        "text": "Sylfaen",
        "font": "Sylfaen"
    },
    {
        "text": "Symbol",
        "font": "Symbol"
    },
    {
        "text": "Tahoma",
        "font": "Tahoma"
    },
    {
        "text": "Webdings",
        "font": "Webdings"
    },
    {
        "text": "Wingdings",
        "font": "Wingdings"
    },
    {
        "text": "Yu Gothic",
        "font": "\"Yu Gothic\""
    },
    {
        "text": "Yu Gothic Light",
        "font": "\"Yu Gothic Light\""
    },
    {
        "text": "Yu Gothic Medium",
        "font": "\"Yu Gothic Medium\""
    },
    {
        "text": "Yu Gothic UI",
        "font": "\"Yu Gothic UI\""
    },
    {
        "text": "Yu Gothic UI Light",
        "font": "\"Yu Gothic UI Light\""
    },
    {
        "text": "Yu Gothic UI Semibold",
        "font": "\"Yu Gothic UI Semibold\""
    },
    {
        "text": "Courier",
        "font": "Courier"
    },
    {
        "text": "Times",
        "font": "Times"
    }
]
