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

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

class province extends CI_Controller {

    private $userInfo = array(); 

    public function __construct()
    {
        parent::__construct();
        $this->load->model("user_model");
        $this->load->model("province_model");
        $this->user_model->check_login("admin");

        $this->userInfo = $this->user_model->userInfo("first_name,last_name");
    }

         public function check_useredit($pid,$pname,$ctry)
    {
     
    $this->db->select();
	$this->db->from('province');
	$this->db->where('province_id!=',$pid);
	$this->db->where('province_name',$pname);
	$this->db->where('country_id',$ctry);
	$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($pname,$ctry)
    {
    $this->db->select();
	$this->db->from('province');
	$this->db->where('province_name',$pname);
	$this->db->where('country_id',$ctry);
	$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("province/index");
        $data['edit_delete']=$this->user_model->checkEditDelete('province/index');
        
        $data['is_super'] = $this->user_model->is_super();
        $data['menu'] = $this->load_model->menu();
        
        $query = $this->db->query("SELECT *  FROM `province` inner join `country` on country.country_id = province.country_id  where province.is_delete='0' ");

        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $data['province'] = $query->result_array();
                //for calling views
        $b = $this->user_model->getbranch();
        $data['branch']=$this->db->select("*")->from("branch")->where('id',$b)->get()->result_array();
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view('province/province',$data);
    }

    public function save()
    {
            if($this->input->post())
            {
                $data = $this->input->post();
                $db_insert = $this->province_model->save($data);
                redirect("province/index","refresh");
                
            }
            else
            {
                redirect("province/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['province']=$this->province_model->edit($id);
                     $data['userInfo'] = $this->userInfo;
                     $data['base_url'] = base_url();
                            //for calling views             
                     $this->load->view('header',$data);
                     $this->load->view('sidebar',$data);
                     $this->load->view('province/province_edit',$data);
  
                }
                else
                {
                    redirect("province/index","refresh");

                }
            

      
    }

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

        }
        

   }
   public function delete($id)
   {
        if($this->uri->segment(3))
        {       
           $id = explode(SALT,json_decode(base64_decode($this->uri->segment(3))))[0];

           $this->province_model->delete($id);
           redirect('province/index','refresh');   
        }
        else
        {
           redirect('province/index','refresh');   

        }
   }

}
