import axios from 'axios'
import qs from 'qs'
import store from '@/store'
import auth from './auth'
import router from '../router/index'
import { Toast } from 'vant'
import constant from '../common/js/constant'

export function request(request, isFile) {
    // console.log('get token:', request.headers, store.getters.token, getToken())
    if (!request.headers) {
        request.headers = {}
        // request.headers = { 'Content-Type': 'application/json' }
    }
    // console.log('token:', auth.getToken())
    if (request.headers && auth.getToken()) {
        // ['RC-Token'] is a custom headers key
        request.headers['RC-Token'] = auth.getToken()
    }
    const requestBody = {
        method: request.method,
        url: constant.BASE_URI + request.url,
        data: qs.stringify(request.data)
    }
    if (isFile) {
        requestBody.data = request.data
    }
    if (request.headers) {
        requestBody.headers = request.headers
        requestBody.headers['MVVM-Key'] = String(new Date().getTime())
        requestBody.headers['xx'] = 'anything'
        requestBody.headers['Refere'] = 'xxxx'
        requestBody.headers['Cache-Control'] = 'no-cache'
    }

    // console.log('request: ', request)
    return new Promise((resolve, reject) => {
        axios.defaults.withCredentials = true
        axios(requestBody).then((res) => {
            if (res && res.status === 200) {
                const resData = res.data
                switch (resData.errno) {
                    case 501:
                        Toast(resData.errmsg)
                        setTimeout(() => {
                            store.dispatch('user/logout').then(() => {
                                router.push('/login')
                            })
                        }, 1500)
                        reject(resData.errmsg)
                        break
                    case 401:
                    case 402:
                    case 502:
                    case 503:
                    case 504:
                    case 505:
                    case 506:
                    case 507:
                    case 508:
                    case 801:
                        Toast(resData.errmsg)
                        reject(resData.errmsg)
                        break
                }
                resolve(resData)
            } else {
                console.log(res)
                reject(res)
            }
        }).catch((err, x) => {
            console.log(request.url + ' fail', err, x)
            Toast(err)
            reject(err, x)
        })
    })
}

export function request2(request) {
    if (!request.headers) {
        request.headers = {}
    }
    if (request.headers && auth.getToken()) {
        request.headers['RC-Token'] = auth.getToken()
    }
    const requestBody = {
        method: request.method,
        url: constant.BASE_URI + request.url,
    }
    requestBody.data = request.data
    if (request.headers) {
        requestBody.headers = request.headers
        requestBody.headers['MVVM-Key'] = String(new Date().getTime())
        requestBody.headers['xx'] = 'anything'
        requestBody.headers['Refere'] = 'xxxx'
        requestBody.headers['Cache-Control'] = 'no-cache'
        requestBody.headers['Content-Type'] = 'application/json;charset=UTF-8'
    }
    // console.log('request: ', request)
    return new Promise((resolve, reject) => {
        axios.defaults.withCredentials = true
        axios(requestBody).then((res) => {

            if (res && res.status === 200) {
                const resData = res.data
                switch (resData.errno) {
                    case 501:
                        Toast(resData.errmsg)
                        setTimeout(() => {
                            store.dispatch('user/logout').then(() => {
                                router.push('/login')
                            })
                        }, 1500)
                        reject(resData.errmsg)
                        break
                    case 401:
                    case 402:
                    case 502:
                    case 503:
                    case 504:
                    case 505:
                    case 506:
                    case 507:
                    case 508:
                    case 801:
                        Toast(resData.errmsg)
                        reject(resData.errmsg)
                        break
                }
                resolve(resData)
            } else {
                console.log(res)
                reject(res)
            }
        }).catch((err, x) => {
            console.log(request.url + ' fail', err, x)
            Toast(err)
            reject(err, x)
        })
    })
}