<?php

class Combo_model extends CI_Model {

    var $optCons = '';
    var $currentStyle = 'form-control select';
    var $readOnlyMode = false;
    var $defaultComboMessage = 'Select...';
    var $showDefaultSelect = true;

    function __construct() {
        
    }

        public function getProvidersListTypeCombo($option_name, $curVal) {
        $sql = "SELECT id,firstname||' '||lastname||'-'||email AS  driver_name FROM providers WHERE status=1 ORDER BY ID ASC ";
        $q = $this->db->query($sql);
        $option_value = $this->optionValueObject($q->result(), "id", "driver_name", $curVal);
        return $this->comboFrame($option_name, $option_value);
    }

    public function getJobPostDuration($option_name, $duration) {
        $this->defaultComboMessage = 'Select duration of post';
        $cmbstr = "";
        for ($ii = 2; $ii <= 10; $ii++) {

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

        return $this->comboFrame($option_name, $cmbstr);
    }

    public function getUserAccountTypeCombo($option_name, $curVal) {
        $sql = "SELECT id,type_name FROM members_acc_types WHERE status=1 ORDER BY ID ASC ";
        $q = $this->db->query($sql);
        $option_value = $this->optionValueObject($q->result(), "id", "type_name", $curVal);
        return $this->comboFrame($option_name, $option_value);
    }

    public function getUserJobGroupCombo($option_name, $member_id, $curVal) {

//  $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 $this->comboFrame($option_name, $option_value);
    }

    public function getUserRecipientCombo($option_name, $member_id, $curVal) {

        $sql = "SELECT b.id,b.firstname||' '||b.lastname||' '||b.account_no||' '||k.name AS recipient "
                . "FROM sendmoney_recipient b "
                . "LEFT JOIN bank_entity_codes k ON k.code=b.bank_code "
                . "WHERE b.member_id = $member_id AND b.status=1";

        $q = $this->db->query($sql);
        $option_value = $this->optionValueObject($q->result(), "id", "recipient", $curVal);
        return $this->comboFrame($option_name, $option_value);
    }

    public function getAccountTypeCombo($option_name, $curVal) {
        $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);

        return $this->comboFrame($option_name, $option_value);
    }

    public function getGeneralSkillCombo($option_name, $curVal) {
        $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);

        return $this->comboFrame($option_name, $option_value);
    }

    public function getUserNewSkillCombo($option_name, $curVal, $category_id, $member_id) {
        $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);

        return $this->comboFrame($option_name, $option_value);
    }

    public function setReadOnly($bl) {
        $this->readOnlyMode = $bl;
    }

    public function getCountryCombo($option_name, $curVal) {
        $q = $this
                ->db
                ->where('status', 1)
                ->order_by('country', 'ASC')
                ->get('country');

        $option_value = $this->optionValueObject($q->result(), "code", "country", $curVal);

        return $this->comboFrame($option_name, $option_value);
    }

    public function getBankCombo($option_name, $curVal) {
        /*
          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);

        return $this->comboFrame($option_name, $option_value);
    }

    public function geReasonCombo($rkey, $option_name, $curVal) {
        /*
          utransport=> SELECT * FROM reason_items WHERE rkey='AREASON';
          id | rkey | lkey| name | weight | lorder | created
          ----+------+------+--------+--------+---------
         */
        $q = $this
                ->db
                ->where('rkey', $rkey)
                ->order_by('name', 'ASC')
                ->get('reason_items');

        $option_value = $this->optionValueObject($q->result(), "lkey", "name", $curVal);
        return $this->comboFrame($option_name, $option_value);
    }

    private function optionValueObject($sdStd, $val, $valname, $curVal) {
        $this->optCons = '';
        if ($this->showDefaultSelect == true) {
            $this->optCons .= "<option value=''>" . $this->defaultComboMessage . "</option>";
        }


        foreach ($sdStd as $row) {
            $selV = '';
            if ($curVal == $row->$val) {
                $selV = " selected ";
            }
            $this->optCons .= "<option value='" . $row->$val . "' " . $selV . ">" . $row->$valname . "</option>";
        }
        return $this->optCons;
    }

    private function comboFrame($option_name, $option_value) {
        $addReaOnly = "";
        if ($this->readOnlyMode == true) {
            $addReaOnly = " disabled ";
        }

        return $cmb = "<select data-placeholder='" . $this->defaultComboMessage . "' class='" . $this->currentStyle . "' name='$option_name' $addReaOnly >$option_value</select>";
    }

    public function getStatesCombo($option_name, $curVal) {
        //  global $stateCodes;           
        $option_value = "";

        //  print_r($stateCodes);

        foreach ($this->stateCodes as $i => $srow) {
            $sel = "";

            if ($i == $curVal) {
                $sel = " selected ";
            }

            $option_value.="<option value='" . $i . "' " . $sel . ">" . $srow . "</option>";
        }

        return $this->comboFrame($option_name, $option_value);
    }

    var $stateCodes = array(
        "AK" => "Alaska",
        "AL" => "Alabama",
        "AZ" => "Arizona",
        "AR" => "Arkansas",
        "CA" => "California",
        "CO" => "Colorado",
        "CT" => "Connecticut",
        "DC" => "District of Columbia",
        "DE" => "Delaware",
        "FL" => "Florida",
        "GA" => "Georgia",
        "HI" => "Hawaii",
        "IA" => "Iowa",
        "ID" => "Idaho",
        "IL" => "Illinois",
        "IN" => "Indiana",
        "KS" => "Kansas",
        "KY" => "Kentucky",
        "LA" => "Louisiana",
        "ME" => "Maine",
        "MD" => "Maryland",
        "MA" => "Massachusetts",
        "MI" => "Michigan",
        "MN" => "Minnesota",
        "MO" => "Missouri",
        "MS" => "Mississippi",
        "MT" => "Montana",
        "NC" => "North Carolina",
        "ND" => "North Dakota",
        "NE" => "Nebraska",
        "NH" => "New Hampshire",
        "NJ" => "New Jersey",
        "NM" => "New Mexico",
        "NV" => "Nevada",
        "NY" => "New York",
        "OH" => "Ohio",
        "OK" => "Oklahoma",
        "OR" => "Oregon",
        "PA" => "Pennsylvania",
        "PR" => "Puerto Rico",
        "RI" => "Rhode Island",
        "SC" => "South Carolina",
        "SD" => "South Dakota",
        "TN" => "Tennessee",
        "TX" => "Texas",
        "UT" => "Utah",
        "VA" => "Virginia",
        "VI" => "Virgin Islands",
        "VT" => "Vermont",
        "WA" => "Washington",
        "WI" => "Wisconsin",
        "WV" => "West Virginia",
        "WY" => "Wyoming"
    );

}
