<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * PreviewModel class.
 * 
 * @extends CI_Model
 */
class PreviewModel extends CI_Model {
	
	public function __construct() {
		
		parent::__construct();
		$this->load->database();
	}
	
	public function getData()
    {
        $offset = 0;
        $limit = 1;
        $sort = 'page_name';
        $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_name') {
                $sort = "page_name";
            } 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_name', $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('pages')->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_name', $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_name", "ASC")->limit($limit, $offset)->get('pages')->result_array();
        $bulkData = array();
        $bulkData['total'] = $total;
        $rows = array();
        $tempRow = array();

        foreach ($brand_search_res as $row) {
            $hyperlink=base_url().strtolower(str_replace(" ","",$row['page_name']));
			$operate = '<a href="'.$hyperlink.'" target="_blank" title="Preview Page"><button class="dropdown-item"><i class="ri-eye-fill align-bottom me-2 text-muted"></i> </button></a>';
			$operate .= '<a href="#" onclick=\'copyLink("'.$hyperlink.'")\' title="Copy Link"><button class="dropdown-item"><i class="ri-link align-bottom me-2 text-muted" aria-hidden="true"></i> </button></a>';
			$tempRow['page_name'] = $row['page_name'];
            $tempRow['hyperlink'] = $hyperlink;
             /* $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('pages', $data)){
				return 1; 
			 }else{
				return 0;
		}
	}
	public function updateDataProcess($data,$id)
	{
		if($this->db->update('pages', $data, array('id' => $id))){
				return 1; 
			 }else{
				return 0;
		}
	}
	public function editData($id) {
		$this->db->select('*');
		$this->db->from('pages');
		$this->db->where('id',$id);
		return $this->db->get()->result();
	}
	
	
	
	/* UPDATE WITH IMAGE */
	public function updateDataProcessImage($data,$id,$oldImage)
	{   
		if($this->db->update('pages', $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);
			 $this->db->select('page_name');
			 $this->db->from('pages');
			 $this->db->where('id', $id);
			 $page_name=$this->db->get()->row('page_name');
			 if($this->db->delete('pages',array('id' => $id))){
				if($page_name != ""){
				$this->deleteDirectory("pages/".$page_name);
				}
				return 1; 
			 }else{
				return 0;
			}
	}
	public function deleteDirectory($dirPath) {
	   if (is_dir($dirPath)) {
		  $files = scandir($dirPath);
		  foreach ($files as $file) {
			 if ($file !== '.' && $file !== '..') {
				$filePath = $dirPath . '/' . $file;
				if (is_dir($filePath)) {
				   $this->deleteDirectory($filePath);
				} else {
				   unlink($filePath);
				}
			 }
		  }
		  rmdir($dirPath);
	   }
	}
	/* public function deleteDirectory($dirPath) {
	   $files = glob($dirPath . '/*');
	   foreach ($files as $file) {
		  if (is_dir($file)) {
			 $this->deleteDirectory($file);
		  } else {
			 unlink($file);
		  }
	   }
	   rmdir($dirPath);
	} */
	/* DELETE WITH IMAGE */
	 public function deleteDataProcessImage($id) {
			 $this->db->select('image');
			 $this->db->from('pages');
			 $this->db->where('id', $id);
			 $image=$this->db->get()->row('image');
			 /* DATA UPDATE ONLY-- NOT DELETE */
			 //$data['isDelete'] = 1;
			 //if($this->db->update('pages', $data, array('id' => $id))){
			 
			 /* DATA UPDATE ONLY --NOT DELETE END */
			 if($this->db->delete('pages',array('id' => $id))){
				//if($image != "no-user-image.jpg"){
				unlink('uploads/'.$image);
				//}
				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('pages')){
			 }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('pages');
			 $this->db->where('id', $ids[$i]);
			 $image=$this->db->get()->row('image');
			 /* DATA UPDATE ONLY-- NOT DELETE */
			 //$data['isDelete'] = 1;
			 //if($this->db->update('pages', $data, array('id' => $ids[$i]))){
			 /* DATA UPDATE ONLY-- NOT DELETE  END */
			 /* if($this->db->delete('pages',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('pages');
		$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('pages');
		$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('pages');
		$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('pages');
		$this->db->where('page_name', $name);
		//$this->db->where('isDelete', 0);
		$this->db->where('id !=', $id);
		return $this->db->get()->num_rows();
	}

}