<?php
error_reporting(0);
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * HeaderModel class.
 * 
 * @extends CI_Model
 */
class HeaderModel extends CI_Model {
	
	public function __construct() {
		
		parent::__construct();
		$this->load->database();
	}
	
	public function getData()
    {
        $offset = 0;
        $limit = 1;
        $sort = 'page';
        $order = 'DESC';
        $multipleWhere = '';
        $where = "";

        if (isset($_GET['offset']))
            $offset = $_GET['offset'];
        if (isset($_GET['limit']))
            $limit = $_GET['limit'];

        if (isset($_GET['sort']))
            if ($_GET['sort'] == 'page') {
                $sort = "page";
            } else {
                $sort = $_GET['sort'];
            }
        if (isset($_GET['order']))
            $order = $_GET['order'];

        if (isset($_GET['search']) and $_GET['search'] != '') {
            $multipleWhere = $_GET['search'];
        }
        $count_res = $this->db->select(' COUNT(id) as `total` ');

        if(!empty($multipleWhere)){
			$count_res->group_start();
			$count_res->or_like('page', $multipleWhere);
			/* $count_res->or_like('lastname', $multipleWhere);
			$count_res->or_like('email', $multipleWhere);
			$count_res->or_like('phone', $multipleWhere); */
			$count_res->group_end();
		}
        $brand_count = $count_res->get('header')->result_array();
        foreach ($brand_count as $row) {
            $total = $row['total'];
        }

        $search_res = $this->db->select(' * ');
        if(!empty($multipleWhere)){
			$search_res->group_start();
			$search_res->or_like('page', $multipleWhere);
			/* $search_res->or_like('lastname', $multipleWhere);
			$search_res->or_like('email', $multipleWhere);
			$search_res->or_like('phone', $multipleWhere); */
			$search_res->group_end();
		}
        if (isset($where) && !empty($where)) {
            $search_res->where($where);
        }

        $brand_search_res = $search_res->order_by("page", "ASC")->limit($limit, $offset)->get('header')->result_array();
        $bulkData = array();
        $bulkData['total'] = $total;
        $rows = array();
        $tempRow = array();

        foreach ($brand_search_res as $row) {
               $operate = '<a href="#" data-bs-toggle="modal" data-bs-target="#modalAdd" onclick=\'addForm('.$row['id'].',"Edit '.ucfirst($this->uri->segment(2)).'","modal-lg","addForm")\' title="Edit"><button class="dropdown-item"><i class="ri-pencil-fill align-bottom me-2 text-muted"></i> </button></a>';
               //$operate ='<a href="'.base_url($this->uri->segment(1)."/".$this->uri->segment(2)."/addFormPage/".$row['id']).'" title="Edit"><button class="dropdown-item"><i class="ri-pencil-fill align-bottom me-2 text-muted"></i> </button></a>';
               //$operate .= '<a href="#" data-bs-toggle="modal" data-bs-target="#modalView" onclick=\'viewForm('.$row['id'].',"modal-lg")\' title="View"><button class="dropdown-item"><i class="ri-eye-fill align-bottom me-2 text-muted"></i> </button></a>';
               $operate .= '<a href="#" data-bs-toggle="modal" data-bs-target="#modalDelete" onclick=\'deleteForm('.$row['id'].')\' title="Delete"><button class="dropdown-item"><i class="ri-delete-bin-fill align-bottom me-2 text-muted"></i> </button></a>';
            
            //$tempRow['id'] = $row['id'];
           // $tempRow['image'] = "<p><a href='".base_url().'/uploads/'.$row['image']. "' class='glightbox'> <img class='rounded' src='".base_url().'/uploads/'. $row['image']. "' width='200' height='auto'></a><script>var lightbox=GLightbox({selector:'.glightbox',title:false,});</script></p>";
            $tempRow['page'] = $row['page'];
            /* $tempRow['lastname'] = $row['lastname'];
            $tempRow['email'] = $row['email'];
            $tempRow['phone'] = $row['phone']; */
           	$tempRow['operate'] = $operate;
			$rows[] = $tempRow;
        }
        $bulkData['rows'] = $rows;
        print_r(json_encode($bulkData));
		
	}
	/* public function addDataProcess($data) {
		if($this->db->insert('header', $data)){
				return 1; 
			 }else{
				return 0;
		}
	} */
	public function addDataProcess($data,$files,$files1,$page) {
		if($this->db->insert('header', $data)){
		$header_id=$this->db->insert_id();
		}
		$csss = '';
		$tmp_name=array();
		//echo "<pre>";print_r($files);	exit;
			if (isset($files)) {
				//echo $tmp_name=$files['css']['tmp_name'][0];exit;
				foreach ($files['tmp_name'] as $i => $tmp_name) {
					if ($files['error'][$i] == UPLOAD_ERR_OK) {
						// Generate a unique file name
						$uniqueName = basename($files['name'][$i]);
						
						$filePath = "pages/".$page."/css/" . $uniqueName;
						if (move_uploaded_file($tmp_name, $filePath)) {
							$csss = $filePath;
							//array_push($datafile['csss'],$filePath);
						} else {
							echo "Failed to move file55: " . $files['name'][$i] . "<br>";
						}
					} else {
						echo "Error uploading CSS file: " . $files['error'][$i] . "<br>";
					}
					//$csss = $csss, ',');
					$data1=array("header_id"=>$header_id,"css"=>$csss);
					$this->db->insert('header_details', $data1);
				} 
			}else{
				return 0;
			}	
			$images = '';
			$tmp_name1=array();
			//echo "<pre>";print_r($files);	exit;
				if (isset($files1)) {
					//echo $tmp_name=$files['css']['tmp_name'][0];exit;
					foreach ($files1['tmp_name'] as $i => $tmp_name1) {
						if ($files1['error'][$i] == UPLOAD_ERR_OK) {
							// Generate a unique file name
							$uniqueName1 = basename($files1['name'][$i]);
							
							$filePath1 = "pages/".$page."/images/" . $uniqueName1;
							if (move_uploaded_file($tmp_name1, $filePath1)) {
								$images = $filePath1;
								//array_push($datafile['csss'],$filePath);
							} else {
								echo "Failed to move file: " . $files1['name'][$i] . "<br>";
							}
						} else {
							echo "Error uploading Image file: " . $files1['error'][$i] . "<br>";
						}
						//$csss = $csss, ',');
						$data2=array("header_id"=>$header_id,"image"=>$images);
						$this->db->insert('header_image_details', $data2);
					} 
					
				return 1; 
			}else{
				return 0;
			}
			
	}
	public function updateDataProcessImage($data,$id,$oldImage,$files,$page) {
		if($this->db->update('header', $data, array('id' => $id))){
		if(!empty($oldImage)){
			unlink($oldImage);
		}
		$header_id=$id;
		$csss = '';
		$tmp_name=array();
		//echo "<pre>";print_r($files);	exit;
			if (isset($files)) {
				//echo $tmp_name=$files['css']['tmp_name'][0];exit;
				foreach ($files['tmp_name'] as $i => $tmp_name) {
					if ($files['error'][$i] == UPLOAD_ERR_OK) {
						// Generate a unique file name
						$uniqueName = basename($files['name'][$i]);
						
						$filePath = "pages/".$page."/css/" . $uniqueName;
						if (move_uploaded_file($tmp_name, $filePath)) {
							$csss = $filePath;
							//array_push($datafile['csss'],$filePath);
						} else {
							echo "Failed to move file: " . $files['name'][$i] . "<br>";
						}
					} else {
						echo "Error uploading CSS file: " . $files['error'][$i] . "<br>";
					}
					//$csss = $csss, ',');
					$data1=array("header_id"=>$header_id,"css"=>$csss);
					$this->db->insert('header_details', $data1);
				} 
				
			return 1; 
			}/* else{
				$csss="";
			} */
			}else{
				return 0;
		}
	}
	public function updateDataProcess($data,$id)
	{
		if($this->db->update('header', $data, array('id' => $id))){
				return 1; 
			 }else{
				return 0;
		}
	}
	public function editData($id) {
		$this->db->select('*');
		$this->db->from('header');
		$this->db->where('id',$id);
		return $this->db->get()->result();
	}
	public function getPages() {
		$this->db->select('*');
		$this->db->from('pages');
		return $this->db->get()->result();
	}
	public function getHeaderDetails($id) {
		$this->db->select('*');
		$this->db->from('header_details');
		$this->db->where('header_id',$id);
		return $this->db->get()->result();
	}
	public function getHeaderImagedetails($id) {
		$this->db->select('*');
		$this->db->from('header_image_details');
		$this->db->where('header_id',$id);
		return $this->db->get()->result();
	}
	public function deleteImage($id) {
			 $this->db->select('image');
			 $this->db->from('header_image_details');
			 $this->db->where('id', $id);
			 $image=$this->db->get()->row('image');
			 if($this->db->delete('header_image_details',array('id' => $id))){
				//if($image != "no-user-image.jpg"){
				unlink($image);
				//}
				return 1; 
			 }else{
				return 0;
			}
	} 
	
	
	/* UPDATE WITH IMAGE */
	public function updateDataProcessImage1($data,$id,$oldImage)
	{   
		if($this->db->update('header', $data, array('id' => $id))){
				if(!empty($oldImage)){
				unlink('uploads/'.$oldImage);
				}
				return 1; 
			 }else{
				return 0;
		}
	}
	/* UPDATE WITH IMAGE END */
	
