// Topup management toosl
#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 <curl/curl.h>
#include "medTEmails.h"
/* -- */
#include "function_agents.h"
#include "medTrTransporter.h"
//#include "function_driver.h"




void GenerateSeed( char * number, int sz, int seed );



long medTrDeviceList(CVars in, CVars &out) {
    long ret = PHP_API_BAD_PARAM;
    char vname[30];
    try {
        OPTIONAL(in, "limit") REQ_LONG(in, "limit", 0, -1);
        REQ_LONG(in, "agent_id", 0, -1);
        REQ_LONG(in, "location_id", 0, -1);
        OPTIONAL(in, "device_id") REQ_LONG(in, "device_id", 0, -1);
             
        out["total_record"] = "0";
       
        const PGresult *res;
        if (in["device_id"].Long() > 0) {
       res = pgsql_query("SELECT * FROM transport_devices "
                    "WHERE agent_id = %lu  AND transporter_id  = %lu "
                    "AND id=%ly", in["agent_id"].Long(), in["location_id"].Long(), in["device_id"].Long() );

        } else {
            res = pgsql_query("SELECT * FROM transport_devices "
                    "WHERE agent_id = %lu  AND transporter_id  = %lu "
                    "ORDER BY description ASC", in["agent_id"].Long(), in["location_id"].Long() );
        }

         if (res != NULL && pgsql_num_rows(res) > 0) {
            out["total_record"] = pgsql_num_rows(res);

            for (int i = 0, n = pgsql_num_rows(res); i < n; i++) {
                map<const char*, const char*>f = pgsql_fetch_assoc(res, i);
                if (f.empty()) continue;
                CVars rec;
                map_to_cvars(f, rec);

                snprintf(vname, sizeof (vname), "description_id_%05d", i);
                out[vname] = rec["description"];

                snprintf(vname, sizeof (vname), "lic_number_%05d", i);
                out[vname] = rec["lic_number"];

                snprintf(vname, sizeof (vname), "lic_state_%05d", i);
                out[vname] = rec["lic_state"];

                snprintf(vname, sizeof (vname), "expr_month_%05d", i);
                out[vname] = rec["expr_month"];

                snprintf(vname, sizeof (vname), "expr_year_%05d", i);
                out[vname] = rec["expr_year"];

                snprintf(vname, sizeof (vname), "state_%05d", i);
                out[vname] = rec["state"];

                snprintf(vname, sizeof (vname), "country_%05d", i);
                out[vname] = rec["country"];

                snprintf(vname, sizeof (vname), "agent_id_%05d", i);
                out[vname] = rec["agent_id"];

                snprintf(vname, sizeof (vname), "transporter_id_%05d", i);
                out[vname] = rec["transporter_id"];

            
            }
        }
        ret = PHP_API_OK;
        out["status"] = "OK";
    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long medTrDeviceList(CVars in, CVars &out)");
    }
    return ret;
}

long medAssignTranspoter(CVars in, CVars &out) {
    long ret = PHP_API_BAD_PARAM;

    REQ_LONG(in, "pid", 0, -1);
    REQ_LONG(in, "service_id", 0, -1);
    REQ_LONG(in, "driver_id", 0, -1);
    const char * loc = getenv("REMOTE_ADDR");
    CVars x;
    const PGresult *res;

    res = pgsql_query("SELECT * FROM members_service_request WHERE id=%lu AND status= 1 AND flags = %lu", in["service_id"].Long(), CONFIRMED);
    if (res != NULL && pgsql_num_rows(res) > 0) {

        if (load_db_record(out, "SELECT id AS driver_id FROM providers WHERE id= %lu ", in["driver_id"].Long()) > 0) {

            if (out["driver_id"].Long() > 0) {
                pgsql_exec("UPDATE members_service_request SET flags = %lu, provider =%lu WHERE id = %lu AND flags=%lu AND status = 1 ", ASSIGNED, out["driver_id"].Long(), in["service_id"].Long(), CONFIRMED);
                ret = PHP_API_OK;  
                provider_email_calls( in["action"].Long(), in, out );
                agent_email_calls(in["action"].Long(), in, out);
             
            }
        }
    } else {
        out["status"] = "Valid driver not found";
    }
    return ret;
}


long medTrInterpreterLangList(CVars in, CVars &out) {
    long ret = PHP_API_BAD_PARAM;
    char vname[30];
    try {
        REQ_LONG(in, "limit", 0, -1);
        REQ_LONG(in, "agent_id", 0, -1);
        REQ_LONG(in, "interpreter_id", 0, -1);
        REQ_LONG(in, "language_id", 0, -1);
             
        out["total_record"] = "0";
       
        const PGresult *res;
        if (in["language_id"].Long() > 0) {
            res = pgsql_query("SELECT i.id as language_id,i.*,l.language FROM interpreters_language i "
                    "LEFT JOIN languages l ON l.code=i.lang_code "
                    "WHERE i.agent_id = %lu  "
                    "AND i.interpreter_id  = %lu AND i.id= %lu", in["agent_id"].Long(), in["interpreter_id"].Long(), in["language_id"].Long());

        } else {
            res = pgsql_query("SELECT i.id as language_id,i.*,l.language FROM interpreters_language i "
                    "LEFT JOIN languages l ON l.code=i.lang_code "
                    "WHERE i.agent_id = %lu  AND i.interpreter_id  = %lu "
                    "ORDER BY l.language ASC LIMIT %lu", in["agent_id"].Long(), in["interpreter_id"].Long(), in["limit"].Long());
        }

         if (res != NULL && pgsql_num_rows(res) > 0) {
            out["total_record"] = pgsql_num_rows(res);

            for (int i = 0, n = pgsql_num_rows(res); i < n; i++) {
                map<const char*, const char*>f = pgsql_fetch_assoc(res, i);
                if (f.empty()) continue;
                CVars rec;
                map_to_cvars(f, rec);

                snprintf(vname, sizeof (vname), "language_id_%05d", i);
                out[vname] = rec["language_id"];

                snprintf(vname, sizeof (vname), "lang_code_%05d", i);
                out[vname] = rec["lang_code"];

                snprintf(vname, sizeof (vname), "agent_id_%05d", i);
                out[vname] = rec["agent_id"];


                snprintf(vname, sizeof (vname), "interpreter_id_%05d", i);
                out[vname] = rec["interpreter_id"];


                snprintf(vname, sizeof (vname), "flags_%05d", i);
                out[vname] = rec["flags"];

                snprintf(vname, sizeof (vname), "status_%05d", i);
                out[vname] = rec["status"];

                snprintf(vname, sizeof (vname), "language_%05d", i);
                out[vname] = rec["language"];

                snprintf(vname, sizeof (vname), "weekend_%05d", i);
                out[vname] = rec["weekend"];

                snprintf(vname, sizeof (vname), "document_%05d", i);
                out[vname] = rec["document"];

                snprintf(vname, sizeof (vname), "document_id_%05d", i);
                out[vname] = rec["document_id"];
                
                // | 
                
            }
        }
        ret = PHP_API_OK;
        out["status"] = "OK";
    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long medTrInterpreterLangList(CVars in, CVars &out)");
    }
    return ret;
}

