import { getMaxMinDoaseNumber, getRationalSafeUse } from '@/api/diagnosis.js'
import {addRecipeFrom} from '@/api/dataAnalysis';

let hack_openAddress_true;

export default {
    data() {
        return {
            showCase: false, // 参考医案弹窗
            showUnifyPresc: false, // 统建处方弹窗
            showPresc: false, // 方剂 协定方弹窗
            showSuitUnifyPresc: false, // 统建处方弹窗副本(适宜技术-医派协定方按钮，可独立修改)
            showSuitPresc: false, // 统建处方弹窗副本(适宜技术-协定方按钮，可独立修改)
            showSafeD: false, // 安全合理用药
            isShrink: false, // 是否是收缩状态

            saving: false,

            // 中药处方 中药制剂 适宜技术处方提交前暂存
            zhongPrescriptionVo: null,
            chengPrescriptionVo: null,
            technologyPrescriptionVo: null,

            doseMax: 0, // 最大开方数量
            doseMin: 0, // 最小开方数量

            doseAllowMax: 0, // 是否允许最大开方
            doseAllowMin: 0, // 是否允许最小开方

            innerWidth: 0,// 屏幕宽度

            isDaiJian: 2, // 1是 0 否  2:自煎
            isPs: 1, // 是否配送  0:是 1:否

            isShowDj: true, // 是否展示代煎
            isShowPs: true, // 是否展示配送
            optionsAgency: [], // 代煎选项
            optionsDelivery: [], // 配送选项


            showAddress: false, // 展示选择代煎 地址

        }
    },
    beforeRouteEnter(to, from, next) {


        next(vm => {
            vm.setWidth()
        })
    },
    beforeRouteLeave(to, from, next) {
        let width = window.innerWidth

        if (width <= 1280 && width > 800) {
            document.body.style.width = '1280px'
        }

        if (width < 800) {
            document.body.style.width = '100%'
        }

        next()
    },
    beforeCreate() {
        this.$nextTick(vm => {
            let width = window.innerWidth
            this.innerWidth = width
        })
    },
    mounted() {
        this.isShrink = window.sessionStorage.getItem('isShrink') == 1
        this.getMaxMinDoaseNumber()
    },
    methods: {
        psChange(e) {

            this.isPs = e
        },
        djChange(e) {

            this.isDaiJian = e
        },
        // 展示收缩模式弹窗
        openDCase() {
            this.showCase = true
        },
        openPrec() {
            this.showPresc = true
            addRecipeFrom({ type: 2 }).catch();
            addRecipeFrom({ type: 4 }).catch();
        },
        openUnifyPrescPrec() {
            this.showUnifyPresc = true
            addRecipeFrom({ type: 2 }).catch();
        },
        // 适宜技术-医派协定方按钮 打开独立副本弹窗
        openSuitUnifyPresc() {
            this.showSuitUnifyPresc = true
            addRecipeFrom({ type: 2 }).catch();
        },
        // 适宜技术-协定方按钮 打开独立副本弹窗
        openSuitPresc() {
            this.showSuitPresc = true
            addRecipeFrom({ type: 2 }).catch();
        },
        opensafeD() {
            this.showSafeD = true
        },
        // 设置收缩模式
        setShrink() {
            this.isShrink = !this.isShrink
            window.sessionStorage.setItem('isShrink', this.isShrink ? 1 : 0)
            this.setWidth()
        },
        // 
        setWidth() {

            let width = window.innerWidth
            this.innerWidth = width
            if (width <= 1280 && width > 800) {
                document.body.style.width = '1280px'
            }
            if (width < 800) {
                let isShrink = window.sessionStorage.getItem('isShrink')
                isShrink = Number(isShrink)
                if (!isShrink) {
                    // 扩展状态
                    document.body.style.width = '1280px'
                } else {
                    // 收缩模式
                    document.body.style.width = '100%'
                }

            }
        },
        // 我的协定方转方
        agreeTurnRecipe(agreeInfo) {
            console.log('[协定方转方] agreeInfo:', agreeInfo);
            // this.turnRecipe2(agreeInfo)
            this.transposition({...agreeInfo, medicine: agreeInfo.preStiDetails, __RecipeFrom__: '2'});

            if (agreeInfo.diseaseid) {
                this.$refs.TCM.setParams({
                    namemedicine: agreeInfo.diseasename,
                    disid: agreeInfo.diseaseid,
                    disCode: agreeInfo.diseaseid, // 协定方 diseaseid 字段实际存的是病名 code，添加 disCode 
                    symptomid: agreeInfo.symptomid,
                    syndrometypes: agreeInfo.symptomname,
                    treatment: agreeInfo.treatment
                });

                setTimeout(() => {
                    this.tcmClick(false);
                }, 200);
            }
        },
        // 补充中医电子病历
        handleReplenish() {
            let params = this.$refs.TCM.getParams();
            this.$router.push({
                path: `/index/dialectical?name=${params.namemedicine}`
            })
        },
        // 切换药房时 监听煎 配送是否显示
        onUpdateDp(e) {
            const displayAgency = Array.isArray(e.displayAgency) ? e.displayAgency : [];
            const displayDelivery = Array.isArray(e.displayDelivery) ? e.displayDelivery : [];
            const optionsAgency = Array.isArray(e.optionsAgency) ? e.optionsAgency : [];
            const optionsDelivery = Array.isArray(e.optionsDelivery) ? e.optionsDelivery : [];

            this.isDaiJian = e.defaultAgency;
            this.isShowDj = displayAgency.length > 0
            this.isShowPs = displayDelivery.length > 0
            this.optionsAgency = optionsAgency.filter(option => displayAgency.includes(option.value));
            this.optionsDelivery = optionsDelivery.filter(option => displayDelivery.includes(option.value));

            this.openAddress()

        },
        setUpdateDp(){/* 将模态框的值同步到处方数据 bottom_form */},
        openAddress() {
            clearTimeout(hack_openAddress_true);
            //避免页面返回地址弹窗
            if (!this._prescribingInited) return;
            // 只在中药处方 tab 高亮时才弹出地址弹窗
            if (this.container_i !== 0) return;

            if (!this.isShowPs) this.isPs = 1;
            if (!this.isShowDj) this.isDaiJian = 2;

            if (this.isShowDj || this.isShowPs) {
                this.showAddress = true;

                hack_openAddress_true = setTimeout(() => {
                    let children = this.$children.filter(item => {
                        return (
                            item.name == "中药处方" ||
                            item.name == "中药制剂" ||
                            item.name == "适宜技术处方"
                        );
                    });
                    let child = children[0];

                    const isDaiJian = child.recipe_tabs[child.recipe_tabs_c].bottom_form.isDaiJian
                    const isPs = child.recipe_tabs[child.recipe_tabs_c].bottom_form.radio
                    if (isPs != null) this.isPs = isPs;
                    if (isDaiJian != null) this.isDaiJian = isDaiJian;
                    this.setUpdateDp = (/* 模态框选择的值 */ model) => {
                        child.recipe_tabs[child.recipe_tabs_c].bottom_form.isDaiJian = model.isDaiJian
                        child.recipe_tabs[child.recipe_tabs_c].bottom_form.radio = model.iskD
                    }
                }, 200)
            } else {
                this.showAddress = false;
            }
        },

        // 提交地址信息
        submitAddress(cancel) {
            this.showAddress = false;
            if (cancel) return;
            const model = this.$refs.medAdress.form;
            this.psChange(model.iskD)
            this.djChange(model.isDaiJian)
            this.setUpdateDp(model)
        },
        // 获取合理安全用药提醒配置
        async getRationalSafeUse() {
            try {
                const res = await getRationalSafeUse();
                return {
                    filter: res.Data.rational_safe,
                    force: +res.Data.rational_safe_mandatoryOrNot === 1 // 0 否(可以继续开方) 1是(不可以继续开方)
                }
            } catch (e) {
                return {filter: '', force: false,}
            }
        },
        // 获取医保规则最大/最小药味数
        async getMaxMinDoaseNumber() {
            let res = await getMaxMinDoaseNumber()
            if (res.ResultCode == 0) {
                this.doseMax = res.Data.zdyws
                this.doseMin = res.Data.zxyws
                this.doseAllowMax = res.Data.zdyws_mandatoryOrNot // 0 否(可以继续开方) 1是(不可以继续开方)
                this.doseAllowMin = res.Data.zxyws_mandatoryOrNot // 0 否 1是
            }
        }
    },
}