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=>{});
    })
};
/**
 *
 * @param info
 * @param exceptionType
 * @param getTask 判断是否获取工单
 * @returns {Function}
 */
const uploadError = (info,exceptionType,getTask=true)=>{
    let {taskInfo={},userInfo={},storeInfo={},taskList={}} = info;
    let bodyData = Object.assign({},{
        storeId : storeInfo.id || 0,
        id : taskList.workListId || 0,
        userId : userInfo.employeeId || 0,
        taskId : taskInfo.taskId || 0,
        exceptionType : exceptionType,
        taskType : taskInfo.currentTaskType || '',
        area : taskInfo.area || ''
    });
    UTILPATH.myLogger.info("exception_skupass_sku bodyData ",bodyData);
    UTILPATH.myLogger.info("exception_skupass_sku getTask ",getTask);
    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=>{
                if(getTask){
                    dispatch(getTaskList(info));
                    dispatch(saveProductInfo({}))
                }
            }).catch(e=>{
            UTILPATH.myLogger.error(`exception_skupass_sku error ${e}`);
            // dispatch(uploadError(info,exceptionType,getTask))
        });
    })
};

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

const timeoutAlarm = (info)=>{
    let {storeInfo={},taskInfo={},userInfo={}} = info;
    let bodyData = Object.assign({},{
        storeId : storeInfo.id || 0,
        userId : userInfo.employeeId || 0,
        area : taskInfo.area || ""
    });
    UTILPATH.myLogger.info("expired_send_error_msg bodyData ",bodyData);
    return (dispatch=>{
        fetch(domain + 'machine_pad/expired_send_error_msg.action',{
            credentials : 'include',
            method : 'POST',
            mode : 'cors',
            headers:myHeaders,
            body : JSON.stringify(bodyData)

        }).then((response)=>response.json())
            .then(json=>{
            }).catch(e=>{
            UTILPATH.myLogger.error(`expired_send_error_msg error ${e}`);
            // dispatch(timeoutAlarm(info))
        });
    })
}

export {uploadError,addErrorInfo,timeoutAlarm}