long medTrAgentLangList(CVars in, CVars &out)
{
    
    long ret = PHP_API_BAD_PARAM;
    char vname[30];
    try {
       
        REQ_LONG(in, "agent_id", 0, -1);
        REQ_LONG(in, "language_id", 0, -1);
             
        out["total_record"] = "0";
        const PGresult *res;
        if (in["language_id"].Long() > 0) {
           
            res = pgsql_query("SELECT  t.firstname,t.lastname,t.phone,t.city,t.zipcode, i.lang_code,l.language,i.interpreter_id,i.id AS language_id,i.agent_id,i.flags,i.weekend,t.street,t.state,t.city "
                    "FROM interpreters_language i "
                    "LEFT JOIN languages l ON l.code=i.lang_code "
                    "LEFT JOIN interpreters t ON t.id=i.interpreter_id WHERE i.agent_id=%lu", in["agent_id"].Long(), in["language_id"].Long());

        } else {
            res = pgsql_query("SELECT  t.firstname,t.lastname,t.phone,t.city,t.zipcode, i.lang_code,l.language,i.interpreter_id,i.id AS language_id,i.agent_id,i.flags,i.weekend,t.street,t.state,t.city "
                    "FROM interpreters_language i "
                    "LEFT JOIN languages l ON l.code=i.lang_code "
                    "LEFT JOIN interpreters t ON t.id=i.interpreter_id WHERE i.agent_id=%lu", in["agent_id"].Long());
        }

         if (res != NULL && pgsql_num_rows(res) > 0) {
            out["total_record"] = pgsql_num_rows(res);

            for (int i = 0, n = pgsql_num_rows(res); i < n; i++) {
                map<const char*, const char*>f = pgsql_fetch_assoc(res, i);
                if (f.empty()) continue;
                CVars rec;
                map_to_cvars(f, rec);

                snprintf(vname, sizeof (vname), "language_id_%05d", i);
                out[vname] = rec["language_id"];

                snprintf(vname, sizeof (vname), "lang_code_%05d", i);
                out[vname] = rec["lang_code"];

                snprintf(vname, sizeof (vname), "agent_id_%05d", i);
                out[vname] = rec["agent_id"];


                snprintf(vname, sizeof (vname), "firstname_%05d", i);
                out[vname] = rec["firstname"];

                              snprintf(vname, sizeof (vname), "lastname_%05d", i);
                out[vname] = rec["lastname"];

                
                                snprintf(vname, sizeof (vname), "state_%05d", i);
                out[vname] = rec["state"];
                                snprintf(vname, sizeof (vname), "street_%05d", i);
                out[vname] = rec["street"];
                

                snprintf(vname, sizeof (vname), "flags_%05d", i);
                out[vname] = rec["flags"];

                snprintf(vname, sizeof (vname), "phone_%05d", i);
                out[vname] = rec["phone"];

                snprintf(vname, sizeof (vname), "language_%05d", i);
                out[vname] = rec["language"];

                snprintf(vname, sizeof (vname), "weekend_%05d", i);
                out[vname] = rec["weekend"];

                snprintf(vname, sizeof (vname), "zipcode_%05d", i);
                out[vname] = rec["zipcode"];

                snprintf(vname, sizeof (vname), "city_%05d", i);
                out[vname] = rec["city"];
                
                                snprintf(vname, sizeof (vname), "interpreter_id_%05d", i);
                out[vname] = rec["interpreter_id"];
                
                
                
                // | 
                
            }
        }
        ret = PHP_API_OK;
        out["status"] = "OK";
    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long medTrInterpreterLangList(CVars in, CVars &out)");
    }
    return ret;
}
/*
 jubabox=> SELECT i.*,l.language FROM interpreters_language i LEFT JOIN languages l ON l.code=i.lang_code WHERE agent_id=8;
 id | pid | agent_id | interpreter_id | lang_code | document | document_id | weekend | flags | status |           added            |       loc       |    language     
----+-----+----------+----------------+-----------+----------+-------------+---------+-------+--------+----------------------------+-----------------+-----------------
 71 |     |        8 |              5 | L0019     |          |           0 |       0 |     0 |      1 | 2017-10-14 20:05:29.179305 | 104.238.127.115 | Bislama
 65 |     |        8 |              5 | L0023     |          |           0 |       0 |     0 |      1 | 2017-10-14 20:04:51.771004 | 104.238.127.115 | Burmese
 */

