<?php


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





class Profile extends CI_Controller {


    


    public function __construct(){


        parent::__construct();      


        $this->load->model('M_profile');


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


    }


	public function data_user(){


		$data['kode_user'] = $this->input->post('kode_user',TRUE);


		$d['r'] = $this->M_profile->data_log($data)->row_array();


		


		echo json_encode($d);


    } 


    public function update_detail_user(){


		$data['kode_user'] = $this->input->post('kode_user',TRUE);


		$data['username'] = $this->input->post('username',TRUE);


		$data['email']    = $this->input->post('email',TRUE);
        $data['tlp']      = "62".substr($this->input->post('tlp',TRUE),1);





		$r = $this->M_profile->update_data_log($data);


		


		$this->session->set_userdata('username', $data['username']);

        $this->session->set_userdata('tlp', $data['tlp']);


		echo json_encode($r);


    } 


    public function ganti_pw(){


    	$data['kode_user'] = $this->input->post('kode_user',TRUE);


		$data['pw'] = $this->input->post('pw',TRUE);


		$response= $this->M_profile->ganti_pw($data);


		echo json_encode($response);


    } 


    public function update_foto_profile(){


    	$token = "";


        $codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


        $codeAlphabet.= "0123456789";


        $max = strlen($codeAlphabet) - 1;


        for ($i=0; $i < 5; $i++) {


            $token .= $codeAlphabet[mt_rand(0, $max)];


        } 





    	$data['kode_user'] = $this->input->post('kode_user_image',TRUE);





    	$data['file_name'] = $token.$_FILES["gambar"]['name'];


    	$config['upload_path'] = './assets/img/profile/';


        $config['allowed_types'] = 'gif|jpg|png';


        $config['max_size'] = 2097152;


        $config['file_name'] = $data['file_name'];


        $this->load->library('upload', $config);


 		





        $file_size = $_FILES['gambar']['size'];


        $file_type = $_FILES['gambar']['type'];





        if (($file_size > 2097152)){      


            echo json_encode("size");


        }elseif (  


            ($file_type != "image/jpeg") &&


            ($file_type != "image/jpg") &&


            ($file_type != "image/gif") &&


            ($file_type != "image/png")    


        ){


            echo json_encode("ext");  


        }else{


            if (!$this->upload->do_upload('gambar')) {


                $error = array('error' => $this->upload->display_errors());


                // $this->load->view('imageupload_form', $error);


                echo json_encode($error);


            }else{


                $ambil_image = $this->M_profile->ambil_image($data)->row_array();


                if ($ambil_image['foto'] == "") {


                    $upload_image = $this->M_profile->upload_image($data);


                    $this->session->set_userdata("foto",$data['file_name']);  


                }else{


                    unlink('./assets/img/profile/'.$ambil_image['foto']);


                    $upload_image = $this->M_profile->upload_image($data);      


                    $this->session->set_userdata("foto",$data['file_name']);               


                }


                $data = array('image_metadata' => $this->upload->data());


     


                // $this->load->view('imageupload_success', $data);


                echo json_encode(true);


            }     


        }


    }   


} 





