<?php

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

class Signup extends AGT_Controller {

    public function index() {
        // https://www.codeigniter.com/userguide3/libraries/form_validation.html#form-validation-tutorial
        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');

        //$this->form_validation->set_rules('username', 'Username', 'trim|required');
        $this->form_validation->set_rules('password', 'Password', 'trim|required', array('required' => 'You must provide a %s.')
        );
        $this->form_validation->set_rules('firstname', 'First name', 'trim|required');
        $this->form_validation->set_rules('lastname', 'Last name', 'trim|required');
        $this->form_validation->set_rules('agent_name', 'Agent, Transporter or Interpreter business name', 'trim|required');
        $this->form_validation->set_rules('tos', 'terms of service', 'trim|required', array('required' => 'You must accept the %s.')
        );
        $this->form_validation->set_rules('city', 'City', 'trim|required');
        $this->form_validation->set_rules('zipcode', 'Zip code', 'trim|required');
        $this->form_validation->set_rules('street', 'Business address', 'trim|required');
        $this->form_validation->set_rules('phone', 'Phone', 'trim|required');
        $this->form_validation->set_rules('username', 'Your Username - Email', 'trim|required|valid_email');
        $this->form_validation->set_rules('state', 'State', 'trim|required|min_length[2]');
        $this->form_validation->set_rules('agent_type', 'Account type', 'trim|required');

        $data = array();
        $data['username'] = $data['password'] = $data['action_message'] = $data['firstname'] = $data['lastname'] = $data['agent_name'] = $data['tos'] = '';
        $data['city'] = $data['zipcode'] = $data['street'] = $data['phone'] = $data['state'] = $data['agent_type'] = '';

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

            $data = $this->input->post();

            if ($this->form_validation->run() == FALSE) {
                $data['action_message'] = $this->formatedMesage('signup', 'Invalid input, please correct the fields below');
                $this->load->view('user/view_signup', $data);
            } else {
    
                $post = array(
                    "email" => $data['username'],
                    "agent_name" => $data['agent_name'],
                    "firstname" => $data['firstname'],
                    "lastname" => $data['lastname'],
                    "street" => $data['street'],
                    "city" => $data['city'],
                    "zipcode" => $data['zipcode'],
                    "state" => $data['state'],
                    "country" => "US",
                    "agent_type" => $data['agent_type'],
                    "username" => $data['username'],
                    "password" => $data['password'],
                    "phone" => $data['phone'],
                    "login" => 1
                );


                $ret = $this->medtrans_call_api(MEDTRANS_TRANSP_CREATE, $post, $out); // call the API

                if ($ret == PHP_LOGIN_OK) {
                    $out = $this->populateAgentsession($out); // just for testing - will be removed
                    // $this->buildUserSession($ret, $out);
                    redirect('dash');
                } else {
                    ob_start();
                    //var_dump($response);
                    $data['action_message'] = $this->formatedMesage('signup', ob_get_clean());
                    $this->load->view('user/view_signup_success', $data);
                }

                /*
                 * 
         
            
                //  $data['action'] = MEDTRANS_TRANSP_LOGIN;
                //  $out = array();
                //  $ret = $this->medtrans_call_api(MEDTRANS_TRANSP_CREATE, $data, $out);
                
                //    echo $ret;
                //     echo "<hr size='1'>";
                //    var_dump($out);
        
                 *
                  $content = json_encode($post);
                  $url = "https://oameye.extlayer.medtransgo.net/agent/createagent";
                  $curl = curl_init($url);
                  curl_setopt($curl, CURLOPT_HEADER, false);
                  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                  curl_setopt($curl, CURLOPT_HTTPHEADER,
                  array("Content-type" => "application/json"));
                  curl_setopt($curl, CURLOPT_POST, true);
                  curl_setopt($curl, CURLOPT_POSTFIELDS, $content);

                  $json_response = curl_exec($curl);
                  $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
                  curl_close($curl);

                  if ( $status != 200 ) {
                  $data['action_message'] = $this->formatedMesage('signup',"Error: API call failed with the status $status, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
                  $this->load->view('user/view_signup', $data);
                  return;
                  }

                  $response = json_decode($json_response, true);
                  //ob_start();
                  //var_dump($response);
                  //$data['action_message'] = $this->formatedMesage('signup',ob_get_clean());
                  //$this->load->view('user/view_signup_success', $data);

                  if ($response["internal_return"] == 100) {
                  $out = $this->populateAgentsession($json_response); // just for testing - will be removed
                  redirect('dash');
                  }
                 */
            }
        } else {
            $this->load->view('user/view_signup', $data);
        }
    }

}

// vi:ts=2

