<?php
/*
 * COREGRADE
 */
class Combo_model extends CI_Model {

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

    function __construct() {
        
    }

    public function getCardList(){
        return 'Tokun';
    }
    public function getPageTemplate($option_name, $curVal) {
        $sql = "SELECT id, template_name FROM page_templates ORDER BY template_name ASC";
        $q = $this->db->query($sql);
        $option_value = $this->optionValueObject($q->result(), "id", "template_name", $curVal);
        return $this->comboFrame($option_name, $option_value);
    }

    
    
    
    
    
    
    
    public function getSurvelListGroupCombo($option_name, $curVal) {
        $sql = "SELECT id,title,status FROM main_cards WHERE button1_action = 'SURVEYA' AND status =  1 ORDER BY title";
        $q = $this->db->query($sql);
        $option_value = $this->optionValueObject($q->result(), "id", "title", $curVal);
        return $this->comboFrame($option_name, $option_value);
    }

    public function getfindMemberType($option_name, $curVal) {
        $yesno_array = [
            'email' => 'Email',
            'firstname' => 'Firstname',
            'lastname' => 'Lastname'
        ];
        $option_value = $this->optionValueArray($yesno_array, $curVal);
        return $this->comboFrame($option_name, $option_value);
    }

    public function getDescisionGroupCombo($option_name, $curVal) {
        $sql = "SELECT dkey,description FROM decision_group where status =1 ORDER by lorder ASC";
        $q = $this->db->query($sql);
        $option_value = $this->optionValueObject($q->result(), "dkey", "description", $curVal);
        return $this->comboFrame($option_name, $option_value);
    }

    public function getCardBehaveCombo($option_name, $curVal) {
        $sql = "SELECT * FROM card_behavior WHERE status=1 ";
        $q = $this->db->query($sql);
        $option_value = $this->optionValueObject($q->result(), "key", "name", $curVal);
        return $this->comboFrame($option_name, $option_value);
    }

    public function getCardTargetActionCombo($option_name, $curVal) {
        $sql = "SELECT * FROM card_action_target WHERE status=1 ";
        $q = $this->db->query($sql);
        $option_value = $this->optionValueObject($q->result(), "target_key", "description", $curVal);
        return $this->comboFrame($option_name, $option_value);
    }

    public function getCardCategoryCombo($option_name, $curVal, $fixCat = '') {
        if (trim($fixCat) == '') {
            $sql = "SELECT * FROM card_category WHERE status=1 AND special =0 ";
        } else {
            $sql = "SELECT * FROM card_category WHERE status=1 AND cat='$fixCat' ";
        }
        $q = $this->db->query($sql);
        $option_value = $this->optionValueObject($q->result(), "cat", "name", $curVal);
        return $this->comboFrame($option_name, $option_value);
    }

    public function getStatusCombo($option_name, $curVal) {
        $yesno_array = [
            '0' => 'Not-Active',
            '1' => 'Active'
        ];
        $option_value = $this->optionValueArray($yesno_array, $curVal);
        return $this->comboFrame($option_name, $option_value);
    }

    public function getTitleShowCombo($option_name, $curVal) {
        $yesno_array = [
            '1' => 'Show Title',
            '0' => 'Hide Title'
        ];
        $option_value = $this->optionValueArray($yesno_array, $curVal);
        return $this->comboFrame($option_name, $option_value);
    }

    public function getCardTemplateCombo($option_name, $curVal) {
        $yesno_array = [
            '0' => 'Default Layout 600x600 Text+image',
            '1' => 'Short Layout 600x300 Text+image',
            '2' => 'Short Layout 600x300 Image only',
            '3' => 'Tall Layout 600x800 image only',
            '5' => 'Tall Layout 600x800 image + text',
            '4' => 'Short Layout 600x300 Solid Color 1',
            '6' => 'Default Layout 600x600 Image only',
            '7' => 'Active Tip 600 x 200'
        ];
        $option_value = $this->optionValueArray($yesno_array, $curVal);
        return $this->comboFrame($option_name, $option_value);
    }

    public function getSurveyCardTemplateCombo($option_name, $curVal) {
        $yesno_array = [
            '9901' => 'Default Survey',
            '9902' => 'Swipe Survey 600 x 600'
           
        ];
        $option_value = $this->optionValueArray($yesno_array, $curVal);
        return $this->comboFrame($option_name, $option_value);
    }

    public function getCardCategoryType($option_name, $curVal) {
        $yesno_array = [
            '' => 'No Type',
            'A' => 'Type A',
            'B' => 'Type B',
            'C' => 'Type C'
        ];
        $option_value = $this->optionValueArray($yesno_array, $curVal);
        return $this->comboFrame($option_name, $option_value);
    }

