import {
    GET_ASSIST_SEARCH_LIST,
    GET_ASSIST_LABEL,
    DEL_ASSIST_LABEL,
    CHANGE_ASSIST_VAL,
    CHANGE_DATE,
    CLEAR_ASSIST_DATA
} from '../types/assistCheck';

const initSearchList = {
    list: [], //点击的结果
    assistLabel: [], //搜索的结果
    dataString:'',      //结果拼接
}
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 == GET_ASSIST_SEARCH_LIST) {
        const newState = Object.assign({}, state);
        newState.list = action.list
        return newState;
    }
    if (action.type == GET_ASSIST_LABEL) {
        const newState = Object.assign({}, state);
        const tempArr = newState.list;
        const tempArrs = newState.assistLabel;
        let tmpString = '';
        for (let i = 0; i < tempArr.length; i++) {
            if (tempArr[i].questionId == action.id) {
                tempArr[i].time = getCurrentDate();
                tempArrs.push(tempArr[i]);
                newState.assistLabel = [...tempArrs];
            }
            for (let j = 0; j < tempArrs.length; j++) {
                tmpString += (tempArrs[j].name+(tempArrs[j].value?(','+tempArrs[j].value):'')+(tempArrs[j].time?',报告日期,'+tempArrs[j].time:'')+';')
            }
        }
        newState.dataString = tmpString
        return newState;
    }
    if (action.type == DEL_ASSIST_LABEL) {
        const newState = Object.assign({}, state);
        const tempArr = newState.assistLabel;
        let tmpString = '';
        tempArr.splice(action.idx, 1)
        if(tempArr == []){
            tmpString == ''
            return
        }
        for (let i = 0; i < tempArr.length; i++) {
            tmpString += (tempArr[i].name+(tempArr[i].value?(','+tempArr[i].value):'')+(tempArr[i].time?',报告日期,'+tempArr[i].time:'')+';')
        }
        newState.assistLabel = [...tempArr]
        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]
            }
            tmpString += (tempArr[i].name+(tempArr[i].value?(','+tempArr[i].value):'')+(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]
            }
            tmpString += (tempArr[i].name+(tempArr[i].value?(','+tempArr[i].value):'')+(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;
}