import fetch from 'isomorphic-fetch';
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',
});

let lock = false;

const finishBoxRecovery = (info)=>{
    //已放入回收箱
    return async dispatch => {
        if(!lock){
            lock = true;
            let {userInfo={},taskInfo={},storeInfo={},taskList={}} = info;
            let bodyData = Object.assign({},{
                storeId : storeInfo.id || 0,
                taskId : taskInfo.taskId || 0,
                userId : userInfo.employeeId || 0,
                id : taskList.workListId || 0,
                taskType : taskInfo.currentTaskType || "",
                area : taskInfo.area || ""
            });
            UTILPATH.myLogger.info("update_machine_recovery_sku request ",bodyData);
            let url=`${domain}machine_pad/update_machine_recovery_sku.action`;

            let success = (res)=>{
                dispatch(saveProductInfo({}));
                dispatch(getTaskList(info,()=>{
                    lock = false;
                }));
            };

            let fail = (err)=>{
                lock = false;
                UTILPATH.myLogger.error(`update_machine_recovery_sku error ${err}`);
            };

            try {
                const result = await fetch(url,{
                    credentials : 'include',
                    method : 'POST',
                    mode : 'cors',
                    headers: myHeaders,
                    body:JSON.stringify(bodyData)
                }).then(response=>response.json());
                return success(result)
            } catch (err) {
                return fail(err)
            }
        }
    }
};

export {finishBoxRecovery}
