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) {
    console.log(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) {
    console.log('==================data===========');
    console.log(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) {
    console.log("=================reducers==============")
    console.log(data)
    let newInfo = Object.assign({},state,data);
    return newInfo;
}




function editProblem(state,data) {
    console.log(data);
    let problemId = data.troubelId;
    console.log("=====================problemId");
    console.log(problemId)
    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) {
    console.log(data);
}
function setRemark(state,data) {
    console.log(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;
    }
}