<?php
include_once("includes/email_functions.php");

function mail_settings() {
	
}

function send_mail_page($vars=null) {
	/*
		$vars['']
		$vars['to']
		$vars['subject']
		$vars['content']
		$vars['preview'] = true/false(default)
	*/
	include(LANGUAGEFILE);
	
	$returndata = "";
	
	if(!isset($vars['preview'])) {
		$vars['preview'] = false;
	}

	$returndata .= "<div class='center_container'>";
		// page heading
		$returndata .= "<div class='create_menu_heading mobile-zoom-100 font-bold'>";
			$returndata .= "@ ".$l_send_mail;
		$returndata .= "</div>";
		
		// email to
		$returndata .= "<div class='email_header_container'>";
		
			// Send button
			$returndata .= "<div class='mt-2 mb-2' style='padding:3px 8px;border:solid 2px;border-radius:8px;width:25%;margin:auto;box-shadow:4px 4px 2px lightgray' onClick='sendMail()'><img src='images/send_email.png' class='iconM' id='sendmail_icon'></div>";
		
			// Recipiants
			$recipiants = "";
			if(isset($vars['to'])) {
				$recipiants = $vars['to'];
			}
			$returndata .= "<div class='mt-2 mb-2'><span class='bluebox' style='margin-right:4px;'>".$l_to."</span><input type='text' size='80' name='emailTo' id='emailTo' value='".$recipiants."' placeholder='".$l_recipiants."'></div>";
			
			// Email subject
			$subject = "";
			if(isset($vars['subject'])) {
				$subject = $vars['subject'];
			}
			$returndata .= "<div class='mt-2 mb-2'><input type='text' size='80' name='emailSubject' id='emailSubject' value='".$subject."' placeholder='".$l_subject."'></div>";
			
			// Email body content
			$returndata .= "<div class='email_body_content_container'>";
			if($vars['preview']) {
				// Edit not possible
				$returndata .= "<div id='email_content_container'>".$vars['content']."</div>";
			} else {
				$returndata .= "<div id='email_content_container' contenteditable='true'>".$vars['content']."</div>";
			}
			$returndata .= "</div>";
			
		$returndata .= "</div>";
		
		
	$returndata .= "</div>";
	
	return $returndata;
}

function email_week_menu() {
	include(LANGUAGEFILE);
	include_once("includes/lunch/lunch_layout_components.php");
	include("includes/lunch/lunch_variables.php");

	$lunch = new lunch($_SESSION['id']);
	
	if(isset($_GET['week'])) {
		$vars['week'] = check_vars($_GET['week']);
	}
	
	if(isset($_GET['year'])) {
		$vars['year'] = check_vars($_GET['year']);
	}

	$returndata = array('preview' => true, 'to' => $week_menu_send_to_list, 'subject' => 'Smörmagasinet lounas valikko vko '.$vars['week'].'/'.$vars['year'].' ');
	
	$body_content = "<div style='width:100%;text-align:center;margin-top:5px;'><img src='".smormagasinet_logo()."' style='max-width:250px;'></div>";
	$body_content .= "<div style='width:100%;text-align:center;margin-top:5px;font-weight:bold;'>".$vars['week']." / ".$vars['year']."</div>";
	
	$week_dates = get_week_dates($vars['week'], $vars['year'], true);
	
	foreach($week_dates as $this_date => $this_date_data) {
		$week_day_nbr = date("N", strtotime($this_date));
		if($week_day_nbr > 5) continue;

		$date_data = get_date_data(0, $this_date);
		$set_not_serving_checked = "";
		$set_heading_css = "";
		$input_disabled = "";
		
		if(count($date_data)>0) {
			
			if($date_data) {
				$menu_data = $date_data['menu'];
				$date_data = $date_data['date_data'];
				
			}
			
			$date_data_id = $date_data['date_data_id'];
			
		}

		$body_content .= "<div style='width:98%;margin: 6px auto;padding:4px;border-bottom:solid 1px;text-align:left;'>";
		
		// Date and day
		$body_content .= "<div style='padding-left:25px;font-weight:bold;width:calc(100% - 25px);'>";
			$body_content .= "<div style='display:inline-block;width:140px;'>".$day_names[date("N", strtotime($this_date))]."</div>";
			$body_content .= "<div style='display:inline-block;'>".date("d.m.Y", strtotime($this_date))."</div>";
		$body_content .= "</div>";

		if($date_data['not_serving'] == 1) {
			$body_content .= "<div style='padding-left:85px;font-weight:bold;width:calc(100% - 85px);color:red;'>SULJETTU</div>";
		} else {
			$this_day_menu = get_todays_menu(array('date' => $this_date));
			
			foreach($shift_to_meal as $this_shift => $this_shift_name) {
				if($this_shift != "M") continue;
				
				$body_content .= "<div style='paddig-left:40px;width:calc(100% - 40px)'>";
					$body_content .= "<div style='width:30%;display:inline-block;'>".$this_shift_name."</div>";
					
					// Choise 1
					$body_content .= "<div style='width:70%;display:inline-block;text-aling:center;'>";
					if(isset($this_day_menu['menu'][$this_shift])) {
						foreach($this_day_menu['menu'][$this_shift] as $this_choise_id => $this_choise_data) {
							$choise_name = "";
							if((isset($this_choise_data['dish_name_fin']) ? $this_choise_data['dish_name_fin'] : "") != "")
								$choise_name = $this_choise_data['dish_name_fin'];
							elseif((isset($this_choise_data['dish_name_swe']) ? $this_choise_data['dish_name_swe'] : "") != "") {
								$choise_name = $this_choise_data['dish_name_swe'];
							}
							
							$body_content .= "<div style='display:inline-block;width:50%;'>";
								$body_content .= $choise_name;
							$body_content .= "</div>";
						}
					}
					$body_content .= "</div>";
					
				$body_content .= "</div>";
			}
			
		}
		
		$body_content .= "</div>";
		
	}
	
	$returndata['content'] = $body_content;
	
	return $returndata;
}

?>