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

class Customer extends CI_Controller {

	public function __construct() {
		parent::__construct();
		date_default_timezone_set("Asia/Kolkata");
		$this->load->model('Customer_model');
		
		if(!$this->session->userdata('logged_in')) {
			redirect(base_url('Login'));
		}

        if($this->session->userdata['user_type'] != 1){
            redirect(base_url());
        }
 	}

    public function addCustomerUser(){
        $template['page'] = 'Customer/add-customer-user';

        $template['page_title'] = "Add New Patient";
        $template['page_desc'] = "Create New Patient";

        $template['menu'] = "Patient Management";
        $template['sub_menu'] = "Add Patient";

        $this->load->view('template',$template);
    }
    
    public function listCustomerUsers(){
        $template['page'] = 'Customer/list-customer-users';

        $template['page_title'] = "View All Patients";
        $template['page_desc'] = "View and Manage All Patients";

        $template['menu'] = "Patients Management";
        $template['sub_menu'] = "View Patients";

        $template['customerData'] = $this->Customer_model->getCustomer();
        $this->load->view('template',$template);
    }

    public function createCustomer(){
        $err = 0;
        $errMsg = '';
        $flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'error');
        if(!isset($_POST) || empty($_POST)){
            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Customer/addCustomerUser'));
        }
        if($err == 0 && (!isset($_POST['first_name']) || empty($_POST['first_name']))){
            $err = 1;
            $errMsg = 'Provide a First Name';
        }
        else if($err == 0 && (!isset($_POST['last_name']) || empty($_POST['last_name']))){
            $err = 1;
            $errMsg = 'Provide a Last Name';
        }
        else if($err == 0 && (!isset($_POST['phone']) || empty($_POST['phone']))){
            $err = 1;
            $errMsg = 'Provide a Phone Number';
        }
        else if($err == 0 && (!isset($_POST['email']) || empty($_POST['email']))){
            $err = 1;
            $errMsg = 'Provide an Email ID';
        }
        else if($err == 0 && (!isset($_POST['date_of_birth']) || empty($_POST['date_of_birth']))){
            $err = 1;
            $errMsg = 'Provide Date Of Birth';
        }
        else if($err == 0 && (!isset($_POST['address']) || empty($_POST['address']))){
            $err = 1;
            $errMsg = 'Provide an Address';
        }
        else if($err == 0 && (!isset($_FILES['profile_image']) || empty($_FILES['profile_image']))){
            $err = 1;
            $errMsg = 'Provide Profile Picture';
        }
        $_POST['age'] = '';
        $_POST['profile_image'] = '';
        if($err == 0){
            $config = set_upload_service("assets/uploads/services");
            $this->load->library('upload');
            $config['file_name'] = time()."_".$_FILES['profile_image']['name'];
            $this->upload->initialize($config);
            if(!$this->upload->do_upload('profile_image')){
                $err = 1;
                $errMsg = $this->upload->display_errors();
            }else{
                $upload_data = $this->upload->data();
                $_POST['profile_image'] = $config['upload_path']."/".$upload_data['file_name'];
            }

            $_POST['age'] = $this->calculateAge($_POST['date_of_birth']);
            if($_POST['age'] < 0){
                $err = 1;
                $errMsg = 'Provide a valid date of birth';
            }
        }
        if($err == 1){
            $flashMsg['message'] = $errMsg;
            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Customer/addCustomerUser'));
        }
        $status = $this->Customer_model->createCustomer($_POST);
        if($status == 1){
            $flashMsg['class'] = 'success';
            $flashMsg['message'] = 'User Created';

            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Customer/listCustomerUsers'));
        }else if($status == 2){
            $flashMsg['message'] = 'Email ID already in use.';
        }else if($status == 3){
            $flashMsg['message'] = 'Phone Number already in use.';
        }
        $this->session->set_flashdata('message',$flashMsg);
        redirect(base_url('Customer/addCustomerUser'));
    }

    public function calculateAge($birthDate = ''){
        if(empty($birthDate))
            return;

        $birthDate = explode("/", $birthDate);
        $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md")
                ? ((date("Y") - $birthDate[2]) - 1)
                : (date("Y") - $birthDate[2]));
        return $age;
    }

    public function getCustomerData(){
        $return_arr = array('status'=>'0');
        if(!isset($_POST) || empty($_POST) || !isset($_POST['customer_id']) || empty($_POST['customer_id'])){
            echo json_encode($return_arr);exit;
        }
        $customer_id = decode_param($_POST['customer_id']);
        $customer_data = $this->Customer_model->getCustomer(array('customer_id'=>$customer_id));
        if(!empty($customer_data)){
            $return_arr['status'] = 1;
            $return_arr['customer_data'] = $customer_data;
        }
        echo json_encode($return_arr);exit;
    }

    function changeStatus($customer_id = '',$status = '1'){
        $flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'error');
        if(empty($customer_id)){
            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Customer/listCustomerUsers'));
        }
        $customer_id = decode_param($customer_id);
        $status = $this->Customer_model->changeStatus($customer_id,$status);
        if(!$status){
            $this->session->set_flashdata('message',$flashMsg);
        }
        redirect(base_url('Customer/listCustomerUsers'));
    }

    function editCustomer($customer_id = ''){
        $flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'error');
        if(empty($customer_id)){
            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Customer/listCustomerUsers'));
        }

        $template['page'] = 'Customer/add-customer-user';

        $template['menu'] = "Patient Management";
        $template['sub_menu'] = "Edit Patient";

        $template['page_desc'] = "Edit Patient Details";
        $template['page_title'] = "Edit Patient";
        $template['customer_id'] = $customer_id;
        
        $customer_id = decode_param($customer_id);
        $template['customer_data'] = $this->Customer_model->getCustomer(array('customer_id'=>$customer_id));

        $this->load->view('template',$template);
    }

    function updateCustomer($customer_id = ''){
        $flashMsg = array('message'=>'Something went wrong, please try again..!','class'=>'error');
        if(empty($customer_id)){
            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Customer/listCustomerUsers'));
        }
        $customerIdDec = decode_param($customer_id);

        $err = 0;
        $errMsg = '';
        if(!isset($_POST) || empty($_POST)){
            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Customer/addCustomerUser'));
        }
        if($err == 0 && (!isset($_POST['first_name']) || empty($_POST['first_name']))){
            $err = 1;
            $errMsg = 'Provide a First Name';
        }
        else if($err == 0 && (!isset($_POST['last_name']) || empty($_POST['last_name']))){
            $err = 1;
            $errMsg = 'Provide a Last Name';
        }
        else if($err == 0 && (!isset($_POST['phone']) || empty($_POST['phone']))){
            $err = 1;
            $errMsg = 'Provide a Phone Number';
        }
        else if($err == 0 && (!isset($_POST['email']) || empty($_POST['email']))){
            $err = 1;
            $errMsg = 'Provide an Email ID';
        }
        else if($err == 0 && (!isset($_POST['date_of_birth']) || empty($_POST['date_of_birth']))){
            $err = 1;
            $errMsg = 'Provide Date Of Birth';
        }
        else if($err == 0 && (!isset($_POST['address']) || empty($_POST['address']))){
            $err = 1;
            $errMsg = 'Provide an Address';
        }

        if($err == 0){
            $_POST['age'] = $this->calculateAge($_POST['date_of_birth']);
            if($_POST['age'] < 0){
                $err = 1;
                $errMsg = 'Provide a valid date of birth';
            }
        }

        if($err == 1){
            $flashMsg['message'] = $errMsg;
            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Customer/editCustomer/'.$customer_id));
        }
        $config = set_upload_service("assets/uploads/services");
        $this->load->library('upload');
        $config['file_name'] = time()."_".$_FILES['profile_image']['name'];
        $this->upload->initialize($config);
        if($this->upload->do_upload('profile_image')){
            $upload_data = $this->upload->data();
            $_POST['profile_image'] = $config['upload_path']."/".$upload_data['file_name'];
        }

        $status = $this->Customer_model->updateCustomer($customerIdDec,$_POST);
        if($status == 1){
            $flashMsg['class'] = 'success';
            $flashMsg['message'] = 'User Details Updated';

            $this->session->set_flashdata('message',$flashMsg);
            redirect(base_url('Customer/listCustomerUsers'));
        }else if($status == 2){
            $flashMsg['message'] = 'Email ID already in use.';
        }else if($status == 3){
            $flashMsg['message'] = 'Phone Number already in use.';
        }
        $this->session->set_flashdata('message',$flashMsg);
        redirect(base_url('Customer/editCustomer/'.$customer_id));
    }

}
?>