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



long members_call(CVars in, CVars &out) {
    logfmt(logINFO, "members_call()");

    long action = REQ_LONG(in, "action", 0, -1);
    CVars x;
    long ret = 0;
    const char * loc = getenv("REMOTE_ADDR");

    switch (action) {
        case MOBIDELIV_USER_DRYCLIST:
            return kleenDryCleanList(in, out);
            break;

        case MEDTRANS_USER_LOGIN:
            return medTMemberLogin(in, out);
            break;

        case MEDTRANS_USER_CREATE:
            return medTrCreateMember(in, out);
            break;
        case MOBIDELIV_USER_COMPLETEPROFILE:
            return medTrUpdateStartProfile(in, out);
            break;

        case MEDTRANS_USER_UPDATE:
            return medTrUpdateProfile(in, out);
            break;

        case MEDTRANS_USER_PROFILE:
            REQ_LONG(in, "member_id", 0, -1);
            REQ_STRING(in, "sessionid", 2, 100, "(.*)");
            if (load_db_record(out, "SELECT m.*,p.* FROM members m LEFT JOIN members_profile p ON p.member_id=m.id WHERE m.id=%lu", in["member_id"].Long()) > 0) {
                return PHP_API_OK;
            }
            break;

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

        case MOBIDELIV_USER_GETCCLIST:
            return getUserCCList(in, out);
            break;


        case MEDTRANS_USER_NEWLUNDRYPICK:
            return kleenNewLundryPickUp(in, out);
            break;

        case MEDTRANS_USER_GET_TRAN_BYID:
            return medTrMemberTransportById(in, out);
            break;

        case MOBIDELIV_USER_GETSERVICEITEM:
            return medTrMemberServiceById(in, out);
            break;
        case MOBIDELIV_USER_GETSERVICELIST:
            return medTrMemberTransportList(in, out);
            break;

        case MEDTRANS_USER_LUNDRYLOCATION:
            return medTrLanguageList(in, out);
            break;

        case MEDTRANS_USER_VERIFYSESSION:
            REQ_LONG(in, "member_id", 0, -1);
            REQ_STRING(in, "sessionid", 2, 100, "(.*)");
            ret = PHP_INVALID_SESSION;
            out["status"] = "INVALID_SESSION";
            if (SessionCheck(in["member_id"].Long(), in["sessionid"].c_str(), 0) == 1L) {
                out = in;
                out["status"] = "VALID_SESSION";
                ret = PHP_VALID_SESSION;
            }
            break;

        case MOBIDELIV_USER_CONFIRMPICKUP:
            logfmt(logINFO, "MOBIDELIV_USER_CONFIRMPICKUP()");
            REQ_LONG(in, "member_id", 0, -1);
            REQ_STRING(in, "sessionid", 2, 100, "(.*)");
            REQ_LONG(in, "service_id", 0, -1);
            REQ_LONG(in, "paymentid", 0, -1);
            return kleenConfirmPickup(in, out);
            break;

        case MEDTRANS_USER_PASSRESET: //     200067
            REQ_LONG(in, "mode", 0, -1);
            return passwordReset(in, out);
            break;

        case MOBIDELIV_USER_DELETECARD:
            out = in;
            return userDeleteCard(in, out);
            break;

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

