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

	class Hc_folders extends My_Model{
		protected $_table_name = 'hc_folders';
		protected $_order_by = 'hc_folders.id';

		public function __construct(){
			parent::__construct();
		}


		public function get_new(){
			$folders = new stdClass();
			$folders->folder_name = '';
			$folders->is_active = '1';

			//Find out order of slider
			$count_records = $this->folders->get_count(array("is_active != " => 2));
			$folders->display_order = $count_records + 1;

			return $folders;
		}


		public $rules = array(

			'folder_name' => array(
				'field' => 'folder_name', 
				'label' => 'folder name', 
				'rules' => 'trim|required|xss_clean'
			)
		);

		public function get_folder_search_count($search_params, $like_params){
			$this->db->join("hc_products", "hc_products.id = hc_folders.product_id");
			if(!empty($search_params)){
				$this->db->where($search_params);
			}
			$this->db->group_by("hc_folders.product_id, hc_products.title");

			if(!empty($like_params)){
				foreach ($like_params as $key => $value) {
					$this->db->like($key, $value);
				}
			}
			return $this->db->count_all_results($this->_table_name);
		}


		public function get_data_for_sorting(){
			$data = $this->db->get($this->_table_name)->result_array();

			$array = array();
			foreach ($data as $key => $value) {
				$array[$value['id']] = $value;
			}

			return $array;
		}

		public function save_order($array){
			if(count($array)){
				foreach ($array as $key => $value) {
					$data = array('id' => $value, 'display_order' => ($key + 1));
					$this->db->set($data)->where($this->_primary_key, $value)->update($this->_table_name);
				}
			}
		}

		public function get_data($data = array()){
			if(isset($data['id']) && $data['id'] != ""){
				if(isset($data["is_active"])){
					$this->db->where(array("is_active" => $data["is_active"]));
				}
				$result = $this->folders->get_by(array("id" => $data["id"]), TRUE);

				return $result;
			}

			return array();
		}

		public function getFolderStructure($folderId, $tfi_user_id) {
			$this->db->select('*');
	        $this->db->from($this->_table_name);
	        if(!$tfi_user_id){
				$this->db->where("has_lock = 0");
			}
	        $this->db->where('parent_id', $folderId);
	        $this->db->where(array("is_active" => 1));
	        return $this->db->get()->result();
		}

		public function getSubfolders($parentFolderId) {
			$this->db->select('*');
	        $this->db->from($this->_table_name);
	        $this->db->where('parent_id', $parentFolderId);
	        $this->db->where(array("is_active" => 1));
	        return $this->db->get()->result();
		}

		public function getFileData($fileId) {
			$this->db->select('file_name, folder_name');
		    $this->db->from($this->_table_name);
		    $this->db->where('id', $fileId);
		    $this->db->where(array("is_active" => 1));
		    $query = $this->db->get();

		    if ($query->num_rows() > 0) {
		    	$file_name = $query->row()->folder_name;
		    	$actual_file_name = $query->row()->file_name;
		    	$file_path = './uploads/media/source/' . $file_name;

		    	if (file_exists($file_path)) {
		    		$file_content = file_get_contents($file_path);
		    		return array(
		                'content' => $file_content,
		                'filename' => $actual_file_name  // You can modify this filename as needed
		            );
		    	}else{
		    		return null;
		    	}
		    }

		    return null;
		}

		public function get_product_size($product_id){
			$this->db->select_sum('file_size', 'total_file_size');
			$this->db->from('hc_folders');
			$this->db->where('product_id', $product_id);
			$this->db->where('is_active', 1);
			$this->db->where('type', 'file');

			$query = $this->db->get();

			if ($query->num_rows() > 0) {
			    $row = $query->row();
			    $totalFileSize = $row->total_file_size;
			    return $totalFileSize;
			} else {
			    $totalFileSize = 0; // or any default value
			    return $totalFileSize;
			}

		}
	}
?>