
<?php
class Training extends BM_Controller {
    public function denyEntry(){
        $id = $_POST['id'];
        $result = $this->sp('sp_denyApplicationEntry', array(
            'applicationID' => $id
        ),'BLUEFIREDATABASE')->row();
        return $this->load->view('json_view',array('json' => 'success'));
    }

    public function acceptEntry(){
        $id = $_POST['id'];
        $result = $this->sp('sp_hiredApplicationEntry', array(
            'applicationID' => $id
        ),'BLUEFIREDATABASE')->row();
        return $this->load->view('json_view',array('json' => 'success'));
    }

    public function getEntries(){
 //      $id = $_POST['id'];
        $res = $this->sp('sp_getAllEntry', array(
//           'userId'=>$id
        ), 'BLUEFIREDATABASE')->result();
        $data = array(
            'json' => $res,
        );
        return $this->load->view('json_view', $data);
    }

    public function getBookmark(){
        $id = $_POST['id'];
        $res = $this->sp('sp_getBookmark', array(
            'userId'=>$id
        ), 'BLUEFIREDATABASE')->result();
        $data = array(
            'json' => $res,
        );
        return $this->load->view('json_view', $data);
    }

    public function insertBookmark(){
        $id = $_POST['id'];
        $userId = $_POST['userId'];

        $result = $this->sp('sp_insertBookmark', array(
            'userId' => $userId,
            'jobId' => $id
        ),'BLUEFIREDATABASE')->row();
        return $this->load->view('json_view',array('json' => 'success'));
    }

    public function deleteBookmark(){
        $id = $_POST['id'];
        $result = $this->sp('sp_deleteBookmark', array(
            'bookmarkID' => $id
        ),'BLUEFIREDATABASE')->row();
        return $this->load->view('json_view',array('json' => 'success'));
    }

    public function getAllCountry(){
        $res = $this->sp('sp_getAllCountry',array(),'BLUEFIREDATABASE')->result();
            $data = array(
                'json'=>$res,
            );
            return $this->load->view('json_view',$data);
    }

    public function getAllCity(){
        $res = $this->sp('sp_getAllCity', array(), 'BLUEFIREDATABASE')->result();
        $data = array(
            'json' => $res,
        );
        return $this->load->view('json_view', $data);
    }

    public function uploadFile(){
        if(!file_exists('storage')) mkdir('storage');
        $filename = null;
        foreach($_FILES as $file){
            file_put_contents('storage/'.$file["name"], fopen($file["tmp_name"],"r"));
            $filename = $file["name"];
        } 
        return $this->load->view('json_view', array('json' => $filename));
    }

    public function updateJob(){
        $jobId      = $_POST['jobId'];
        $name       = $_POST['jobName'];
        $cityId     = $_POST['cityId'];  
        $deadline   = $_POST['deadline'];
        $jobLevel   = $_POST['jobLevel'];
        $jobDesc    = $_POST['jobDesc'];

        $res = $this->sp('sp_updateJob',array(
        'jobId'=>$jobId,
        'name'=>$name,
        'cityId'=>$cityId,
        'jobLevel'=>$jobLevel,
        'deadline'=>$deadline,
        'description'=>$jobDesc
        ),'BLUEFIREDATABASE')->row();
        return $this->load->view('json_view',array('json'=>$res));
    }

    public function getListJob(){
        $res = $this->sp('sp_getListJob', array(), 'BLUEFIREDATABASE')->result();
        $data = array(
            'json' => $res,
        );
        return $this->load->view('json_view', $data);
    }

    public function getJob(){
        $id = (int)$_GET['jobId'];
        $res = $this->sp('sp_getJob', array(
            'jobId' => $id
        ), 'BLUEFIREDATABASE')->result();
        $data = array(
            'json' => $res,
        );
        return $this->load->view('json_view', $data);
    }

