// Topup management toosl
#include "clog.h"
#include "cgi.h"
#include "input.h"
#include "mermsemr_api.h"
#include "safestring.h"
#include <string>
#include "pgsql.h"
#include "pgsql_wrapper.h"
#include <curl/curl.h>
/* -- */
#include "function_members.h"
#include "medTUpload.h"

#define AGENT_SITE_LOGO          10005
#define INTERPRETER_CERTIFICATE  20005

long medTRegisterUpload(CVars in);
long medTRegisterDownload(CVars in, CVars &out);

long medtrans_upload(CVars in, CVars &out)
{
    logfmt( logINFO, " **** +++ **** long medtrans_upload(CVars in, CVars &out)");
    long ret  = 0;
     long file_type = REQ_LONG( in, "file_type", 0, -1);
     
     if (file_type == INTERPRETER_CERTIFICATE)
     {
          logfmt( logINFO, " **** +++ **** long medtrans_upload This is interpreter language system");
          ret = medTRegisterUpload(in);
          if (ret > 0 )
          {
                pgsql_query("UPDATE interpreters_language SET document_id = %lu, document='Language Certificate' WHERE id  = %lu AND agent_id=%lu",ret, in["language_id"].Long(), in["agent_id"].Long()); // setting the password MD5 now 
          }
       
     }
     
    
    return 0;
}

long medtrans_download(CVars in, CVars &out)
{
    logfmt( logINFO, " **** +++ **** long medtrans_download(CVars in, CVars &out)");
    long ret  = 0;
    long file_type = REQ_LONG( in, "file_type", 0, -1);
     
    if (file_type == INTERPRETER_CERTIFICATE)
    {
         logfmt( logINFO, " **** +++ **** long medtrans_download This is interpreter language system");
         ret = medTRegisterDownload(in, out);       
    }   
    return ret;
}

long medTRegisterUpload(CVars in) {
    long ret = 0;
    try {
        REQ_LONG(in, "pid", 0, -1);
        REQ_STRING(in, "part", 2, 10, "(.*)");
        REQ_LONG(in, "file_type", 0, -1);
        REQ_STRING(in, "folder", 6, 15, "(.*)");
        REQ_LONG(in, "file_type", 0, -1);
        REQ_STRING(in, "extension", 1, 10, "(.*)");
        REQ_STRING(in, "file_name", 1, 50, "(.*)");
        const char * loc = getenv("REMOTE_ADDR");
        CVars x;
        x["pid"] = in["pid"];
        x["pid"].set_valid(true);
        x["part"] = in["part"];
        x["part"].set_valid(true);
        x["file_type"] = in["file_type"];
        x["file_type"].set_valid(true);
        x["folder"] = in["folder"];
        x["folder"].set_valid(true);
        x["file_type"] = in["file_type"];
        x["file_type"].set_valid(true);
        x["extension"] = in["extension"];
        x["extension"].set_valid(true);
        x["file_name"] = in["file_name"];
        x["file_name"].set_valid(true);
        x["loc"] = loc;
        x["loc"].set_valid(true);

        ret = insert_db_record(DBS_VALID, "uploads", "uploads_id_seq", x);

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

long medTRegisterDownload(CVars in, CVars &out) {
    long ret = PHP_API_BAD_PARAM;
    logfmt(FLOG_MAX, "long medTRegisterDownload(CVars in, CVars &out)");
    try {
        long pid = REQ_LONG(in, "pid", 1, -1);
        long id = REQ_LONG(in, "document_id", 1, -1);
        if ( load_db_record( out, "SELECT * FROM uploads WHERE id = %lu AND pid = %lu", id, pid) ) {
            ret = PHP_API_OK;
        }
    } catch (bad_parameter) {
        out["status"] = "Invalid input";
        logfmt(logINFO, "ERROR CALL long medTRegisterDownload(CVars in, CVars &out)");
    }
    logfmt(FLOG_MAX, "/long medTRegisterDownload(CVars in, CVars &out)");
    return ret;
}



/*
 * 
 * wwwadmin/upload/AGT/7W425W24W5/20005/jpg/59E2C24457874.jpg
 * 
  RET: action=4505
- 02:04:52.359 INFO [20012]:    RET: agent_id=4
- 02:04:52.359 INFO [20012]:    RET: extension=jpg
- 02:04:52.359 INFO [20012]:    RET: file_type=20005
- 02:04:52.359 INFO [20012]:    RET: language_id=17
- 02:04:52.359 INFO [20012]:    RET: pid=115
- 02:04:52.359 INFO [20012]:    RET: sesson_id=KKFKFDKDFKKFKDFK
- 02:04:52.359 INFO [20012]:    RET: upload_file_name=59E2C24457874.jpg
- 02:04:52.359 INFO [20012]:    RET: upload_file_path=/home/oameye/mermsemr/svrlayer/../wwwadmin/upload/AGT/7W425W24W5/20005/jpg/59E2C24457874.jpg
- 02:04:52.359 INFO [20012]:    RET: user=AGT

 *  
	CREATE TABLE uploads (
	id SERIAL,
	pid INT REFERENCES platform_user(pid),
	part VARCHAR(10),
	folder VARCHAR(15),
	file_type VARCHAR(50),
	extension VARCHAR(10),
	file_name VARCHAR(50),	
	added timestamp without time zone DEFAULT now(),	
	status INT DEFAULT 1,
	loc INET,
    primary key(id)
);

 */