long medTrInterpreterLanguage(CVars in, CVars &out) {
    logfmt(FLOG_MAX, "long medTrInterpreterLanguage(CVars in, CVars &out)");
    long ret = PHP_API_BAD_PARAM;
    const char * loc = getenv("REMOTE_ADDR");

    try {
        long mode = REQ_LONG(in, "mode", 0, -1);
        REQ_LONG(in, "agent_id", 0, -1); // Always needed
        if (mode == MODE_ADD) {
            REQ_LONG(in, "interpreter_id", 0, -1);
            REQ_STRING(in, "lang_code", 4, 6, "(.*)");
            const PGresult *res = pgsql_query("SELECT * FROM interpreters_language  WHERE agent_id = %lu AND  interpreter_id=%lu AND lang_code='%s' ", in["agent_id"].Long(), in["interpreter_id"].Long(), in["lang_code"].c_str());
            if (res != NULL && pgsql_num_rows(res) > 0) {
                logfmt(FLOG_MAX, "*** Interpreter's language already exists ***");
                map<const char*, const char*>f = pgsql_fetch_assoc(res, 0);
                if (f.empty()) {
                    logfmt(FLOG_MAX, "Record is empty");
                } else {
                    CVars rec;
                    map_to_cvars(f, rec);
                    rec["status"] = 1; rec["status"].set_valid(true);
                    update_db_record( DBS_VALID, "interpreters_language", rec, rec["id"].Long());
                    ret = PHP_API_OK;
                }
            } else {
                CVars x;
                x["interpreter_id"] = in["interpreter_id"];
                x["interpreter_id"].set_valid(true);
                x["agent_id"] = in["agent_id"];
                x["agent_id"].set_valid(true);
                x["lang_code"] = in["lang_code"];
                x["lang_code"].set_valid(true);
                x["loc"] = loc;
                x["loc"].set_valid(true);
                out["language_id"] = insert_db_record(DBS_VALID, "interpreters_language", "interpreters_language_id_seq", x);
                if (out["language_id"].Long() > 0) {
                    ret = PHP_API_OK;
                    load_db_record(out, "SELECT i.*,l.language FROM interpreters_language i LEFT JOIN languages l ON l.code=i.lang_code WHERE i.id= %lu ", out["language_id"].Long());
                    agent_email_calls(in["action"].Long(), in, out);
                }
            }
        }//end of  if ( mode == MODE_ADD )

        if (mode == MODE_UPDATE) {
            REQ_LONG(in, "interpreter_id", 0, -1);
            REQ_STRING(in, "lang_code", 4, 6, "(.*)");
            REQ_LONG(in, "language_id", 0, -1);
            REQ_LONG(in, "flags", 0, -1);
  
            const PGresult *res1 = pgsql_query("SELECT * FROM interpreters_language  WHERE status = 1 AND agent_id = %lu AND  interpreter_id=%lu AND id=%lu ", in["agent_id"].Long(), in["interpreter_id"].Long(), in["language_id"].Long());
            if (res1 != NULL && pgsql_num_rows(res1) > 0) {
                pgsql_exec("UPDATE interpreters_language SET flags = %lu WHERE agent_id = %lu AND  interpreter_id=%lu AND id=%lu ",in["flags"].Long(), in["agent_id"].Long(), in["interpreter_id"].Long(), in["language_id"].Long());
                ret = PHP_API_OK;
            }
            else
            {
                out["status"] ="Error - Applicable entry not found"; 
            }
        }

        if (mode == MODE_DELETE) {
            REQ_LONG(in, "interpreter_id", 0, -1);
            REQ_STRING(in, "lang_code", 4, 6, "(.*)");
            REQ_LONG(in, "language_id", 0, -1);

            const PGresult *res1 = pgsql_query("SELECT * FROM interpreters_language  WHERE status = 1 AND agent_id = %lu AND  interpreter_id=%lu AND id=%lu ", in["agent_id"].Long(), in["interpreter_id"].Long(), in["language_id"].Long());
            if (res1 != NULL && pgsql_num_rows(res1) > 0) {
                pgsql_exec("UPDATE interpreters_language SET status = 0 WHERE agent_id = %lu AND  interpreter_id=%lu AND id=%lu ", in["agent_id"].Long(), in["interpreter_id"].Long(), in["language_id"].Long());
                ret = PHP_API_OK;
            }
            else
            {
                out["status"] ="Error - Applicable entry not found"; 
            }
        }



    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long medTrInterpreterLanguage(CVars in, CVars &out)");
    }
    logfmt(FLOG_MAX, "/long medTrInterpreterLanguage(CVars in, CVars &out)");
    return ret;
}

long medTrTransporterTranslatortList(CVars in, CVars &out) {
    long ret = PHP_API_BAD_PARAM;
    char vname[20];
    try {
        REQ_LONG(in, "limit", 0, -1);
        REQ_LONG(in, "agent_id", 0, -1);

        out["total_record"] = "0";

         const PGresult *res = pgsql_query("SELECT * FROM interpreters WHERE agent_id = %lu ORDER BY id DESC LIMIT %lu", in["agent_id"].Long(), in["limit"].Long());
        // const PGresult *res = pgsql_query("SELECT * FROM interpreters WHERE agent_id >0 ORDER BY id DESC LIMIT %lu", in["limit"].Long());
        if (res != NULL && pgsql_num_rows(res) > 0) {
            out["total_record"] = pgsql_num_rows(res);

            for (int i = 0, n = pgsql_num_rows(res); i < n; i++) {
                map<const char*, const char*>f = pgsql_fetch_assoc(res, i);
                if (f.empty()) continue;
                CVars rec;
                map_to_cvars(f, rec);

                snprintf(vname, sizeof (vname), "firstname_%05d", i);
                out[vname] = rec["firstname"];
                snprintf(vname, sizeof (vname), "lastname_%05d", i);
                out[vname] = rec["lastname"];


                snprintf(vname, sizeof (vname), "id_%05d", i);
                out[vname] = rec["id"];
                snprintf(vname, sizeof (vname), "agent_id_%05d", i);
                out[vname] = rec["agent_id"];
                snprintf(vname, sizeof (vname), "street_%05d", i);
                out[vname] = rec["street"];
                snprintf(vname, sizeof (vname), "city_%05d", i);
                out[vname] = rec["city"];
                snprintf(vname, sizeof (vname), "zipcode_%05d", i);
                out[vname] = rec["zipcode"];
                snprintf(vname, sizeof (vname), "state_%05d", i);
                out[vname] = rec["state"];


            }
        }
        ret = PHP_API_OK;
        out["status"] = "OK";
    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long medTMemberLogin(CVars in, CVars &out)");
    }
    return ret;
}


