import {
  SEARCH_HOSPITAL_PAC,
  SEARCH_HOSPITAL_PAC_SON,
  SET_ALLCHECKS,
  SELECT_ONE_CHECKS,
  SHOW_IN_ICSSS
} from '../types/wrapModalBodyPac';
import {getAllString} from '@utils/tools.js';
import store from '@store';
import { getMRAnalyse } from '@store/async-actions/pushMessage';

const initSearchList = {
  list: [], //点击的结果
  assistLabel: [], //搜索的结果
  dataString: '', //结果拼接
  assistVal: '',

  checkedListImport: [], //检查导入
  hospitalPac: [], //医院检索到的
  hospitalPacObj: {}, //组对应的明细（单选多选全选）
  selectGroupList: [], //选的组的明细可能有多个组
  allCheck: false, //全选反选
  checkedList: [], //选中的小项
  allCheckLis:[],//所有导入数据
  msgObj:{
    name:'',
    patientNum:''
  }
}

export default (state = initSearchList, action) => {
  if (action.type == SHOW_IN_ICSSS) {
    let allState = store.getState().assistCheck;
    const newState = Object.assign({}, state);
    let tmpGroup = JSON.parse(JSON.stringify(newState.selectGroupList))
    let tmpListImport = JSON.parse(JSON.stringify(allState.checkedListImport))
    let tempArrs = JSON.parse(JSON.stringify(allState.assistLabel)),importLis=[];
    for (let i = 0; i < tmpGroup.length; i++) {
      for (let k = 0; k < tmpGroup[i].pacsResultDTO.length; k++) {
        let tmpObj = {}
        tmpObj.time = tmpGroup[i].Rtime
        tmpObj.disabled = true
        tmpObj.name = tmpGroup[i].pacsResultDTO[k].checkItem
        tmpObj.value = tmpGroup[i].pacsResultDTO[k].imagingResults
        tmpObj.imagingDiagnose = tmpGroup[i].pacsResultDTO[k].imagingDiagnose
        importLis.push(tmpObj)
        tempArrs.push(tmpObj)
      }
    }
    if(JSON.stringify(importLis) != '[]'){
      tmpListImport.push(importLis)
    }

    allState.checkedListImport = [...tmpListImport]
    allState.allCheckLis = [...tempArrs]
    allState.dataString = getAllString(tmpListImport,allState.assistLabel)

    newState.hospitalPac = [] //医院检索到的
    newState.hospitalPacObj = {} //组对应的明细（单选多选全选）
    newState.selectGroupList = [] //选的组的明细可能有多个组
    newState.allCheck = false //全选反选
    newState.checkedList = [] //选中的小项
    newState.msgObj={name:'',patientNum:''}
    if(importLis.length > 0) {
      store.dispatch(getMRAnalyse())
    }
    return newState;
  }
  if (action.type == SELECT_ONE_CHECKS) {
    const newState = Object.assign({}, state);
    let listWrap = JSON.parse(JSON.stringify(newState.hospitalPacObj))
    let tmpGroup = JSON.parse(JSON.stringify(newState.selectGroupList))
    let tmpLen = listWrap.pacsResultDTO.length;
    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].checkItem == action.checkItem) { //右侧有选中相同组的明细
          tmpGroup[m].pacsResultDTO.push(action.item)
        } else {
          ++numS
        }
      }
      if (numS == tmpGroup.length) { //直接添加到右侧
        listWrap.pacsResultDTO = []
        listWrap.pacsResultDTO.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].checkItem == action.checkItem) { //右侧肯定有相同组的
          let tmpArr = []
          for (let k = 0; k < tmpGroup[i].pacsResultDTO.length; k++) {
            if (action.name != tmpGroup[i].pacsResultDTO[k].checkItem) {
              tmpArr.push(tmpGroup[i].pacsResultDTO[k])
            }
          }
          if(tmpArr.length == 0){
            tmpGroup.splice(i,1)
          }else{
            tmpGroup[i].pacsResultDTO = [...tmpArr]
          }
        }
      }
      newState.selectGroupList = tmpGroup //右侧已选中的项目
      if (tempArr.length == listWrap.pacsResultDTO.length) {
        newState.allCheck = true;
      } else {
        newState.allCheck = false;
      }
    }
    return newState;
  }
  if (action.type == SET_ALLCHECKS) {
    const newState = Object.assign({}, state);
    let flg = newState.allCheck
    let listWrap = newState.hospitalPacObj
    let tmpGroup = newState.selectGroupList
    let tmpCheckedLis = []
    newState.allCheck = !flg
    if (!flg) { //全选
      let numFst = 0
      for (let i = 0; i < tmpGroup.length; i++) {
        if (listWrap.checkItem == tmpGroup[i].checkItem) {
          ++numFst
          tmpGroup[i].pacsResultDTO = listWrap.pacsResultDTO
        }
      }
      for (let m = 0; m < listWrap.pacsResultDTO.length; m++) {
        tmpCheckedLis.push(listWrap.pacsResultDTO[m].checkItem)
      }
      if (numFst == 0) {
        tmpGroup.push(listWrap)
      }
      newState.checkedList = [...tmpCheckedLis]
      newState.selectGroupList = [...tmpGroup]
    } else { //反选
      for (let j = 0; j < tmpGroup.length; j++) {
        if (listWrap.checkItem == tmpGroup[j].checkItem) {
          tmpGroup.splice(j, 1)
        }
      }
      newState.checkedList = []
      newState.selectGroupList = [...tmpGroup]
    }
    return newState;
  }
  if (action.type == SEARCH_HOSPITAL_PAC_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].checkItem == action.name) {
        for (let k = 0; k < tmpSelect[i].pacsResultDTO.length; k++) {
          tmpSelectSon.push(tmpSelect[i].pacsResultDTO[k].checkItem)
        }
      }
    }
    newState.checkedList = [...tmpSelectSon]
    if (tmpSelectSon.length == tmpArr.pacsResultDTO.length) {
      newState.allCheck = true;
    } else {
      newState.allCheck = false;
    }
    newState.hospitalPacObj = tmpArr
    return newState;
  }
  if (action.type == SEARCH_HOSPITAL_PAC) {
    const newState = Object.assign({}, state);
    newState.hospitalPac = action.list
    newState.msgObj = action.msg
    newState.allCheck = false
    newState.hospitalPacObj = {}
    return newState;
  }
  return state;
}

