import {
    SEARCH_LIST,
    SET_LABEL,
    FILL_ACTIVE,
    GET_EXCEL,
    CHECK_VALUE_IS_CHANGE,
    CLEAR_LABEL,
    RESET_LABEL,
    CLEAR_ALL_LABEL,
    DEL_PART_ITEM,
    DEL_EXCEL_LIST,
    ADD_LABEL
} from '../types/inspect';
import store from '@store'
import {dateParser} from '@common/js/func';

const initSearchList = {
    list: [], //搜索的结果
    labelList: [], //选中的结果,也是需要保存的数据
    labelListActive: [], //缓存值
    fillActive: {}, //选中的标签的填写单

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

    getExcelDataList: [], //导入的数据
    pushItem:[],    //推送的数据
    pushItems:[],    //右侧模板预览的数据
    inspectVal:''
}

export default (state = initSearchList, action) => {
    if (action.type == SEARCH_LIST) {
        const newState = Object.assign({}, state);
        newState.list = action.list
        newState.inspectVal = action.val
        return newState;
    }
    if (action.type == SET_LABEL) {             //选中的结果    
        const newState = Object.assign({}, state);
        let tempArr = [];
        if(action.sign == 'common'){
          let tmpInspectList = store.getState().homePage.inspectList
          tempArr = tmpInspectList;
        }else{
          tempArr = newState.list
        }
        const tempArrs = newState.labelList;
        for (let i = 0; i < tempArr.length; i++) {
            if (i == action.idx) {
                tempArrs.push(tempArr[i])
                newState.labelList = [...tempArrs]
            }
        }
        
        let tmpArr = newState.getExcelDataList;
        let tmpArr2 = newState.labelList;
        newState.pushItems = getPushLists(tmpArr,tmpArr2);
        newState.inspectStrPlus = getStringPlus(newState.pushItems)
        return newState;
    }
    if (action.type == RESET_LABEL) {             //选中的结果重组
        const newState = Object.assign({}, state);
        const tempArr = newState.labelList;
        tempArr[tempArr.length-1].details = action.list;
        return newState;
    } 
    if (action.type == ADD_LABEL) {             //右侧推送的化验辅检项，点击开单放到左侧化验辅检的位置上
        const newState = Object.assign({}, state);
        const tempArr = newState.labelList;
        for(let i = 0;i <action.lis.length;i++){
          tempArr.push(action.lis[i]);
        }
        newState.labelList = [...tempArr]
        
        let tmpArr = newState.getExcelDataList;
        let tmpArr2 = newState.labelList;
        newState.pushItems = getPushLists(tmpArr,tmpArr2);
        newState.inspectStrPlus = getStringPlus(newState.pushItems);
        return newState;
    }
    if (action.type == FILL_ACTIVE) {                        //点击标签放到暂存里
        const newState = Object.assign({}, state);
        const tempArr = newState.labelList;
        for (let i = 0; i < tempArr.length; i++) {
            if (i == action.idx) {
                newState.fillActive = tempArr[i]
            }
        }
        return newState;
    }
    if (action.type == CLEAR_LABEL) {      //清空数据填写
        const newState = Object.assign({}, state);
        const tempArrs = newState.fillActive;
        const tempArr = newState.fillActive.details;
        let tmpArr = newState.getExcelDataList;
        let tmpArr2 = newState.labelList;
        for (let i = 0; i < tempArr.length; i++) {
          // tempArr[i].value = ''
        }
        tempArrs.show = false;
        newState.pushItem = getPushList(tmpArr,tmpArr2);
        newState.pushItems = getPushLists(tmpArr,tmpArr2);
        newState.inspectStrPlus = getStringPlus(newState.pushItems)
        return newState;
    }
    if (action.type == GET_EXCEL) {              //导入数据
        const newState = Object.assign({}, state);
        let tmpArr = newState.getExcelDataList;
        let tmpArr2 = newState.labelList;
        let temPush = newState.pushItem;
        // if(action.data && action.data.lisExcelRes){     //前面五条数据显示，超出折叠
        //   for(let i = 0;i < action.data.lisExcelRes.length;i++){
        //     let tmpEx = action.data.lisExcelRes[i]
        //     if(tmpEx && tmpEx.lisExcelItem){
        //       if(tmpEx.lisExcelItem.length > 4){ //数据超过四条的，显示展开true，点击展开后改变值为false，用于判断页面展开还是收起
        //         tmpEx.slideDir = true
        //       }
        //       for(let k = 0;k < tmpEx.lisExcelItem.length;k++){
        //         let tmpLisItm = tmpEx.lisExcelItem[k]
        //         if(k > 3){
        //           tmpLisItm.isShow = false

        //         }else{
        //           tmpLisItm.isShow = true
        //         }
        //       }
        //     }
        //   }
        // }
        newState.getExcelData = action.data
        tmpArr.push(action.data)
        newState.getExcelDataList = [...tmpArr]
        getExcelString(action,newState,tmpArr,tmpArr2)
        return newState;
    }
    if(action.type == DEL_EXCEL_LIST){       //删除excel列表的某个数据
        const newState = Object.assign({}, state);
        const tempArr2 = newState.labelList;
        let tmpArr = newState.getExcelDataList;
        tmpArr.splice(action.idx,1);
        newState.getExcelDataList = [...tmpArr];
        let tempArr1 = newState.getExcelDataList;
        newState.pushItem = getPushList(tempArr1,tempArr2);
        newState.pushItems = getPushLists(tempArr1,tempArr2);
        newState.inspectStrPlus = getStringPlus(newState.pushItems)
        return newState;
    }
    if (action.type == CHECK_VALUE_IS_CHANGE) {     //数据添加后显示与否
        const newState = Object.assign({}, state);
        // const tempArrAct = newState.fillActive;
        const tempArr = action.arr;
        tempArr.time = action.time;
        let tmpArr = newState.getExcelDataList;
        const tempArrs = newState.labelList;
        tempArrs[action.idx] = tempArr;
        newState.fillActive = tempArr;
        newState.labelList = tempArrs;
        // tempArrAct.show = true;
        newState.pushItem = getPushList(tmpArr,tempArrs);
        newState.pushItems = getPushLists(tmpArr,tempArrs);
        newState.inspectStrPlus = getStringPlus(newState.pushItems)
        return newState;
    }
    if (action.type == CLEAR_ALL_LABEL) {     //清空所有数据
        const newState = Object.assign({}, state);
        newState.labelList =action.data;
        newState.getExcelDataList = action.dataExcel;
        let tmpArr = action.dataExcel;
        const tempArrs = action.data;
        newState.pushItem = getPushList(tmpArr,tempArrs);
        newState.inspectStrPlus = action.saveTextData;
        return newState;
    }
    if (action.type == DEL_PART_ITEM) {     //清空填寫数据
        const newState = Object.assign({}, state);
        const tempArr = newState.labelList;
        tempArr.splice(action.idx,1);
        newState.labelList = [...tempArr];
        let tmpArr = newState.getExcelDataList;
        newState.pushItem = getPushList(tmpArr,tempArr);
        newState.pushItems = getPushLists(tmpArr,tempArr);
        newState.inspectStrPlus = getStringPlus(newState.pushItems)
        return newState;
    }
    return state;
}