    public function getCardLocation($option_name, $curVal) {

        $sql = "SELECT id,description FROM address WHERE description != ''";
        /* 	
          $yesno_array = [
          '0' => 'No Location',
          '1' => '4201 Defoors Farm trail, ga 30127',
          '2' => 'Location B',
          '3' => 'Location C'

          ];
          $option_value = $this->optionValueArray($yesno_array, $curVal);
         */ $this->defaultComboMessage = "No Location Attached";
        $this->defaultComboValue = '0';
        $q = $this->db->query($sql);
        $option_value = $this->optionValueObject($q->result(), "id", "description", $curVal);

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

    public function getReciept($option_name, $curVal) {

        $sql = "SELECT id,name,active FROM transport_providers WHERE active =1 ORDER BY name";
        $q = $this->db->query($sql);
        $option_value = $this->optionValueObject($q->result(), "id", "name", $curVal);
        return $this->comboFrame($option_name, $option_value);
    }

    public function getCardPoint($option_name, $curVal) {
        $cardtime_array = [
            '0' => 'No Points Applicable'
        ];
        for ($i = 1; $i < 24; $i++) {
            $points = $i * 50;
            $cardtime_array = array_merge($cardtime_array, array($points => "$points Points"));
        }

        $option_value = $this->optionValueArray($cardtime_array, $curVal);
        return $this->comboFrame($option_name, $option_value);
    }

    public function getCardTime($option_name, $curVal) {
        $cardtime_array = [
            '' => 'No Specific Time',
            '12 AM' => '12 AM'
        ];
        for ($i = 1; $i < 12; $i++) {
            $cardtime_array = array_merge($cardtime_array, array("$i: AM" => "$i: AM"));
        }
        $cardtime_array = array_merge($cardtime_array, array("12: PM" => "12: PM"));
        for ($i = 1; $i < 12; $i++) {
            $cardtime_array = array_merge($cardtime_array, array("$i: PM" => "$i: PM"));
        }

        $option_value = $this->optionValueArray($cardtime_array, $curVal);
        return $this->comboFrame($option_name, $option_value);
    }

    public function getYesNoCombo($option_name, $curVal) {
        $yesno_array = [
            '0' => 'No',
            '1' => 'Yes'
        ];
        $option_value = $this->optionValueArray($yesno_array, $curVal);
        return $this->comboFrame($option_name, $option_value);
    }

    public function getCardPicturesCombo($option_name, $curVal) {
        $sql = "SELECT 'https://resources.float.sg/cards/'||uniqueid||'.'||format AS id,"
                . " id||'-https://resources.float.sg/cards/'||uniqueid||'.'||format AS val FROM card_images";
        $q = $this->db->query($sql);
        $option_value = $this->optionValueObject($q->result(), "id", "val", $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>";
        }

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

    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 getTransportProvidersCombo($option_name, $curVal) {
        $q = $this
                ->db
                ->where('active', 1)
                ->order_by('name', 'ASC')
                ->get('transport_providers');

        $option_value = $this->optionValueObject($q->result(), "id", "name", $curVal);

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

    private function optionValueArray($sdStd, $val) {
        $this->optCons = '';
        foreach ($sdStd as $key => $value) {
            $selV = '';
            if ($key == $val) {
                $selV = " selected ";
            }
            $this->optCons .= "<option value='" . $key . "' " . $selV . ">" . $value . "</option>";
        }
        return $this->optCons;
    }

    private function optionValueObject($sdStd, $val, $valname, $curVal) {
        $this->optCons = '';
        if ($this->showDefaultSelect == true) {
            $this->optCons .= "<option value='" . $this->defaultComboValue . "'>" . $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 ";
        }



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

    public function getCardActionTypeCombo($option_name, $curVal) {
        $options_array = [
            'alert' => 'Show Alert',
            'browser' => 'Open Browser',
            'deeplink' => 'Open App via Deeplink',
            'callback' => 'Call API'
        ];
        $option_value = $this->optionValueArray($options_array, $curVal);
        return $this->comboFrame($option_name, $option_value);
    }

    public function getTimezoneCombo($option_name) {
        $q = $this->db->get('address_timezone');

        $option_value = $this->optionValueObject($q->result(), "id", "timezone", "");

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

    // put this function in the model and use for now for the Neighborhood combo
    public function getCountryNeighborhoodCombo($option_name, $country, $cState, $curVal) {
        $hood_array = [
            '0' => 'Neighborhood 100',
            '1' => 'Neighborhood 200',
            '2' => 'Neighborhood 300',
            '3' => 'Neighborhood 400',
            '5' => 'Neighborhood 500',
            '4' => 'Neighborhood 600',
            '6' => 'Neighborhood 700'
        ];
        $option_value = $this->optionValueArray($hood_array, $curVal);
        return $this->comboFrame($option_name, $option_value);
    }

  public function getControllerCombo($option_name, $options_array, $curVal) {
    $option_value = $this->optionValueArray($options_array, $curVal);
    return $this->comboFrame($option_name, $option_value);
  }

  public function getPermissionLevel($option_name, $curVal) {
    $sql = "SELECT * FROM bko_permission_level WHERE status=1 ";
    $q = $this->db->query($sql);
    $option_value = $this->optionValueObject($q->result(), "plevel", "name", $curVal);
    return $this->comboFrame($option_name, $option_value);
  }

  public function getControllerAndMethodCombo($option_name, $options_array, $curVal) {
    $option_value = $this->optionValueArray($options_array, $curVal);
    return $this->comboFrame($option_name, $option_value);
  }
}