long medTrTransporterLocationtList(CVars in, CVars &out) {
    long ret = PHP_API_BAD_PARAM;
    char vname[20];
    try {
        REQ_LONG(in, "limit", 0, -1);
        REQ_LONG(in, "agent_id", 0, -1);

        out["total_record"] = "0";

        const PGresult *res = pgsql_query("SELECT * FROM transporter WHERE agent_id = %lu ORDER BY id DESC LIMIT %lu",in["agent_id"].Long(), in["limit"].Long());
        if (res != NULL && pgsql_num_rows(res) > 0) {
            out["total_record"] = pgsql_num_rows(res);

            for (int i = 0, n = pgsql_num_rows(res); i < n; i++) {
                map<const char*, const char*>f = pgsql_fetch_assoc(res, i);
                if (f.empty()) continue;
                CVars rec;
                map_to_cvars(f, rec);

                snprintf(vname, sizeof (vname), "id_%05d", i);
                out[vname] = rec["id"];
                snprintf(vname, sizeof (vname), "agent_id_%05d", i);
                out[vname] = rec["agent_id"];
                snprintf(vname, sizeof (vname), "street_%05d", i);
                out[vname] = rec["street"];
                snprintf(vname, sizeof (vname), "city_%05d", i);
                out[vname] = rec["city"];
                snprintf(vname, sizeof (vname), "zipcode_%05d", i);
                out[vname] = rec["zipcode"];
                snprintf(vname, sizeof (vname), "state_%05d", i);
                out[vname] = rec["state"];
                snprintf(vname, sizeof (vname), "loc_name_%05d", i);
                out[vname] = rec["loc_name"];
                

            }
        }
        ret = PHP_API_OK;
        out["status"] = "OK";
    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long medTMemberLogin(CVars in, CVars &out)");
    }
    return ret;
}