function getExcelString(action,newState,tmpArr,tmpArr2) {
  let tmpString = '';
  if(JSON.stringify(action.data||{}) != '{}' && action.data.lisExcelRes.length > 0){
    action.data.lisExcelRes.map((item)=>{
        item.lisExcelItem.map((part)=>{
            if(part.type == 1){
                if(part.max != '' && (part.value-0) > (part.max-0) ) {
                    let rate = (part.value/part.max).toFixed(2);
                    let tmpStr = part.mealName+','+part.itemName+','+part.value+' '+part.unit+'升高'+rate+';'
                    tmpString += tmpStr;
                }else if(part.min != '' && (part.value-0) < (part.min-0) ){
                    let rate = (part.value/part.min).toFixed(2);
                    let tmpStr = part.mealName+','+part.itemName+','+part.value+' '+part.unit+'降低'+rate+';'
                    tmpString += tmpStr;
                }else{
                    let tmpStr = part.mealName+','+part.itemName+','+part.value+' '+part.unit+'异常;'
                    tmpString += tmpStr;
                }
            }else{
                let tmpStr = part.mealName+','+part.itemName+','+part.value+' '+part.unit+'正常;'
                tmpString += tmpStr;
            }
        })
    })
    newState.pushItem = getPushList(tmpArr,tmpArr2);
    newState.pushItems = getPushLists(tmpArr,tmpArr2);
    newState.inspectStrPlus = getStringPlus(newState.pushItems)
  }else{
    newState.inspectStrPlus = '';
    newState.pushItem = []
  }
}
function getString(lists) {
    let tmpString = '';
    lists.map((item)=>{
        if(item.show == true){
            item.details.map((part)=>{
                if(part.value){
                    if(typeof(part.value) == "number"){
                        if(part.maxValue != '' && (part.value-0) > (part.maxValue-0) ) {
                            let rate = (part.value/part.maxValue).toFixed(2);
                            let tmpStr = item.name+','+part.name+','+part.value+' '+part.labelSuffix+'升高'+rate+';'
                            tmpString += tmpStr;
                        }else if(part.minValue != '' && (part.value-0) < (part.minValue-0) ){
                            let rate = (part.value/part.minValue).toFixed(2);
                            let tmpStr = item.name+','+part.name+','+part.value+' '+part.labelSuffix+'降低'+rate+';'
                            tmpString += tmpStr;
                        }else{
                            let tmpStr = item.name+','+part.name+','+part.value+' '+part.labelSuffix+';'
                            tmpString += tmpStr;
                        }
                    }else{
                        tmpString += item.name+','+part.name+','+part.value+' '+part.labelSuffix+';'
                    }
                }
            })
        }
    })
    return tmpString;
}

