import axios from "axios";

export const ApiClass ={
    state: {
            auth:false,
            kag:null,
            kagArr:[
                {firma:0,name:'Беру Яндекс (ТД БММ)','kag': 217445,'barcode_length':8},
                {firma:1,name:'Озон_1 (ТД БММ)','kag': 219807,'barcode_length':15 },
                {firma:2,name:'Озон_2 (bmm.ru,ОК-Книга)','kag': 225202,'barcode_length':15},
                {firma:3,name:'Озон_3 (Терминал-книга)','kag': 225645,'barcode_length':15},
                {firma:4,name:'Озон_4 (МногоКниг)','kag': 226683,'barcode_length':15},
                {firma:5,name:'Озон_5 (Книгариум)','kag': 228681,'barcode_length':15},
                {firma:6,name:'Озон_6 (СмартБук)','kag': 229077,'barcode_length':15},
               // {firma:7,name:'Озон_7 (Дом Историй)','kag': 236185,'barcode_length':15},
                {firma:8,name:'Озон_8_FBS (Руграм)','kag': 238376,'barcode_length':15},
                {firma:9,name:'Озон_9_FBS (Омега-Л)','kag': 240346,'barcode_length':15},
                {firma:10,name:'Озон_10_FBS','kag': 244632,'barcode_length':15},
                {firma:11,name:'Экспотрейд','kag': 238346,'barcode_length':8},
                {firma:12,name:'Магнит_Маркет_FBS','kag': 244419,'barcode_length':16},
                {firma:13,name:'Экспотрейд МП 2.0','kag': 248257,'barcode_length':8},
                {firma:14,name:'Авита','kag': 222860,'barcode_length':0},
            ],


            data:null,
            url_api:'https://tsd.book-online.ru',
            // url_api:'http://10.10.7.169:8100',
    },
    getters: {
    },
    mutations: {
        setAuth(state,result){
            state.auth=result
        },
        setUser(state,result){
            state.user=result
        },
        dataSet(state,result){
            state.data=result;
        },
        kagSet(state,result){
            state.kag=result;
        },
    },
    actions: {


        async loadData({state, commit}, param) {
            try {

                //let json='';
                // if(param.json){
                let   json=JSON.stringify(param.json);
                //}

                let response=null;
                let headers= {};

               // if(typeof localStorage.token!=='undefined'){
                //    headers.Authorization='token '+localStorage.token;
               // }
                if(param.method==='post'){
                    headers['Content-Type']='application/json';
                }

                if(param.method==='put'){
                    headers['Content-Type']='application/json';
                }
                if(param.method==='get'){
                    json={};
                }


                response = await axios[param.method](param.url,json,{headers:headers});

                //console.log(response.data);
                return response.data;
            } catch (e) {

                //console.log(e);
                return e;
            } finally {

            }
        },









        async queryApi({state, commit}, param) {
            try {

                console.log(param);
                const json = JSON.stringify({
                    data: JSON.stringify(param),
                });


                const response = await axios.post(state.url_api+'/api/queryMainPost.php',json,{
                    headers: {'Content-Type': 'application/json'}
                });

                console.log(response.data);
                //commit('dataSet',response.data );

                return response.data;

            } catch (e) {
                console.log(e)
            } finally {

            }
        },

        async postingStatus({state, commit}, param) {
            try {


                const json = JSON.stringify(
                     param,
                );


                // const response = await axios.post(state.url_api+'/api/posting_status.php',json,{
                //     headers: {'Content-Type': 'application/json'}
                // });
                const response = await axios.post('https://marketplace.book-online.ru/api/posting_status/',json,{
                    headers: {Authorization: 'fi9tzpIHgp84E1Pf45rMzR/hy8lA2zE5','Content-Type': 'application/json'}
                });
                //https://marketplace.book-online.ru/api/posting_status/

                console.log(response.data);
                //commit('dataSet',response.data );

                return response.data;

            } catch (e) {
                console.log(e)
            } finally {

            }
        },


        async getCollect({state, commit}, param) {
            try {


                const json = JSON.stringify({
                    data: JSON.stringify(param),
                });


                const response = await axios.post(state.url_api+'/api/getCollect.php',json,{
                    headers: {'Content-Type': 'application/json'}
                });

                //console.log(response.data);
                commit('dataSet',response.data );
                return response.data;

            } catch (e) {
                console.log(e)
            } finally {

            }
        },

        async queryHistory({state, commit}, param) {
            //let error=new Event('error');

            try {






               // console.log(param);
                const json = JSON.stringify({
                    data: JSON.stringify(param),
                });


                const response = await axios.post(state.url_api+'/api/queryHistory.php',json,{
                    headers: {'Content-Type': 'application/json'}
                });

               // console.log(response.data);
                commit('dataSet',response.data.data );
                return response.data;

            } catch (e) {
                console.log(e)
            } finally {

            }
        },


        async pushCollect({state, commit}, param) {
            try {//добавить в сборку баркод


                const json = JSON.stringify({
                    data: JSON.stringify(param),
                });


                const response = await axios.post(state.url_api+'/api/pushCollect.php',json,{
                    headers: {'Content-Type': 'application/json'}
                });

                // console.log(response.data);

                return response.data;

            } catch (e) {
                console.log(e)
            } finally {

            }
        },




    },
    modules: {
    },

    namespaced: true
}

