import fetch from 'isomorphic-fetch';
import * as actionTypes from '../actiontype/ScanBarCode';
import actionTypes_error  from '../actiontype/error'
let domain = ENV.domain;
export function scanBarCodeGetProductInfo(postData) {
    return (dispatch) => {
        UTILPATH.myLogger.info("scanning_sku_barcode_for_add_loss_task bodyData ",postData);
        let url = String(domain+'machine_pad/scanning_sku_barcode_for_add_loss_task');
        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)=>{
                    UTILPATH.myLogger.info("scanning_sku_barcode_for_add_loss_task response ",json);
                    //处理数据如果
                    let success = json.meta.success;
                    if(success){
                        dispatch(initSuccess(json.data))
                    }else{
                        dispatch(throwError(json.meta.message))
                    }
                }
            ).catch(e=>{
            UTILPATH.myLogger.error(`scanning_sku_barcode_for_add_loss_task error ${e}`);
            // dispatch(scanBarCodeGetProductInfo(postData))
        })
    }
}
export function submitProductInfo(postData) {
    return (dispatch)=>{
         let url = String(domain+'machine_pad/submit_add_loss_task');
        UTILPATH.myLogger.info("submit_add_loss_task bodyData ",postData);
        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)=>{
                    UTILPATH.myLogger.info("submit_add_loss_task response ",json);
                    if(json.data){
                        let submit = {
                            "submit":true
                        };
                        dispatch(submitProductSuccess(submit))
                    }
                }
            ).catch(e=>{
            UTILPATH.myLogger.error(`submit_add_loss_task error ${e}`);
            // dispatch(submitProductInfo(postData));
            }
        )

      /*  */
    }
}
export function saveEditProductInfo(data) {
    return (dispatach) =>{
        dispatach(saveEditProductInfoSuccess(data))
    }
}


function initSuccess(data) {
    return {
        type: actionTypes.INIT_SCANBARCODEGETPRODUCTINFO_SUCCESS,
        data
    }
}
export function submitProductSuccess(data) {
    return {
        type:actionTypes.SUBMIT_PRODUCT_SUCCESS,
        data
    }
}
export function clearProductSuccess() {
    return{
        type:actionTypes.ClEAR_PRODUCT_SUCCESS,
        data:{}
    }
}
function saveEditProductInfoSuccess(data){
    return {
        type:actionTypes.SAVE_PRODUCTEDITINFO_SUCCESS,
        data
    }
}
function throwError(data) {
    return{
        type:actionTypes_error.ADDERRORINFO,
        errMsg:data
    }
}