<?php
/**
 * @copyright (C) 2016 - 2025 Holger Brandt IT Solutions
 * @license GNU/GPL, see license.txt
 * WP Mailster is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License 2
 * as published by the Free Software Foundation.
 *
 * WP Mailster is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WP Mailster; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 * or see http://www.gnu.org/licenses/.
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Not the droids
}

$page_sfx_attr = ''; // TODO FIXME DYNAMIC PARAMETER DRIVEN PARAMETER

$mailUtils 		= MstFactory::getMailUtils();
$dateUtils 		= MstFactory::getDateUtils();
$attachUtils 	= MstFactory::getAttachmentsUtils();

$model = MstFactory::getThreadModel();
$threadUtils = MstFactory::getThreadUtils();

if( isset($_REQUEST['id']) ) {
	$id = intval($_REQUEST['id']); // thread id
} else {
	$id = 0;
}
if( isset($_REQUEST['bl']) ) {
    $backlink = base64_decode( sanitize_text_field($_REQUEST['bl']) );
} else {
	$backlink = "";
}

$threadMails = $model->getData($id, true, true);
//$pagination = $model->getPagination();

$subject = $threadUtils->getThreadSubject($id);
?>

<div class="componentheading<?php echo $page_sfx_attr; ?>">
	<h2><?php echo $subject; ?></h2>
</div>
<div class="mailster_thread_backlink<?php echo $page_sfx_attr; ?>"><a href="<?php echo esc_url($backlink); ?>"><?php _e( 'Back to previous page', 'wpsmt-mailster'); ?></a></div>
<div class="mailster_thread contentpane<?php echo $page_sfx_attr; ?>">
	<table class="mailster_thread_conversation<?php echo $page_sfx_attr; ?> mailster_std_table" >
		<thead>
		</thead>
		<tbody>
		<?php
		for($i=0;$i<count($threadMails);$i++){
			$mail = &$threadMails[$i];
			$postId ='p' .  $mail->thread_id . '_' . $mail->id; // build unique permanent link target
			$permalink = "?subpage=mailsterthread&amp;id=" . $mail->thread_id;
			$attachments = $attachUtils->getAttachmentsOfMail($mail->id);
			$subject = (is_null($mail->subject) || strlen($mail->subject) == 0) ? '<em>'.__( '(No Subject)', 'wp-mailster' ).'</em>' : $mail->subject;
			?>
			<tr class="mailster_thread_conv_entry<?php echo $page_sfx_attr; ?> mailster_thread_conv_entry<?php echo $i; ?>">
				<td>
					<table class="mailster_mail<?php echo $page_sfx_attr; ?>" style="width:100%">
						<tbody>
						<tr class="mailster_mail_header_info<?php echo $page_sfx_attr; ?>">
							<td class="mailster_mail_from<?php echo $page_sfx_attr; ?>">
								<div class="mailster_mail_from_name<?php echo $page_sfx_attr; ?>"><?php echo esc_html($mail->from_name); ?></div>
								<div class="mailster_mail_from_email<?php echo $page_sfx_attr; ?>"><?php echo esc_html($mail->from_email); ?></div>
							</td>
							<td class="mailster_mail_subject<?php echo $page_sfx_attr; ?>">
								<div class="mailster_mail_subject_permalink<?php echo $page_sfx_attr; ?>">
									<a name="<?php echo esc_attr($postId); ?>"></a>
									<a href="<?php echo esc_url($permalink); ?>" ><?php echo esc_html($subject); ?></a>
								</div>
							</td>
							<td class="mailster_mail_date<?php echo $page_sfx_attr; ?>">
								<div class="mailster_mail_timestamp<?php echo $page_sfx_attr; ?>"><?php echo $dateUtils->formatDateAsConfigured($mail->receive_timestamp); ?></div>
							</td>
						</tr>
						<tr class="mailster_mail_attachments<?php echo $page_sfx_attr; ?>">
							<?php
							if(count($attachments)>0){
								?>
								<td class="mailster_mail_attachment_desc<?php echo $page_sfx_attr; ?>"><?php _e( 'Attachments', 'wp-mailster'); ?></td>
								<td class="mailster_mail_attachment_list<?php echo $page_sfx_attr; ?>" colspan="2">
									<table class="mailster_mail_attachment_link_tbl<?php echo $page_sfx_attr; ?>">
										<tbody>
										<tr class="mailster_mail_attachment_link_tbl_line<?php echo $page_sfx_attr; ?>">
											<?php
											$attachStr = '';
											$upload_dir = wp_upload_dir();
											$dir = $upload_dir["baseurl"];
											for($j=0; $j < count($attachments); $j++){
												$attach = &$attachments[$j];
												if($attach->disposition == MstConsts::DISPOSITION_TYPE_ATTACH){
													$filePath = $attach->filepath;
													$fileName = $attach->filename;
													$dwlLink = $dir.$filePath."/".$fileName;
													?>
													<td class="mailster_mail_attachment<?php echo $page_sfx_attr; ?>">
														<a href="<?php echo esc_url($dwlLink); ?>" ><?php echo rawurldecode($attach->filename); ?></a>
													</td>
													<?php
												}
											}
											?>
										</tr>
										</tbody>
									</table>
								</td>
								<?php
							}
							?>
						</tr>
						<tr class="mailster_mail_body_info<?php echo $page_sfx_attr; ?>">
							<td class="mailster_mail_content<?php echo $page_sfx_attr; ?>" colspan="3">
								<div class="mailster_mail_content_container<?php echo $page_sfx_attr; ?>">
									<?php
									if(is_null($mail->html) || strlen(trim($mail->html))<1){
										$body =  nl2br($mail->body);
										$content = $body;
									}else{
										$content = $mail->html;
										$content = $mailUtils->replaceContentIdsWithAttachments($content, $attachments);
									}
									echo $content;
									?>
								</div>
							</td>
						</tr>
						</tbody>
					</table>
				</td>
			</tr>
			<?php
		}
		?>
		</tbody>
	</table>
</div>