<?php

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

class Transp extends Agent_Controller {

    
    public function traspsel() {
        $in["transport_id"] = $this->input->get('transport_id');

        if ($in["transport_id"] != '' and $in["transport_id"] > 0) {

            $in['agent_id'] = $_SESSION['agent_id'];
            $in['limit'] = 30;
            //$in["transport_id"] = 0;
            $in['action'] = MEDTRANS_USER_GET_TRANSLIST;
            $out = array();
            $ret = $this->medtrans_call_api(MEDTRANS_TRANSP_GET_TRANSLIST, $in, $out);
            $data['selected_transport'] = $out['result_list'];

            $data['rect'] = $data['selected_transport'][0];
            $data['assign_detail'] = "<b>Assign Date : </b>" . $data['rect']['assign_date'];  // "<b>Assigned to :</b> " . $data['rect']['agent_name'] . "<br><b>Location :</b>" . $data['rect']['loc_name'].",". $data['rect']['street'].",". $data['rect']['city'].",". $data['rect']['state']."-". $data['rect']['zipcode'];
            $data['assign_phone'] = ""; //"<b>Phone :</b> " . $data['rect']['phone']; 
            $data['cancel_reason_combo'] = "Cancellation Reason Combo Here";

            $this->load->view('agent/extra/transport_form', $data);
        }
        // echo 'A transport line was selected';
    }

    
    private function transportList($data)
    {
        
        $in['agent_id'] = $_SESSION['agent_id'];
        $in['limit'] = 10;
        $in["transport_id"] = 0;
        //   $in['action'] = MEDTRANS_USER_GET_TRANSLIST;
        //   $out = array();
        //   $ret = $this->medtrans_call_api(MEDTRANS_TRANSP_GET_TRANSLIST, $in, $out);
        //  $data['result_list'] = $out['result_list'];

        $data['page_name'] = "Completed Transport Request";
        $this->renderAgentPage('view_agent_transport', $data);
        
    }
    
    
    public function completed() {

        $data = array();
        $data = $this->indexPageReal(array());
        $data['flags'] = 1000;
        $data['page_name'] = "Completed Transport Request";
        $this->transportList($data);
    }

    public function active() {

        $data = array();
        $data = $this->indexPageReal(array());
        $data['flags'] = 1000;
        $data['page_name'] = "Active Transport Request";
        $this->transportList($data);
    }

    public function confirmed() {
        $data = array();
        $data = $this->indexPageReal(array());

        $in['agent_id'] = $_SESSION['agent_id'];
        $in['limit'] = 10;
        $in["transport_id"] = 0;
        //   $in['action'] = MEDTRANS_USER_GET_TRANSLIST;
        //   $out = array();
        //   $ret = $this->medtrans_call_api(MEDTRANS_TRANSP_GET_TRANSLIST, $in, $out);
        //  $data['result_list'] = $out['result_list'];

        $data['page_name'] = "Confirmed Transport Request";
        $this->renderAgentPage('view_agent_transport', $data);
    }

    public function pending() {
        $data = array();
        $data = $this->indexPageReal(array());

        $in['agent_id'] = $_SESSION['agent_id'];
        $in['limit'] = 10;
        $in["transport_id"] = 0;
        //   $in['action'] = MEDTRANS_USER_GET_TRANSLIST;
        //   $out = array();
        //   $ret = $this->medtrans_call_api(MEDTRANS_TRANSP_GET_TRANSLIST, $in, $out);
        //  $data['result_list'] = $out['result_list'];

        $data['page_name'] = "Pending Transport Request";
        $this->renderAgentPage('view_agent_transport', $data);
    }

    public function addlanguage() {
        $data["translator_id"] = $this->input->get('translator_id');
        $data["lang_code"] = $this->input->get('lang_code');
        $ret = $this->add_language($data);
        echo $ret=="Language added"?"0":$ret;
    }
    
