
import { API_FAST_PLANS, API_FAST_CHECKS,API_FAST_CLOCKS, API_CLOCK_RECORDS } from './http/api'
import { request } from './http/request';

export const getPlans = () => {
    return new Promise((resolve) => {
        request({
            url: API_FAST_PLANS, method: 'GET', data: {}
        }).then(res => {
            resolve(res);
            console.log(res);
            // dispatch(loginSuccess(res));
        })
    })
}

export const setPlan = (params: Record<string, any> | undefined) => {
    return new Promise((resolve,reject) => {
        request({
            url: API_FAST_PLANS, method: 'POST', data: { ...params }
        }).then(res => {
            resolve(res);
            console.log(res);
            // dispatch(loginSuccess(res));
        }).catch(e=>{
            reject(e)
        })
    })
}

export const getChecks = () => {
    return new Promise((resolve) => {
        request({
            url: API_FAST_CHECKS, method: 'GET', data: {}
        }).then(res => {
            resolve(res);
        })
    })
}

export const getClocks = () => {
    return new Promise((resolve,reject) => {
        request({
            url: API_FAST_CLOCKS, method: 'GET', data: {}
        }).then(res => {
            resolve(res);
        }).catch(e=>{
            reject(e)
        })
    })
}

export const getClockRecords = (params:any)=>{
    return new Promise((resolve) => {
        request({
            url: API_CLOCK_RECORDS, method: 'GET', data: {...params}
        }).then(res => {
            resolve(res);
        })
    })

}

export const recordCheck = (params: Record<string, any>) => {
    if (params.real_check_time){
        var date = new Date((params as any).real_check_time)
        var month = date.getMonth()+1
        var day = date.getDate()
        
        params.real_check_date = date.getFullYear()+(month<10?'0'+month:month+'')+(day<10?'0'+day:day+'')
    }
    return new Promise((resolve) => {
        request({
            url: API_FAST_CLOCKS, method: 'POST', data: { ...params }
        }).then(res => {
            resolve(res);
        }).catch(e=>{
            global.postBtnUpdateStatus('idle')
        })
    })
}

export const delRecord = (id: string) => {
    return new Promise((resolve) => {
        request({
            url: API_CLOCK_RECORDS + '/' + id, method: 'DELETE', data: {}
        }).then(res => {
            resolve(res);
        })
    })
}

export const clearTimeRecords = ()=>{
    return new Promise((resolve) => {
        request({
            url: API_CLOCK_RECORDS, method: 'DELETE', data: {}
        }).then(res => {
            resolve(res);
        })
    })
    

}