import fetch from 'isomorphic-fetch'
import {getTaskList} from "./common";
import breakageActionType from '../actiontype/breakageContainer'
let 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  submitBreakageProduct = (data)=>{
    let {productDetail,commonInfo}=data;
    let userId=commonInfo.userInfo.employeeId;
    let {taskId}=commonInfo.taskInfo;
    let machineId=commonInfo.storeInfo.id;
    let {skuPosition,skuId,skuStatus,area}=productDetail;
    let bodyData={skuPosition,skuId,skuStatus,machineId,taskId,area}
    let url=domain+`machine_pad/lr/${userId}`;
    UTILPATH.myLogger.info("submitBreakageProduct url ",url);
    UTILPATH.myLogger.info("submitBreakageProduct request ",bodyData);
    return (dispatch)=>{
        fetch(url,{
            // credentials : 'include',
            method : 'POST',
            mode : 'cors',
            headers:{
                'Access-Control-Allow-Origin': '*',
                'Content-Type': 'application/json; charset=UTF-8'
            },
            body:JSON.stringify(bodyData)
        }).then((response)=>response.json())
          .then((json)=>{
              UTILPATH.myLogger.info("submitBreakageProduct response ",json);
              dispatch(getSubmitStatus(json.meta))
          }).catch(e=>{
            UTILPATH.myLogger.error(`submitBreakageProduct error ${e}`);
            // dispatch(submitBreakageProduct(data))
        })
  }
}


// 没有散落货品
const hasNoSku = (commonInfo)=>{
    let {storeInfo={},userInfo={},taskInfo={}} = commonInfo;
    let url = `${domain}machine_pad/work/lr/status/${taskInfo.taskId}/${taskInfo.area}/${taskInfo.currentTaskType}`;
    UTILPATH.myLogger.info("hasNoSku url ",url);
    return (dispatch)=>{
        fetch(url,{
            // credentials : 'include',
            method : 'PUT',
            mode : 'cors',
            headers: myHeaders
        }).then(response=>response.json())
            .then((json)=>{
                dispatch(getTaskList(commonInfo))
            }).catch(e=>{
            UTILPATH.myLogger.error(`hasNoSku 没有散落货品 error ${e}`);
        })
    }
}

const getSubmitStatus = (data)=>{
     return {
         type:breakageActionType.SUBMITSTATUS,
         data
     }
}
export {submitBreakageProduct,hasNoSku}