<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
	
	
	class Login_check extends CI_Model 
	{
		
		function __construct()
		{
			// Call the Model constructor
			parent::__construct();
			$this->load->database();
			$this->load->library('session');
			
		}
		public function login($username,$password) 
		{
			$last_login_date=Date('Y-m-d'); 
			$admin_query = $this->db->get_where('tblSuperAdmin', array('username' => $username,'password'=>$password), 1);
			$admin_query->num_rows();
			if($admin_query->num_rows() ==1)
			{
				$row=$admin_query->row();
			 	$this->session->set_userdata('login_id', $row->superID);
				$this->session->set_userdata('login_type',$row->loginType);
			 	
				$sql = "update tblSuperAdmin set lastLogin='$last_login_date' where superID=$row->superID";//.$row->superID;
				$result = $this->db->query($sql);     
				
				return true;
			}
			
		}
		public function duplicate_contact($dup_contact) 
		{
			$this->db->from('tbl_customers');	
			$this->db->where('cust_contact',$dup_contact);
			$query = $this->db->get();
			$query->num_rows();
			if($query->num_rows()==1)
			{
				$row=$query->row();
				$conact_id=$row->customer_id;
				$contact_flag='1';
				
			}
			else{
				$contact_flag='0';
			}
			return $contact_flag;
		}
		
		
		
		
		
		
		function get_logged_in_employee_info()
		{
			if($this->is_logged_in())
			{
				return $this->get_info($this->session->userdata('user_id'));
			}
			
			return false;
		}
		
		
		
		
		public function save_reset_password($check_id,$clinic_username,$password,$check_flag)
		{
			
			if($check_flag=='rest')
			{	 
				$loginsql = "update tblClinicRegistration set  clinicUsername='".$clinic_username."',clinicPassword='".$password."'  where clinicID=".$check_id;	
				$this->db->query($loginsql); 	
				
				$this->db->from('tblClinicRegistration');	
				$this->db->where('clinicID',$check_id);
				$query = $this->db->get();
				$row=$query->row();
				
    		 	$active_flag=$row->activeFlag;
    			if($active_flag==0)
    			{
					$dbname=$row->clinicDbName; 
					
					$config_app['hostname'] = '173.225.105.132';
					$config_app['username'] = 'sa';
					$config_app['password'] = 'Ssc@123';
					$config_app['database'] = $dbname;
					$config_app['dbdriver'] = 'sqlsrv';
					$config_app['dbprefix'] = '';
					$config_app['pconnect'] = FALSE;
					$config_app['db_debug'] = TRUE;
					
					
					
					$this->db->close();		                  
					$DB2=$this->load->database($config_app); 
					//clinic details copy
					$reg_no=$row->regNo;
					$clinic_email=$row->clinicEmail;
					$clinic_contact=$row->clinicContact; 
					$clinic_name=$row->clinicName;
					$clinic_info=$row->clinicInfo;
					$fk_package_id=$row->fkPackageID;
					$website=$row->clinicWebsite;
					
					//contact person details
					$contact_person=$row->contactPerson;  
					$email=$row->contactEmail;
					$contact_no=$row->contactNumber;
					$clinicIconFile=$row->clinicIconFile; 
					$smsLink=$row->smsLink; 
					$payemntApiKey=$row->payemntApiKey; 
					$paymentSecretKey=$row->paymentSecretKey; 
					$newpatient=$row->newPatientDos; 
					$fkClinicID=$row->ClinicID; 
					$existingpatient=$row->existingPatientDos; 
					$bankAcc_name=$row->bankAccountName; 
					$accno=$row->accountNo; 
					$bank_name=$row->bankName; 
					$ifcsccode=$row->IFSCCode; 
					$bankbranch=$row->bankBranch; 
					$prescriptionTemplateID=$row->prescriptionTemplateID; 
					$prescriptionTemplateurl=$row->prescriptionTemplateurl; 
				 
					$log_type='admin';
					$role_id='1';
					$clinic_created_date=date('Y-m-d');
					 
					$sql3 = "INSERT INTO tblClinicMaster (clinicRegNo,clinicName,	clinicEmail,	clinicContact,	clinicCreateDate	,fkpackageID,	clinicInfo,	clinicWebsite,iconFile,smsLink,payemntApiKey,paymentSecretKey,fkClinicID,newPatientDos ,existingPatientDos ,bankAccountName,accountNo ,bankName ,IFSCCode,bankBranch,prescriptionTemplateID,prescriptionTemplateurl) values ('$reg_no','$clinic_name','$clinic_email','$contact_no','$clinic_created_date','$fk_package_id','$clinic_info','$website','$clinicIconFile','$smsLink','$payemntApiKey','$paymentSecretKey','$fkClinicID','$newpatient','$existingpatient','$bankAcc_name','$accno','$bank_name','$ifcsccode','$bankbranch','$prescriptionTemplateID','$prescriptionTemplateurl')";	
					
					$res=$this->db->query($sql3); 
					
					$clinic_id=$this->db->insert_id();	
					$user_qry = "INSERT INTO tblUserMaster (userName, userContact,	userEmail,regDate,roleID) values ('$contact_person','$contact_no','$email','$clinic_created_date','$role_id')";	
					$uid=$this->db->query($user_qry); 
					
    				$log_qry = "INSERT INTO tblLogin (logUserID,username,password,loginType) values ('$uid','$clinic_username','$password','$log_type')";	
					$this->db->query($log_qry); 
					
					
					$this->db->close();		                  
					$this->load->database(); 
                 	$branchQry= $this->db->query("select * from tblBranchMaster where fkClinicID=".$check_id); 
					$barr=array();
					foreach ($branchQry->result() as $row)
					{
						$branch_name=$row->branchName;
						$branch_address=$row->branchAddress; 
						$email=$row->branchEmail;
						$branch_contact=$row->branchContact;
						$branch_code=$row->branchCode;
						
						$this->db->close();		                  
						$this->db2=$this->load->database($config_app,true); 
						$branchsql = "INSERT INTO tblBranchMaster (branchName, branchAddress,branchEmail,branchContact,branchCode) values ('$branch_name','$branch_address','$email','$branch_contact' ,'$branch_code')";
						   $this->db2->query($branchsql);
						  $bid=$this->db2->insert_id();
						 array_push($barr, $bid);	
						 
						 $branch=implode(",",$barr);
					  	$bransql = "update tblUserMaster set  branchID='$branch' where userID=".$uid;	
						$this->db2->query($bransql); 	 
					}
				
					$this->db2->close();		                  
					$DB2=$this->load->database(); 
					$loginsql = "update  tblClinicRegistration set  activeFlag=1 where clinicID=".$check_id;	
					$this->db->query($loginsql); 	
					 redirect('https://e-vaidyak.com/clinic/');
				}
		    	
			}
			else
			{
				redirect(base_url() . 'index.php');
			}
			
		}
		
		
	}	
	
?>	