import {
    GET_ASSIST_SEARCH_LIST,
    GET_ASSIST_LABEL,
    DEL_ASSIST_LABEL,
    CHANGE_ASSIST_VAL,
    CHANGE_DATE,
    CLEAR_ASSIST_DATA,
    ADD_ASSIST_LABEL
} from '../types/assistCheck';
import store from '@store';
const initSearchList = {
    list: [], //点击的结果
    assistLabel: [], //搜索的结果
    dataString:'',      //结果拼接
    assistVal:''
}
function getCurrentDate() {
    let myDate = new Date();
    let year = myDate.getFullYear();       //获取完整的年份(4位,1970-????)
    let mon = myDate.getMonth()-0+1;       //获取当前月份(0-11,0代表1月)
    let day = myDate.getDate();            //获取当前日(1-31)
    let date = year+'-'+(mon<10?'0'+mon:mon)+'-'+(day<10?'0'+day:day);
    return date;
}
export default (state = initSearchList, action) => { 
    if (action.type == ADD_ASSIST_LABEL) {
        const newState = Object.assign({}, state);
        let tempArr = newState.assistLabel,tmpString='';
        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
            newState.assistLabel = [...tempArr]
          }
          let tmpVal = tempArr[i].value?tempArr[i].value.trim():tempArr[i].value;
          tmpString += (tempArr[i].name+(tmpVal?(':'+tmpVal)+', ':': ')+(tempArr[i].time?'报告日期:'+tempArr[i].time:'')+';')
        }
        newState.assistLabel=[...tempArr]
        newState.dataString = tmpString
        return newState;
    }
    if (action.type == GET_ASSIST_SEARCH_LIST) {    //右侧推送添加到左侧
        const newState = Object.assign({}, state);
        newState.list = action.list
        newState.assistVal = action.val
        return newState;
    }
    if (action.type == GET_ASSIST_LABEL) {             //默认
        const newState = Object.assign({}, state);
        const tempArrs = newState.assistLabel;
        let tempArr = [];
        let tmpString = '';
        if(action.sign == 'common'){
          let tmpAssistList = store.getState().homePage.assistList
          tempArr = tmpAssistList;
        }else{
          tempArr = newState.list
        }
        for (let i = 0; i < tempArr.length; i++) {
            if (tempArr[i].questionId == action.id && i == action.idx) {
                tempArr[i].time = getCurrentDate();
                tempArrs.push(tempArr[i]);
                newState.assistLabel = [...tempArrs];
            }
        }
        for (let j = 0; j < tempArrs.length; j++) {
            let tmpVal = tempArrs[j].value?tempArrs[j].value.trim():tempArrs[j].value;
            tmpString += (tempArrs[j].name+(tmpVal?(':'+tmpVal)+', ':': ')+(tempArrs[j].time?'报告日期:'+tempArrs[j].time:'')+';')
        }
        newState.dataString = tmpString
        return newState;
    }
    if (action.type == DEL_ASSIST_LABEL) {        //删除
        const newState = Object.assign({}, state);
        let tempArr = newState.assistLabel,tempArrs=[];
        let tmpString = '';
        // tempArr.splice(action.idx, 1)
        // let tempArrs = tempArr.slice()
        for(let k = 0;k < tempArr.length;k++){
          if(k != action.idx){
            tempArrs.push(tempArr[k])
          }
        }
        if(tempArr == []){
          tmpString == ''
          return
        }
        for (let i = 0; i < tempArr.length; i++) {
          let tmpVal = tempArr[i].value?tempArr[i].value.trim():tempArr[i].value;
          tmpString += (tempArr[i].name+(tmpVal?(':'+tmpVal)+', ':': ')+(tempArr[i].time?'报告日期:'+tempArr[i].time:'')+';')
        }
        newState.assistLabel = [...tempArrs]
        // console.log(tempArrs)
        newState.dataString = tmpString
        return newState;
    }
    if (action.type == CHANGE_ASSIST_VAL) {           //改变输入值
        const newState = Object.assign({}, state);
        const tempArr = newState.assistLabel;
        let tmpString = '';
        for (let i = 0; i < tempArr.length; i++) {
            if (i == action.idx) {
                tempArr[i].value = action.val
                newState.assistLabel = [...tempArr]
            }
            let tmpVal = tempArr[i].value?tempArr[i].value.trim():tempArr[i].value;
            tmpString += (tempArr[i].name+(tmpVal?(':'+tmpVal)+', ':': ')+(tempArr[i].time?'报告日期:'+tempArr[i].time:'')+';')
        }
        newState.dataString = tmpString
        return newState;
    }
    if (action.type == CHANGE_DATE) {             //新增
        const newState = Object.assign({}, state);
        const tempArr = newState.assistLabel;
        let tmpString = '';
        for (let i = 0; i < tempArr.length; i++) {
            if (i == action.idx) {
                tempArr[i].time = action.date
                newState.assistLabel = [...tempArr]
            }
            let tmpVal = tempArr[i].value?tempArr[i].value.trim():tempArr[i].value;
            tmpString += (tempArr[i].name+(tmpVal?(':'+tmpVal)+', ':': ')+(tempArr[i].time?'报告日期:'+tempArr[i].time:'')+';')
        }
        newState.dataString = tmpString
        return newState;
    }
    if (action.type == CLEAR_ASSIST_DATA) {
        const newState = Object.assign({}, state);
        newState.assistLabel = [...action.data];
        newState.dataString = action.saveText;
        return newState;
    }
    return state;
}