import fetch from 'isomorphic-fetch'
import {saveCommonInfo} from './common'
import barcodeActionType from '../actiontype/barcodeCommon'
import replenishAction from './replenish'
let domain1 = ENV.domain;
let domain = 'http://192.168.10.37:8080/maxbox/web/'
// const changeStatus = (data)=>{
      
//       let {taskId,currentTaskType}=data.taskInfo;
//       let url = `http://192.168.10.128:8080/web/flat/work/lr/status/${taskId}/${currentTaskType}`
//       return (dispatch)=>{
//             fetch(url,{
//                 credentials : 'include',
//                 method : 'PUT',
//                 mode : 'cors',
//                 body : {}
//             }).then((response)=>response.json())
//              .then((json)=>{
//                   if(json.data){
//                         getTaskList(data)
//                   }
//              })
//       }
// }


const changeTaskStatus = (data)=>{
      return {

      }
}

//test 输入条形码获取商品信息

const getBarCodeProductInfo = (data)=>{
      let {barcode='',commonInfo={}}=data;
      let {taskInfo={},storeInfo={}}=commonInfo;
      let storeId=storeInfo.id || 0;
      let currentTaskType = taskInfo.currentTaskType || '';
      let area = taskInfo.area || ''
      let taskId = taskInfo.taskId || 0
      let url=`${domain}flat/barcode/${storeId}/${taskId}/${area}/${currentTaskType}/${barcode}`
      return (dispatch)=>{
            fetch(url,{
                // credentials : 'include',
                method : 'GET',
                mode : 'cors'
            }).then((response)=>response.json())
            .then((json)=>{
                if(json.meta.success&&json.data){
                    dispatch(replenishAction.emptyErrorInfo()); //补货清空错误数据
                    let info = Object.assign({},{taskList:json.data});
                    dispatch(saveCommonInfo(info))
                    dispatch(saveProductInfo(json.data))
                }else{
                    switch (taskInfo.currentTaskType){
                        case 3 :
                            //回收任务
                            break;
                        case 'ADDR' :
                            //补货任务
                            let errorInfo = {
                                errorType : json.data.errorType || "",
                                errorMessage : json.meta.message
                            };
                            dispatch(replenishAction.wrongQrcode(errorInfo));
                            break;
                    }
                }
            })

      }
}


const saveProductInfo = (data)=>{
      return{
          type : barcodeActionType.GETPRODUCTINFO,
          data
      }
  }

  export {getBarCodeProductInfo}