// Combo Data
#include "clog.h"
#include "cgi.h"
#include "input.h"
#include "wrenchboard_api.h"
#include "sel_data.h"
#include "email.h"
#include "payments.h"
#include "safestring.h"
#include <string>
#include "pgsql.h"
#include "pgsql_wrapper.h"
#include "cfg.h"
#include <curl/curl.h>


long  getJobPriceCombo( CVars in, CVars &out )
{

/*      $this->defaultComboMessage ='Select Price...';
    $cmbstr ="<option value='0'>". $this->defaultComboMessage ."</option>";
    for ($i = 2000; $i <= 10000; $i = $i + 1000) {
      //  $amountArray[] = [$i,$i,'Naira'];
        $selV = '';
        if ($curVal == $i) {
            $selV = " selected ";
        }
        $cmbstr .= "<option value='".$i."' ".$selV.">".$i." ".$description."</option>";
    }
*/
   return 0;
}

long getJobPostDuration( CVars in, CVars &out )
{
/*
    $this->defaultComboMessage ='Select duration of post';
    $cmbstr ="";
    for ($ii = 2; $ii <= 10; $ii++) {

        $cmbstr .= "<option value='$ii'>$ii day(s)</option>";
    }

    for ($ii = 2; $ii <= 5; $ii++) {
        $days_c = $ii*7;
        $cmbstr .= "<option value='$days_c'>$ii Weeks</option>";
    }
    */
    return 0;
}

long getUserJobGroupCombo( CVars in, CVars &out )
{
  /*
    //  $sql = "SELECT id,group_name FROM members_job_group WHERE member_id = $member_id  AND status = 1 ORDER BY group_name ASC ";
    $sql = "SELECT m.id,m.group_name, m.group_name||' ['||count(g.group_id)||' members]' AS member_group_count "
            . "FROM members_job_group m "
            . "LEFT JOIN members_job_groupmember g ON g.group_id = m.id "
            . "WHERE m.member_id = $member_id  "
            . "AND m.status = 1 GROUP BY m.id,m.group_name ORDER BY m.group_name ASC";

    $q = $this->db->query($sql);
    $option_value = $this->optionValueObject($q->result(), "id", "member_group_count", $curVal);
    */
    return 0;
}

long getUserRecipientCombo( CVars in, CVars &out ){

      char vname[30];
        long ret = PHP_API_BAD_PARAM;

        logfmt( logINFO, "getUserRecipientCombo()" );
        out["result"] = "Yes i go to this back end";

        try {
            long member_id = REQ_LONG( in, "member_id", 1, -1 );
            long limit = REQ_LONG( in, "limit", 1, -1 );
            REQ_LONG( in, "page", 1, -1 );
            REQ_STRING (in, "uid",   10, 100, "(.*)");

            out["total_record"] = "0";
            const PGresult *res;
            res = pgsql_query("SELECT b.id,b.firstname||' '||b.lastname||' '||b.account_no||' '||k.name AS recipient, "
                                " b.account_no,b.country,b.added,b.uid AS recipient_uid ,b.uid ,k.name AS bank_name,k.uid AS bank_uid "
                                "FROM sendmoney_recipient b "
                                "LEFT JOIN bank_entity_codes k ON k.code=b.bank_code "
                                "WHERE b.member_id = %lu AND b.status=1",member_id);

            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), "recipient_%05d", i);
                    out[vname] = rec["recipient"];

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

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

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

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

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

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

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

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

                    snprintf(vname, sizeof (vname), "bank_uid_%05d", i);
                    out[vname] = rec["bank_uid"];
                }
            }
            ret = PHP_API_OK;
            out["status"] = "OK";
        } catch (bad_parameter) {
            logfmt(logINFO, "ERROR CALL long getUserRecipientCombo(CVars in, CVars &out)");
        }

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

long getAccountTypeCombo( CVars in, CVars &out ) {
/*       $q = $this
            ->db
            ->where('status', 1)
            ->order_by('type_name', 'ASC')
            ->get('account_types');

    $option_value = $this->optionValueObject($q->result(), "type_value", "type_name", $curVal);
    SELECT id,type_name,type_value FROM account_types  WHERE status = 1 ORDER BY type_name ASC
*/
           char vname[30];
            long ret = PHP_API_BAD_PARAM;

              logfmt( logINFO, "getAccountTypeCombo()" );
              out["result"] = "Yes i go to this back end";

              try {
                  out["total_record"] = "0";
                  const PGresult *res;
                  res = pgsql_query("SELECT id,type_name,type_value "
                                      "FROM account_types  "
                                      "WHERE status = 1 ORDER BY type_name ASC");

                  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), "value_%05d", i);
                          out[vname] = rec["type_value"];

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

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

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

long getGeneralSkillCombo( CVars in, CVars &out ) {
/*     $this->showDefaultSelect = false;
    $q = $this
            ->db
            ->where('status', 1)
            ->order_by('lorder', 'DESC')
            ->get('skill_category');
    $option_value = $this->optionValueObject($q->result(), "id", "category", $curVal);
*/
        char vname[30];
         long ret = PHP_API_BAD_PARAM;

           logfmt( logINFO, "getGeneralSkillCombo()" );
           out["result"] = "Yes i go to this back end";

           try {
               out["total_record"] = "0";
               const PGresult *res;
               res = pgsql_query("SELECT id,category "
                                   "FROM skill_category  "
                                   "WHERE status = 1 ORDER BY lorder DESC");

               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), "category_%05d", i);
                       out[vname] = rec["category"];

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

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

