<?php
$wrapper_width = (int) get_post_meta( $certificate_id, '_certificate_content_wrapper_width', true );
$wrapper_font_family = get_post_meta( $certificate_id, '_certificate_content_wrapper_font_family', true );
$wrapper_textalign = get_post_meta( $certificate_id, '_certificate_content_wrapper_textalign', true );
if ( empty( $wrapper_textalign ) ) {
	$wrapper_textalign = 'left';
}
$wrapper_text_color = get_post_meta( $certificate_id, '_certificate_content_wrapper_font_color', true );
if ( empty( $wrapper_text_color ) ) {
    $wrapper_text_color = '#00305b';
}

$wrapper_font_size = (int) get_post_meta( $certificate_id, '_certificate_content_wrapper_font_size', true );
if ( ! $wrapper_font_size ) {
	$wrapper_font_size = 18;
}

$wrapper_mt = get_post_meta( $certificate_id, '_certificate_content_wrapper_mt', true );
if ( $wrapper_mt == '0' || $wrapper_mt ) {
	$wrapper_mt .= 'pt';
}
$wrapper_mb = get_post_meta( $certificate_id, '_certificate_content_wrapper_mb', true );
if ( $wrapper_mb == '0' || $wrapper_mb ) {
	$wrapper_mb .= 'pt';
}
$wrapper_ml = get_post_meta( $certificate_id, '_certificate_content_wrapper_ml', true );
if ( ! $wrapper_ml ){
	$wrapper_ml = 'auto';
}
$wrapper_mr = get_post_meta( $certificate_id, '_certificate_content_wrapper_mr', true );
if ( ! $wrapper_mr ){
	$wrapper_mr = 'auto';
}

//Student
$student_name_font_family = get_post_meta( $certificate_id, '_certificate_student_name_font_family', true );
$student_name_font_size = (int) get_post_meta( $certificate_id, '_certificate_student_name_font_size', true );
if ( ! $student_name_font_size ) {
	$student_name_font_size = 18;
}

?>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>John Doe's Certificate</title>
    <style type="text/css">
        /* Certificate Body, reset */
        @page {
            margin-top: 0;
            margin-right: 0;
            margin-bottom: 0;
            margin-left: 0;
        }
        body {
            color: #373535;
            font-weight: normal;
            font-size: 18pt;
            margin: 0;
            line-height: 100%;
        }
        h1, h2, h3, h4 {
            font-weight: bold;
            margin: 0;
        }
        h1 {
            font-size: 22pt;
            margin: 5mm 0;
        }
        h2 {
            font-size: 18pt;
        }
        h3{
            font-size: 15pt;
        }
        h4 {
            font-size: 12pt;
        }
        ol,
        ul {
            list-style: none;
            margin: 0;
            padding: 0;
        }
        li,
        ul {
            margin-bottom: 0.75em;
        }
        p {
            margin: 0;
            padding: 0;
        }
        p + p {
            margin-top: 1.25em;
        }
        a {
            border-bottom: 1px solid;
            text-decoration: none;
        }
        /* default DOMPDF, page numbers */
        .pagenum:before {
            content: counter(page);
        }
        .pagenum,.pagecount {
            font-family: sans-serif;
        }
        
        /**
        * Layout Custom CSS
        */
        #certificate-background {
            position: fixed;
            top: 0;
            left:     0;
            width:    29.7cm;
            height:   21cm;
            z-index:  -1000;
        }
        .wrapper{
            position: relative;
        }
        .certificate-content{
            font-size: <?php echo $wrapper_font_size ?>pt;
            max-width: <?php echo $wrapper_width; ?>pt;
            text-align: <?php echo $wrapper_textalign; ?>;
            margin-top: <?php echo $wrapper_mt; ?>pt;
            margin-left: <?php echo $wrapper_ml ?>;
            margin-right: <?php echo $wrapper_mr ?>;
            font-family: <?php echo $wrapper_font_family; ?>;
            <?php
             if ( ! empty( $this->debug ) && $this->debug ) {
                 echo 'border: 1px solid red;';
             }
             ?>
            line-height: 1.5;
            color: <?php echo $wrapper_text_color; ?>;
        }
        .student_name {
            font-size: <?php echo $student_name_font_size; ?>pt;
            line-height: 1;
            font-family: <?php echo $student_name_font_family ?>;
        }
    </style>
</head>
<body>
<?php
$student = wp_get_current_user();
$content = get_post_meta( $certificate_id,'_certificate_content', true );
$content = nl2br( $content );
$student_name = "<span class='student_name'> {$student->display_name} </span>";
$course_name = __( 'Course Title', 'dozent-lms-certificate' );
if ( ! empty( $course_id ) ) {
    $course = get_post( $course_id );
    if ( ! empty( $course->post_title ) ) {
        $course_name = $course->post_title;
    }
}

$course_name = "<span class='course_name'> {$course_name} </span>";
$date = date_i18n( get_option( 'date_format' ) );
$date = "<span class='completion_date'> {$date} </span>";


$variable = [
        '{student_name}',
        '{course_name}',
        '{date}',
];
$replace = [
        $student_name,
        $course_name,
        $date,
];

$content = str_replace( $variable, $replace, $content );

?>

<div class="wrapper">

    <div class="certificate-content">

        <?php

        echo $content;

        ?>

    </div>

</div>

<div id="certificate-background">
    <img src="<?php echo $certificate_background_url; ?>" width="100%" height="100%" />
</div>


</body>
</html>


