<?php
    class MailSend{
        
        public $mail_array = [];
        private $attachment   = "";
        
        public function Mail(){
            //echo "<pre>";
            //print_r($this->mail_array);
            $id=session_id();      
            if(is_array($this->mail_array))
            {
                if($this->mail_array['txt_form_field1'] == '' && $this->mail_array['txt_form_field2'] == $id && $this->mail_array['txt_iagree'] == 'on')
                {
                      
                    #Sender Name  
                    $from_name = ucwords($this->mail_array['txt_fromname']);       
                
                    #Sender Email
                    $from_email = $this->mail_array['txt_email'];
                    $from_emailmember = $this->mail_array['txt_fromemail'];
                    $to_emailmember = $this->mail_array['txt_email'];
                    $to_namemember = ucwords($this->mail_array['txt_firstname'])." ".ucwords($this->mail_array['txt_lastname']);
                    
                    #Recipient Name
                    $to_name = ucwords($this->mail_array['txt_recipientname']);
                
                    #Recipient Email
                    $to_email = $this->mail_array['txt_recipientemail'];
                    //$to_email = "salvisaurabh00@gmail.com";
                    
                    #Subject
                    $subject = ucwords($this->mail_array['txt_subject']); 
                    
                    #Generate Mail Contecnt
                   $table ="<table border='1'>
                                <thead>
                                    <tr>
                                    <th>Day</th>
                                    <th>Time</th>
                                    <th>Event</th>
                                </tr>
                            </thead>";
                                                    
                    $event = $this->mail_array['txt_event'];            
    					        $eventcount= count($event);
                                if($eventcount > 0)
                                {
                                    $olddaydata = "";
                                    $oldtimedata = "";
                                    for($j=0;$j<$eventcount;$j++)
                                    {
                                        
                                       $eventexplod[$j] 				= explode('||',$event[$j]);
                                        $daydata 				= $eventexplod[$j][0];
                                        $timedata 				= $eventexplod[$j][1];
                                        $eventdata 				= $eventexplod[$j][2];
                                    
                                        
                                            /*if($daydata != $olddaydata) 
    			                            {
                                                    $tr .="<tr>
                                                            <td>$daydata</td>
                                                            <td>$timedata</td>
                                                            <td>$eventdata</td>
                                                        </tr>";
    			                            }
    			                            else
    			                            {
                                                    $tr .="<tr>
                                                            <td></td>
                                                            <td>$timedata</td>
                                                            <td>$eventdata</td>
                                                        </tr>";
    			                                
    			                            }*/
    			                            if($daydata == 'Day1:14-09-2022')
    			                            {
                                                
                                                        $tr1 .="<tr>
                                                                <td>$daydata</td>
                                                                <td>$timedata</td>
                                                                <td>$eventdata</td>
                                                            </tr>";
        			                            
    			                            }
    			                            elseif($daydata == 'Day2:15-09-2022')
    			                            {
                                                
                                                        $tr2 .="<tr>
                                                                <td>$daydata</td>
                                                                <td>$timedata</td>
                                                                <td>$eventdata</td>
                                                            </tr>";
        			                           
    			                            }
    			                            elseif($daydata == 'Day3:16-09-2022')
    			                            {
                                                
                                                        $tr3 .="<tr>
                                                                <td>$daydata</td>
                                                                <td>$timedata</td>
                                                                <td>$eventdata</td>
                                                            </tr>";
        			                            
    			                            }
                                        
                                     	$olddaydata = $daydata;
                                     	$oldtimedata = $timedata;
                                    }
                                }
                         $tableclose ="</tbody></table>";
        			 $tabledata = $table.$tr1.$tr2.$tr3.$tableclose; 
        			 
        			 $usermailsendcontain="<p>Dear ".$to_namemember.",</p>
                                            <p>Thank you for your interest in the Guided Factory Tour at ANUTEC - International FoodTec India 2022. </p>
                                             <p>Below is the complete schedule of your tour.</p></br>
                                            ";
        			 
        			 $usermailsendcontainfooter="<p>Note:- Please gather 10mins prior to the scheduled time at the guided factory tour information counter next to the registration area.</p><p>Regards</p><p>ANUTEC - International FoodTec India 2022.</p>";
                    foreach ($this->mail_array as $key => $value) 
        			{ 
        				if (($key!="btn_submit")&&($key!="txt_form")&&($key!="txt_fromname")&&($key!="txt_fromemail")&&($key!="txt_recipientemail")&&($key!="txt_recipientname")&&($key!="txt_subject")&&($key!="txt_redirect")&&($key!="verif_box")&&($key!="txt_form_field1")&&($key!="txt_form_field2")&&($key!="txt_formtype")&&($key!="txt_iagree")&&($key!="txt_event")&&($key!="txt_formnamedis")&&($key!="txt_date")&&($key!="txt_time")&&($key!="txt_topic")) // everything except record ID,title,submit 
    					{ 
    					    #Replce prefix of input field name
    					    $key_replace = str_replace(array('txt_', 'cmb_', 'sel_'), '', $key);
    					    
    					    #keep first letter is capital of key
    					    $keyval = ucfirst($key_replace);
    					   
    					    #Email content
    						$emailMsg.= "$keyval : $value <BR>";
    					}	 
        			}
        			if($this->mail_array['txt_formname']=='Guided Factory Tour')
        			{
        			    $html_messagemember .= $usermailsendcontain.$tabledata.$usermailsendcontainfooter;
        			    $html_message .= $emailMsg.$tabledata;
        			}
        			
        			else if($this->mail_array['txt_formname']=='Visitor Registration')
        			{
        			    $userVisitormailsendcontain="<p><span>Dear Industry Colleague,</span><br /><br />
        			    <span>We are excited to express our heartfelt thanks for your registration at the upcoming International Hardware Fair India! Your participation in this premier event is highly appreciated, and we can't wait to welcome you.</span>
        			    <br /><br /><span>Your Unique Registration Number (URN): ".$this->mail_array['txt_visitor_code']." </span><br />
        			                            <br /><span>Event Details:</span><br /><br />
        			                            <span>Dates : 2-4 December 2023</span><br />
        			                            <span>Venue : Pragati Maidan, New Delhi, India</span><br />
        			                            <span>&nbsp;</span><br />
        			                            <span>The International Hardware Fair India is going to be an exceptional gathering of industry leaders, innovators, and enthusiasts. Here's what you can look forward to:</span><br /><br />
        			                            <span>*Network with Key Players: Engage with renowned industry experts, decision-makers, and peers to exchange ideas, forge partnerships, and explore new business opportunities.</span><br /><br />
        			                            <span>*A complete showcase: Exhibiting companies from all the main product sectors will present their new products at the fair. A unique opportunity to start new and profitable business relationships.</span>
        			                            <br /><br /><span>*Discover Innovations: Explore an extensive range of innovative hardware products and technologies, enabling you to stay at the forefront of the rapidly evolving market.</span><br /><br />
        			                            <span>*An international platform: The only event in India with not only a national focus, but also aimed to reach buyers from all Asian, MENA and SAARC countries</span><br /><br />
        			                            <span>Stay updated on event news, exhibitors, and special announcements by following us on our social media channels</span><br /><br />
        			                            <span>We genuinely appreciate your support and participation. Let&rsquo;s make it a mega successful event.</span><br /><br />
        			                            <span>See you at the International Hardware Fair India!</span><br /><br /><span>&nbsp;</span><br /><br />
        			                            <span>Warm regards,</span><br /><br />
        			                            <span>Team IHF India</span><br /><br /><a href='http://www.hardwarefair-india.com/' target='_blank' data-saferedirecturl='https://www.google.com/url?q=http://www.hardwarefair-india.com&amp;source=gmail&amp;ust=1694755089827000&amp;usg=AOvVaw0cKxvWj_S_vkh08vmc4aKE' rel='noopener'>www.hardwarefair-india.com</a></p>
                                            ";
                                            
                        $html_messagemember .= $userVisitormailsendcontain;     
                        $html_message .= $emailMsg;
        			}
        			
        			/*elseif($this->mail_array['txt_formname']=='Conference By AIFPA National Seminar')
        			{
        			    $html_messagemember .= "<p>Dear ".$to_name_member.",</p>
												<p>Thank you for showing interest in conference by All India Food Processors' Association.</p>
												<p>Topic : Sustainability Of Food Industry Challenges & Solutions </p>
												<p>Date : Tue : 15 Sep 2022</p>
												<p>Time : 11.00 am – 03.00 pm</p>
												<p>Regards,</p>
												<p>ANUTEC - International FoodTec India.</p>";
						$html_message .= $emailMsg;
        			}*/
        			/*elseif($this->mail_array['txt_formname']=='Conference By IFCA')
        			{
        			    $html_messagemember .= "<p>Dear ".$to_name_member.",</p>
												<p>Thank you for showing interest in conference by .</p>
												<p>Topic : Packaging At Crossroads</p>
												<p>Date : Tue : 15 Sep 2022</p>
												<p>Time : 11.00 am – 06.00 pm</p>
												<p>Regards,</p>
												<p>ANUTEC - International FoodTec India.</p>";
					    $html_message .= $emailMsg;
        			}*/
        			elseif($this->mail_array['txt_formtype']=='15')
        			{
        			    $html_messagemember .= "<p>Dear ".$to_namemember.",</p> 
												<p>Thank you for showing interest in ".$this->mail_array['txt_formnamedis'].".</p>
												<p>Topic : ".$this->mail_array['txt_topic']."</p>
												<p>Date : ".$this->mail_array['txt_date']."</p>
												<p>Time : ".$this->mail_array['txt_time']."</p>
												<p>Regards,</p>
												<p>ANUTEC - International FoodTec India.</p>";   
						$html_message .= $emailMsg;
        			}
        			else
        			{
        			    $html_message .= $emailMsg;
        			}
			        
                    
                    if($this->mail_array['txt_form'] == ''){
                        
                        if(SENDMAIL_MODE == 0)
                        {
                        
                            /*if($this->mail_array['txt_formname']=='Guided Factory Tour' || $this->mail_array['txt_formname']=='Conference By AIFPA National Seminar'
                                || $this->mail_array['txt_formtype']=='15')*/
                            if($this->mail_array['txt_formname']=='Guided Factory Tour' || $this->mail_array['txt_formtype']=='15')
            			    {
                            //    $sendmail = sendmail($from_name, $from_email, $to_name, $to_email, $subject, $text_message, $html_message, $this->attachment, $this->mail_array['txt_email']);
                                $sendmailmemeber = sendPHPMail($from_name, $from_emailmember, $to_namemember, $to_emailmember, $subject, $text_message, $html_messagemember, $this->attachment, $this->mail_array['txt_email']);
            			        $sendmailadmin = sendPHPMail($from_name, $from_emailmember, $to_name, $to_email, $subject, $text_message, $html_messagemember, $this->attachment, $this->mail_array['txt_email']);
        			   
            			        
            			    } 
            			    else if($this->mail_array['txt_formname']=='Visitor Registration')
            			    {
            			       $subject_member="Thank You for Registering at International Hardware Fair India!";
                              $sendmailmemeber = sendPHPMail($from_name, $from_emailmember, $to_namemember, $to_emailmember, $subject_member, $text_message, $html_messagemember, $this->attachment, $this->mail_array['txt_email']);
            			     // $to_email_evergrow="salvisaurabh00@gmail.com,rajivatre@gmail.com,poo1706@gmail.com ";
            			     // $sendmail = sendPHPMail($from_name, $from_email, $to_name, $to_email_evergrow, $subject, $text_message, $html_message, $this->attachment, $this->mail_array['txt_email']); 
        			   
            			    
            			    }   
            			    
        			     $sendmail = sendPHPMail($from_name, $from_email, $to_name, $to_email, $subject, $text_message, $html_message, $this->attachment, $this->mail_array['txt_email']); 
        			   
                        }  
                        
                        if($sendmail){
                            $redirect = $this->mail_array['txt_redirect'];
                            $redirectform = $this->mail_array['txt_formname'];
                                if($this->mail_array['txt_formname'] == 'Sponsorship & Advertising' || $this->mail_array['txt_formname'] == 'Exhibitor Registration' ||  $this->mail_array['txt_formname'] == 'Brochure Form' ||  $this->mail_array['txt_formname'] == 'Visitor Registration')
                        		 {
                           		    echo true;
                        		 }
                        		 else
                        		 {
                        		 	$this->showPopup($redirect,$redirectform);
                        		 }
                        }
                    }
                
                }
                else if(isset($this->mail_array['verif_box']) && md5($this->mail_array['verif_box']).'a4xn' == $_COOKIE['tntcon'])
                {
                   
                    #Sender Name
                    $from_name = $this->mail_array['txt_fromname'];                 
                
                    #Sender Email
                    $from_email = $this->mail_array['txt_email'];
                    
                    #Recipient Name
                    $to_name = $this->mail_array['txt_recipientname'];          
                
                    #Recipient Email
                    $to_email = $this->mail_array['txt_recipientemail'];        
                     //$to_email = "salvisaurabh00@gmail.com";
                    #Subject
                    $subject = $this->mail_array['txt_subject']; 
                    
                    #Generate Mail Contecnt
                    foreach ($this->mail_array as $key => $value) 
        			{ 
        				if (($key!="btn_submit")&&($key!="txt_form")&&($key!="txt_fromname")&&($key!="txt_fromemail")&&($key!="txt_recipientemail")&&($key!="txt_recipientname")&&($key!="txt_subject")&&($key!="txt_redirect")&&($key!="verif_box")&&($key!="txt_form_field1")&&($key!="txt_form_field2")&&($key!="txt_formtype")&&($key!="txt_iagree")) // everything except record ID,title,submit 
    					{ 
    					    #Replce prefix of input field name
    					    $key_replace = str_replace(array('txt_', 'cmb_', 'sel_'), '', $key);        
    					    
    					    #keep first letter is capital of key
    					    $keyval = ucfirst($key_replace);
    					    
    					    #Email content
    						$emailMsg.= "$keyval : $value <BR>";      
    					}	 
        			}
			        $html_message .= $emailMsg;         
                    
                    if($this->mail_array['txt_form'] == ''){
                        
                        if(SENDMAIL_MODE == 0){
                        
                           //$sendmail = sendmail($from_name, $from_email, $to_name, $to_email, $subject, $text_message, $html_message, $this->attachment, $this->mail_array['txt_email']);
                            $sendmail = sendPHPMail($from_name, $from_email, $to_name, $to_email, $subject, $text_message, $html_message, $this->attachment, $this->mail_array['txt_email']);
                           
                        }
                        
                        if($sendmail){
                            $redirect = $this->mail_array['txt_redirect'];
                            $redirectform = $this->mail_array['txt_formname'];
                            $redirect = $this->mail_array['txt_redirect'];
                                if($this->mail_array['txt_formname'] == 'Sponsorship & Advertising' ||  $this->mail_array['txt_formname'] == 'Brochure Form'  ||  $this->mail_array['txt_formname'] == 'Show Catalogue 2023')
                        		 {
                           		    echo true;
                        		 }              
                        		 else
                        		 {
                        		 	$this->showPopup($redirect,$redirectform);
                        		 }
                            
                        }
                    }
                
                }                                   
                else{
                    $_SESSION['codeerror'] = 'Please enter the correct code';
                    $redirect = $this->mail_array['txt_redirect'];
                    echo "<script>window.location.href='".$redirect."';</script>";
                }
            }
            
        }
        
        public function showPopup($redirect,$redirectform=''){
            echo "<script>alert('Your Request Has Been Sent !');</script>";
            if($redirectform == 'Catalogue')
            {
                //echo "test ".$redirect;
                echo "<script>window.location.href='".$redirect."';</script>";
                //echo "<script>window.open('".$redirect."','_blank');";
                
            }
            else
            {
                echo "<script>window.location.href='".$redirect."';</script>";
            }
            
        }
        
    }
?>