    private function add_language($data) {
        $in = array();
        $in["mode"] = 100; // => MODE_ADD
        $in["interpreter_id"] = $data["translator_id"];
        $in["lang_code"] = $data["lang_code"];
        $in["action"] = MEDTRANS_TRANSP_MANAGELANGUAGE;
        $out = array();
        $ret = $this->medtrans_call_api($in["action"], $in, $out);
        return $ret==0?"Language added":"Failed to add language";
    }

    private function add_edit_location($data) {
        $in = $data; // Do we validate?
        if ($data['location_id']>0) {
            $action = MEDTRANS_TRANSP_EDITLOCATION;
        } else {
            $action = MEDTRANS_TRANSP_ADDLOCATION;
        }
        $in["action"] = $action;
        $out = array();
        $ret = $this->medtrans_call_api($action, $in, $out);
        $data["message"] = isset($out["status"])?$out["status"]:($data['location_id']>0?"Location updated":"Location added");
        if ($data["message"]==1) $data["message"] = "Location added";
        return $data["message"]; 
    }

    public function mylocation() {
        $data = array();
        $data["message"] = "";
        if ($this->input->post()) {
           $data = $this->input->post();
           if ($data['mode']=='add_language') {
               $data['message'] = $this->add_language($data);
           } else {
               $data['message'] = $this->add_edit_location($data);
           }
        }       
        
        $in['agent_id'] = $_SESSION['agent_id'];
        $in['limit'] = 5;
        $in['action'] = MEDTRANS_TRANSP_LISTLOCATION;
        $out = array();
        $ret = $this->medtrans_call_api(MEDTRANS_TRANSP_LISTLOCATION, $in, $out);
        $data['result_list'] = $out['result_list'];        
        
        $this->load->model('combo_model');
        $this->combo_model->defaultComboMessage = 'Select your State';
        $data['states_combo'] = $this->combo_model->getStatesCombo('state', "GA");
        
        
        $data['page_name'] = "Transport Locations";
        $this->renderAgentPage('view_agent_locations', $data);
    }
 
    public function mylocation_load()
    {
        $data['location_id'] = $this->input->get('location_id');
        if ($data['location_id']>0) {
            $in['agent_id'] = $_SESSION['agent_id'];
            $in['location_id'] = $data['location_id'];
            $in['action'] = MEDTRANS_TRANSP_GET_TRANSPORTER;
            $out = array();
            $ret = $this->medtrans_call_api(MEDTRANS_TRANSP_GET_TRANSPORTER, $in, $out);
            echo json_encode($out);
        } else {
            echo "{\"status\":\"-2\"}";
        }
    }
 
    private function add_edit_driver($data) {
        $in = $data; // Do we validate?
        $action = MEDTRANS_TRANSP_DRIVERSCALL;
        if ($data['driver_id']>0) {
            $in['mode'] = MODE_UPDATE;
        } else {
            $in['mode'] = MODE_ADD;
        }
        $in["action"] = $action;
        $out = array();
        $ret = $this->medtrans_call_api($action, $in, $out);
        $data["message"] = isset($out["status"])?$out["status"]:($data['driver_id']>0?"Driver updated":"Driver added");
        if ($data["message"]==1) $data["message"] = "Driver added";
        return $data["message"]; 
    }

 
    public function drivers() {
        $data = array();
        $data["message"] = "";
        if ($this->input->post()) {
            $data = $this->input->post();
            $data['message'] = $this->add_edit_driver($data);
        }

        $this->load->model('combo_model');
        $this->combo_model->defaultComboMessage = 'Select your State';
        $data['states_combo'] = $this->combo_model->getStatesCombo('state', "GA");

        
              
        $this->combo_model->defaultComboMessage = 'Select your Licence State';
        $data['lic_states_combo'] = $this->combo_model->getStatesCombo('lic_state', "GA");
        
        
        $in['agent_id'] = $_SESSION['agent_id'];
        $in['limit'] = 50;
        $in['driver_id'] = 0;
        $in['mode'] =  MODE_LIST;
        
        $in['action'] = MEDTRANS_TRANSP_DRIVERSCALL;
        $out = array();
        $ret = $this->medtrans_call_api(MEDTRANS_TRANSP_DRIVERSCALL, $in, $out);
        $data['result_list'] = $out['result_list'];

        $data['page_name'] = "Tranport Drivers";
        $this->renderAgentPage('view_agent_drivers', $data);
    }

