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

class TambahPemilik extends CI_Controller {

	public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->model("UserModel");
        $this->load->model("KelurahanModel");

		if($this->UserModel->isNotLogin()) {
			redirect(site_url('login'));
		} elseif(!($this->UserModel->isAdmin())) {
			redirect(site_url('dashboard'));
		} 
			
		
    }

	public function index()
	{
		if($this->input->post()){
			$post=$this->input->post();
			if(strlen($_FILES['foto_user']['tmp_name'])>0){
				$config['upload_path']          = './assets/images/';
				$config['allowed_types']        = 'jpg|jpeg|gif|png';
				$config['max_size']             = 100;
				$config['max_width']            = 1024;
				$config['max_height']           = 768;
				$config['file_name']			= $post['username'];
				$config['overwrite'] 			= TRUE;
		
				$this->load->library('upload', $config);
		
				if ( ! $this->upload->do_upload('foto_user')){
					$data['error_upload'] = $this->upload->display_errors();
				}else{
					$file = $this->upload->data();
					if($post['username']!=='user'){
						if($this->UserModel->save($file['file_name'])){
							$data['msg']="success";
						} else {
							unlink('./assets/images/'.$file['file_name']);
							$data['msg']="error!";
						}
					} else {
						unlink('./assets/images/'.$file['file_name']);
						$data['msg']="error!";
					}
					
				}
			} else {
				$foto_user = 'user.png';
				if($post['username']!=='user'){
					if($this->UserModel->save($foto_user)){
						$data['msg']="success";
					} else {
						$data['msg']="error!";
					}
				} else {
					$data['msg']="error!";
				}
			}

			
			
			
        }
			$data['listkelurahan'] = $this->KelurahanModel->getKelurahan();
			$data['user_logged'] = $this->session->userdata('user_logged');
			$this->load->view('admin/tambahpemilik',$data);
		
	}

	
}
