import fetch from 'isomorphic-fetch';
import actionTypes from '../actiontype/trouble'
import userActionTypes from "../actiontype/user";
const domain = ENV.domain;

let json = {
    "meta": {
        "code": "200",
        "message": "成功",
        "success": true
    },
    "data": {
        "details": [
            {
                "id": 1634,
                "errorCode": "ER00001",
                "type": "KN",
                "description": "2:左侧动作故障,4:机械手移动到放货位置出错()",
                "files": [
                    {
                        "id": 16,
                        "filePath": "/tmp/2016-08-16/1471329685902.jpg"
                    },
                    {
                        "id": 17,
                        "filePath": "/sku/0/0/427/1471329704482.jpg"
                    },
                    {
                        "id": 18,
                        "filePath": "/sku/0/0/426/1471330380712.jpg"
                    }
                ],
            },
            {
                "id": 1,
                "errorCode": null,
                "type": "OP",
                "files": [
                    {
                        "id": 16,
                        "filePath": "/tmp/2016-08-16/1471329685902.jpg"
                    },
                    {
                        "id": 17,
                        "filePath": "/sku/0/0/427/1471329704482.jpg"
                    },
                    {
                        "id": 18,
                        "filePath": "/sku/0/0/426/1471330380712.jpg"
                    }
                ],
                "parentProblemName": "机械故障",
                "subProblemName": "其他问题111"
            }
        ],
        "employee": {
            "id": 5,
            "employeeName": "机器库管员",
            "headImageUrl": "http://www.mjitech.com/static/coupon/gounian/banner_hongbaotupian-3-14.jpg"
        },
        "taskIds": [
            155
        ]
    }
}

const saveTroubleList = (data)=>{
    return{
        type:actionTypes.SAVETROUBLELIST,
        data
    }
};


const saveUserInfo = (data)=>{
    return{
        type : userActionTypes.SAVEUSERINFO,
        data
    }
}


const getTroubleList = ()=>{
    return(dispatch)=>{
        // fetch(domain + '/web/pad_warehouse/getMachineData.action',{
        //     credentials : 'include',
        //     method : 'POST',
        //     mode : 'cors',
        //     body : {}
        //
        // }).then((response)=>response.json())
        //     .then(json=>{
        //         console.log(json);
        //     }).catch(e=>{console.error(e)})
        if(json.meta.success && json.data){
            if(json.data.employee){
                //存储用户信息
                dispatch(saveUserInfo(json.data.employee));
            }
            dispatch(saveTroubleList(json.data))
        }

    }
}

export {getTroubleList}