<?php

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

/**
 * staff Class
 *
 * @package     edusol
 * @subpackage  country
 * @author      Atif Razzaq
 * @link        http://atifrazzaq.arteck.xyz
 */

class country extends CI_Controller {

    private $userInfo = array();

    public function __construct()
    {
        parent::__construct();
        $this->load->model("user_model");
        $this->load->model("country_model");
        $this->user_model->check_login("admin");
        $this->userInfo = $this->user_model->userInfo("first_name,last_name");
    }
    
     public function check_useredit($cid,$cname)
    {
     
    $this->db->select();
	$this->db->from('country');
	$this->db->where('country_id!=',$cid);
	$this->db->where('country_name',$cname);
	$this->db->where('is_delete',0);
	$query = $this->db->get();
	
   
	if($query->num_rows() > 0)
			{
				 
				 $data =  1;
			}
		else{
		   
		    $data =  0;
		    }
		echo json_encode($data);
    }
    
    
    public function check_user($cname)
    {
    $this->db->select();
	$this->db->from('country');
	$this->db->where('country_name',$cname);
	$this->db->where('is_delete',0);
	$query = $this->db->get();
   
	if($query->num_rows() > 0)
			{
				 
				 $data =  1;
			}
		else{
		   
		    $data =  0;
		    }
		echo json_encode($data);
    }
    
    
    

     public function index($q="all",$p=1)
    {
        $this->user_model->check_permissions("country/index");
        $data['edit_delete']=$this->user_model->checkEditDelete('country/index');
        $data['is_super'] = $this->user_model->is_super();
        $data['menu'] = $this->load_model->menu();
        $branch=$this->user_model->getbranch();
        $query = $this->db->query("SELECT * FROM `country` WHERE  is_delete='0'");
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $data['country'] = $query->result_array();
        $data['branch']=$this->db->select("*")->from("branch")->where('id',$branch)->get()->result_array();
                //calling view pages
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view('country/country',$data);
    }

    public function save()
    {
        if($this->input->post())
            {
            
                $data = $this->input->post();
               
              $c=$data['country'];
        $co=strtoupper($c);
//echo'<pre>';print_r($co);die;
if($co=='PAKISTAN')
 redirect("country/index?status=Country Name already available!","refresh");
                
$db_insert = $this->country_model->save($data);
                redirect("country/index","refresh"); 
            }
        else
            {
                redirect("country/index","refresh"); 

            }
        
    }

    public function edit($id)
    {
        if($this->uri->segment(3))
            {
                $data['menu'] = $this->load_model->menu();
                $id = explode(SALT,json_decode(base64_decode($this->uri->segment(3))))[0];
                $data['country']=$this->country_model->edit($id);
                $data['userInfo'] = $this->userInfo;
                $data['base_url'] = base_url();
                    //calling view pages
                $this->load->view('header',$data);
                $this->load->view('sidebar',$data);
                $this->load->view('country/country_edit',$data);

            }
         else
         {
                redirect("country/index","refresh"); 

         }   
             

      
    }

    public function update()
    {
        if($this->input->post())
        {
            $data = $this->input->post();
            $db_update = $this->country_model->update($data);
            redirect("country/index","refresh");
    
        }
        else
        {
            redirect("country/index","refresh");
        }
        

   }
   public function delete($id)
   {
        if($this->uri->segment(3))
        {
           $id = explode(SALT,json_decode(base64_decode($this->uri->segment(3))))[0];
            $this->country_model->delete($id);
            redirect('country/index','refresh');
     
        }
        else
        {
            redirect('country/index','refresh');
            
        }
          }

}
