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

let 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 wrongQrcode = (errorInfo)=>{
    return {
        type : actionType.SETREPLENISHERRORINFO,
        errorInfo
    }
}
const emptyErrorInfo = ()=>{
    return {
        type : actionType.EMPTYREPLENISHERRORINFO,
    }
}

const finishReplenish = (info,source)=>{
    //source 1 : 商品页 ， 2 : 扫码页
    //已放入回收箱
    let {userInfo,taskInfo,storeInfo,taskList} = info;
    let bodyData = Object.assign({},{
        storeId : storeInfo.id,
        taskId : taskInfo.taskId,
        userId : userInfo.employeeId,
        taskType : taskInfo.currentTaskType,
        area : taskInfo.area,
        id : source === 1 ? taskList.workListId : 0,
    });
    let url=`${domain}machine_pad/trigger_ck_task_sku.action`
    return (dispatch)=>{
        fetch(url,{
            credentials : 'include',
            method : 'POST',
            mode : 'cors',
            headers: myHeaders,
            body:JSON.stringify(bodyData)
        }).then(response=>response.json())
            .then((json)=>{
                dispatch(getTaskList(info));
            })
    }
}

export default {wrongQrcode,emptyErrorInfo,finishReplenish}
