import { getMaxMinDoaseNumber } from '@/api/diagnosis.js'

export default {
    data() {
        return {
            showCase: false, // 参考医案弹窗
            showPresc: false, // 方剂 协定方弹窗
            showSafeD: false, // 安全合理用药
            isShrink: 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, // 是否展示配送

            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.localStorage.getItem('isShrink') == 0 ? false : true
        this.getMaxMinDoaseNumber()
    },
    methods: {
        psChange(e) {

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

            this.isDaiJian = e
        },
        // 展示收缩模式弹窗
        openDCase() {
            this.showCase = true
        },
        openPrec() {
            this.showPresc = true
        },
        opensafeD() {
            this.showSafeD = true
        },
        // 设置收缩模式
        setShrink() {
            this.isShrink = !this.isShrink
            window.localStorage.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.localStorage.getItem('isShrink')
                isShrink = Number(isShrink)
                if (!isShrink) {
                    // 扩展状态
                    document.body.style.width = '1280px'
                } else {
                    // 收缩模式
                    document.body.style.width = '100%'
                }

            }
        },
        // 我的协定方转方
        agreeTurnRecipe(agreeInfo) {
            this.turnRecipe2(agreeInfo)

            if (agreeInfo.diseaseid) {
                this.$refs.TCM.setParams({
                    namemedicine: agreeInfo.diseasename,
                    disid: agreeInfo.diseaseid,
                    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) {

            let isShowDj = e.displayAgency == 0 ? true : false // 是否展示代煎 0 展示 1不展示
            let isShowPs = e.displayDelivery == 0 ? true : false // 是否展示配送 0 展示 1不展示
            // this.isDaiJian = e.defaultAgency == 0 ? 1 : ''
            this.isShowDj = isShowDj
            this.isShowPs = isShowPs

            this.openAddress()

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

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

                    this.isDaiJian = child.recipe_tabs[child.recipe_tabs_c].bottom_form.isDaiJian
                    this.isPs = child.recipe_tabs[child.recipe_tabs_c].bottom_form.radio
                }, 200)
            }
        },
        // 提交地址信息
        submitAddress() {
            this.showAddress = false
            return
            let zhongPrescriptionVo = this.dealRecipe1();
            let chengPrescriptionVo = {};
            setTimeout(() => {
                chengPrescriptionVo = this.dealRecipe2();
            }, 100);
            let technologyPrescriptionVo = {};
            setTimeout(() => {
                technologyPrescriptionVo = this.dealRecipe3();
            }, 200);
            setTimeout(() => {


                this._getRecipePriview(
                    zhongPrescriptionVo,
                    chengPrescriptionVo,
                    technologyPrescriptionVo
                );


            }, 500);
        },
        // 获取医保规则最大/最小药味数
        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是
            }
        }
    },
}