/*
General Header
ChiefSoft July 2017
 */
#include "clog.h"
#include "cgi.h"
#include "input.h"
#include "jubabox_api.h"
#include "safestring.h"
#include <string>
#include "pgsql.h"
#include "pgsql_wrapper.h"
#include "cfg.h"
#include <curl/curl.h>
/*
Other Headers
 */
#include "medTrBackOffice.h"
#include "medTEmails.h"
#include "function_backoffice.h"
#include "function_members.h"
#include "creditcards.h"

long backoffice_calls(CVars in, CVars &out) {
    logfmt(logINFO, "backoffice_calls()");
    out["result"] = "YES I GET TO BACK END";
    long action = REQ_LONG(in, "action", 0, -1);
    CVars x;
    long ret = 0;
    const char * loc = getenv("REMOTE_ADDR");

    switch (action) {

        case MEDTRANS_BKO_LOGIN:
            return LoginBkoAdmin(in, out);
            break;

        case MEDTRANS_BKO_CREATEUSER:
            return createBkoAdmin(in, out);
            break;

        case JUBABOX_ACCOUNT_PENDING:

            break;

        case JUBABOX_BKO_DELETEALLCARDS:
            return DeletAallCards(in, out);
            break;
        
        case JUBABOX_BKO_MEMBERADD:
            if (medTrCreateMember(in, out) == PHP_API_OK) {
                in["member_id"] = out["member_id"];

                if (medTrUpdateStartProfile(in, out) == PHP_API_OK) {
                    out["status_message"] = "Account Created Successfully";
                } else {
                    out["status_message"] = "Incomplete Account, login to complete";
                }
            } else {
                out["status_message"] = "Account Create Error";
            }
            break;
        case JUBABOX_BKO_MEMBERUPDT:
            return medTrUpdateProfile(in, out);
            break;

        case JUBABOX_BKO_ADDCARD:
            in["paymenttype"] = "1";
            save_creditcard(in, out);
            break;


    }
    logfmt(logINFO, "/backoffice_calls()");
    return ret;
}

long cron_call(CVars in, CVars &out) {
    long action = REQ_LONG(in, "action", 0, -1);
    logfmt(logINFO, "cron_call()");
    CVars x;
    long request_id = 0;

    switch (action) {
        case CRON_PROCESS_DISTANCE:
            out = in;
            request_id = REQ_LONG(in, "request_id", 0, -1);
            REQ_STRING(in, "miles", 1, 12, "(.*)");
            REQ_STRING(in, "dist_mode", 1, 6, "(.*)");
            REQ_LONG(in, "minutes", 0, -1);
            x["miles"] = in["miles"];
            x["miles"].set_valid(true);
            x["dist_mode"] = in["dist_mode"];
            x["dist_mode"].set_valid(true);
            x["minutes"] = in["minutes"];
            x["minutes"].set_valid(true);

            update_db_record(DBS_VALID, "members_transport_request", x, request_id);

            //{
            //pgsql_query("UPDATE members_transport_request SET last_login = now() WHERE id  = %lu",out["member_id"].Long() );

            //	}			
            break;

        case CRON_USER_REMINDER:

            logfmt(logINFO, "cron_call() = CRON_USER_REMINDER");

            break;



    }





    logfmt(logINFO, "/cron_call()");
    return 0;
}




