import fetch from 'isomorphic-fetch';
import actionTypes from '../actiontype/trouble'
import userActionTypes from "../actiontype/user";
import {saveHeaderInfo} from './initHeader'
import {saveCommonInfo} from "./common";

const domain = ENV.domain;

let meta = {
    "meta": {"code": "200", "message": "成功", "success": true},
    "data": {
        "details": [{
            "id": 660,
            "storeId": 7,
            "errorCode": "ER00021",
            "type": "KN",
            "description": "1:回零故障,2:直流电机回位错误或步进电机初始化错误(根据前一条故障代码判断)",
            "taskId": 0,
            "isExistProblem": 0,
            "imagesFileId": "3879,3878,3846,3843",
            "superType": 1,
            "subType": 2,
            "parentProblemType": 0,
            "subProblemType": 0,
            "explanation": null,
            "handler": 0,
            "files": [{
                "id": 3843,
                "filePath": "http://preprod.mjitech.com/static/tmp/2018-08-07/1533637758512.jpg"
            }, {
                "id": 3846,
                "filePath": "http://preprod.mjitech.com/static/tmp/2018-08-07/1533638079031.jpg"
            }, {
                "id": 3878,
                "filePath": "http://preprod.mjitech.com/static/tmp/2018-08-08/1533721929622.jpg"
            }, {"id": 3879, "filePath": "http://preprod.mjitech.com/static/tmp/2018-08-08/1533722112773.jpg"}],
            "parentProblemName": null,
            "subProblemName": null,
            "sellOrderNumber": "0"
        },{
            "id": 660,
            "storeId": 7,
            "errorCode": "ER00022",
            "type": "KN",
            "description": "1:回零故障,2:直流电机回位错误或步进电机初始化错误(根据前一条故障代码判断)",
            "taskId": 0,
            "isExistProblem": 0,
            "imagesFileId": "3879,3878,3846,3843",
            "superType": 1,
            "subType": 2,
            "parentProblemType": 0,
            "subProblemType": 0,
            "explanation": null,
            "handler": 0,
            "files": [{
                "id": 3843,
                "filePath": "http://preprod.mjitech.com/static/tmp/2018-08-07/1533637758512.jpg"
            }, {
                "id": 3846,
                "filePath": "http://preprod.mjitech.com/static/tmp/2018-08-07/1533638079031.jpg"
            }, {
                "id": 3878,
                "filePath": "http://preprod.mjitech.com/static/tmp/2018-08-08/1533721929622.jpg"
            }, {"id": 3879, "filePath": "http://preprod.mjitech.com/static/tmp/2018-08-08/1533722112773.jpg"}],
            "parentProblemName": null,
            "subProblemName": null,
            "sellOrderNumber": "0"
        },{
            "id": 660,
            "storeId": 7,
            "errorCode": "ER00023",
            "type": "KN",
            "description": "1:回零故障,2:直流电机回位错误或步进电机初始化错误(根据前一条故障代码判断)",
            "taskId": 0,
            "isExistProblem": 0,
            "imagesFileId": "3879,3878,3846,3843",
            "superType": 1,
            "subType": 2,
            "parentProblemType": 0,
            "subProblemType": 0,
            "explanation": null,
            "handler": 0,
            "files": [{
                "id": 3843,
                "filePath": "http://preprod.mjitech.com/static/tmp/2018-08-07/1533637758512.jpg"
            }, {
                "id": 3846,
                "filePath": "http://preprod.mjitech.com/static/tmp/2018-08-07/1533638079031.jpg"
            }, {
                "id": 3878,
                "filePath": "http://preprod.mjitech.com/static/tmp/2018-08-08/1533721929622.jpg"
            }, {"id": 3879, "filePath": "http://preprod.mjitech.com/static/tmp/2018-08-08/1533722112773.jpg"}],
            "parentProblemName": null,
            "subProblemName": null,
            "sellOrderNumber": "0"
        },
            {
                "id": 660,
                "storeId": 7,
                "errorCode": "ER00024",
                "type": "KN",
                "description": "1:回零故障,2:直流电机回位错误或步进电机初始化错误(根据前一条故障代码判断)",
                "taskId": 0,
                "isExistProblem": 0,
                "imagesFileId": "3879,3878,3846,3843",
                "superType": 1,
                "subType": 2,
                "parentProblemType": 0,
                "subProblemType": 0,
                "explanation": null,
                "handler": 0,
                "files": [{
                    "id": 3843,
                    "filePath": "http://preprod.mjitech.com/static/tmp/2018-08-07/1533637758512.jpg"
                }, {
                    "id": 3846,
                    "filePath": "http://preprod.mjitech.com/static/tmp/2018-08-07/1533638079031.jpg"
                }, {
                    "id": 3878,
                    "filePath": "http://preprod.mjitech.com/static/tmp/2018-08-08/1533721929622.jpg"
                }, {"id": 3879, "filePath": "http://preprod.mjitech.com/static/tmp/2018-08-08/1533722112773.jpg"}],
                "parentProblemName": null,
                "subProblemName": null,
                "sellOrderNumber": "0"
            }

        ], "taskId": 256, "sellOrderId": "0"
    }
}

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


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

var 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 getTroubleList = (info) => {
    let {storeInfo={},userInfo={},taskInfo={}} = info;
    return (dispatch) => {
        let bodyData = Object.assign({}, {
            employeeId: userInfo.employeeId,
            storeId: storeInfo.id
        });
        fetch(domain + 'machine_pad/list_fault.action', {
            credentials: 'include',
            method: 'POST',
            mode: 'cors',
            headers: myHeaders,
            body: JSON.stringify(bodyData)
        }).then((response) => response.json())
            .then(json => {
                if (json.meta.success && json.data) {
                    dispatch(saveCommonInfo({taskInfo : {taskId: json.data.taskId, sellOrderId: json.data.sellOrderId}}))
                    dispatch(saveTroubleList(json.data))
                }
            }).catch(e => {
            console.error(e)
        })
    }
};

const updateTroubleList = (info, headerInfo) => {
    let {taskInfo={},userInfo={},storeInfo={}} = headerInfo;
    let bodyData = Object.assign({}, {details: info}, {taskId:taskInfo.taskId,employeeId:storeInfo.employeeId,storeId:storeInfo.id});
    bodyData = Object.assign({}, {details: info}, {taskId:taskInfo.taskId,employeeId:"",storeId:""});
    return (dispatch) => {
        fetch(domain + 'machine_pad/save_all.action', {
            credentials: 'include',
            method: 'POST',
            mode: 'cors',
            headers: myHeaders,
            body: JSON.stringify(bodyData)
        }).then((response) => response.json())
            .then(json => {
            }).catch(e => {
            console.error(e)
        })
    }
};

export {getTroubleList, updateTroubleList}