	public function deleteDataProcess($id) {
			 $this->db->where('id', $id);
			 if($this->db->delete('header')){
				return 1; 
			 }else{
				return 0;
			}
	}
	
	/* DELETE WITH IMAGE */
	 public function deleteDataProcessImage($id) {
			 $this->db->select('favicon');
			 $this->db->from('header');
			 $this->db->where('id', $id);
			 $favicon=$this->db->get()->row('favicon');
			 if($this->db->delete('header',array('id' => $id))){
				if($favicon != ""){
				unlink($favicon);
				}
				$this->db->select('css');
				$this->db->from('header_details');
				$this->db->where('header_id', $id);
			    $csss=$this->db->get()->result();
				foreach($csss as $cs){
				$css=$cs->css;	
				if($css != ""){
				unlink($css);
				}
				}
				$this->db->delete('header_details',array('header_id' => $id));

				$this->db->select('image');
				$this->db->from('header_image_details');
				$this->db->where('header_id', $id);
			    $images=$this->db->get()->result();
				foreach($images as $img){
				$image=$img->image;	
				if($image != ""){
				unlink($image);
				}
				}
				$this->db->delete('header_image_details',array('header_id' => $id));
				
				return 1; 
			 }else{
				return 0;
			}
	} 
	public function deleteCSS($id) {
			 $this->db->select('css');
			 $this->db->from('header_details');
			 $this->db->where('id', $id);
			 $css=$this->db->get()->row('css');
			 /* DATA UPDATE ONLY-- NOT DELETE */
			 //$data['isDelete'] = 1;
			 //if($this->db->update('header', $data, array('id' => $id))){
			 
			 /* DATA UPDATE ONLY --NOT DELETE END */
			 if($this->db->delete('header_details',array('id' => $id))){
				//if($image != "no-user-image.jpg"){
				unlink($css);
				//}
				return 1; 
			 }else{
				return 0;
			}
	} 
	/* DELETE WITH IMAGE END */
	
