import {
  SEARCH_HOSPITAL_INSPECT,
  SEARCH_HOSPITAL_INSPECT_SON,
  SET_ALLCHECK_INS,
  SELECT_ONNY_CHECK,
  SELECT_WARING,
  SHOW_IN_ICSS_INS
} from '../types/wrapModalBody';
import store from '@store';
import { getStringPlus,  getPushLists,  getPushList} from '@utils/tools.js';
import { getMRAnalyse } from '@store/async-actions/pushMessage';

const initWrapModal = {
  list: [], //搜索的结果

  getExcelData: {}, //导入的数据

  hospitalInspect:[],//医院检索到的

  hospitalInspectObj:{},//组对应的明细（单选多选全选）
  selectGroupList:[],//选的组的明细可能有多个组
  allCheck:false,//全选反选
  checkedList:[],//选中的小项
  btnMsg:'选中全部异常值数据'
}

export default (state = initWrapModal, action) => {
  if (action.type == SHOW_IN_ICSS_INS) {
      let allState = store.getState().inspect
      const newState = Object.assign({}, state);
      let tmpGroup = JSON.parse(JSON.stringify(newState.selectGroupList))
      let tmpArr = JSON.parse(JSON.stringify(allState.getExcelDataList));
      let tmpArr2 = allState.labelList;
      let excelDataLis = {
        lisExcelRes:[],
        messages:[]
      }
      for(let i = 0;i < tmpGroup.length;i++){
        let tmpObj = {//excelDataLis.lisExcelRes[0]
          menus:'',
          lisExcelItem:[]
        }
        for(let k = 0;k < tmpGroup[i].lisExcelWrapper.length;k++){
          tmpGroup[i].lisExcelWrapper[k].time = tmpGroup[i].Rtime
        }
        tmpObj.menus = tmpGroup[i].groupName
        tmpObj.lisExcelItem = tmpGroup[i].lisExcelWrapper
        excelDataLis.lisExcelRes.push(tmpObj)
      }
      newState.getExcelData = excelDataLis
      tmpArr.push(excelDataLis)
      newState.hospitalInspect = [] //医院检索到的
      newState.hospitalInspectObj = {} //组对应的明细（单选多选全选）
      newState.selectGroupList = [] //选的组的明细可能有多个组
      newState.allCheck = false //全选反选
      newState.checkedList = [] //选中的小项
      allState.getExcelDataList = [...tmpArr]
      allState.pushItem = getPushList(tmpArr,tmpArr2);
      allState.pushItems = getPushLists(tmpArr,tmpArr2);
      allState.inspectStrPlus = getStringPlus(getPushLists(tmpArr,tmpArr2))
      if(excelDataLis.lisExcelRes.length > 0) {
        store.dispatch(getMRAnalyse())
      }

      return newState;
  }
  if (action.type == SELECT_WARING) {//选中异常值
      const newState = Object.assign({}, state);
      let listWrap = JSON.parse(JSON.stringify(newState.hospitalInspectObj))
      let listWrapLen = listWrap&&listWrap.lisExcelWrapper.length
      let tmpGroup = JSON.parse(JSON.stringify(newState.selectGroupList))
      let tmpCheckedLis = JSON.parse(JSON.stringify(newState.checkedList))
      let warArr = [],checked = [],numS=0,selectNum=0;
      // console.log(action,listWrap,tmpCheckedLis)
      for(let m =0;m < listWrap.lisExcelWrapper.length;m++){
        let tmpName = listWrap.lisExcelWrapper[m].itemName
        let tmpType = listWrap.lisExcelWrapper[m].type
        for(let n = 0;n < tmpCheckedLis.length;n++){
          if(tmpType == 0 && tmpName == tmpCheckedLis[n]){//已选的正常的选项要保留
            warArr.push(listWrap.lisExcelWrapper[m])
            checked.push(tmpName)
          }
        }
        if(tmpType != 0 && (tmpCheckedLis.filter(item=>item == tmpName)).length == 0){
          ++selectNum
        }
      }
      // console.log(selectNum)
      for(let m =0;m < listWrap.lisExcelWrapper.length;m++){
        let tmpName = listWrap.lisExcelWrapper[m].itemName
        let tmpType = listWrap.lisExcelWrapper[m].type
        if(selectNum>0){
          if(listWrap.lisExcelWrapper[m].type != 0){//没有选中的选项要加上,有一个异常未选中就选中
            warArr.push(listWrap.lisExcelWrapper[m])
            checked.push(tmpName)
          }
          newState.btnMsg = "取消选中全部异常值数据"
        }else{//全部的选中就去掉所有异常选中的
          for(let n = 0;n < tmpCheckedLis.length;n++){
            if(tmpType == 0 && tmpName == tmpCheckedLis[n]){//已选的正常的选项要保留
              checked=[]
              warArr=[]
              warArr.push(listWrap.lisExcelWrapper[m])
              checked.push(tmpName)
            }
          }
          newState.btnMsg = "选中全部异常值数据"
        }
      }
      for(let i = 0;i < tmpGroup.length;i++){
        if(tmpGroup[i].groupName == listWrap.groupName){//右侧有选中相同组的明细
          if(warArr.length == 0){
            tmpGroup.splice(i,1)
          }else{
            tmpGroup[i].lisExcelWrapper = warArr
          }
        }else{//右侧没有选中相同组的明细
          ++numS
        }
      }
      if(numS == tmpGroup.length){//直接添加到右侧
        if(warArr.length > 0){
          listWrap.lisExcelWrapper = warArr
          tmpGroup.push(listWrap)
        }
      }
      if(checked.length == listWrapLen){
        newState.allCheck = true;            
      }else{
        newState.allCheck = false;
      }
      newState.selectGroupList = [...tmpGroup]//右侧已选中的项目
      newState.checkedList = [...checked]
      return newState;
  }
  if (action.type == SELECT_ONNY_CHECK) {
      const newState = Object.assign({}, state);
      let listWrap = JSON.parse(JSON.stringify(newState.hospitalInspectObj))
      let tmpLen = listWrap.lisExcelWrapper.length;
      let tmpGroup = JSON.parse(JSON.stringify(newState.selectGroupList))
      let tmpCheckedLis = JSON.parse(JSON.stringify(newState.checkedList))
      let numS = 0;
      if (tmpCheckedLis.indexOf(action.name) == -1) {//添加选中
        let tempArr = newState.checkedList;
        tempArr.push(action.name);
        newState.checkedList = [...tempArr]
        for(let m = 0;m < tmpGroup.length;m++){
          if(tmpGroup[m].groupName == action.groupName){//右侧有选中相同组的明细
            tmpGroup[m].lisExcelWrapper.push(action.item)
          }else{
            ++numS
          }
        }
        if(numS == tmpGroup.length){//直接添加到右侧
          listWrap.lisExcelWrapper = []
          listWrap.lisExcelWrapper.push(action.item)
          tmpGroup.push(listWrap)
        }
        newState.selectGroupList = [...tmpGroup]//右侧已选中的项目
        if(tempArr.length == tmpLen){
          newState.allCheck = true;            
        }else{
          newState.allCheck = false;
        }
      } else {//去除选中
        let tempArr = newState.checkedList;
        tempArr.splice(tempArr.findIndex(item => item === action.name), 1)
        newState.checkedList = [...tempArr]
        for(let i = 0;i < tmpGroup.length;i++){
          if(tmpGroup[i].groupName == action.groupName){//右侧肯定有相同组的
            let tmpArr = []
            for(let k = 0;k < tmpGroup[i].lisExcelWrapper.length;k++){
              if(action.name != tmpGroup[i].lisExcelWrapper[k].itemName){
                tmpArr.push(tmpGroup[i].lisExcelWrapper[k])
              }
            }
            if(tmpArr.length == 0){
              tmpGroup.splice(i,1)
            }else{
              tmpGroup[i].lisExcelWrapper = [...tmpArr]
            }
          }
        }
        newState.selectGroupList = tmpGroup//右侧已选中的项目
        if(tempArr.length == listWrap.lisExcelWrapper.length){
          newState.allCheck = true;            
        }else{
          newState.allCheck = false;
        }
      }

      let selectNum=0;
      let tmpCheckedList = JSON.parse(JSON.stringify(newState.checkedList))
      let tmpListWrap = JSON.parse(JSON.stringify(newState.hospitalInspectObj))
      for(let m =0;m < tmpListWrap.lisExcelWrapper.length;m++){
          let tmpName = tmpListWrap.lisExcelWrapper[m].itemName
          let tmpType = tmpListWrap.lisExcelWrapper[m].type
          if(tmpType != 0&& (tmpCheckedList.filter(item=>item == tmpName)).length == 0){
            selectNum = selectNum+1
        }
      }
      if(selectNum>0){//说明有没选中的
          newState.btnMsg = "选中全部异常值数据"
      }else{
          newState.btnMsg = "取消选中全部异常值数据"
      }
      return newState;
  }
  if (action.type == SET_ALLCHECK_INS) {
      const newState = Object.assign({}, state);
      let flg = newState.allCheck
      let listWrap = newState.hospitalInspectObj
      let tmpGroup = newState.selectGroupList
      let tmpCheckedLis = []
      newState.allCheck = !flg
      if(!flg){//全选
        let numFst=0
        for(let i = 0;i < tmpGroup.length;i++){
          if(listWrap.groupName == tmpGroup[i].groupName){
            ++numFst
            tmpGroup[i].lisExcelWrapper = listWrap.lisExcelWrapper
          }
        }
        for(let m =0;m < listWrap.lisExcelWrapper.length;m++){
          tmpCheckedLis.push(listWrap.lisExcelWrapper[m].itemName)
        }
        if(numFst==0){
          tmpGroup.push(listWrap)
        }
        newState.btnMsg = "取消选中全部异常值数据"
        newState.checkedList=[...tmpCheckedLis]
        newState.selectGroupList = [...tmpGroup]
      }else{//反选
        for(let j = 0;j < tmpGroup.length;j++){
          if(listWrap.groupName == tmpGroup[j].groupName){
            tmpGroup.splice(j,1)
          }
        }
        newState.btnMsg = "选中全部异常值数据"
        newState.checkedList=[]
        newState.selectGroupList = [...tmpGroup]
      }
      return newState;
  }
  if (action.type == SEARCH_HOSPITAL_INSPECT) {
      const newState = Object.assign({}, state);
      newState.hospitalInspect = action.list
      newState.allCheck = false
      newState.hospitalInspectObj={}
      return newState;
  }
  if (action.type == SEARCH_HOSPITAL_INSPECT_SON) {
      const newState = Object.assign({}, state);
      let tmpArr = action.list
      let tmpSelect = newState.selectGroupList
      let tmpSelectSon = []
      for(let i = 0;i < tmpSelect.length;i++){
        if(tmpSelect[i].groupName == action.name){
          for(let k = 0;k < tmpSelect[i].lisExcelWrapper.length;k++){
            tmpSelectSon.push(tmpSelect[i].lisExcelWrapper[k].itemName)
          }
        }
      }
      newState.checkedList = [...tmpSelectSon]
      if(tmpSelectSon.length == tmpArr.lisExcelWrapper.length){
        newState.allCheck = true;            
      }else{
        newState.allCheck = false;
      }
      tmpArr.Rtime = action.time
      newState.hospitalInspectObj = tmpArr
      return newState;
  }
  return state;
}
