import fetch from 'isomorphic-fetch'
import {saveCommonInfo} from './common'
import barcodeActionType from '../actiontype/barcodeCommon'
import replenishAction from './replenish'
let domain = ENV.domain;
//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}machine_pad/barcode/${storeId}/${taskId}/${area}/${currentTaskType}/${barcode}`;
      UTILPATH.myLogger.info("getBarCodeProductInfo url ",url);
      return (dispatch)=>{
            fetch(url,{
                credentials : 'include',
                method : 'GET',
                mode : 'cors'
            }).then((response)=>response.json())
            .then((json)=>{
                UTILPATH.myLogger.info("getBarCodeProductInfo response ",json);
                if(!json.meta.success&&!json.data){
                   dispatch(saveProductInfo(json.meta))
                    switch (taskInfo.currentTaskType){
                        case 3 :
                            //回收任务
                            break;
                        case 'ADDR' :
                            //补货任务
                            let errorType = "WrongBarcode";
                            let errorMessage = json.meta.message;
                            let errorInfo = {
                                errorType : errorType,
                                errorMessage : json.meta.message
                            };
                            dispatch(replenishAction.wrongQrcode(errorInfo));
                            break;
                    }
                }else{
                    if(json.meta.success&&json.data){
                        dispatch(replenishAction.emptyErrorInfo()); //补货清空错误数据
                        let info = Object.assign({},{taskList:json.data});
                        dispatch(saveCommonInfo(info))
                        dispatch(saveProductInfo(json.data))
                    }else{
                        dispatch(saveProductInfo({}));
                        switch (taskInfo.currentTaskType){
                            case 3 :
                                //回收任务
                                break;
                            case 'ADDR' :
                                //补货任务
                                let errorType = json.data && json.data.errorType ? json.data.errorType : "WrongBarcode";
                                let errorMessage = json.meta.message;
                                switch (errorType){
                                    case "NT":
                                        errorMessage = "不在本次补货范围内";
                                        break;
                                    case "NA" :
                                        errorMessage = "不在本区补货范围";
                                        break;
                                    case "NS":
                                        errorMessage = "需要补完上一次的商品";
                                        break;
                                    case "TW":
                                        errorMessage = "拿错货品";
                                        break;
                                }
                                let errorInfo = {
                                    errorType ,
                                    errorMessage
                                };
                                dispatch(replenishAction.wrongQrcode(errorInfo));
                                break;
                        }
                    }
                }
              
            }).catch(e=>{
                UTILPATH.myLogger.error(`getBarCodeProductInfo error ${e}`);
                dispatch(getBarCodeProductInfo(data));
            })

      }
}


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

export {getBarCodeProductInfo,saveProductInfo}