<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Member extends Admin_Controller {

    function __construct() {
        parent::__construct();
        /*
          if (!isset($_SESSION['bko_member_selected']) or $_SESSION['bko_member_selected_id'] == '') {
          redirect(home);
          }
         */
    }

    public function addnew() {
        $data = array();
        $data['street'] = $data['aptno'] = $data['zipcode'] = $data['phone'] = $data['city'] = $data['state'] = $data['street2'] = "";
        $data['firstname'] = $data['lastname'] = $data['username'] = '';
        $data['create_message'] ='';
        
        $this->load->model('admindash_model');

        if ($this->input->post()) { // if data is posted 
            $this->createMember($this->input->post());
        }

        $out = $this->admindash_model->getDashRecentSignup(10); // note specific function in this model - done use get dash data- too much data
        $data['recent_signup'] = $out['recent_signup'];
        $this->renderAdminPage('view_bko_new_member', $data);
    }

    private function createMember($data) {
//print_r($data);
        $member_id = $this->registerMember($data);
        if ($member_id > 0) {
            $this->updateMemberAddress($member_id, $data);
        }
    }

    private function registerMember($data) {
        $ret = 0;
        $in["firstname"] = $data['firstname'];
        $in["lastname"] = $data['lastname'];
        $in["pid"] = 110;
        $in["username"] = $data['username'];
        $in["email"] = $in["username"];
        $in["loc"] = "192.168.2.200";
        $in["password"] = $data['password'];
        $in['phone'] = $data['phone'];
		$in["plevel"] = 100;
        $in['action'] = MOBIDELIV_BKO_MEMBERADD;
        $out = array();
        $ret = $this->medtrans_api($in, $out);
//print_r($out);
        if ($ret == PHP_API_OK) {// if good make sure you return member_id
            $ret = $out['member_id'];
        }
        return $ret;
    }

    private function updateMemberAddress($member_id, $data) {

        $in["street1"] = $data['street'];
        //$in["street2"] = $value['1'];
        $in["pid"] = 110;
        $in["city"] = $data['city'];
        $in["zipcode"] = $data['zipcode'];
        $in["state"] = $data['state'];
        $in["country"] = "US";
        $in["loc"] = "192.168.2.200";
        $in['member_id'] = $member_id;
        $in['action'] = MOBIDELIV_BKO_MEMBERUPDT;
        $out = array();
        $ret = $this->medtrans_api($in, $out);
    }

    public function find() {
        $data = array();
        $this->load->model('admindash_model');

        if ($this->input->post()) {
            $data = $this->input->post();
            //  print_r($data);
            $srcht = trim($data['searchtext']);
            $mysql = "SELECT  m.id AS sel_member_id,m.*,mp.*,m.phone AS primary_phone "
                    . "FROM members m LEFT JOIN members_profile mp ON mp.member_id =  m.id "
                    . "WHERE LOWER(m.firstname) LIKE LOWER('$srcht%') "
                    . "ORDER BY m.id DESC LIMIT 20";
        } else {
            $data['searchtext'] = '';
            $mysql = "SELECT  m.id AS sel_member_id,m.*,mp.*,m.phone AS primary_phone FROM members m LEFT JOIN members_profile mp ON mp.member_id =  m.id ORDER BY m.id DESC LIMIT 20";
        }
        $q = $this->db->query($mysql);
        $data['member_search'] = $q->result();

        $data['search_combo'] = $this->memberSearchCombo('searchparam');

        $out = $this->admindash_model->getDashRecentSignup(10); // note specific function in this model - done use get dash data- too much data
        $data['recent_signup'] = $out['recent_signup'];
        $this->renderAdminPage('view_bko_find_member', $data);
    }

    private function memberSearchCombo($cmbName) {
        
         $selected_val = $this->input->post($cmbName);
        
        $memberSearchList = array(
            'm.lastname' => 'Lastname',
            'm.firstname' => 'Firstname',
            'm.email' => 'Email',
            'm.phone' => 'Phone',
            'mp.zipcode' => 'Zip Code'
        );

        $str_Combo = " <select class=\"form-control\" name ='".$cmbName."'>";
        foreach ($memberSearchList as $key => $value) {
            $selV='';
            if ( $selected_val == $key)
            {
                $selV = ' selected';
            }
            
            $str_Combo .="<option  value='" . $key . "' ".$selV.">" . $value . "</option>";
            //echo "Key: $key, Value: $value\n";
        }
        $str_Combo .="</select>";
        return $str_Combo;
    }

    public function findbylinkid() {

        // get the user id 
        $member_id = $this->input->get('member_id');

        $mysql = "SELECT m.*,mp.*,m.phone AS primary_phone, m.id AS member_id FROM members m LEFT JOIN members_profile mp ON mp.member_id =  m.id WHERE m.id=" . $member_id;
        $q = $this->db->query($mysql);
        $out = $q->row();

        $_SESSION['selected_user_row'] = $out;
        redirect('members/profile');
    }

}