    public function drivers_load()
    {
        $data['driver_id'] = $this->input->get('driver_id');
        if ($data['driver_id']>0) {
            $in['agent_id'] = $_SESSION['agent_id'];
            $in['driver_id'] = $data['driver_id'];
            $in['mode'] =  MODE_LIST;
            $in['action'] = MEDTRANS_TRANSP_DRIVERSCALL;
            $out = array();
            $ret = $this->medtrans_call_api(MEDTRANS_TRANSP_DRIVERSCALL, $in, $out);
            echo json_encode($out);
        } else {
            echo "{\"status\":\"-2\"}";
        }
    }

    public function cfglocation() {
        echo 'ameye   ....';
    }

    // array(5) { ["document_id"]=> string(1) "2" ["agent_id"]=> string(1) "4" ["interpreter_id"]=> string(2) "16" ["language_id"]=> string(2) "99" ["ts"]=> string(10) "1508509712" }
    public function download() {
        $data = $this->input->get();
        $in['agent_id'] = $_SESSION['agent_id'];
        $in['interpreter_id'] = $data['interpreter_id'];
        $in['language_id'] = $data['language_id'];
        $in['document_id'] = $data['document_id'];
        $in['action'] = MEDTRANS_DOWNLOAD;
        $out = array();
        $ret = $this->medtrans_call_api(MEDTRANS_DOWNLOAD, $in, $out);
        if ($ret==0 && isset($out["file"]) && $out["file"]!="") {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="'.basename($out["file"]).'"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . strlen($out["data"]));
            echo $out["data"];
            exit();
        } else {
            header('HTTP/1.1 400 Bad Request');
            header('Status: 400 Bad Request');
            echo "{\"status\":\"Cannot download file\"}";
        }
    }

    public function cfgtranslator() {
   
    	$proc = $this->input->get('proc');    
        $data['translator_id'] = $this->input->get('translator_id');
        if ($data['translator_id']>0) {
            $in['agent_id'] = $_SESSION['agent_id'];
            $in['interpreter_id'] = $data['translator_id'];
            if ($proc=='STATUS') {
                $in['language_id'] = $this->input->get('language_id');
                $in['lang_code'] = $this->input->get('lang_code');
                $in['mode'] = $this->input->get('status')=='Disable' ? MODE_DELETE : MODE_ADD;
                $in['action'] = MEDTRANS_TRANSP_MANAGELANGUAGE;
                $out = array();
                $ret = $this->medtrans_call_api($in['action'], $in, $out);
if ($ret!=0) {
var_dump($out);
echo "\\r";
}
                echo $ret;
            } else if ($proc=='FLAGS') {
                $in['language_id'] = $this->input->get('language_id');
                $in['lang_code'] = $this->input->get('lang_code');
                $in['flags'] = (int)$this->input->get('flags');
                $in['mode'] = MODE_UPDATE;
                $in['action'] = MEDTRANS_TRANSP_MANAGELANGUAGE;
                $out = array();
                $ret = $this->medtrans_call_api($in['action'], $in, $out);
if ($ret!=0) {
var_dump($out);
echo "\\r";
}
                echo $ret;
            } else {
	
                $in['language_id'] = 0; // LIST ALL
                   
                $in['action'] = MEDTRANS_TRANSP_GET_TRANSLATOR;
                $out = array();
                $ret = $this->medtrans_call_api(MEDTRANS_TRANSP_GET_TRANSLATOR, $in, $translator_data);
                $ret = $this->medtrans_call_api(MEDTRANS_USER_GET_LANGUAGE, $in, $language_list); // may load into global for speed latter
                $ret = $this->medtrans_call_api(MEDTRANS_TRANSP_LANGUAGELIST, $in, $my_language); // may load into global for speed latter
           
                $this->renderlanguageConfig($translator_data, $language_list,$my_language);
            }    
        } else {
            echo "{\"status\":\"-2\"}";
        }
        
    }
    
    protected function renderlanguageConfig($translator_data, $language_list,$my_language) {
        // echo json_encode($translator_data);
        $language_array = $language_list["result_list"];

        echo "
<form method=\"post\" id=\"add_language\" action=\"/transp/addlanguage\">
<input type=\"hidden\" name=\"mode\" value=\"add_language\">
<input type=\"hidden\" name=\"translator_id\" value=\"".$translator_data['id']."\">
Configure languages for :            
<table class=\"table table-striped\">
        <tr>
        <td><b>" . $translator_data['firstname'] . " " . $translator_data['lastname'] . "</b>-[PH: " . $translator_data['phone'] . "]
            " . $translator_data['street'] . "  " . $translator_data['city'] . "  " . $translator_data['state'] . "  " . $translator_data['zipcode'] . "  " . $translator_data['country'] . " 
        </td>
        </tr>   
        <td>
            <div class=\"row\">
                <div class=\"col-xs-2\">
                   Languages
                </div>
                <div class=\"col-xs-8\">
                " . $this->makeCombo($language_array, "language", "code", "lang_code") . " 
                </div>
                <div class=\"col-xs-2\">
                <button class=\"btn btn-info btn-block btn-xs\"style=\"float: right\" onclick=\"return addlanguage('".$translator_data['id']."','".$translator_data['agent_id']."',this.form.lang_code.value,this);\">Add</button>
                </div>
            </div>
                <hr size='1'>
        </td>
        </tr> 
        </table></form>". $this->translatorLanguageList($my_language);
        
        
        
        // echo json_encode($language_list);
    }

    private function makeCombo($dataArray,$text1,$code1, $name="") {
        $sl = "<select ".($name!=""?"name=\"${name}\"":"")." class='form-control'>";
        foreach ($dataArray as $drow) {
            $sl .= "<option value='".$drow[$code1]."'>".$drow[$text1]."</option>";
        }
        $sl .= "</select>";

        return $sl;
    }

    /*
     * translatorLanguageList 
     * THIS FUNCTION LIST THE LANGUAGES THAT A TRANSLATOR HAVE CLAIMED TO UNDERSTAND
     * THERE IS NEED TO UPLOAD CETIFICATIONS FOR LEGEAL PURPOSE
     */    
    private function translatorLanguageList($my_language) {
        
        $result_list = $my_language['result_list'];
        
        $tb1 = "<table class='table'><tr><th>Language</th><th>Document</th><th>Approved</th><th>Disable</th></tr>";
           
        foreach ($result_list as $rect) {

            if ($rect['document_id'] == 0) {
                $flup = "<form method=\"post\" action=\"?\" enctype=\"multipart/form-data\">
                <input type=\"hidden\" name=\"mode\" value=\"file_upload\">
                <input type=\"hidden\" name=\"agent_id\" value=\"".$rect["agent_id"]."\">
                <input type=\"hidden\" name=\"language_id\" value=\"".$rect["language_id"]."\">
                <input type=\"hidden\" name=\"interpreter_id\" value=\"".$rect["interpreter_id"]."\">
                <div class=\"row\" style=\"float: right\">
                <label class=\"custom-file\">
                <input type=\"file\" name=\"file_contents\"  id=\"file_contents\" class=\"custom-file-input\">
                <span class=\"custom-file-control\"></span>
                </label>  <input type=\"submit\" value=\"Upload File\" class=\"btn btn-info btn-xs\" name=\"submit\" onclick=\"return uploadFile(this.form);\">
                <div id=\"progress_".$rect["interpreter_id"]."_".$rect["language_id"]."\" style='display:none;background:#0f0;width:0px;'></div>
                </form></row>";
            } else {
                $flup = "<a href='/transp/download?document_id=".$rect['document_id']."&agent_id=".$rect["agent_id"]."&interpreter_id=".$rect["interpreter_id"]."&language_id=".$rect["language_id"]."&ts=".time()."'>" . $rect['document'] . "</a> | <a class=\"text-danger\" href=\"#\"><b>Del.</b></a>";
            }

            $tb1 .= "<tr><td style='width:180px;'>" . $rect['language'] . "</td>"
                    . "<td>" . $flup . "</td>"
                    . "<td style='width:45px;'><input class=\"form-control\" type=\"checkbox\" value=\"1\" onchange=\"return manageLanguageFlags(this, '".$rect["interpreter_id"]."','".$rect["language_id"]."','".$rect["lang_code"]."')\"".($rect['flags']==1?" checked":"")."></td>";
            if ($rect['status'] == 0) {
                $tb1 .= "<td style='width:85px;'><button class=\"btn btn-primary btn-xs\" style=\"float: right\" onclick=\"return manageLanguageStatus(this,'".$rect["interpreter_id"]."','".$rect["language_id"]."','".$rect["lang_code"]."');\">Enable</button></td></tr>";
            } else {
                $tb1 .= "<td style='width:85px;'><button class=\"btn btn-danger btn-xs\" style=\"float: right\" onclick=\"return manageLanguageStatus(this,'".$rect["interpreter_id"]."','".$rect["language_id"]."','".$rect["lang_code"]."');\">Disable</button></td></tr>";
            }
        }

        // END TO BE REMOVED

        $tb1 .= "</table>";
        return $tb1;
    }

    /*
 * {"added":"2017-10-13 16:10:39.226379","agent_id":"4","city":"Marietta","country":"US","email":"ses66181+interpreter76@gmail.com","firstname":"Anatolii","id":"15","interpreter_id":"15","lastname":"Okhotnikov","loc":"104.238.127.115","phone":"664651123","pid":"100","state":"GA","status":"1","street":"2600 Bentley Rd SE","zipcode":"30067","internal_return":"0"}
 */
    private function add_edit_translator($data) 
    {
        $in = $data; // Do we validate?
        if ($data['interpreter_id']>0) {
            $action = MEDTRANS_TRANSP_EDIT_TRANSLATOR;
        } else {
            $action = MEDTRANS_TRANSP_ADD_TRANSLATOR;
        }
        $in["action"] = $action;
        $in["email"] = urlencode($data["email"]);
        $out = array();
        $ret = $this->medtrans_call_api($action, $in, $out);
        $data["message"] = isset($out["status"])?$out["status"]:($data['interpreter_id']>0?"Translator updated":"Translator added");
        if ($data["message"]==1) $data["message"] = "Translator added";
        return $data["message"]; 
    }

    public function upload()
    {
        if ($this->input->post()) {
            $data = $this->input->post();
/*
unset($data['data']);
var_dump($data);return; */
            echo $this->file_upload($data);
        } else {
            echo "Invalid request";
        }
    }

    private function file_upload($data)
    {
        $in['interpreter_id'] = $data['interpreter_id'];
        $in['language_id'] = $data['language_id'];
        $in['action'] = MEDTRANS_UPLOADS; // 4505;
        $in['name'] = $data['name'];
        $in['data'] = $data['data'];
        unset($data);
        $out = array();
        $ret = $this->medtrans_call_api($in['action'], $in, $out);
        return $ret==0?"File uploaded":"Failed to upload"; 
    }

    public function translators()
    {
        $data = array(); 
        $data["message"] = "";
        if ($this->input->post()) {
           $data = $this->input->post();
           if ($data['mode']=='add_language') {
               $data['message'] = $this->add_language($data);
           } else if ($data['mode']=='file_upload') {
               $data['message'] = $this->file_upload($data);
           } else {
               $data['message'] = $this->add_edit_translator($data);
           }
        }       
        $in['agent_id'] = $_SESSION['agent_id'];
        $in['limit'] = 5;
        $in['action'] = MEDTRANS_TRANSP_LIST_TRANSLATOR;
        $out = array();
        $ret = $this->medtrans_call_api(MEDTRANS_TRANSP_LIST_TRANSLATOR, $in, $out);
        $data['result_list'] = $out['result_list'];

        
        
        $data['page_name'] = "Interpreters";
        $this->renderAgentPage('view_agent_interpreters', $data);
        
       // 
    }

      public function language()
    {
        $data = array(); 
        $data["message"] = "";
        if ($this->input->post()) {
           $data = $this->input->post();
           if ($data['mode']=='add_language') {
               $data['message'] = $this->add_language($data);
           } else if ($data['mode']=='file_upload') {
               $data['message'] = $this->file_upload($data);
           } else {
               $data['message'] = $this->add_edit_translator($data);
           }
        }       
        $in['agent_id'] = $_SESSION['agent_id'];
        $in['language_id'] = 0; // all
        $in['action'] = MEDTRANS_TRANSP_AGENTLANGUAGE;
        $out = array();
        $ret = $this->medtrans_call_api(MEDTRANS_TRANSP_AGENTLANGUAGE, $in, $out);
        $data['result_list'] = $out['result_list'];
       
        
        $data['page_name'] = "Language List";
        $this->renderAgentPage('view_agent_language', $data);
        
       // 
    }

    
    
    
    public function translators_load()
    {
        $data['translator_id'] = $this->input->get('translator_id');
        if ($data['translator_id']>0) {
            $in['agent_id'] = $_SESSION['agent_id'];
            $in['interpreter_id'] = $data['translator_id'];
            $in['action'] = MEDTRANS_TRANSP_GET_TRANSLATOR;
            $out = array();
            $ret = $this->medtrans_call_api(MEDTRANS_TRANSP_GET_TRANSLATOR, $in, $out);
            echo json_encode($out);
        } else {
            echo "{\"status\":\"-2\"}";
        }
    }

    public function mydevices() {
        $data = array();
       
         $in['agent_id'] = $_SESSION['agent_id'];
        $in['limit'] = 5;
        $in['action'] = MEDTRANS_TRANSP_LISTLOCATION;
        $out = array();
        $ret = $this->medtrans_call_api(MEDTRANS_TRANSP_LISTLOCATION, $in, $out);
        $data['result_list'] = $out['result_list'];        
        
        $this->load->model('combo_model');
        $this->combo_model->defaultComboMessage = 'Select your State';
        $data['states_combo'] = $this->combo_model->getStatesCombo('state', "GA");
        
        
        
         $data['page_name'] = "Transport Devices";
        $this->renderAgentPage('view_agent_device', $data);
    }
    
    public function listdevices() {
        //  echo 'ameye';
        //   url: "/transp/listdevices?proc=NCOMPLETE&locID=" + locID + "&agent_id=" + agent_id 

        
    	$in['location_id'] = $this->input->get('locID'); 
        
                if ($in['location_id'] > 0) {
            $in['agent_id'] = $_SESSION['agent_id'];
            $in['location_id'] = $in['location_id'];
            $in['action'] = MEDTRANS_TRANSP_GET_TRANSPORTER;
            $out = array();
            $ret = $this->medtrans_call_api(MEDTRANS_TRANSP_GET_TRANSPORTER, $in, $outx);
         //   echo json_encode($out);



            $data = array(
                "agent_id" => $_SESSION["agent_id"],
                "location_id" => $in["location_id"],
                "sessionid" => $_SESSION["agent_sessionid"]
            );

            $ret = $this->medtrans_call_api(MEDTRANS_TRANSP_DEVICELIST, $data, $data); // call the API
            $data['rect'] = $outx;
            $this->load->view('/agent/extra/devicelist_form', $data);
        }
    }
    
    public function adddevices()
    {
        
       // echo 'Ameye';
        
                $this->load->model('combo_model');
        $this->combo_model->defaultComboMessage = 'Select your State';
        $data['states_combo'] = $this->combo_model->getStatesCombo('state', "GA");

        
              
        $this->combo_model->defaultComboMessage = 'Select your Licence State';
        $data['lic_states_combo'] = $this->combo_model->getStatesCombo('lic_state', "GA");
        $data['form_title'] = "Add New Device";
        
        $this->load->view('/agent/extra/deviceadd_edit', $data);
        
    }

}
