<?php

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

class Members extends Admin_Controller {

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

        if (!isset($_SESSION['selected_user_row'])) {
            redirect('member/find');
        }
    }

      public function newservice() {
        $data = array();
        $this->renderAdminPage('members/view_bko_members_newservice', $data);
    }

    public function deletaallcards() {
        if ($this->input->get()) {
            $member_id = $this->input->get('member_id');
            if ($member_id != '' && $member_id > 0) {

                $data['action'] = MOBIDELIV_BKO_DELETEALLCARDS;
                $data['member_id'] = $member_id;
                $out = array();
                $ret = $this->medtrans_api($data, $out);
                echo $out["status"];
            }
        }
    }

    public function transport() {
        $data = array();
 
        if ($this->input->post()) {
            $data = $this->getTrasportPost();
            $data['action'] = MOBIDELIV_USER_REQUEST_TRAN;
            $out = array();
            $ret = $this->medtrans_api($data, $out);
            //  print_r($out);
        } else {
            $data['firstname'] = $_SESSION['selected_user_row']->firstname;
            $data['lastname'] = $_SESSION['selected_user_row']->lastname;
            $data['from_street'] = $data['from_aptno'] = $data['from_zipcode'] = "";
            $data['from_phone'] = $_SESSION['selected_user_row']->primary_phone;
            $data['from_city'] = '';

            $data['to_street'] = $data['to_city'] = $data['to_phone'] = $data['to_zipcode'] = $data['to_aptno'] = $data['ccc'] = '';
        }

        $mysql = "SELECT * FROM members_service_request WHERE member_id= " . $_SESSION['selected_user_row']->member_id . "ORDER BY id DESC LIMIT 10";
        $q = $this->db->query($mysql);
        $data['transport_request'] = $q->result();

        $data['destination_option'] = $this->buildMemberTransportOption($data['transport_request']);
        $data['loc_address'] = $this->input->post('loc_address');
        $data['location_types'] = array("Residence" => "Residence", "Facility" => "Facility", "Doctor's Office" => "Doctor's Office");



        $mysql = "SELECT DATE_PART('hour', service_date - now() ) AS day_gap, *, id AS transport_request_id FROM members_service_request WHERE member_id = " . $_SESSION['selected_user_row']->member_id . "LIMIT 10";
        $q = $this->db->query($mysql);
        $data['transport_request'] = $q->result();



// print_r($data);
        $data['page_title'] = "Request new transport";



        $this->renderAdminPage('members/view_bko_member_transport', $data);
    }

    private function buildMemberTransportOption($prev_transp) {




        $out = array('0:0' => "New Location...");

        foreach ($prev_transp as $drow) {
            $myAddress = $drow->to_street . "," . $drow->to_city . "," . $drow->to_state . " " . $drow->to_zipcode;
            $out = $out + array($drow->id . ':T' => $myAddress);
        }

        if (trim($_SESSION['selected_user_row']->street1) != '') {
            $myAddress = $_SESSION['selected_user_row']->street1 . "," . $_SESSION['selected_user_row']->street2 . "," . $_SESSION['selected_user_row']->city . "," . $_SESSION['selected_user_row']->state . " " . $_SESSION['selected_user_row']->zipcode;
            // $out = array('0:P' => "$myAddress", '0:0' => "New Location...", 'ID:T' => "This will be a list of other previous location for the member -to be implemented");
            $out = $out + array('0:P' => $myAddress);
        }
         $out = $out + array('1000000:P' => "Place holder - bug jump tempora");
        //  $out = array('0:P' => "$myAddress", '0:0' => "New Location...", 'ID:T' => "This will be a list of other previous location for the member -to be implemented");
        return $out;
    }

    public function profileOLD() {
        $data = array();

        $data['street1'] = $data['street2'] = $data['city'] = $data['zipcode'] = $data['phone'] = $data['state'] = '';


        $selected_user_row = $_SESSION['selected_user_row'];
        $selected_user_id = $selected_user_row->member_id;
        $mysql = "SELECT * FROM members_service_request WHERE member_id = $selected_user_id";
        $q = $this->db->query($mysql);
        $data['transport_request'] = $q->result();

        $mysql = "SELECT * FROM members WHERE id = $selected_user_id";
        $q = $this->db->query($mysql);
        $data['member_data'] = $q->row();

        $mysql = "SELECT * FROM members_profile WHERE member_id = $selected_user_id";
        $q = $this->db->query($mysql);
        $data['member_profile_data'] = $q->row();



        /*
          SELECT * FROM members_profile WHERE id =1;
          id | pid | member_id |      street1       | street2 |   city   | zipcode | phone | state | country |           added            |       loc
          ----+-----+-----------+--------------------+---------+----------+---------+-------+-------+---------+----------------------------+-----------------
          1 | 100 |         1 | 2600 Bentley Rd SE |         | Marietta | 30567   |       | GA    | US      | 2017-08-22 14:10:45.845175 | 104.238.127.115
         */

        //print_r( $data['member_data'] );


        $this->renderAdminPage('members/view_bko_members_profile', $data);
    }
    
    
    
    public function profile() {
        $data = array();

        $data['street1'] =  $data['street1'] = $data['street2'] = $data['city'] = $data['zipcode'] = $data['phone'] = $data['state'] = '';

        $selected_user_row = $_SESSION['selected_user_row'];
        $selected_user_id = $selected_user_row->member_id;
        /*  $mysql = "SELECT * FROM members_transport_request WHERE member_id = $selected_user_id";
          $q = $this->db->query($mysql);
          $data['transport_request'] = $q->result();
         */


        // $this->load->view('admin/transp/extra/transport_form', $data);




       
        
        if ($this->input->post()) {

            $acc_type = $this->input->post('acc_type');
            if ($acc_type == '') {
                $acc_type = 1;
            }

         //   $data["acc_type"] = $acc_type;
            $data["description"] = $this->input->post('company_name');
          //  $data["company_name"] = $this->input->post('company_name');
            $data["email"] = $this->input->post('email');

            $data["firstname"] = $this->input->post('firstname');
            $data["lastname"] = $this->input->post('lastname');
            $data["street1"] = $this->input->post('street1');
            $data["city"] = $this->input->post('city');
            $data["phone"] = $this->input->post('phone');
            $data["state"] = $this->input->post('state');
            $data["zipcode"] = $this->input->post('zipcode');

            if ($data["firstname"] != '' AND $data["lastname"] != '' AND $data["zipcode"] != '') {
                $in = array();
                $in = array(
                    "pid"=>100,
                    "bko_users_id" => 0,
                    "action"=> MOBIDELIV_BKO_MEMBERUPDT,
                    "member_id" => $selected_user_id,                 
                    "email" => $data["email"],
                    "firstname" => $data["firstname"],
                    "lastname" => $data["lastname"],
                    "street1" => $data["street1"],
                    "city" => $data["city"],
                    "phone" => $data["phone"],
                    "state" => $data["state"],
                    "zipcode" => $data["zipcode"],                  
                    "country"=>'US',
                    "loc"=>'38.101.241.200'
                    
                );

                $out = array();
                $ret = $this->medtrans_api($in, $out);
            }





           // echo 'oya update now';
        } else {

            $mysql = "SELECT * FROM members WHERE id = $selected_user_id";
            $q = $this->db->query($mysql);
            $data['member_data'] = $q->row();

              $data['firstname'] = $data['member_data']->firstname;
            $data['lastname'] = $data['member_data']->lastname;
            $data['email'] = $data['member_data']->email; //company_name
            $data['phone'] = $data['member_data']->phone;
           // $data['company_name'] = $data['member_data']->company_name;
           // $data['acc_type'] = $data['member_data']->acc_type;


            $mysql = "SELECT * FROM members_profile WHERE member_id = $selected_user_id";
            $q2 = $this->db->query($mysql);
            $data['member_profile_data'] = $q2->row();

            if ($q2->num_rows() > 0) {
                $data['street1'] = $data['member_profile_data']->street1;
                $data['city'] = $data['member_profile_data']->city;
                $data['street2'] = $data['member_profile_data']->street2;
                $data['city'] = $data['member_profile_data']->city;
                $data['zipcode'] = $data['member_profile_data']->zipcode;
                
                $data['state'] = $data['member_profile_data']->state;
            } else {
                $data['street1'] = $data['city'] = $data['street2'] = $data['city'] = $data['zipcode'] = $data['phone'] = $data['state'] = '';
            }
        }


  $this->load->model('service_model');
        $outx = $this->service_model->getMemberServiceRequestList($selected_user_id,100);
        $data['transport_request'] = $outx['service_request_list'];

        



        //$this->load->model('combo_model');
       // $this->combo_model->defaultComboMessage = 'Select Account Type';
     //   $data['acc_type_combo'] = $this->combo_model->getUserAccountTypeCombo('acc_type',  $data['acc_type']);      
        $this->renderAdminPage('members/view_bko_members_profile', $data);
    }
    
    
    

    public function pending() {
        $data = array();
        $selected_user_row = $_SESSION['selected_user_row'];
        $selected_user_id = $selected_user_row->member_id;

  

        $this->load->model('service_model');
        $outx = $this->service_model->getMemberServiceRequestList($selected_user_id,100);
        $data['transport_request'] = $outx['service_request_list'];

        $this->renderAdminPage('members/view_bko_members_pending', $data);
    }

    public function report() {
        $data = array();
        $selected_user_row = $_SESSION['selected_user_row'];
        $selected_user_id = $selected_user_row->member_id;



        $this->load->model('service_model');
        $outx = $this->service_model->getMemberServiceRequestList($selected_user_id,100);
        $data['transport_request'] = $outx['service_request_list'];

        $this->renderAdminPage('members/view_bko_members_report', $data);
    }

       public function invoice() {
        $data = array();

        $mysql = "SELECT id,description,digits,expiration_month,expiration_year,added_dt::date FROM creditcard WHERE member_id=" . $_SESSION['selected_user_row']->member_id;
        $q = $this->db->query($mysql);

        $cmbs = " <select name=\"card_id\" class=\"form-control\" id=\"card_id\">";
        foreach ($q->result_array() AS $drw) {
            $cmbs .= "  <option value='" . $drw['id'] . "'>" . $drw['description'] . " XXXX-XXXX-XXXX-" . $drw['digits'] . " Added " . $drw['added_dt'] . " Expire " . $drw['expiration_month'] . "/" . $drw['expiration_year'] . " </option>";
        }
        $cmbs .= " </select>";
        $data['user_cards_combo'] = $cmbs;


        $mysql2 = "SELECT * FROM members_service_request WHERE payment_id is null AND  member_id=" . $_SESSION['selected_user_row']->member_id." ORDER BY ID desc";
        $q2 = $this->db->query($mysql2);

        $cmbs2 = " <select name=\"card_id\" class=\"form-control\" id=\"service_id\">";
        foreach ($q2->result_array() AS $drw) {
            $total = ( $drw['est_cost']) * 0.01; //transport | translate | concierge
            $cmbs2 .= "  <option value='" . $drw['id'] . "'>" . $drw['service_date'] . " - Total: $ " . $total . " </option>";
        }

        $cmbs2 .= " </select>";
        $data['invoice_combo'] = $cmbs2;

        $this->renderAdminPage('members/view_bko_members_payments', $data);
    }
    
    public function payments() {
      //  $data = array();
        $data = array(
            "card_save_message" =>'',
            "action"=> MOBIDELIV_BKO_ADDCARD,
            "sessionid" => 'CALL_FROM_BACKOFFICE',
            "member_id" => $_SESSION['selected_user_row']->member_id,
            "cardnumber" => "",
            "exp_month" => "",
            "exp_year" => "",
            "cvc" => "",
            "cardname" => $_SESSION['selected_user_row']->firstname . " " . $_SESSION['selected_user_row']->lastname,
            "zipcode" => $_SESSION['selected_user_row']->zipcode,
            "city" => $_SESSION['selected_user_row']->city,
            "street1" => $_SESSION['selected_user_row']->street1 . " " . $_SESSION['selected_user_row']->street2
        );
    //   $data["card_save_message"] = "";

        if ($this->input->post()) 
        {
          $in = $this->input->post();
          $in["action"] = MOBIDELIV_BKO_ADDCARD;
           $out = array();
                $ret = $this->medtrans_api($in, $out);
          if (isset($out["card_id"]) && $out["card_id"] !='' && $out["card_id"]>0   )
          {
              $data["card_save_message"] = "Card added with id ".$out["card_id"];
              $data["cardnumber"] =$data["cvc"]=$data["exp_year"]="";
          }
         // print_r($out);
          
            //echo 'Ameye';
        }
        
        $mysql = "SELECT id,description,digits,expiration_month,expiration_year,added_dt::date,status,"
                . " (CASE WHEN active=1 THEN 'Active' ELSE 'Not Active' END) AS active FROM creditcard WHERE member_id=" . $_SESSION['selected_user_row']->member_id;
        $q = $this->db->query($mysql);

        $cmbs = " <table class=\"table\">";
        foreach ($q->result_array() AS $drw) {
            $cmbs .= "<tr><td>" . $drw['id'] . "</td><td>" . $drw['description'] . " XXXX-XXXX-XXXX-" . $drw['digits'] . " Added " . $drw['added_dt'] . " Expire " . $drw['expiration_month'] . "/" . $drw['expiration_year'] . " </td><td>" . $drw['status'] . "</td><td>" . $drw['active'] . "</td></tr>";
        }
        $cmbs .= " </table>";
        $data['other_cards'] = $cmbs;




        $this->renderAdminPage('members/view_bko_members_cards', $data);
    }

    private function getTrasportPost() {
        $out = array();

        $out['member_id'] = $this->input->post('member_id');

        $out['firstname'] = $this->input->post('firstname');
        $out['lastname'] = $this->input->post('lastname');
        
        $out['translate'] = 0; // for now
        
        $out['from_street'] = $this->input->post('from_street');
        $out['from_aptno'] = $this->input->post('from_aptno');
        $out['from_zipcode'] = $this->input->post('from_zipcode');
        $out['from_phone'] = $this->input->post('from_phone');
        $out['from_city'] = $this->input->post('from_city');
        $out['from_state'] = $this->input->post('from_state');


        $loc_address = $this->input->post('loc_address');

        if ($loc_address == '0:P') {
            $out['to_street'] = $_SESSION['selected_user_row']->street1;
            $out['to_aptno'] = "";
            $out['to_zipcode'] = $_SESSION['selected_user_row']->zipcode;
            $out['to_city'] = $_SESSION['selected_user_row']->city;
            $out['to_state'] = $this->input->post('from_state');
        } else if ($loc_address == '0:0') {
            $out['to_street'] = $this->input->post('to_street');
            $out['to_aptno'] = $this->input->post('to_aptno');
            $out['to_zipcode'] = $this->input->post('to_zipcode');
            $out['to_city'] = $this->input->post('to_city');
            $out['to_state'] = $this->input->post('to_state');
            $out['loc_type'] = $this->input->post('loc_type');
        } else {
            
        }

        $out['service_date'] = $this->input->post('service_date');
        return $out;
    }

}
