import fetch from 'isomorphic-fetch'
import actionType from '../actiontype/error'
import {getTaskList} from './common';
import {saveProductInfo} from './barcodeCommon'
let domain = ENV.domain;

var myHeaders = new Headers({
    'Access-Control-Allow-Origin': '*',
    // 'Content-Type': 'text/plain;charset=UTF-8',
    // 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Content-Type': 'application/json; charset=UTF-8',
});
const uploadErrorOld = (info)=>{
    let bodyData = {
        warehouseId : info.storeId,
        exceptionType : info.exceptionType,
        taskId : info.taskId
    };
    return (dispatch =>{
        fetch(domain + '/machine_pad/flat_warn_record.action',{
            credentials : 'include',
            method : 'POST',
            mode : 'cors',
            headers:myHeaders,
            body : JSON.stringify(bodyData)

        }).then((response)=>response.json())
            .then(json=>{
            }).catch(e=>{console.error(e)});
    })
};

const uploadError = (info,exceptionType)=>{
    let {taskInfo={},userInfo={},storeInfo={},taskList={}} = info;
    let bodyData = {
        storeId : storeInfo.id || 0,
        id : taskList.workListId || 0,
        userId : userInfo.id || 0,
        taskId : taskInfo.taskId || 0,
        exceptionType : exceptionType,
        taskType : taskInfo.currentTaskType || '',
        area : taskInfo.area || ''
    };
    return (dispatch =>{
        fetch(domain + 'machine_pad/exception_skupass_sku.action',{
            credentials : 'include',
            method : 'POST',
            mode : 'cors',
            headers:myHeaders,
            body : JSON.stringify(bodyData)

        }).then((response)=>response.json())
            .then(json=>{
                dispatch(getTaskList(info));
                dispatch(saveProductInfo({}))
            }).catch(e=>{console.error(e)});
    })
}

const addErrorInfo = (errMsg)=>{
    return{
        type : actionType.ADDERRORINFO,
        errMsg
    }
}

export {uploadError,addErrorInfo}
