import {
  GET_ASSIST_SEARCH_LIST,
  GET_ASSIST_LABEL,
  DEL_ASSIST_LABEL,
  CHANGE_ASSIST_VAL,
  CHANGE_DATE,
  CLEAR_ASSIST_DATA,
  ADD_ASSIST_LABEL,
  CHECK_OFF_ON,
  REASSISTCHECKDATA,
  CLEARASSISTCHECKDATA,
  SET_TIP_VAL,
} from '../types/assistCheck';
import store from '@store';
const initSearchList = {
  list: [], //搜索的结果
  assistLabel: [], //点击的结果
  dataString: '', //结果拼接
  dataArr: [], //结果拼接
  assistVal: '',

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

export default (state = initSearchList, action) => {
  if (action.type == SET_TIP_VAL) { //改变shijian
    const newState = Object.assign({}, state);
    const tempArr = newState.assistLabel;
    for (let i = 0; i < tempArr.length; i++) {
      if (i == action.idx) {
        tempArr[i].dateValue = action.val
        tempArr[i].time = action.val
        newState.assistLabel = [...tempArr]
      }
    }
    let tmpObj = getAllString(newState.checkedListImport,newState.assistLabel)
    newState.dataArr = tmpObj.arr
    newState.dataString = tmpObj.str
    return newState;
  }
  if (action.type == ADD_ASSIST_LABEL) {
    const newState = Object.assign({}, state);
    let tempArr = newState.assistLabel;
    for (let i = 0; i < action.lis.length; i++) {
      tempArr.push(action.lis[i]);
    }
    for (let i = 0; i < tempArr.length; i++) {
      if (i == action.idx) {
        tempArr[i].time = action.date
        tempArr[i].dateValue = action.date
        newState.assistLabel = [...tempArr]
      }
    }
    newState.assistLabel = [...tempArr]
    let tmpObj = getAllString(newState.checkedListImport,newState.assistLabel)
    newState.dataString = tmpObj.str
    newState.dataArr = tmpObj.arr
    return newState;
  }
  if (action.type == GET_ASSIST_SEARCH_LIST) { //搜索结果
    const newState = Object.assign({}, state);
    newState.list = action.list
    newState.assistVal = action.val
    let tmpObj = getAllString(newState.checkedListImport,newState.assistLabel)
    newState.dataString = tmpObj.str
    newState.dataArr = tmpObj.arr
    return newState;
  }
  if (action.type == GET_ASSIST_LABEL) { //选中搜索结果
    const newState = Object.assign({}, state);
    let tempArrs = newState.assistLabel;
    let tempList = action.item
    tempList['dateValue'] = getCurrentDate(1)
    tempList['time'] = getCurrentDate(1)
    tempArrs.push(tempList)
    let tmpObj = getAllString([],newState.assistLabel)
    newState.assistLabel = [...tempArrs]
    newState.dataArr = tmpObj.arr
    newState.dataString = tmpObj.str
    return newState;
  }
  if (action.type == CHECK_OFF_ON) { //选中开单项
    const newState = Object.assign({}, state);
    let tempArrs = newState.assistLabel;
    let tempList = action.item,tempOrder=[]
    for(let i = 0;i < tempArrs.length;i++){
        if(action.tip == i){
            let flg = tempArrs[i].check;
            tempArrs[i].check = !flg
        }
    }
    let tmpObj = getAllString([],newState.assistLabel)
    tempOrder=tempArrs.filter((item)=>item.check)
    newState.totalOrder = [...tempOrder]
    newState.assistLabel = [...tempArrs]
    newState.dataArr = tmpObj.arr
    newState.dataString = tmpObj.str
    return newState;
  }
  if (action.type == DEL_ASSIST_LABEL) { //删除
    const newState = Object.assign({}, state);
    let tempArr = newState.assistLabel,tempArrs = [],tempOrder=[];
    let tmpImportLis = newState.checkedListImport;
    if(action.flg == 0){//删除检查导入
      tmpImportLis.splice(action.idx,1)
    }else{
      for (let k = 0; k < tempArr.length; k++) {
        if (k != action.idx) {
          tempArrs.push(tempArr[k])
        }
      }
      newState.assistLabel = [...tempArrs]
    }
    let tmpObj = getAllString(newState.checkedListImport,newState.assistLabel)
    tempOrder=tempArrs.filter((item)=>item.check)
    newState.totalOrder = [...tempOrder]
    newState.dataArr = tmpObj.arr
    newState.dataString = tmpObj.str
    return newState;
  }
  if (action.type == CHANGE_ASSIST_VAL) { //改变输入值
    const newState = Object.assign({}, state);
    const tempArr = newState.assistLabel;
    for (let i = 0; i < tempArr.length; i++) {
      if (i == action.idx) {
        tempArr[i].value = action.val
        tempArr[i].result = action.val
        newState.assistLabel = [...tempArr]
      }
    }
    let tmpObj = getAllString(newState.checkedListImport,newState.assistLabel)
    newState.dataArr = tmpObj.arr
    newState.dataString = tmpObj.str
    return newState;
  }
  if (action.type == CHANGE_DATE) { //新增
    const newState = Object.assign({}, state);
    const tempArr = newState.assistLabel;
    for (let i = 0; i < tempArr.length; i++) {
      if (i == action.idx) {
        tempArr[i].time = action.date
        newState.assistLabel = [...tempArr]
      }
    }
    let tmpObj = getAllString(newState.checkedListImport,newState.assistLabel)
    newState.dataArr = tmpObj.arr
    newState.dataString = tmpObj.str
    return newState;
  }
  if (action.type == CLEAR_ASSIST_DATA) {
    const newState = Object.assign({}, state);
    newState.assistLabel = [...action.data];
    newState.dataString = action.saveText;
    newState.checkedListImport = action.checkedListImport;
    if(action.checkedListImport.length == 0&&action.data.length==0){
      newState.dataArr = [];
    }
    return newState;
  }
  if (action.type == REASSISTCHECKDATA) {
    const newState = Object.assign({}, state);
    newState.assistLabel = action.params[9].contentValue;
    newState.totalOrder = action.params[9].contentValue;
    newState.dataArr = action.params[9].contentValue;
    newState.dataString = action.params[9].content;
    return newState;
  }
  if (action.type == CLEARASSISTCHECKDATA) {
    let newState = Object.assign({}, state);
    newState.assistLabel = [];
    newState.dataArr = [];
    newState.dataString = [];
    newState.totalOrder = [];
    return newState;
  }
  return state;
}
