import * as actionTypes from '../actiontype/otherQuestion';
/*// 维护本地的一套数据 问题列表
{
    id:{
        fontEndId:2222,
            id:dfd,
            files:[],
            problemType:fdf,
            detailProblemType：dfdf,
            remark:dfdfd,
            imagesFileId:dfdf

    }
}*/
let allFeeds = {};

function initSuccess(state,data) {
    let initFeeds = Object.assign([],data.feeds);
    let problemList = {};
    initFeeds.map((item,index)=>{
        problemList[String(item.id)] = {}
        problemList[String(item.id)].id = item.id;
        problemList[String(item.id)].foreEndId = item.id;
        problemList[String(item.id)].files = item.files;
        problemList[String(item.id)].problemType = item.parentProblemName;
        problemList[String(item.id)].detailProblemType = item.subProblemName;
        problemList[String(item.id)].remark = "";
    });
    allFeeds = problemList;
    data.data_allProblemList = allFeeds;
    let newInfo = Object.assign({},state,data);
    return newInfo;
}
function addNewProblem(state,data) {
    let feed = data.feed;
    let temObj = {};
    temObj.id = feed.id;
    temObj.files = [];
    allFeeds[String(temObj.id)] = {};
    allFeeds[String(temObj.id)].id = String(temObj.id);
    allFeeds[String(temObj.id)].foreEndId = temObj.id;
    allFeeds[String(temObj.id)].files = [];
    allFeeds[String(temObj.id)].problemType = '';
    allFeeds[String(temObj.id)].detailProblemType = '';
    allFeeds[String(temObj.id)].remark = "";
    state.data_allProblemList = allFeeds;
    let newInfo = Object.assign({},state,data);
    return newInfo;
}
function deleteProblemItem(state,data) {
    let feedId = data.feedIds;
    if(feedId){
        delete allFeeds[feedId]
    }else{
        allFeeds = {}
    }
    state.data_allProblemList = allFeeds;
    let newInfo = Object.assign({},state,data);
    return newInfo;
}
function submitProblemList(state,data) {
    let newInfo = Object.assign({},state,data);
    return newInfo;
}




function editProblem(state,data) {
    let problemId = data.troubelId;
    let type = data.type;
    let currentProblemObj =  allFeeds[problemId];
    if(type == "remark"){
        currentProblemObj.remark = data. remarkText;
    }
    if(type == "problemType"){
        currentProblemObj.problemType = data.problemId;
        currentProblemObj.message = data.message;
    }
    if(type == "detailProblemType"){
        currentProblemObj.detailProblemType = data.problemId;
        currentProblemObj.smallMessage = data.smallMessage;
    }
    if(type == "imgFiles"){
        currentProblemObj.files = data.files;
    }
    state.data_allProblemList = allFeeds;

    return state;
}
function setProblem(state,data) {
}
function setRemark(state,data) {
}
export default function(state={}, action) {
    switch (action.type) {
        case actionTypes.INIT_GETNEWPROBLEM_SUCCESS:
            return initSuccess(state,action.data);
        case actionTypes.ADD_NEWPROBLEM_SUCCESS:
            return addNewProblem(state,action.data);
        case actionTypes.EDIT_PROBLEM_SUCCESS:
            return editProblem(state,action.data);
        case actionTypes.DELETE_PROBLEM_SUCCESS:
            return deleteProblemItem(state,action.data);
        case actionTypes.SUBMIT_PROBLEM_SUCCESS:
            return  submitProblemList(state,action.data)
        default:
            return state;
    }
}