<?php
    class MailSend{
        
        public $mail_array = [];
        private $attachment   = "";
        
        public function Mail(){
            // echo "<pre>";
            // print_r($this->mail_array);
            
            if(is_array($this->mail_array)){
                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'];
                	$to_name_fname = $this->mail_array['txt_firstname'];
                	$to_name_lname = $this->mail_array['txt_lastname'];
                	$to_name_member =$to_name_fname." ".$to_name_lname;
                	
                	
                	$to_emailmember = $this->mail_array['txt_email'];
                    $to_namemember = ucwords($this->mail_array['txt_firstname'])." ".ucwords($this->mail_array['txt_lastname']);
                    
                    
                    
                    #Recipient Email
                    $to_email = $this->mail_array['txt_recipientemail'];
                     $to_email_member = $this->mail_array['txt_email'];
                    
                   // $to_email = "salvisaurabh00@gmail.com";
                    #Subject
                    $subject = $this->mail_array['txt_subject']; 
                    $attachmentfilepath =GLOBAL_INCLUDE_PATH."assets/images/bg/";
                    $attachmentfilepath1 =DOC_ROOT."assets/images/bg/";
                     $attachment1 =$attachmentfilepath.$this->mail_array['txt_attachment1']; 
                     $attachment2 = $attachmentfilepath.$this->mail_array['txt_attachment2']; 
                     $attachment3 =$attachmentfilepath.$this->mail_array['txt_attachment3']; 
                     $attachment4 = $attachmentfilepath.$this->mail_array['txt_attachment4']; 
                     $attachment5 =$attachmentfilepath.$this->mail_array['txt_attachment5']; 
                     $attachmentnames = $this->mail_array['chk_collaterals']; 
                     $attachment2path = $attachmentfilepath1.$this->mail_array['txt_attachment2']; 
                     
                     for($i=0;$i<count($attachmentnames);$i++)
                     {
							$needinfoabout    						     .= $attachmentnames[$i].',';
						}
                    //die();
                    #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_attachment1")&&($key!="txt_attachment2")&&($key!="txt_attachment3")&&($key!="txt_attachment4")&&($key!="txt_attachment5")&&($key!="txt_formtype")&&($key!="txt_event")
        						&&($key!="txt_form_field1")&&($key!="txt_form_field2")&&($key!="chk_collaterals")&&($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>";
    					}	 
        			}
        			
        			
        			 #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 == 'Day1 Wed : 14 Sep 2022')
    			                            {
                                                
                                                        $tr1 .="<tr>
                                                                <td>$daydata</td>
                                                                <td>$timedata</td>
                                                                <td>$eventdata</td>
                                                            </tr>";
        			                            
    			                            }
    			                            elseif($daydata == 'Day2 Thu : 15 Sep 2022')
    			                            {
                                                
                                                        $tr2 .="<tr>
                                                                <td>$daydata</td>
                                                                <td>$timedata</td>
                                                                <td>$eventdata</td>
                                                            </tr>";
        			                           
    			                            }
    			                            elseif($daydata == 'Day3 Fri : 16 Sep 2022')
    			                            {
                                                
                                                        $tr3 .="<tr>
                                                                <td>$daydata</td>
                                                                <td>$timedata</td>
                                                                <td>$eventdata</td>
                                                            </tr>";
        			                            
    			                            }
                                        
                                     	$olddaydata = $daydata;
                                     	$oldtimedata = $timedata;
                                    }
                                }
                         $tablecltxt_formnamedisose ="</tbody></table>";
        			 $tabledata = $table.$tr1.$tr2.$tr3.$tableclose;
        			 
        			 $usermailsendcontain="<p>Dear $to_namemember,</p>
                                            <p>Thank you for showing interest in the MasterClass by Western Indian Culinary Association.</p>
                                              <p>Below is the complete schedule of your Annapoorna ANUFOOD MasterClass Session</p></br>.
                                            ";
        			 $usermailsendcontainfooter="<p>Regards</p><p>Annapoorna ANUFOOD India</p>";
        			 #Generate Mail Contecnt END 
        			 
        			 
        			if($this->mail_array['txt_formname'] == 'Sponsorship & Advertising' )
                    {
        			    $attachmentnamesdis     .= $needinfoabout;
			            $html_message .= $emailMsg."Downloded PDF : ".$attachmentnamesdis;
			            $html_message_member .= "<p>Dear ".$to_name_member.",</p>
																					<p>Thank you for showing interest in sponsorship & advertising. Please find attached sponsorship brochure.</p>
																					<p>Regards,</p>
																					<p>Annapoorna ANUFOOD India</p>";
                    }
                    elseif($this->mail_array['txt_formname']=='MasterClass by WICA')
        			{
        			    $html_messagemember .= $usermailsendcontain.$tabledata.$usermailsendcontainfooter;
        			    $html_message .= $emailMsg.$tabledata;
        			}
        		    /*elseif($this->mail_array['txt_formname']=='Conference By AFSTI (Mumbai Chapter)')
        			{
        			    $html_messagemember .= "<p>Dear ".$to_name_member.",</p>
												<p>Thank you for showing interest in conference by Association of Food Scientist & Technologists (India) - (Mumbai Chapter).</p>
												<p>Topic : The New Food Economy</p>
												<p>Date : Wed : 14 Sep 2022</p>
												<p>Time : 11.00 am – 3.00 pm</p>
												<p>Regards,</p>
												<p>Annapoorna ANUFOOD India</p>";
						 $html_message .= $emailMsg;
        			}*/
        			elseif($this->mail_array['txt_formtype']=='15')
        			{
        			    $html_messagemember .= "<p>Dear ".$to_name_member.",</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>Annapoorna - ANUFOOD India</p>";
						 $html_message .= $emailMsg;
        			}
        			
                    else
                    {
                        $html_message .= $emailMsg;
                    }
                    
                    if($this->mail_array['txt_form'] == ''){
                        
                        if(SENDMAIL_MODE == 0)
                        {
                        	$formname=mail_array['txt_formname'];
                        		/* if($this->mail_array['txt_formname'] == 'Sponsorship & Advertising')
                        		 {
                        		 		$sendmail = sendPHPMail($from_name, $from_email, $to_name_member, $to_email_member, $subject, $text_message, $html_message_member, $attachment1, $attachment2, $attachment3, $attachment4, $attachment5, $this->mail_array['txt_fromemail']);
                         
                        		 }*/
                        		 
                            if($this->mail_array['txt_formname']=='MasterClass by WICA' || $this->mail_array['txt_formtype']=='15')
            			    {
                                 $sendmailmemeber = sendPHPMail($from_name, $from_email, $to_namemember, $to_emailmember, $subject, $text_message, $html_messagemember, $this->attachment, $this->mail_array['txt_fromemail']);
            			          $sendmailadmin = sendPHPMail($from_name, $from_email, $to_name, $to_email, $subject, $text_message, $html_messagemember,'', '','','','', $this->mail_array['txt_email']);
                          
            			        
            			    } 
            			    
                            //$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']);
                           $sendmail = sendPHPMail($from_name, $from_email, $to_name, $to_email, $subject, $text_message, $html_message,'', '','','','', $this->mail_array['txt_email']);
                          // $sendmail = sendPHPMail($from_name, $from_email, $to_name, $to_email, $subject, $text_message, $html_message,$attachment1, $attachment2, $attachment3, $attachment4, $attachment5, $this->mail_array['txt_email']);
                           
                        }
                        
                        // var_dump($sendmail);
                        // die();
                        
                        if($sendmail){
                            $redirect = $this->mail_array['txt_redirect'];
                            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'] == 'Post show report Form' || $this->mail_array['txt_formname'] == 'Post show report 2019 Form') 
                        		 {
                           		 echo true;
                        		 }
                        		 else
                        		 {
                        		 	$this->showPopup($redirect);
                        		 }
                            
                        }
                    }
                
                }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){
        
         		echo "<script>alert('Your Request Has Been Sent !');</script>";
           	echo "<script>window.location.href='".$redirect."';</script>";
        }
        
    }
?>