import fetch from 'isomorphic-fetch';
import * as actionTypes from '../actiontype/otherQuestion';
const domain = ENV.domain;
//有服务器交互的actions
export function initGetOtherProblem(postData) {
    return (dispatch) => {
        let url = String(domain+'machine_pad/list_other_fault.action');
        console.log(url)
        fetch(url,{
            credentials : 'include',
            method : 'POST',
            mode : 'cors',
            headers: {
                'content-type':'application/json;charset=UTF-8',
            },
            body : JSON.stringify(postData)

        }).then((response) => response.json())
            .then(
                (json)=>{
                    if(json.meta.success){
                        dispatch(initSuccess(json.data))
                    }else{

                    }
                }
            ).catch(e=>{console.error(e)}
        )
      /*  */
    }
}
export function addNewProbem(postData) {
    return (dispatch) => {
        let url = String(domain+'machine_pad/insert_problem.action');
        console.log(url)
        fetch(url,{
            credentials : 'include',
            method : 'POST',
            mode : 'cors',
            headers: {
                'content-type':'application/json;charset=UTF-8',
            },
            body : JSON.stringify(postData)

        }).then((response) => response.json())
            .then(
                (json)=>{
                    if(json.meta.success){
                        dispatch(addNewProblemSuccess(json.data))
                    }else{

                    }
                }
            ).catch(e=>{console.error(e)}
        )
        /*  */
    }

}
export function deleteProblem(postData) {
    return (dispatch) => {
        let url = String(domain+'machine_pad/skip_feed_problem.action');
        fetch(url,{
            credentials : 'include',
            method : 'POST',
            mode : 'cors',
            headers: {
                'content-type':'application/json;charset=UTF-8',
            },
            body : JSON.stringify(postData)

        }).then((response) => response.json())
            .then(
                (json)=>{
                    if(json.meta.success){
                        if(postData.type == "deleteProblemAll"){
                            dispatch(deleteProblemSuccess({"submitOtherProblem":true,"feedIds":json.data.feedIds}))
                        }
                        if(postData.type == "deleteProblemOne"){
                            dispatch(deleteProblemSuccess({"feedIds":json.data.feedIds}))
                        }


                    }else{

                    }
                }
            ).catch(e=>{console.error(e)}
        )
    }

}
export function submitProblemList(postData) {
    return (dispatch) => {
        let url = String(domain+'machine_pad/add_feed_problem.action');
        console.log(url)
        fetch(url,{
            credentials : 'include',
            method : 'POST',
            mode : 'cors',
            headers: {
                'content-type':'application/json;charset=UTF-8',
            },
            body : JSON.stringify(postData)

        }).then((response) => response.json())
            .then(
                (json)=>{
                    if(json.meta.success){
                        dispatch(submitProblemListSuccess({"submitOtherProblem":true}))
                    }else{

                    }
                }
            ).catch(e=>{console.error(e)}
        )
    }
}
//本地负责数据维护的action
export function editProblemItem(data) {
    return (dispatch) =>{
        dispatch((editProlemSuccess(data)))
    }
}

function editProlemSuccess(data) {
    return {
        type: actionTypes.EDIT_PROBLEM_SUCCESS,
        data
    }
}
function initSuccess(data) {
    return {
        type: actionTypes.INIT_GETNEWPROBLEM_SUCCESS,
        data
    }
}
function addNewProblemSuccess(data) {
    return{
        type:actionTypes.ADD_NEWPROBLEM_SUCCESS,
        data
    }
}
function submitProblemListSuccess(data) {
    return {
        type:actionTypes.SUBMIT_PROBLEM_SUCCESS,
        data
    }
}

function deleteProblemSuccess(data) {
    return{
        type:actionTypes.DELETE_PROBLEM_SUCCESS,
        data
    }
}
function addImgSuccess(data) {
    return{
        type:actionTypes.ADD_IMG_SUCCESS,
        data
    }
}
function deleteImgSuccess(data) {
    return{
        type:actionTypes.DELETE_IMG_SUCCESS,
        data
    }
}
function overStepSuccess(data) {
    return{
        type:actionTypes.OVER_STEP_SUCCESS,
        data
    }
}