    public function deleteJob(){
        $id = $_POST['id'];
        $result = $this->sp('sp_deleteJob', array(
            'jobId' => $id
        ),'BLUEFIREDATABASE')->row();
        return $this->load->view('json_view',array('json' => 'success'));
    }

    public function getAllJob(){
        $res = $this->sp('sp_getAllJob', array(), 'BLUEFIREDATABASE')->result();
        $data = array(
            'json' => $res,
        );
        return $this->load->view('json_view', $data);
    }

    public function insertJob(){
        $name       = $_POST['jobName'];
        $cityId     = $_POST['cityId'];  
        $deadline   = $_POST['deadline'];
        $jobLevel   = $_POST['jobLevel'];
        $jobDesc    = $_POST['jobDesc'];

        $res = $this->sp('sp_insertJob',array(
        'name'=>$name,
        'cityId'=>$cityId,
        'jobLevel'=>$jobLevel,
        'deadline'=>$deadline,
        'description'=>$jobDesc,
        ),'BLUEFIREDATABASE')->row();
        var_dump(sqlsrv_errors());
        return $this->load->view('json_view',array('json'=>$res));
    }

    public function submitApplication(){
        $userId = $_POST['userId'];
        $jobId = $_POST['jobId'];
        $email = $_POST['email'];
        $company = $_POST['company'];
        $position = $_POST['position'];
        $university = $_POST['university'];
        $cv = $_POST['cv'];
        $portofolio = $_POST['portofolio'];

        $res = $this->sp('sp_insertEntry',array(
            'userId' => $userId,
            'jobId'=> $jobId,
            'email'=> $email,
            'prevCompanyName'=> $company,
            'prevPosition'=> $position,
            'universityName'=> $university,
            'cvLink'=> $cv,
            'portofolioLink'=> $portofolio,

        ),'BLUEFIREDATABASE')->row();
        return $this->load->view('json_view', array('json'=>$res));
    }

    public function insertUser(){
        $password   = $_POST['password'];
            $pass       = password_hash($password, PASSWORD_DEFAULT);
        $name       = $_POST['name'];
        $DOB        = $_POST['dob'];
        $gender     = $_POST['gender'];
        $address    = $_POST['address'];
        $phone      = $_POST['phone'];
        $email      = $_POST['email'];
        $res = $this->sp('sp_insertUser',array(
            'password' =>$pass,
            'name' =>$name,
            'DOB' =>$DOB,
            'gender' =>$gender,
            'address' =>$address,
            'phone' => $phone,
            'email' =>$email
        ),'BLUEFIREDATABASE')->row();
        return $this->load->view('json_view', array('json'=>$res));
    }

    public function insertAdmin(){
        $password   = $_POST['password'];
            $pass       = password_hash($password, PASSWORD_DEFAULT);
        $name       = $_POST['name'];
        $res = $this->sp('sp_insertAdmin',array(
            'password' =>$pass,
            'name' =>$name
        ),'BLUEFIREDATABASE')->row();
        return $this->load->view('json_view', array('json'=>$res));
    }

    public function loginUser(){
        $password   = $_POST['Password'];
        $name       = $_POST['Name'];
        $res = $this->sp('sp_loginUser',array(
            'name' =>$name,
        ),'BLUEFIREDATABASE')->result();
        for ($i = 0;$i<count($res);$i++){
            if($res[$i]){
                if(password_verify($password,$res[$i]->Password)){
                    $data = array(
                     'json' =>$res,
                     );
                     return $this->load->view('json_view', $data);
                };
            }
        };
    } 
        public function loginAdmin(){
            $password   = $_POST['Password'];
            $name       = $_POST['Name'];
            $res = $this->sp('sp_loginAdmin',array(
                'name' =>$name,
            ),'BLUEFIREDATABASE')->result();
            for ($i = 0;$i<count($res);$i++){
                if($res[$i]){
                    if(password_verify($password,$res[$i]->AdminPassword)){
                        $data = array(
                         'json' =>$res,
                         );
                         return $this->load->view('json_view', $data);
                    };
                }
            };   
        }
}
