<?php
	
	defined('BASEPATH') OR exit('No direct script access allowed');
	require_once APPPATH."controllers/Secure_area.php";
	
	class Clinic extends Secure_area 
	{
		public $dynamicDB;
		function __construct() {
			// Call the Model constructor
			parent::__construct();
			$this->load->database();
			$this->load->model('ClinicModel');
			$this->load->model('MasterModel');
			
			$this->load->helper('url');
			$this->load->library('parser');
		}
		
		public function index() {
			$cliniclist = $this->ClinicModel->getClinics();
			$data['clinic_list'] = $cliniclist;
			$this->load->view('clinic/clinicDetails',$data);
		}
		function filename_exists()
		{
			$fieldval = $this->input->post('fieldval');
			$btnval = $this->input->post('btnval');
			$pageval = $this->input->post('pageval');
			$exists = $this->ClinicModel->filename_exists($fieldval,$btnval,$pageval);			
			$count = count($exists);
			echo $count; 
		}
		public function clinicRegistration() {
		/*  $packListData = $this->ClinicModel->getpackages();
			$data['packages_data'] = $packListData; 
		  */ 
		  
		 $tempadata = $this->ClinicModel->gettemplates();
			$data['template_data'] = $tempadata; 
			
			$this->load->view('clinic/clinicRegistration',$data);
		}
		
		
		
		public function saveRegistration() {
			$clinic_name = $this->input->post('clinic_name');
			$clinic_email = $this->input->post('clinic_email');
			$clinic_contact = $this->input->post('clinic_contact');
			$clinic_website = $this->input->post('clinic_website'); 
			$package_id = $this->input->post('package_id');
			$reg_no = $this->input->post('reg_no');
			
			$contact_person = $this->input->post("contact_person"); 		 
			$contact_person_email = $this->input->post('contact_person_email');
			$contact_person_contact = $this->input->post('contact_person_contact');
			
			$sms_link = $this->input->post('sms_link');
			$payment_secret_key = $this->input->post('payment_secret_key');
			$payment_api_key = $this->input->post('payment_api_key');
			
			
			$branch_id = $this->input->post('branch_id');
			$branch_name = $this->input->post('branch_name');
			$branch_address = $this->input->post('branch_address');
			$branch_email = $this->input->post('branch_email');
			$branch_contact = $this->input->post('branch_contact');
			
			$branch_code = $this->input->post('branch_code');
			
			$clinic_id_proof = $this->input->post('clinic_id_proof');
			$short_desc = $this->input->post('short_desc');
			//bank details
			
			$newpatient = $this->input->post('newpatient');
			$existingpatient = $this->input->post('existingpatient');
			$bankAcc_name = $this->input->post('bankAcc_name');
			$bank_name = $this->input->post('bank_name');
			$accno = $this->input->post('accno');
			$ifcsccode = $this->input->post('ifcsccode');
			$bankbranch = $this->input->post('bankbranch');
			$prescTemplate = $this->input->post('prescTemplate');
			
			 $type  = pathinfo($_FILES['photo_file']['name'], PATHINFO_EXTENSION);
			$photo_file = 'data:image/' . $type . ';base64,' . base64_encode(file_get_contents( $_FILES["photo_file"]["tmp_name"] ));
			
			
			
			
			// $this->input->post('photo_file');
			
			$no_of_branches				=$this->input->post('no_of_branches');
			$clinic_proof='';
			
			if(!empty($_FILES['clinic_id_proof']['name']))
			{
				$ext = pathinfo($_FILES['clinic_id_proof']['name'], PATHINFO_EXTENSION);
				
				$profileconfig['upload_path'] = './clinic_docs/';
				$profileconfig['allowed_types'] = 'gif|jpg|png|jpeg';
				$profileconfig['max_size'] = '2000000';
				$profileconfig['remove_spaces'] = false;
				$profileconfig['overwrite'] = false;
				$profileconfig['max_width'] = '';
				$profileconfig['max_height'] = '';
				$new_name  =rand(10,10000);
				$profileconfig['file_name'] = $new_name;
				$this->upload->initialize($profileconfig);
				if (!$this->upload->do_upload('clinic_id_proof')) {
					$error = array('error' => $this->upload->display_errors());
					$error['error'];
				}
				
				$clinic_proof =$new_name.".".$ext;   //  time().preg_replace("/['`^&*$#@!|()]/", "",str_replace(' ', '_',$_FILES['rest_profile_pic']['name']));
			}
			
			$clinic_reg_id = $this->input->post('clinic_reg_id');
			$this->input->post('add_clinic_btn');
			$cat_button='';
			if ($this->input->post('add_clinic_btn')) {
				$cat_button = 'add_clinic_btn';
				} else if ($this->input->post('update_user')) {
				$cat_button = 'update_clinic_btn';
			}
			
			
			$res = $this->ClinicModel->insertRegistration($reg_no,$contact_person,$contact_person_email,$contact_person_contact, $clinic_name, $clinic_website,$clinic_email, $clinic_contact, $short_desc,$package_id,$branch_name,$branch_address,$branch_email,$branch_contact,$cat_button,$clinic_reg_id,$no_of_branches,$branch_id,$branch_code,$photo_file,$clinic_proof,$sms_link,$payment_api_key,$payment_secret_key,$newpatient,$existingpatient,$bankAcc_name,$bank_name,$accno,$ifcsccode,$bankbranch,$prescTemplate);
		}	
		
		
		
		
		public function editClinic($id) {
			/* 	$packListData = $this->ClinicModel->getpackages();
				$data['packages_data'] = $packListData;
			*/	
			$branchdata = $this->ClinicModel->getbranchages($id);
			$data['branchdata'] = $branchdata;	
			$data['edit_data'] = $this->ClinicModel->editClinic($id);
			$this->load->view('clinic/clinicRegistration', $data, FALSE);
		}
		
		public function viewClinic($id) {
			$data['viewClinic'] = $this->ClinicModel->viewClinic($id);
			$branchdata = $this->ClinicModel->getbranchages($id);
			$data['branchdata'] = $branchdata;			
			$this->load->view('clinic/viewClinic', $data, FALSE);
		}
		
		public function deleteClinic($id) {
			$t=$this->ClinicModel->deleteClinic($id);
			$cliniclist = $this->ClinicModel->getClinics();
			$data['clinic_list'] = $cliniclist;
			
			$this->load->view('clinic/clinicDetails',$data);
		}	
		//package
		public function packageRegistration() {
			
			$this->load->view('clinic/newpackages');
		}
		public function packageDetails() {
			$packList = $this->ClinicModel->getpackages();
			$data['packages_list'] = $packList;
			
			$this->load->view('clinic/packageDetails',$data);
		}
		public function save_package() {
			$package_name = $this->input->post("package_name");
			$user_limit = $this->input->post('user_limit');
			$per_user_cost = $this->input->post('per_user_cost'); 
			$package_desc = $this->input->post('package_desc');
			$duration = $this->input->post('duration');
			$duration_type = $this->input->post('duration_type');
			
			
			$package_id = $this->input->post('package_id');
			$this->input->post('add_package_btn');
			$cat_button='';
			if ($this->input->post('add_package_btn')) {
				$cat_button = 'add_package_btn';
			}
			else if ($this->input->post('update_package_btn')) {
				$cat_button = 'update_package_btn';
			}
			$res = $this->ClinicModel->insert_newpackage($package_name, $user_limit, $per_user_cost, $package_desc,$duration, $duration_type,$cat_button,$package_id);
		}
		public function editpackage($id) {
			
			$data['edit_data'] = $this->ClinicModel->editpackage($id);
			$this->load->view('clinic/editPackage', $data, FALSE);
		}
		
		
		public function deletepackage($id) {
			$t=$this->ClinicModel->deletepackage($id);
			$packList = $this->ClinicModel->getpackages();
			$data['packages_list'] = $packList;
			
			$this->load->view('clinic/packageDetails',$data);
		}	
		
	}
	
?>
