import { request } from '@/utils/request'
import modal from './modal'
export default {

    post : function({url,  data,loading , config}){
       
        return new Promise((resolve, reject)=>{

            if(loading){
                modal.loading(loading);
            }  
            request(url, 'post', data, config).then(response=>{
                if(!response){
                    reject && reject(response);
                    return;
                } 
                var result = response.data;
                if(!result){
                    reject && reject(response);
                    return;
                } 
                if(loading){
                    modal.hideLoading();
                }  
                if(result.code != 200){
                    if(reject){
                        reject(response);
                    }else{
                        modal.error(result.msg);
                    } 
                    return;
                } 
                resolve(result); 
            }).catch((error)=>{   
                if(loading){
                    modal.hideLoading();
                }  
                modal.error(error.toString());
              }); 
        });

    },

    getPagedData: function({url, method = 'post', data,callback, config}){
       
        return new Promise((resolve, reject)=>{
        
            if(data.start_time && typeof(data.start_time) == "string"){
                data.start_time = parseInt(new Date(data.start_time).getTime()/1000);
            }
            if(data.end_time && typeof(data.end_time) == "string"){
                data.end_time =parseInt(new Date(data.end_time).getTime()/1000);
            } 
            request(url, method, data, config).then(response=>{
                if(!response){
                    reject && reject(response);
                    return;
                } 
                var result = response.data;
                if(!result){
                    reject && reject(response);
                    return;
                } 

                if(result.code != 200){
                    if(reject){
                        reject(response);
                    }else{
                        modal.error(result.msg);
                    } 
                    return;
                } 
                resolve(result.data); 

                if(callback){
                    callback(result.data);
                }
            }).catch((error)=>{    
                resolve({
                    total:0,
                    list :[]
                });
                modal.error(error.toString());
              });  
        });

    }


}