import fetch from 'isomorphic-fetch'
import {logout, getTaskList, saveCommonInfo} from "./common";
let domain = ENV.domain;
// let domain1='http://192.168.10.184:8080/maxbox/web/'
const willCloseDoor=(data,flag=false)=>{
    let url=domain+`machine_pad/finish_task.action`
    let {userInfo,taskInfo,storeInfo}=data;
    let userId=userInfo.employeeId;
    let machineId=storeInfo.id;
    let {taskId,area,currentTaskType}=taskInfo;
    let bodyData={area,storeId:machineId,taskId,userId,taskType:currentTaskType};
    UTILPATH.myLogger.info("finish_task url",url);
    UTILPATH.myLogger.info("finish_task 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)=>{
              if(flag){
                  //回收箱完成
                  dispatch(getTaskList(data))
              }else{
                  //没有问题，完成任务
                  // dispatch(saveCommonInfo(Object.assign({},{taskInfo:{currentTaskType:'FINISH'}})))
              }
              // dispatch(closeDoorSuccess(data));
          }).catch(e=>{
            UTILPATH.myLogger.error("finish_task error ",e);
        })
  }
}


const closeDoorSuccess=(data)=>{
    let {userInfo,taskInfo,storeInfo}=data;
    let userId=userInfo.employeeId;
    let machineId=storeInfo.id;
    let {taskId,area}=taskInfo;
    let bodyData={storeId:machineId,taskId,area,userId}
    let url=domain+'machine_pad/update_door_status.action';
    UTILPATH.myLogger.info("update_door_status url",url);
    UTILPATH.myLogger.info("update_door_status 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)=>{
              dispatch(logout())
          }).catch(e=>{
            UTILPATH.myLogger.error("update_door_status error ",e);
        })
  }
}
export {willCloseDoor,closeDoorSuccess}