	/* public function deleteAll($id) {
		 $id = trim($id);
		 $ids=explode(",",$id);
		 $size=sizeof($ids);
		 for($i=0;$i<$size;$i++){
			 $this->db->where('id', $ids[$i]);
			 if($this->db->delete('header')){
			 }else{
				return 0;
			}
		}
		return 1; 
	} */
	
/* DELETE ALL WITH IMAGE */
	public function deleteAll($id) {
		 $id = trim($id);
		 $ids=explode(",",$id);
		 $size=sizeof($ids);
		 for($i=0;$i<$size;$i++){
			 $this->db->select('image');
			 $this->db->from('header');
			 $this->db->where('id', $ids[$i]);
			 $image=$this->db->get()->row('image');
			 //DATA UPDATE ONLY-- NOT DELETE
			 //$data['isDelete'] = 1;
			 //if($this->db->update('header', $data, array('id' => $ids[$i]))){
			 //DATA UPDATE ONLY-- NOT DELETE  END
			  if($this->db->delete('header',array('id' => $ids[$i]))){
				//if($image != "no-user-image.jpg"){
				unlink('profileimage/'.$image);
				//}
			 }else{
				return 0;
			}
		}
		return 1; 
	} 
	/* DELETE ALL WITH IMAGE END */
	public function checkDuplicateEmail($name) {
		$this->db->select('*');
		$this->db->from('header');
		$this->db->where('email', $name);
		//$this->db->where('isDelete', 0);
		return $this->db->get()->num_rows();
	}
	public function checkDuplicateEmailEdit($id,$name) {
		$this->db->select('*');
		$this->db->from('header');
		$this->db->where('email', $name);
		//$this->db->where('isDelete', 0);
		$this->db->where('id !=', $id);
		return $this->db->get()->num_rows();
	}
	public function checkDuplicatePagename($name) {
		$this->db->select('*');
		$this->db->from('header');
		$this->db->where('page_name', $name);
		//$this->db->where('isDelete', 0);
		return $this->db->get()->num_rows();
	}
	public function checkDuplicatePagenameEdit($id,$name) {
		$this->db->select('*');
		$this->db->from('header');
		$this->db->where('page_name', $name);
		//$this->db->where('isDelete', 0);
		$this->db->where('id !=', $id);
		return $this->db->get()->num_rows();
	}

}