import { SET_DIAG_TO_MAINSUIT, SET_CHRONIC_MAG_ITEM, SHOW_REFER_RECORD } from '@store/types/diagnosticList';
import {getAllDataList,getAllDataStringList,isAllClear,filterDataArr} from '@utils/tools';
import {getChronic,getInitModules} from '@store/async-actions/homePage.js';
import {storageLocal} from '@utils/tools'
import { SAVE_FREE } from '@store/types/mainSuit';
import {ISREAD,MODI_LOADING} from "../types/homePage";
export const isAddMainSuit = () =>{
    return (dispatch, getState) => {
        const state = getState();
        if(state.diagnosticList.diagnosticList.length === 1 && state.diagnosticList.diagnosticList[0].type === 2) {
            const isAddMainSuitFlag = state.mainSuit.data && state.mainSuit.data.length === 0 && state.mainSuit.saveText    //判断主诉和化验查体是否为空
                                      && (state.mainSuit.saveText.length === 0 || state.mainSuit.saveText.length === 1 
                                      &&  state.mainSuit.saveText[0] === '') && state.assistCheck && state.assistCheck.dataString.length === 0
                                      && state.inspect.labelList && state.inspect.labelList.length === 0 && state.inspect.inspectStrPlus.length === 0
            if(isAddMainSuitFlag) {
                dispatch({
                    type:ISREAD
                });
                dispatch({
                    type: SET_DIAG_TO_MAINSUIT,
                    data: state.diagnosticList.diagnosticStr
                })
                dispatch({
                    type: SAVE_FREE,
                    data:state.diagnosticList.diagnosticStr
                })
            }
        }
        
    }
}


export const isChronicMag = (item) => {
    return (dispatch, getState) => {
        const state = getState();
        let jsonData = getAllDataList(state);
        let jsonStr = getAllDataStringList(state);
        let flg = isAllClear(jsonData,jsonStr,state);
        if(!flg) {
             // 从缓存取慢病列表
             let chronicList = JSON.parse(storageLocal.get('chronic'));
             if(!chronicList){
                 getChronic().then(() =>{
                    chronicList = JSON.parse(storageLocal.get('chronic'));
                    for(let i=0; i<chronicList.length; i++){
                        if(chronicList[i].id==item.id&&chronicList[i].name==item.name){
                            //弹窗提示 “是否引用往期病例”？--往期病例接口、弹窗、引用
                            // 是--引用   否--走慢病流程
                            dispatch({
                                type: SET_CHRONIC_MAG_ITEM,
                                chronicMagItem: item
                            })
                            // loading
                            dispatch({type:MODI_LOADING,flag:true});
                            // 获取慢病模板
                            dispatch(getInitModules).then(()=>{
                                dispatch({type:MODI_LOADING,flag:false});
                                dispatch({
                                    type: SHOW_REFER_RECORD
                                })
                            });
                        }
                    }
                 });
             } else {
                for(let i=0; i<chronicList.length; i++){
                    if(chronicList[i].id==item.id&&chronicList[i].name==item.name){
                        //弹窗提示 “是否引用往期病例”？--往期病例接口、弹窗、引用
                        // 是--引用   否--走慢病流程
                        dispatch({
                            type: SET_CHRONIC_MAG_ITEM,
                            chronicMagItem: item
                        })
                        // loading
                        dispatch({type:MODI_LOADING,flag:true});
                        // 获取慢病模板
                        dispatch(getInitModules).then(()=>{
                            dispatch({type:MODI_LOADING,flag:false});
                            dispatch({
                                type: SHOW_REFER_RECORD
                            })
                        });
                        
                    }
                }
             }
             
        }
        
    }
}