function getStringPlus(dataList){
  let strPlus = '';
  dataList && dataList.map((item)=>{
    if(item.detailName){
      let strTmp = item.name + ',' + item.detailName + ',' + (item.otherValue ? item.otherValue : (item.value + item.units)) +';'
      strPlus += strTmp;
    }else{
      let strTmp = item.name+';'
      strPlus += strTmp;
    }
  })
  return strPlus;
}
/**
 * 
 * @param {data1}  excel导入的数据 getExcelDataList
 * @param {data2}  填写单数据 labelList
 */
function getPushList(data1,data2){     //推送数据
    let tmpArr1 = [];
    let tmpArr2 = [];
    let totalTmpArr = [];
    const now = dateParser(new Date());
    data1.length>0 && data1.map((items)=>{    //excel导入的数据拼接
        items.lisExcelRes.map((item)=>{
            item.lisExcelItem.map((part)=>{
                let tmpObj = {};
                tmpObj.name = part.mealName || '';//套餐名
                tmpObj.detailName = part.itemName || '';//化验项名
                tmpObj.uniqueName = part.uniqueName || '';//公表名称
                tmpObj.source = '1';//数据来源
                tmpObj.maxValue = part.max || ''; 
                tmpObj.minValue = part.min || '';
                tmpObj.time = part.time ||now;
                tmpObj.units = part.unit || '';//单位
                tmpObj.value = (part.value-0) === (part.value-0) ? part.value:'';//值
                tmpObj.otherValue = (part.value-0) === (part.value-0) ?'':part.value; //阴性阳性
                tmpArr1.push(tmpObj);
            })
        })
    })
    data2.length>0 && data2.map((item)=>{        //填写单数据拼接
      item.details && item.details.map((val)=>{
            if(val.value != undefined && val.value != ''){
                let tmpObj = {};
                tmpObj.name = item.name || '';
                tmpObj.detailName = val.name || '';
                tmpObj.uniqueName = val.uniqueName || '';//公表名称
                tmpObj.source = '0';     //数据来源
                tmpObj.maxValue = val.maxValue ? val.maxValue : '';
                tmpObj.minValue = val.minValue ? val.minValue : '';
                tmpObj.time = val.time || now;
                tmpObj.units = val.labelSuffix || '';
                tmpObj.value = (val.value-0) === (val.value-0) ? val.value:'';
                tmpObj.otherValue = (val.value-0) === (val.value-0) ?'':val.value;
                tmpArr2.push(tmpObj);
            }
        })
    })
    totalTmpArr = tmpArr1.concat(tmpArr2);
    return totalTmpArr;
}
function getPushLists(data1,data2){     //推送数据
    let tmpArr1 = [];
    let tmpArr2 = [];
    let totalTmpArr = [];
    data1.length>0 && data1.map((items)=>{    //excel导入的数据拼接
        items.lisExcelRes.map((item)=>{
            item.lisExcelItem.map((part)=>{
                let tmpObj = {};
                tmpObj.name = part.mealName || '';//套餐名
                tmpObj.detailName = part.itemName || '';//化验项名
                tmpObj.uniqueName = part.uniqueName || '';//公表名称
                tmpObj.source = '1';//数据来源
                tmpObj.maxValue = part.max || ''; 
                tmpObj.minValue = part.min || '';
                tmpObj.units = part.unit || '';//单位
                tmpObj.value = (part.value-0) === (part.value-0) ? part.value:'';//值
                tmpObj.otherValue = (part.value-0) === (part.value-0) ?'':part.value; //阴性阳性
                tmpArr1.push(tmpObj);
            })
        })
    })
    data2.length>0 && data2.map((item)=>{        //填写单数据拼接
      if(item.show){
          item.details.map((val)=>{
            if(val.value != undefined && val.value != ''){
                let tmpObj = {};
                tmpObj.name = item.name || '';
                tmpObj.detailName = val.name || '';
                tmpObj.uniqueName = val.uniqueName || '';//公表名称
                tmpObj.source = '0';     //数据来源
                tmpObj.maxValue = val.maxValue ? val.maxValue : '';
                tmpObj.minValue = val.minValue ? val.minValue : '';
                tmpObj.units = val.labelSuffix || '';
                tmpObj.value = (val.value-0) === (val.value-0) ? val.value:'';
                tmpObj.otherValue = (val.value-0) === (val.value-0) ?'':val.value;
                tmpArr2.push(tmpObj);
            }
        })
      }else{
        let tmpObj = {};
        tmpObj.name = item.name;
        tmpArr2.push(tmpObj);
      }
    })
    totalTmpArr = tmpArr1.concat(tmpArr2);
    return totalTmpArr;
}