long getUserNewSkillCombo( CVars in, CVars &out ) {
/*    $q = $this
            ->db
            ->where('status', 1)
            ->where('category_id', $category_id)
            ->order_by('lorder', 'DESC')
            ->get('skill_types');
    $option_value = $this->optionValueObject($q->result(), "id", "skill", $curVal);
*/

      char vname[30];
      long ret = PHP_API_BAD_PARAM;

        logfmt( logINFO, "getUserNewSkillCombo()" );
        out["result"] = "Yes i go to this back end";

        try {
            long category_id = REQ_LONG( in, "category_id", 1, -1 );
            out["total_record"] = "0";
            const PGresult *res;
            res = pgsql_query("SELECT id,skill "
                                "FROM skill_types  "
                                "WHERE category_id = %lu AND status = 1 ORDER BY lorder DESC", category_id);

            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), "skill_%05d", i);
                    out[vname] = rec["skill"];

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

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

long getCountryCombo( CVars in, CVars &out ) {
/*     $q = $this
            ->db
            ->where('status', 1)
            ->order_by('country', 'ASC')
            ->get('country');

    $option_value = $this->optionValueObject($q->result(), "code", "country", $curVal);
    id| code |country| phone_code |allow| status |created|updated|jobs
*/

      char vname[30];
      long ret = PHP_API_BAD_PARAM;

        logfmt( logINFO, "getCountryCombo()" );
        out["result"] = "Yes i go to this back end";

        try {
            out["total_record"] = "0";
            const PGresult *res;
            res = pgsql_query("SELECT * "
                                "FROM country  "
                                "WHERE status = 1 ORDER BY country ASC");

            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), "code_%05d", i);
                    out[vname] = rec["code"];

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

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

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

                    snprintf(vname, sizeof (vname), "created_%05d", i);
                    out[vname] = rec["created"];
                }
            }
            ret = PHP_API_OK;
            out["status"] = "OK";
        } catch (bad_parameter) {
            logfmt(logINFO, "ERROR CALL long getCountryCombo(CVars in, CVars &out)");
        }

        logfmt( logINFO, "/getCountryCombo()" );
        return ret;
    return 0;
}

long getCountryJobCombo( CVars in, CVars &out ) {
 /*   $q = $this
        ->db
        ->where('status', 1)
        ->where('jobs is NOT NULL', NULL, FALSE)
        ->order_by('country', 'ASC')
        ->get('country');

    $option_value = $this->optionValueObject($q->result(), "code", "country", $curVal);
*/
    char vname[30];
      long ret = PHP_API_BAD_PARAM;

        logfmt( logINFO, "getCountryJobCombo()" );
        out["result"] = "Yes i go to this back end";

        try {
            out["total_record"] = "0";
            const PGresult *res;
            res = pgsql_query("SELECT * "
                                "FROM country  "
                                "WHERE jobs IS NOT NULL ORDER BY country ASC");

            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), "code_%05d", i);
                    out[vname] = rec["code"];

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

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

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

                    snprintf(vname, sizeof (vname), "created_%05d", i);
                    out[vname] = rec["created"];
                }
            }
            ret = PHP_API_OK;
            out["status"] = "OK";
        } catch (bad_parameter) {
            logfmt(logINFO, "ERROR CALL long getCountryCombo(CVars in, CVars &out)");
        }

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

long getBankCombo( CVars in, CVars &out ) {
    /*
      wrenchboard=> Select code,name from bank_entity_codes WHERE country ='NG' ORDER BY name ASC;
      code |                      name
      ------+------------------------------------------------
      044  | Access Bank
      014  | Afri Bank
      023  | Citi Bank
      063  | Diamond Bank
      050  | Ecobank
      040  | Equitorial Trust Bank
     */

 /*   $q = $this
            ->db
            ->where('status', 1)
            ->order_by('name', 'ASC')
            ->get('bank_entity_codes');

    $option_value = $this->optionValueObject($q->result(), "code", "name", $curVal);
*/
      char vname[30];
      char extra_filter[60];
      long ret = PHP_API_BAD_PARAM;

        logfmt( logINFO, "getBankCombo()" );
        out["result"] = "Yes i go to this back end";

        try {
            OPTIONAL(in, "country")  REQ_STRING(in, "country", 2, 5, "(.*)");

            if (in["country"]!=""){
               snprintf(extra_filter, sizeof (extra_filter), " AND country ='%s' ", in["country"].c_str());
            }
            else{
                snprintf(extra_filter, sizeof (extra_filter), " "); // make empty
            }


            out["total_record"] = "0";
            const PGresult *res;
            res = pgsql_query("SELECT code,name,uid,country "
                                "FROM bank_entity_codes  "
                                "WHERE status = 1 %s ORDER BY name ASC",extra_filter);

            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), "code_%05d", i);
                    out[vname] = rec["code"];

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

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

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

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

        logfmt( logINFO, "/getBankCombo()" );
        return ret;
}
/*
wrenchboard=> SELECT * FROM bank_entity_codes;
 country | channel |                      name                      | code | gid | status | bank_codes |                 uid
---------+---------+------------------------------------------------+------+-----+--------+------------+--------------------------------------
 NG      | INTERSW | Access Bank                                    | 044  |   1 |      1 | ACC044     | a2c6c1a2-5cf2-4396-8630-50c47da15316
 NG      | INTERSW | Afri Bank                                      | 014  |   2 |      1 | AFR014     | 5526cfce-3395-4375-9c43-7b6e94843701
 NG      | INTERSW | Citi Bank                                      | 023  |   3 |      1 | CIT023     | a988b775-0fe7-4180-a7d9-d3492efe812d
 NG      | INTERSW | Diamond Bank                                   | 063  |   4 |      1 | DIA063     | 3c8fc9a2-95d8-4cb9-811b-6eac9cac45c9
*/
