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

/**
 * Reports Model
 *
 * @package Models
 */
class Files_model extends EA_Model {
    /**
     *  Reports_Model constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('data_validation');
        $this->load->helper('string');
    }

    public function get_patient_files( $patientId = 0 )
    {   
		$select  = "SELECT * FROM ea_patient_files WHERE patient_id = '".$patientId."' ORDER BY file_id DESC";
		//echo $select;
		
		$query   = $this->db->query($select);
		$res     = $query->result_array();
		
		return $res;	
	}
	
    public function get_patient_files_withoutId()
    {   
		$select  = "SELECT * FROM ea_patient_files WHERE status = '1' AND CURDATE() > expiry_date ORDER BY file_id DESC";
		//echo $select;
		
		$query   = $this->db->query($select);
		$res     = $query->result_array();
		
		return $res;	
	}
	
	
	public function get_patientfileinfo($fuid) {
		$select = "SELECT * from ea_patient_files WHERE unique_id = '".$fuid."' LIMIT 1";
	    $query = $this->db->query($select);
		$res = $query->row_array();
		return $res;		
	}	

	public function get_customerinfo($cid) {
		$select = "SELECT * from ea_users where id = '".$cid."' LIMIT 1";
	    $query = $this->db->query($select);
		$res = $query->row_array();
		return $res;		
	}	

    /**
     * Insert a new file record to the database.
     *
     * @param array $file Associative array with the file's data. Each key has the same name with the database
     * fields.
     *
     * @return int Returns the id of the new record.
     *
     * @throws Exception If file record could not be inserted.
     */
    public function insert($files)
    {
        // Before inserting the customer we need to get the customer's role id
        // from the database and assign it to the new record as a foreign key.

        if ( ! $this->db->insert('patient_files', $files))
        {
            throw new Exception('Could not insert customer to the database.');
        }

        return (int)$this->db->insert_id();
    }


    /**
     * Update an existing patient file record in the database.
     *
     * @param array $file Associative array with the file's data. Each key has the same name with the database
     * fields.
     *
     * @return int Returns the updated record ID.
     *
     * @throws Exception If file record could not be updated.
     */
    public function update($files)
    {
		$this->db->where('file_id', $files['file_id']);

        if ( ! $this->db->update('patient_files', $files))
        {
            throw new Exception('Could not update files to the database.');
        }

        return (int)$files['file_id'];
    }

}