long medTrLoadTransporter(CVars in, CVars &out) {
    long ret = PHP_API_BAD_PARAM;
    logfmt(logINFO, "medTrLoadTransporter()");

    try {
        REQ_LONG(in, "pid", 0, -1);

        long agent_id = REQ_LONG(in, "agent_id", 0, -1);
        long transporter_id = REQ_LONG(in, "transporter_id", 0, -1);

        ret = load_db_record(out, "SELECT *,id AS transporter_id FROM transporter WHERE id=%lu AND agent_id=%lu",transporter_id, agent_id);
        logfmt(logINFO, "~~~~ **** medTrLoadTransporter(CVars in, CVars &out) ret==>%d", ret);

        if (ret > 0) {
            if (transporter_id>0) {
                load_db_record(out, "SELECT *,id AS transporter_id FROM transporter WHERE id= %lu ", transporter_id);
                if (out["transporter_id"].Long()>0) {
                    ret = PHP_API_OK;
                    out["status"] = "1";
                } else {
                    out["status"] = "Transporter is not found";
                }
            } else {
                out["status"] = "Invalid transporter id";
            }
        } else {
            out["status"] = "Invalid or wrong agent type";
        }

    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long medTrLoadTransporter(CVars in, CVars &out)");
        out["status"] = "Invalid input";
    }

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


long medTrCreateTransporter(CVars in, CVars &out) {
    long ret = PHP_API_BAD_PARAM;
    logfmt(logINFO, "medTrCreateTransporter()");

    try {
        REQ_LONG(in, "pid", 0, -1);
        long agent_id = REQ_LONG(in, "agent_id", 0, -1);
        REQ_STRING(in, "street", 5, 49, "(.*)");
        REQ_STRING(in, "zipcode", 3, 12, "(.*)");
        OPTIONAL(in, "phone") REQ_STRING(in, "phone", 5, 23, "(.*)");
        REQ_STRING(in, "city", 5, 49, "(.*)");
        REQ_STRING(in, "state", 2, 25, "(.*)");
        REQ_STRING(in, "loc_name", 1, 49, "(.*)");
        const char * loc = getenv("REMOTE_ADDR");



        ret = load_db_record(out, "SELECT *,id AS agent_id FROM agents WHERE id=%lu AND agent_type IN (1,3) ", agent_id);
        logfmt(logINFO, "~~~~ **** medTrCreateIntepreter(CVars in, CVars &out) ret==>%d", ret);

        if (ret > 0) {

            // LETS CREATE THE REQUEST NOW
            CVars x;
            x["pid"] = in["pid"];
            x["pid"].set_valid(true);

            x["agent_id"] = in["agent_id"];
            x["agent_id"].set_valid(true);
            
            x["loc_name"] = in["loc_name"];
            x["loc_name"].set_valid(true);
            
            x["street"] = in["street"];
            x["street"].set_valid(true);

            x["zipcode"] = in["zipcode"];
            x["zipcode"].set_valid(true);

            x["phone"] = in["phone"];
            x["phone"].set_valid(true);

            x["city"] = in["city"];
            x["city"].set_valid(true);

            x["state"] = in["state"];
            x["state"].set_valid(true);

            x["loc"] = loc;
            x["loc"].set_valid(true);
            x["country"] = "US";
            x["country"].set_valid(true);

            out["transporter_id"] = insert_db_record(DBS_VALID, "transporter", "transporter_id_seq", x);
            if (out["interpreter_id"].Long() > 0) {
                ret = PHP_API_OK;
                load_db_record(out, "SELECT *,id AS transporter_id FROM transporter WHERE id= %lu ", out["transporter_id"].Long());
                agent_email_calls(in["action"].Long(), in, out);
            }
        } else {
            ret = PHP_API_BAD_PARAM;
            out["status"] = "Invalid or wrong agent type";
        }

    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long medTrCreateTransporter(CVars in, CVars &out)");
    }


    logfmt(logINFO, "/medTrCreateTransporter()");
    return ret;
}
// transport_date::time
long medTrTransporterTransportList(CVars in, CVars &out) {
    long ret = PHP_API_BAD_PARAM;
    char vname[30];
    try {
        REQ_LONG(in, "limit", 0, -1);
        REQ_LONG(in, "agent_id", 0, -1);
        REQ_LONG(in, "transport_id", 0, -1);

        out["total_record"] = "0";
        const PGresult *res;

        if (in["transport_id"].Long() > 0) {
           // res = pgsql_query("SELECT *,transport_date::time AS transport_time FROM members_transport_request WHERE id = %lu", in["transport_id"].Long());
            res = pgsql_query("SELECT r.*,r.transport_date::time AS transport_time,a.added AS assign_date  FROM members_transport_request r LEFT JOIN transport_assignment a ON a.transport_id = r.id WHERE a.agent_id=%lu AND  r.id =%lu", in["agent_id"].Long(), in["transport_id"].Long());
        } else {
            res = pgsql_query("SELECT r.*,r.transport_date::time AS transport_time,a.added AS assign_date  FROM members_transport_request r LEFT JOIN transport_assignment a ON a.transport_id = r.id WHERE a.agent_id=%lu AND a.flags IN (0,2,4,5) ORDER BY r.id DESC LIMIT %lu", in["agent_id"].Long(), in["limit"].Long());
        }
        
        
        
        if (res != NULL && pgsql_num_rows(res) > 0) {
            out["total_record"] = pgsql_num_rows(res);

            for (int i = 0, n = pgsql_num_rows(res); i < n; i++) {
                map<const char*, const char*>f = pgsql_fetch_assoc(res, i);
                if (f.empty()) continue;
                CVars rec;
                map_to_cvars(f, rec);

                snprintf(vname, sizeof (vname), "transport_date_%05d", i);
                out[vname] = rec["transport_date"];

                snprintf(vname, sizeof (vname), "firstname_%05d", i);
                out[vname] = rec["firstname"];

                snprintf(vname, sizeof (vname), "id_%05d", i);
                out[vname] = rec["id"];
                
                snprintf(vname, sizeof (vname), "lastname_%05d", i);
                out[vname] = rec["lastname"];

                snprintf(vname, sizeof (vname), "from_street_%05d", i);
                out[vname] = rec["from_street"];
                snprintf(vname, sizeof (vname), "to_street_%05d", i);
                out[vname] = rec["to_street"];


                snprintf(vname, sizeof (vname), "from_city_%05d", i);
                out[vname] = rec["from_city"];
                snprintf(vname, sizeof (vname), "to_city_%05d", i);
                out[vname] = rec["to_city"];

                snprintf(vname, sizeof (vname), "from_state_%05d", i);
                out[vname] = rec["from_state"];
                snprintf(vname, sizeof (vname), "to_state_%05d", i);
                out[vname] = rec["to_state"];

                snprintf(vname, sizeof (vname), "from_zipcode_%05d", i);
                out[vname] = rec["from_zipcode"];
                snprintf(vname, sizeof (vname), "to_zipcode_%05d", i);
                out[vname] = rec["to_zipcode"];

                snprintf(vname, sizeof (vname), "flags_%05d", i);
                out[vname] = rec["flags"];

                snprintf(vname, sizeof (vname), "from_phone_%05d", i);
                out[vname] = rec["from_phone"]; //"20.7 ml";		
                
                snprintf(vname, sizeof (vname), "distance_%05d", i);
                out[vname] = rec["miles"]; //"20.7 ml";							

                snprintf(vname, sizeof (vname), "transport_%05d", i);
                out[vname] = rec["transport"]; //"20.7 ml";				

                snprintf(vname, sizeof (vname), "translate_%05d", i);
                out[vname] = rec["translate"]; //"20.7 ml";		
                
                snprintf(vname, sizeof (vname), "transport_time_%05d", i);
                out[vname] = rec["transport_time"];
               
                snprintf(vname, sizeof (vname), "lang_code_%05d", i);
                out[vname] = rec["lang_code"]; //
                                               
                snprintf(vname, sizeof (vname), "status_%05d", i);
                out[vname] = rec["status"]; //
                                               
                snprintf(vname, sizeof (vname), "assign_date_%05d", i);
                out[vname] = rec["assign_date"]; //
                
                snprintf(vname, sizeof (vname), "dist_mode_%05d", i);
                out[vname] = rec["dist_mode"]; //
                
                        
                        
            }
        }
        ret = PHP_API_OK;
        out["status"] = "OK";
    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long medTMemberLogin(CVars in, CVars &out)");
    }
    return ret;
}

long TranspLoginAdmin(CVars in, CVars &out) {
    long ret = PHP_API_BAD_PARAM;

    try {
        REQ_STRING(in, "username", 2, 49, "(.*)");
        REQ_STRING(in, "password", 2, 49, "(.*)");
        const char * loc = getenv("REMOTE_ADDR");
        ret = load_db_record(out, "SELECT UPPER( md5( now()::text) ) AS sessionid,m.*,m.id AS agent_id FROM agents m \
			            	            WHERE m.status=1 AND LOWER(m.username)=LOWER('%s') AND m.password= md5('%s')", in["username"].c_str(), in["password"].c_str());
        if (ret && out["agent_id"].Long() > 0) {
            // remove all existing session
            
            if ( out["folder"].length() < 5 ) // backward compatibility - you all need folder path
            {
                 char folder[15] = "";
                 GenerateAgentFolder(out["agent_id"].Long(), folder, sizeof (folder)); 
            }
            pgsql_exec("DELETE FROM agents_session WHERE agent_id=%ld ", out["agent_id"].Long());
            // Create New Session Now
            if (TransporterSessionCheck(out["id"].Long(), out["sessionid"].c_str(), 1) > 0) {


                out["stauts"] = "OK";
                /*LOAD THE SESSION INTO OUT now */
                load_db_record(out, "SELECT session FROM agents_session WHERE agent_id=%lu ORDER BY id DESC LIMIT 1", out["agent_id"].Long());
                //===============================================================================================================================
                pgsql_query("UPDATE agents SET last_login = now() WHERE id  = %lu", out["agent_id"].Long());
                // account_email(ACCOUNT_LOGIN_ALERT,out,out); // ALERT CUSTOMER OF LOGIN
                ret = PHP_LOGIN_OK;
            } else {
                out["status"] = "Session check failed";
            }
        } else {
            out["status_message"] = "Invalid Username/Password";
        }

    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long medTMemberLogin(CVars in, CVars &out)");
    }
    return ret;
}

long TransporterSessionCheck(long uid, const char *sessionid, int create) {
    logfmt(logINFO, "long TransporterSessionCheck(long uid, const char *sessionid, int create )");
    // Sanity check
    if (uid < 1 || sessionid == NULL || strlen(sessionid) < 4) {
        return -1L; // Invalif parameters
    }

    if (create == 1) // Clean Previous session by force
    {
        pgsql_exec("DELETE FROM agents_session WHERE agent_id=%ld", uid);
    }

    long session_expired_minutes = 15; // load in the global 

    // Clean old sessions
    pgsql_exec("DELETE FROM agents_session WHERE agent_id=%ld AND updated < (now() - interval '%lu minutes')", uid, session_expired_minutes);
    // Update/check existing session
    if (create == 0) {

        pgsql_exec("UPDATE agents_session SET updated=NOW() WHERE agent_id=%ld AND session='%s'", uid, sessionid);

        const PGresult *res = pgsql_query("SELECT * FROM agents_session WHERE agent_id=%ld AND session='%s'", uid, sessionid);
        if (res != NULL && pgsql_num_rows(res) > 0) {
            logfmt(logINFO, "VALID SESSION *****");
            return 1L; // Session updated
        } else {
            logfmt(logINFO, "INVALID SESSION *****");
            //INVALID SESSION DETECTED
            return -1L; // Invalid parameters
        }
    }




    if (create > 0) {
        // Check session i?
        const PGresult *res = pgsql_query("SELECT * FROM agents_session WHERE agent_id=%ld AND session<>'%s'", uid, sessionid);
        if (res != NULL && pgsql_num_rows(res) > 0) {
            return -2L; // Active sessions found
        }
        CVars sess; // Do we have the same session already?
        if (load_db_record(sess, "SELECT * FROM agents_session WHERE agent_id=%lu AND session='%s'", uid, sessionid) > 0) {
            pgsql_exec("UPDATE agents_session SET updated=NOW() WHERE agent_id=%lu AND session='%s'", uid, sessionid);
            return sess["id"].Long();
        }
        // Create a new session
        const char * loc = getenv("REMOTE_ADDR");
        sess["loc"] = loc;
        sess["loc"].set_valid(true);
        sess["agent_id"] = uid;
        sess["agent_id"].set_valid(true);
        sess["session"] = sessionid;
        sess["session"].set_valid(true);
        long sid = insert_db_record(DBS_VALID, "agents_session", "agents_session_id_seq", sess); //agents_session_id_seq
        if (sid > 0) {
            return sid; // New session created
        }
        return -3L; // Failed to create new session
    }
    logfmt(logINFO, "/long TransporterSessionCheck(long uid, const char *sessionid, int create )");
    return 0L; // No route
}

long medTrCreateAgent(CVars in, CVars &out) {
    long ret = PHP_API_BAD_PARAM;
    logfmt(logINFO, "medTrCreateAgent()");


    try {
        REQ_LONG(in, "pid", 0, -1);
        long agent_type = REQ_LONG(in, "agent_type", 0, -1);
        REQ_STRING(in, "agent_name", 2, 49, "(.*)");
        REQ_STRING(in, "firstname", 2, 49, "(.*)");
        REQ_STRING(in, "lastname", 2, 49, "(.*)");

        REQ_STRING(in, "username", 2, 49, "(.*)"); // note same as email
        REQ_STRING(in, "password", 2, 15, "(.*)");

        OPTIONAL(in, "login") REQ_LONG(in, "login", 0, -1);

        REQ_STRING(in, "street", 5, 49, "(.*)");
        REQ_STRING(in, "zipcode", 3, 12, "(.*)");
        OPTIONAL(in, "phone") REQ_STRING(in, "phone", 5, 23, "(.*)");
        REQ_STRING(in, "city", 5, 49, "(.*)");
        REQ_STRING(in, "state", 2, 25, "(.*)");


        //REQ_STRING (in, "sessionid",  4, 40, "(.*)");
        const char * loc = getenv("REMOTE_ADDR");

        long login = 0 + in["login"].Long();

        ret = load_db_record(out, "SELECT *,id AS agent_id FROM agents WHERE lower(username)=lower('%s') ", in["username"].c_str());
        logfmt(logINFO, "~~~~ **** medTrCreateAgent(CVars in, CVars &out) ret==>%d", ret);

        if (ret <= 0 && agent_type > 0) {

            // LETS CREATE THE REQUEST NOW
            CVars x;
            x["pid"] = in["pid"];
            x["pid"].set_valid(true);
           // x["agent_type"] = in["agent_type"];
           // x["agent_type"].set_valid(true);

            x["username"] = in["username"];
            x["username"].set_valid(true);
            x["email"] = in["username"];
            x["email"].set_valid(true);
            x["firstname"] = in["firstname"];
            x["firstname"].set_valid(true);
            x["lastname"] = in["lastname"];
            x["lastname"].set_valid(true);
            x["street"] = in["street"];
            x["street"].set_valid(true);
            x["agent_name"] = in["agent_name"];
            x["agent_name"].set_valid(true);

            x["zipcode"] = in["zipcode"];
            x["zipcode"].set_valid(true);
            x["phone"] = in["phone"];
            x["phone"].set_valid(true);
            x["city"] = in["city"];
            x["city"].set_valid(true);

            x["state"] = in["state"];
            x["state"].set_valid(true);

            x["loc"] = loc;
            x["loc"].set_valid(true);
            x["country"] = "US";
            x["country"].set_valid(true);

            out["agent_id"] = insert_db_record(DBS_VALID, "agents", "agents_id_seq", x);
            if (out["agent_id"].Long() > 0) {
                
                
                pgsql_query("UPDATE agents SET password = md5('%s') WHERE id  = %lu", in["password"].c_str(), out["agent_id"].Long()); // setting the password MD5 now
                
                char folder[15] = "";
                GenerateAgentFolder(out["agent_id"].Long(), folder, sizeof (folder)); // this stamp the offer code directly in that call
                
         /*      
                if (agent_type == 1 || agent_type == 3) {
                    CVars y;
                    y["street"] = in["street"];
                    y["street"].set_valid(true);
                    y["agent_id"] = out["agent_id"];
                    y["agent_id"].set_valid(true);

                    y["zipcode"] = in["zipcode"];
                    y["zipcode"].set_valid(true);
                    y["phone"] = in["phone"];
                    y["phone"].set_valid(true);
                    y["city"] = in["city"];
                    y["city"].set_valid(true);

                    y["state"] = in["state"];
                    y["state"].set_valid(true);

                    y["loc_name"] = "Main Branch";
                    y["loc_name"].set_valid(true);
                    
                    y["loc"] = loc;
                    y["loc"].set_valid(true);
                    y["country"] = "US";
                    y["country"].set_valid(true);

                    out["agent_id"] = insert_db_record(DBS_VALID, "transporter", "transporter_id_seq", y);
                }
*/
                // Now Send Email

                // ==============
            }


            if (login == 1) {
                return TranspLoginAdmin(in, out);
            } else {
                load_db_record(out, "SELECT *,id AS agent_id FROM agents WHERE id= %lu ", out["agent_id"].Long());
            }
        }

    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long medTrCreateAgent(CVars in, CVars &out)");
    }


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


void GenerateSeed( char * number, int sz, int seed )
{
        char chars[] = "WRB01234WRB56789WRB";
        int n = sizeof(chars);
        int i;
	srand((unsigned int)time((time_t *)NULL));

        for ( i=0; i<10 && i<sz-1; i++ )
        {
		int k = seed%n;
		if (seed>1 && k==i) {
			number[i] = chars[n-k];
		} else {
                	number[i] = chars[ (int) ( (n-1.0)*rand()/(RAND_MAX+1.0)) ];
		}
        }
        number[i] = 0;
}

void GenerateAgentFolder(unsigned long agent_id, char * number, int sz) {
    SQLINTEGER rows;
    char confirmation[11];
    // Lock table to prevent generation of two identical confirmation numbers
    pgsql_exec("BEGIN; LOCK TABLE agents;");
    int i = 0;
    do {
        GenerateSeed(confirmation, sizeof (confirmation), i++);
        //st.CloseCursor();
        rows = 0;
        const PGresult *res = pgsql_query("SELECT id FROM agents WHERE folder='%s'", confirmation);
        if (res != NULL) {
            rows = pgsql_num_rows(res);
        }
    } while (rows > 0 && i < 100);

    pgsql_exec("UPDATE agents SET folder='%s' WHERE id=%lu AND folder IS NULL", confirmation, agent_id);
    pgsql_exec("COMMIT"); // Unlock table 'data'
    strsafecpy(number, confirmation, sz);
}


long medTrCreateIntepreter(CVars in, CVars &out) {
    long ret = PHP_API_BAD_PARAM;
    logfmt(logINFO, "medTrCreateIntepreter()");

    try {
        REQ_LONG(in, "pid", 0, -1);

        long agent_id = REQ_LONG(in, "agent_id", 0, -1);
        REQ_STRING(in, "firstname", 2, 49, "(.*)");
        REQ_STRING(in, "lastname", 2, 49, "(.*)");
        REQ_STRING(in, "email", 5, 49, "(.*)");
        REQ_STRING(in, "street", 5, 49, "(.*)");
        REQ_STRING(in, "zipcode", 3, 12, "(.*)");
        OPTIONAL(in, "phone") REQ_STRING(in, "phone", 5, 23, "(.*)");
        REQ_STRING(in, "city", 5, 49, "(.*)");
        REQ_STRING(in, "state", 2, 25, "(.*)");
        const char * loc = getenv("REMOTE_ADDR");

        long login = 0 + in["login"].Long();

        ret = load_db_record(out, "SELECT *,id AS agent_id FROM agents WHERE id=%lu AND agent_type IN (2,3) ", agent_id);
        logfmt(logINFO, "~~~~ **** medTrCreateIntepreter(CVars in, CVars &out) ret==>%d", ret);

        if (ret > 0) {

            // LETS CREATE THE REQUEST NOW
            CVars x;
            x["pid"] = in["pid"];
            x["pid"].set_valid(true);

            x["agent_id"] = in["agent_id"];
            x["agent_id"].set_valid(true);

            x["email"] = in["email"];
            x["email"].set_valid(true);

            x["firstname"] = in["firstname"];
            x["firstname"].set_valid(true);

            x["lastname"] = in["lastname"];
            x["lastname"].set_valid(true);

            x["street"] = in["street"];
            x["street"].set_valid(true);

            x["zipcode"] = in["zipcode"];
            x["zipcode"].set_valid(true);

            x["phone"] = in["phone"];
            x["phone"].set_valid(true);

            x["city"] = in["city"];
            x["city"].set_valid(true);

            x["state"] = in["state"];
            x["state"].set_valid(true);

            x["loc"] = loc;
            x["loc"].set_valid(true);
            x["country"] = "US";
            x["country"].set_valid(true);

            out["interpreter_id"] = insert_db_record(DBS_VALID, "interpreters", "interpreters_id_seq", x);
            if (out["interpreter_id"].Long() > 0) {
                ret = PHP_API_OK;
                load_db_record(out, "SELECT *,id AS interpreter_id FROM interpreters WHERE id= %lu ", out["interpreter_id"].Long());
                agent_email_calls(in["action"].Long(), in, out);
            }
        } else {
            ret = PHP_API_BAD_PARAM;
            out["status"] = "Invalid or wrong agent type";
        }

    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long medTrCreateIntepreter(CVars in, CVars &out)");
    }


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

long medTrLoadIntepreter(CVars in, CVars &out) {
    long ret = PHP_API_BAD_PARAM;
    logfmt(logINFO, "medTrLoadItepreter()");

    try {
        REQ_LONG(in, "pid", 0, -1);

        long agent_id = REQ_LONG(in, "agent_id", 0, -1);
        long interpreter_id = REQ_LONG(in, "interpreter_id", 0, -1);

        ret = load_db_record(out, "SELECT *,id AS interpreter_id FROM interpreters WHERE id=%lu AND agent_id=%lu",interpreter_id, agent_id);
        logfmt(logINFO, "~~~~ **** medTrLoadItepreter(CVars in, CVars &out) ret==>%d", ret);

        if (ret > 0) {
            if (interpreter_id>0) {
                load_db_record(out, "SELECT *,id AS interpreter_id FROM interpreters WHERE id= %lu ", interpreter_id);
                if (out["interpreter_id"].Long()>0) {
                    ret = PHP_API_OK;
                    out["status"] = "1";
                } else {
                    out["status"] = "Interpreter is not found";
                }
            } else {
                out["status"] = "Invalid interpreter id";
            }
        } else {
            out["status"] = "Invalid or wrong agent type";
        }

    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long medTrLoadItepreter(CVars in, CVars &out)");
        out["status"] = "Invalid input";
    }

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

long medTrUpdateTransporter(CVars in, CVars &out) {
   long ret = PHP_API_BAD_PARAM;
    
    try {

        REQ_LONG(in, "pid", 0, -1);
        long agent_id = REQ_LONG(in, "agent_id", 0, -1);
        long transporter_id = REQ_LONG(in, "transporter_id", 0, -1);

        REQ_STRING(in, "loc_name", 2, 49, "(.*)");
        REQ_STRING(in, "street", 5, 49, "(.*)");
        REQ_STRING(in, "zipcode", 3, 12, "(.*)");
        OPTIONAL(in, "phone") REQ_STRING(in, "phone", 5, 23, "(.*)");
        REQ_STRING(in, "city", 5, 49, "(.*)");
        REQ_STRING(in, "state", 2, 25, "(.*)");
        const char * loc = getenv("REMOTE_ADDR");
        /*
         jubabox=> select * from transporter;
 id | pid | agent_id |            street             |      city      | zipcode |   phone    | state | country |           added            |       loc       |       loc_name        
----+-----+----------+-------------------------------+----------------+---------+------------+-------+---------+----------------------------+-----------------+-----------------------
 32 | 100 |       28 | 10641 Barberry Drive          | Hampton        | 30228   |            | GA    | US      | 2017-10-13 13:43:31.098596 | 104.238.127.115 | My ayodeji Branch
  1 |     |        1 | 2085 market place blvd        | cumming        | 30041   |            | GA    | US      | 2017-10-05 14:30:43.106611 | 104.238.127.115 | cumming Branch
  2 |     |        3 | 5025 jimmy lee smith pkwy     | hiram          | 30141   |            | GA    | US      | 2017-10-05 14:45:11.384743 | 104.238.127.115 | hiram Branch
  3 |     |        4 | 3205 woodward crossing blvd   | buford         | 30519   |            | GA    | US      | 2017-10-05 14:49:38.489784 | 104.238.127.115 | buford Branch
  4 |     |        5 | 2085 market place blvd        | cumming        | 30041   |            | GA    | US      | 2017-10-05 14:50:04.443468 | 104.238.127.115 | cumming Branch

         */

         ret = load_db_record(out, "SELECT *,id AS transporter_id FROM transporter WHERE id=%lu AND agent_id=%lu",transporter_id, agent_id);
        logfmt(logINFO, "~~~~ **** medTrUpdateItepreter(CVars in, CVars &out) ret==>%d", ret);

                if (ret > 0) {

            // LETS CREATE THE REQUEST NOW
            CVars x;
            x["pid"] = in["pid"];
            x["pid"].set_valid(true);

          //  x["agent_id"] = in["agent_id"];
        //    x["agent_id"].set_valid(true);

            x["loc_name"] = in["loc_name"];
            x["loc_name"].set_valid(true);

            x["street"] = in["street"];
            x["street"].set_valid(true);

            x["zipcode"] = in["zipcode"];
            x["zipcode"].set_valid(true);

            x["phone"] = in["phone"];
            x["phone"].set_valid(true);

            x["city"] = in["city"];
            x["city"].set_valid(true);

            x["state"] = in["state"];
            x["state"].set_valid(true);

            x["loc"] = loc;
            x["loc"].set_valid(true);
            
            x["country"] = "US";
            x["country"].set_valid(true);
          
            
            update_db_record( DBS_VALID, "transporter", x, in["transporter_id"].Long());
            if (1) {
                ret = PHP_API_OK;
                load_db_record(out, "SELECT *,id AS transporter_id FROM transporter WHERE id= %lu ", out["transporter_id"].Long());
                agent_email_calls(in["action"].Long(), in, out);
            }
        } else {
            ret = PHP_API_BAD_PARAM;
            out["status"] = "Invalid or wrong agent type";
        }

    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long medTrUpdateTransporter(CVars in, CVars &out)");
    }


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

long medTrUpdateItepreter(CVars in, CVars &out) {
    long ret = PHP_API_BAD_PARAM;
    logfmt(logINFO, "medTrUpdateItepreter()");

    try {
        REQ_LONG(in, "pid", 0, -1);

        long agent_id = REQ_LONG(in, "agent_id", 0, -1);
        long interpreter_id = REQ_LONG(in, "interpreter_id", 0, -1);
        REQ_STRING(in, "firstname", 2, 49, "(.*)");
        REQ_STRING(in, "lastname", 2, 49, "(.*)");
        REQ_STRING(in, "email", 5, 49, "(.*)");
        REQ_STRING(in, "street", 5, 49, "(.*)");
        REQ_STRING(in, "zipcode", 3, 12, "(.*)");
        OPTIONAL(in, "phone") REQ_STRING(in, "phone", 5, 23, "(.*)");
        REQ_STRING(in, "city", 5, 49, "(.*)");
        REQ_STRING(in, "state", 2, 25, "(.*)");
        const char * loc = getenv("REMOTE_ADDR");

        long login = 0 + in["login"].Long();

        ret = load_db_record(out, "SELECT *,id AS interpreter_id FROM interpreters WHERE id=%lu AND agent_id=%lu",interpreter_id, agent_id);
        logfmt(logINFO, "~~~~ **** medTrUpdateItepreter(CVars in, CVars &out) ret==>%d", ret);

        if (ret > 0) {

            // LETS CREATE THE REQUEST NOW
            CVars x;
            x["pid"] = in["pid"];
            x["pid"].set_valid(true);

          //  x["agent_id"] = in["agent_id"];
        //    x["agent_id"].set_valid(true);

            x["email"] = in["email"];
            x["email"].set_valid(true);

            x["firstname"] = in["firstname"];
            x["firstname"].set_valid(true);

            x["lastname"] = in["lastname"];
            x["lastname"].set_valid(true);

            x["street"] = in["street"];
            x["street"].set_valid(true);

            x["zipcode"] = in["zipcode"];
            x["zipcode"].set_valid(true);

            x["phone"] = in["phone"];
            x["phone"].set_valid(true);

            x["city"] = in["city"];
            x["city"].set_valid(true);

            x["state"] = in["state"];
            x["state"].set_valid(true);

            x["loc"] = loc;
            x["loc"].set_valid(true);
            x["country"] = "US";
            x["country"].set_valid(true);

           
            
            update_db_record( DBS_VALID, "interpreters", x, in["interpreter_id"].Long());
            if (1) {
                ret = PHP_API_OK;
                load_db_record(out, "SELECT *,id AS interpreter_id FROM interpreters WHERE id= %lu ", out["interpreter_id"].Long());
                agent_email_calls(in["action"].Long(), in, out);
            }
        } else {
            ret = PHP_API_BAD_PARAM;
            out["status"] = "Invalid or wrong agent type";
        }

    } catch (bad_parameter) {
        logfmt(logINFO, "ERROR CALL long medTrUpdateItepreter(CVars in, CVars &out)");
    }


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