import fetch from 'isomorphic-fetch';
import actionTypes from '../actiontype/refreshImg'
import {addErrorInfo} from './uploadError'
import {getIp} from "./getIp";
const domain = ENV.domain;

let json = {
    "meta": {
        "code": "200",
        "message": "成功",
        "success": true
    },
    "data": {
        "files":[
            {
                "id": 17,
                "filePath": "http://static.mjitech.com/static/sku/0/0/427/1471329704482.jpg"
            },
            {
                "id": 18,
                "filePath": "http://static.mjitech.com/static/sku/0/0/427/1471329704482.jpg"
            },
            {
                "id": 19,
                "filePath": "http://static.mjitech.com/static/sku/0/0/427/1471329704482.jpg"
            },
            {
                "id": 21,
                "filePath": "http://static.mjitech.com/static/sku/0/0/427/1471329704482.jpg"
            },
            {
                "id": 22,
                "filePath": "http://static.mjitech.com/static/sku/0/0/427/1471329704482.jpg"
            },
            {
                "id": 23,
                "filePath": "http://static.mjitech.com/static/sku/0/0/427/1471329704482.jpg"
            },
            {
                "id": 24,
                "filePath": "http://static.mjitech.com/static/sku/0/0/427/1471329704482.jpg"
            },
            {
                "id": 25,
                "filePath": "http://static.mjitech.com/static/sku/0/0/427/1471329704482.jpg"
            },
            {
                "id": 26,
                "filePath": "http://static.mjitech.com/static/sku/0/0/427/1471329704482.jpg"
            }
        ]
    }
}

const refreshImg = (data)=>{
    return{
        type:actionTypes.REFRESHIMG,
        data
    }
};

const checkStatus = (response)=>{
    console.log("===========")
    console.log(response);
    if (response.status >= 200 && response.status < 300) {
        return response;
    }
    const error = new Error(response.statusText);
    error.response = response;
    throw error;
};

const parseJson = (response)=>{
    return response.json();
};


const getRefreshImg = (errorId,type)=>{
    return(dispatch)=>{
        let bodyData = Object.assign({},{errorId,type})
        fetch(domain + 'flush_image.action',{
            credentials : 'include',
            method : 'POST',
            mode : 'cors',
            headers: {
                'content-type':'application/json;charset=UTF-8',
            },
            body: JSON.stringify(bodyData)

        }).then((response)=>response.json())
            .then(json=>{
                console.log(json);
                if(json.meta.success && json.data){
                    let data = {
                        errorId,
                        type,
                        files : json.data.files ? json.data.files : []
                    };
                    dispatch(refreshImg(data))
                }
            }).catch(e=>{console.error(e)})
        // let data = {
        //     errorId,
        //     type,
        //     files : json.data.files ? json.data.files : []
        // };
        // dispatch(refreshImg(data))

    }
};

const deleteImg = (errorId,type,fileId)=>{
    return(dispatch)=>{
        console.log("deleteImg action :",errorId,type,fileId);
        let bodyData = Object.assign({},{errorId,type,fileId})
        console.log(bodyData);
        fetch(domain + 'delete_image_error.action',{
            credentials : 'include',
            method : 'POST',
            mode : 'cors',
            headers: {
                'content-type':'application/json;charset=UTF-8',
            },
            body: JSON.stringify(bodyData)
        }).then(checkStatus)
            .then(parseJson)
            .then((data) => {
                console.log("data :",data);
                if(data.meta.success){
                    dispatch(getRefreshImg(errorId,type))
                }
            })
            .catch((err) =>{
                console.error("Err:",err);
            });

        // let info = json.data.files.filter(i=>i.id !== fileId);
        // let data = {
        //     errorId,
        //     type,
        //     files : info
        // };
        // dispatch(refreshImg(data))
    }

}

export {getRefreshImg,deleteImg}