<?php

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

class Customernotes_controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        // Your own constructor code

    }

    public function index($customer_id){

        $data['nav'] = 'reception';
        $data['subnav'] = 'scheduler';

        $data['customer_id'] = $customer_id;
        
        $this->load->view('includes/header', $data);
        $this->load->view('clinical/customer_notes');
        $this->load->view('includes/footer');


    }

    public function get_customer_notes() {
        
        $customer_id = $this->input->post('customer_id');

        $sql = "SELECT id_customer_notes, date_format(note_date,'%d-%m-%Y %H:%i') note_date, customer_notes.created_on, ifnull(note_owner, '') note_owner, note_type, 
            note_status, id_customers, customer_name, customer_cell, 
            Case When customers.customer_gender = 'F' then 'Female' 
            When customers.customer_gender = 'M' Then 'Male' Else 'NA' End as customer_gender, 
            ifnull(customer_notes.age, '') age, ifnull(customer_notes.height, '') height, ifnull(customer_notes.weight, '') weight, ifnull(customer_notes.blood_pressure, '') blood_pressure,
            date_format(customer_note_detail.detail_date,'%d-%m-%Y %H:%i') detail_date, customer_note_detail.detail_added_by, 
            CONCAT('-', replace(customer_note_detail.detail_text,'.','<br>-')) detail_text, note_detail_status
            FROM 
            customer_notes
            JOIN customer_note_detail on customer_notes.id_customer_notes = customer_note_detail.customer_note_id
            JOIN customers on customers.id_customers = customer_notes.customer_id            
            WHERE customer_id = ".$customer_id." 
            ORDER BY note_date DESC, customer_note_detail.detail_date DESC
            ";



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

        $data = $query->result_array();
        if(sizeof($data) > 0) {        
            echo(json_encode($data));
        }else {
            $sql = "SELECT '' id_customer_notes, '' note_date, '' created_on, '' note_owner, '' note_type, 
            '' note_status, id_customers, customer_name, customer_cell, 
            Case When customers.customer_gender = 'F' then 'Female' 
            When customers.customer_gender = 'M' Then 'Male' Else 'NA' End as customer_gender, 
            '' age, '' height, '' weight, '' blood_pressure,
            '' detail_date, '' detail_added_by, 
            '' detail_text, '' note_detail_status
            FROM 
             customers 
            WHERE id_customers = ".$customer_id."             
            ";

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

            $data = $query->result_array();
            echo(json_encode($data));
        }
        
    }    
    
    public function add_new_note(){

        $detail_text = urldecode($this->input->post('detail_text'));
        $customer_id = $this->input->post('customer_id');


        $data = array(

            'customer_id'=>$customer_id,
            'business_id'=> $this->session->userdata("businessid"),
            'note_owner'=> $this->session->userdata("username"),
            'created_by'=> $this->session->userdata("username"),
            'blood_pressure'=> $this->input->post('customer_bp'),
            'height'=> $this->input->post('customer_height'),
            'weight'=> $this->input->post('customer_weight'),
            'age'=> $this->input->post('customer_age'),
            'note_type'=> 'Doctors Note'
            
        );

        $this->db->insert('customer_notes', $data);

        $note_id = $this->db->insert_id();
        
        $data = array(
            'customer_note_id' => $note_id,
            'detail_text' => $detail_text,
            'detail_added_by' => $this->session->userdata('username')                
        );
        $this->db->insert('customer_note_detail', $data);      
        echo('success|'. $this->db->insert_id());      
        
    }

    public function add_note_detail(){
       // exit();
        $note_id = $this->input->post('note_id');
        $detail_text = urldecode($this->input->post('detail_text'));


        if(null != $note_id && $note_id > 0){
            $data = array(
                'customer_note_id' => $note_id,
                'detail_text' => $detail_text,                
                'detail_added_by' => $this->session->userdata('username')                
            );
            $this->db->insert('customer_note_detail', $data);      


            $this->db->where('id_customer_notes', $note_id);
            $this->db->set('blood_pressure', $this->input->post('customer_bp'));
            $this->db->set('height', $this->input->post('customer_height'));
            $this->db->set('weight', $this->input->post('customer_weight'));
            $this->db->set('age', $this->input->post('customer_age'));            
            $this->db->update('customer_notes');      


            echo('success|'. $this->db->insert_id());      
        }else{
            echo('error');
        }

        
    } 


    public function update_customer(){

        $id_customers = $this->input->post('id_customers');

        $this->db->where('id_customers = ', $id_customers);
        $this->db->set('age', $this->input->post('customer_age'));
        $this->db->set('height', $this->input->post('customer_height'));
        $this->db->set('weight', $this->input->post('customer_weight'));
        $this->db->set('blood_pressure', $this->input->post('customer_bp'));

        $this->db->update('customer_notes');

        echo 'success';

    }

    public function customer_notes_list(){
        
        $data['nav'] = 'reception';
        $data['subnav'] = 'scheduler';

        $this->load->view('includes/header', $data);
        $this->load->view('clinical/customer_notes_list');
        $this->load->view('includes/footer');

    }

    public function get_customer_notes_list(){      

        $data['draw']= $this->input->post('draw');
        $data['$start'] = $this->input->post('start');
        $data['$length'] = $this->input->post('length');

        $customer_name = $this->input->post('customer_name');
        $note_date = $this->input->post('note_date');
        $created_by = $this->input->post('created_by');
        $note_type = $this->input->post('note_type');
        $note_status = $this->input->post('note_status');
        $detail_text = $this->input->post('detail_text');
        

        $this->db->select("count(*) count");                        
        $this->db->join('customers','customer_id= id_customers');        
        $this->db->where('customer_notes.business_id = ', $this->session->userdata('businessid'));        
        
        $where = "";
        if($customer_name!=null)
        {
            if($where != ''){$where=$where.' and ';}
            $where = $where . " customers.customer_name like '%". $customer_name."%' ";
        }
        if($note_date!=null){
            if($where != ''){$where=$where.' and ';}
            $where = $where . " note_date = '". $note_date."' ";
        }
        if($created_by!=null){
            if($where != ''){$where=$where.' and ';}
            $where = $where . " created_by like '%". $created_by."%' ";
        }
        if($note_type!=null){
            if($where != ''){$where=$where.' and ';}
            $where = $where . " note_type like '%". $note_type."%' ";
        }
        if($note_status!=null){
            if($where != ''){$where=$where.' and ';}
            $where = $where . " note_status like '%". $note_status."%' ";
        }
        if($detail_text!=null){
            if($where != ''){$where=$where.' and ';}
            $where = $where . " detail_text like '%". $detail_text."%' ";
        }

        $querycount = $this->db->get('customer_notes');

       
        $this->db->select("id_customer_notes, customer_name, date(note_date) note_date, date_format(customer_notes.created_on,'%d-%m-%Y %H:%i') created_on, customer_notes.created_by, note_type, note_status ");        
        $this->db->select("concat('<div style=\'font-size:14px\'>', group_concat(concat(detail_text, ' ', date_format(detail_date,'%d-%m-%y %H:%i'), '<br>By:', detail_added_by, '<br><br>') ORDER BY detail_date DESC SEPARATOR ' '),  '</div>') detail_text, note_detail_status", true);
        $this->db->select('concat("<a href=\''.base_url().'customer_notes/", id_customers, "\' target=\'_blank\' class=\'btn btn-sm btn-info\'><i class=\'fa fa-icon fa-keyboard-o\'></i></a>") action');        
        $this->db->join('customer_note_detail','customer_note_id=id_customer_notes');
        $this->db->join('customers','customer_id = id_customers');        
        $this->db->where('customer_notes.business_id = ', $this->session->userdata('businessid'));
        if($where != ''){
            $this->db->where($where);
        }
        $this->db->group_by('id_customer_notes');
        $this->db->order_by('id_customer_notes, id_customer_note_detail');
        $query = $this->db->get('customer_notes');
        // echo $query;
        // exit();

        $total= $querycount->row(); 
        //print_r($total); exit();
        $data['data'] = $query->result_array();
        
        $data['filtered']=  sizeof($data['data']);
        $data['recordsTotal'] = $total->count;
        $data['recordsFiltered'] = sizeof($data['data']);
        
        echo(json_encode($data));
    
    }
    
}



   