<?php
/**
 * Template One - Professional CV Layout
 * File: templates/templateTwo/templateTwo.php
 */

if ( !defined( 'ABSPATH' ) ) {
    exit;
}
if ( !function_exists( 'format_month_display' ) ) {
    function format_month_display( $date_string ) {
        if ( empty( $date_string ) ) {
            return '';
        }

        $timestamp = strtotime( $date_string );
        if ( !$timestamp ) {
            return $date_string;
        }

        return date_i18n( 'M Y', $timestamp );
    }
}
function bwdcv_template_two( $placeholder_image_url, $profile_image, $showQRCode, $linkedin_url, $waterMarkCanvas, $firstName, $lastName, $designation, $email, $phoneNumber, $address, $city, $postalCode, $website, $attributes ) {
    ?>
    <div class="bwdcvs-cv-builder-common">
        <div class="wpcv-container temp3 <?php echo esc_attr( $waterMarkCanvas ); ?>">
            <div class="wpcv-temp3-content">
                <?php if ( $showQRCode ): ?>
                <div class="cv-qrWrapper">
                    <?php bwdcv_output_qr_canvas( $linkedin_url, $qrWidth = '100' ); ?>
                </div>

                <?php endif; ?>
                <div class="wpcv-header">
                    <div class="wpcv-header-hero text-center">
                        <?php if ( !empty( $firstName ) ): ?>
                            <h1 class="wpcv-name"><?php echo esc_html( $firstName ); ?></h1>
                        <?php endif; ?>

                        <?php if ( !empty( $firstName ) && !empty( $lastName ) ): ?>
                            <span>&nbsp;</span>
                        <?php endif; ?>

                        <?php if ( !empty( $lastName ) ): ?>
                            <h1 class="wpcv-name"><?php echo esc_html( $lastName ); ?></h1>
                        <?php endif; ?>

                        <?php if ( !empty( $designation ) ): ?>
                            <h4 class="wpcv-designation"><?php echo esc_html( $designation ); ?></h4>
                        <?php endif; ?>
                    </div>

                    <div class="wpcv-header-contact">
                        <ul class="d-flex justify-content-center">

                            <?php if ( !empty( $address ) ): ?>
                                <li><a><?php echo esc_html( $address ); ?></a></li>
                            <?php endif; ?>

                            <?php if ( !empty( $phoneNumber ) ): ?>
                                <li><a href="tel:<?php echo esc_attr( $phoneNumber ); ?>"><?php echo esc_html( $phoneNumber ); ?></a></li>
                            <?php endif; ?>

                            <?php if ( !empty( $email ) ): ?>
                                <li><a href="mailto:<?php echo esc_attr( $email ); ?>"><?php echo esc_html( $email ); ?></a></li>
                            <?php endif; ?>

                            <?php if ( !empty( $city ) ): ?>
                                <li><span><?php echo esc_html( $city ); ?></span></li>
                            <?php endif; ?>

                            <?php if ( !empty( $website ) ): ?>
                                <li><a href="<?php echo esc_url( $website ); ?>"><?php echo esc_html( $website ); ?></a></li>
                            <?php endif; ?>

                            <?php if ( !empty( $postalCode ) ): ?>
                                <li><span><?php echo esc_html( $postalCode ); ?></span></li>
                            <?php endif; ?>

                        </ul>
                    </div>
                </div>
                <hr />

                <?php if ( ! empty( $attributes['sectionItems27'] ) && is_array( $attributes['sectionItems27'] ) ) : ?>
                    <?php foreach ( $attributes['sectionItems27'] as $section ) : ?>

                        <?php if ( isset( $section['type'] ) && $section['type'] === 'about' ) : ?>
                            <div class="wpcv-about-content">
                                <?php if ( ! empty( $section['description'] ) ) : ?>
                                    <p><?php echo esc_html( wp_strip_all_tags( $section['description'] ) ); ?></p>
                                <?php endif; ?>

                                <?php if ( ! empty( $section['generatedContent'] ) ) : ?>
                                    <p><?php echo esc_html( $section['generatedContent'] ); ?></p>
                                <?php endif; ?>
                            </div>
                        <?php endif; ?>

                        <?php if ( isset( $section['type'] ) && $section['type'] === 'experience' && ! empty( $section['items'] ) && is_array( $section['items'] ) ) : ?>
                            <div class="wpcv-experience-content">
                                <div class="wpcv-section-title">
                                    <h3><?php echo esc_html( $section['title'] ); ?></h3>
                                </div>
                                <hr />
                                <?php foreach ( $section['items'] as $item ) : ?>
                                    <div class="wpcv-experience">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <div class="d-flex align-items-center">
                                            <?php if ( ! empty( $item['name'] ) ) : ?>
                                                <div class="wpcv-old-company">
                                                    <?php echo esc_html( $item['name'] ); ?>
                                                </div>
                                            <?php endif; ?>
                                            <?php if ( ! empty( $item['designation'] ) ) : ?>
                                                <div class="wpcv-oldc-designation">
                                                    <?php echo esc_html( $item['designation'] ); ?>
                                                </div>
                                            <?php endif; ?>
                                        </div>
                                        <?php if ( ! empty( $item['fromExpDate'] ) ) : ?>
                                            <div class="wpcv-experience-date">
                                                <span>
                                                <?php echo esc_html( format_month_display( $item['fromExpDate'] ) ); ?>
                                                <?php
                                                if ( ! empty( $item['current'] ) || ! empty( $item['toExpDate'] ) ) {
                                                    echo ' – ';
                                                    echo ! empty( $item['current'] )
                                                        ? 'Present'
                                                        : esc_html( format_month_display( $item['toExpDate'] ) );
                                                }
                                                ?>
                                                </span>
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                    <div>
                                        <div class="wpcv-experience-description">
                                            <?php
                                            if ( ! empty( $item['experienceDescription'] ) ) {
                                                echo wp_kses_post( $item['experienceDescription'] );
                                            }
                                            ?>
                                        </div>

                                    </div>
                                </div>
                                <?php endforeach; ?>
                            
                            </div>
                        <?php endif; ?>

                        <?php if ( isset( $section['type'] ) && $section['type'] === 'education' && ! empty( $section['items'] ) && is_array( $section['items'] ) ) : ?>
                            <div class="wpcv-education-content">
                                <div class="wpcv-section-title">
                                    <h3><?php echo esc_html( $section['title'] ); ?></h3>
                                </div>
                                <hr />
                                <?php foreach ( $section['items'] as $item ) : ?>
                                    <div class="wpcv-education">
                                        <div class="d-flex justify-content-between align-items-center">
                                            <div class="d-flex align-items-center">
                                                <?php if ( !empty( $item['institution'] ) ): ?>
                                                    <div class="wpcv-education-institute">
                                                        <strong><?php echo esc_html( $item['institution'] ); ?></strong>
                                                    </div>
                                                <?php endif; ?>
                                                <?php if ( !empty( $item['name'] ) ): ?>
                                                    <div class="wpcv-education-qualification">
                                                        <?php echo esc_html( $item['name'] ); ?>
                                                    </div>
                                                <?php endif; ?>
                                                
                                            </div>
                                            <div class="wpcv-education-date">
                                                <span>
                                                    <?php
                                                    $from = ! empty( $item['fromDate'] ) ? format_month_display( $item['fromDate'] ) : '';
                                                    $to = ! empty( $item['toDate'] ) ? format_month_display( $item['toDate'] ) : '';
                                                    $is_present = ! empty( $item['current'] );

                                                    if ( $from ) {
                                                        echo esc_html( $from );
                                                    }

                                                    if ( ( $to || $is_present ) && $from ) {
                                                        echo ' &nbsp;–&nbsp; ';
                                                    }

                                                    if ( $is_present ) {
                                                        echo esc_html__( 'Present', 'cv-builder' );
                                                    } elseif ( $to ) {
                                                        echo esc_html( $to );
                                                    }
                                                    ?>
                                                </span>
                                            </div>
                                        </div>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        <?php endif; ?>

                        <?php if ( isset( $section['type'] ) && $section['type'] === 'skills' && ! empty( $section['items'] ) && is_array( $section['items'] ) ) : ?>
                            <div class="wpcv-competencies-content">
                                <div class="wpcv-section-title">
                                    <h3><?php echo esc_html( $section['title'] ); ?></h3>
                                </div>
                                <hr />
                                <div class="wpcv-competencies">
                                    <div>
                                        <ul>
                                            <?php foreach ( $section['items'] as $item ) : ?>
                                                <?php if ( ! empty( $item['name'] ) ) : ?>
                                                    <li>
                                                        <span><?php echo esc_html( $item['name'] ); ?></span>
                                                    </li>
                                                <?php endif; ?>
                                            <?php endforeach; ?>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        <?php endif; ?>

                        <?php if ( isset( $section['type'] ) && $section['type'] === 'tools' && ! empty( $section['items'] ) && is_array( $section['items'] ) ) : ?>
                            <div class="wpcv-tools-content">
                                <div class="wpcv-section-title">
                                    <h3><?php echo esc_html( $section['title'] ); ?></h3>
                                </div>
                                <hr />
                                <div class="wpcv-tools">
                                    <div>
                                        <ul>
                                            <?php foreach ( $section['items'] as $item ) : ?>
                                                <?php if ( ! empty( $item['name'] ) ) : ?>
                                                    <li>
                                                        <span><?php echo esc_html( $item['name'] ); ?></span>
                                                    </li>
                                                <?php endif; ?>
                                            <?php endforeach; ?>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        <?php endif; ?>

                        <?php if ( isset( $section['type'] ) && $section['type'] === 'languages' && ! empty( $section['items'] ) && is_array( $section['items'] ) ) : ?>
                            <div class="wpcv-languages-content">
                                <div class="wpcv-section-title">
                                    <h3><?php echo esc_html( $section['title'] ); ?></h3>
                                </div>
                                <hr />
                                <div class="wpcv-languages">
                                    <div>
                                        <ul>
                                            <?php foreach ( $section['items'] as $item ) : ?>
                                                <?php if ( ! empty( $item['name'] ) ) : ?>
                                                    <li>
                                                        <span><?php echo esc_html( $item['name'] ); ?></span>
                                                    </li>
                                                <?php endif; ?>
                                            <?php endforeach; ?>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        <?php endif; ?>

                        <?php if ( isset( $section['type'] ) && $section['type'] === 'contact' ) : ?>
                            <div class="wpcv-contacts-content">
                                <div class="wpcv-section-title">
                                    <h3><?php echo esc_html( $section['title'] ); ?></h3>
                                </div>
                                <hr />
                                <div class="wpcv-contact">
                                    <ul>
                                        <?php if ( ! empty( $section['address'] ) ) : ?>
                                            <li><span><?php echo esc_html( $section['address'] ); ?></span></li>
                                        <?php endif; ?>
                                            <?php if ( ! empty( $section['email'] ) ) : ?>
                                            <li><span><?php echo esc_html( $section['email'] ); ?></span></li>
                                        <?php endif; ?>
                                        <?php if ( ! empty( $section['website'] ) ) : ?>
                                            <li><span><?php echo esc_html( $section['website'] ); ?></span></li>
                                        <?php endif; ?>
                                        <?php if ( ! empty( $section['phoneNumber'] ) ) : ?>
                                            <li><span><?php echo esc_html( $section['phoneNumber'] ); ?></span></li>
                                        <?php endif; ?>
                                        <?php if ( ! empty( $section['city'] ) ) : ?>
                                            <li><span><?php echo esc_html( $section['city'] ); ?></span></li>
                                        <?php endif; ?>

                                        <?php if ( ! empty( $section['postalCode'] ) ) : ?>
                                            <li><span><?php echo esc_html( $section['postalCode'] ); ?></span></li>
                                        <?php endif; ?>
                                    </ul>
                                </div>
                            </div>
                        <?php endif; ?>

                        <?php if ( isset( $section['type'] ) && $section['type'] === 'awards' && ! empty( $section['items'] ) && is_array( $section['items'] ) ) : ?>
                            <div class="wpcv-award-content">
                                <div class="wpcv-section-title">
                                    <h3><?php echo esc_html( $section['title'] ); ?></h3>
                                </div>
                                <hr />
                                <div class="wpcv-award">
                                    <ul>
                                        <?php foreach ( $section['items'] as $item ) : ?>
                                            <?php if ( ! empty( $item['name'] ) ) : ?>
                                                <li>
                                                    <span><?php echo esc_html( $item['name'] ); ?></span>
                                                </li>
                                            <?php endif; ?>
                                        <?php endforeach; ?>
                                    </ul>
                                </div>
                            </div>
                        <?php endif; ?>

                        <?php if ( isset( $section['type'] ) && $section['type'] === 'hobbies' && ! empty( $section['items'] ) && is_array( $section['items'] ) ) : ?>
                            <div class="wpcv-hobbies-content">
                                <div class="wpcv-section-title">
                                    <h3><?php echo esc_html( $section['title'] ); ?></h3>
                                </div>
                                <hr />
                                <div class="wpcv-hobbies">
                                    <ul>
                                        <?php foreach ( $section['items'] as $item ) : ?>
                                            <?php if ( ! empty( $item['name'] ) ) : ?>
                                                <li>
                                                    <span><?php echo esc_html( $item['name'] ); ?></span>
                                                </li>
                                            <?php endif; ?>
                                        <?php endforeach; ?>
                                    </ul>
                                </div>
                            </div>
                        <?php endif; ?>

                        <?php if ( isset( $section['type'] ) && $section['type'] === 'reference' && ! empty( $section['items'] ) && is_array( $section['items'] ) ) : ?>
                        <div class="wpcv-reference-content">
                                    <div class="wpcv-section-title">
                                        <h3><?php echo esc_html( $section['title'] ); ?></h3>
                                    </div>
                                    <hr />
                                    <div class='d-flex flex-gap15'>
                                        <?php foreach ( $section['items'] as $item ) : ?>
                                            <div class="wpcv-reference">
                                                <?php if ( ! empty( $item['name'] ) ) : ?>
                                                    <div class="wpcv-section-title">
                                                        <h3><?php echo esc_html( $item['name'] ); ?></h3>
                                                    </div>
                                                <?php endif; ?>

                                                <div>
                                                    <ul>
                                                        <?php if ( ! empty( $item['company'] ) || ! empty( $item['jobPosition'] ) ) : ?>
                                                            <li>
                                                                <?php if ( ! empty( $item['company'] ) ) : ?>
                                                                    <span><?php echo esc_html( $item['company'] ); ?></span>
                                                                <?php endif; ?>

                                                                <?php if ( ! empty( $item['company'] ) && ! empty( $item['jobPosition'] ) ) : ?>
                                                                    <span>,&nbsp;</span>
                                                                <?php endif; ?>

                                                                <?php if ( ! empty( $item['jobPosition'] ) ) : ?>
                                                                    <span><?php echo esc_html( $item['jobPosition'] ); ?></span>
                                                                <?php endif; ?>
                                                            </li>
                                                        <?php endif; ?>

                                                        <?php if ( ! empty( $item['phone'] ) ) : ?>
                                                            <li><span><?php echo esc_html( $item['phone'] ); ?></span></li>
                                                        <?php endif; ?>

                                                        <?php if ( ! empty( $item['relation'] ) ) : ?>
                                                            <li><span><?php echo esc_html( $item['relation'] ); ?></span></li>
                                                        <?php endif; ?>
                                                    </ul>
                                                </div>
                                            </div>
                                        <?php endforeach; ?>
                                    </div>
                                </div>
                        <?php endif; ?>

                    <?php endforeach; ?>
                <?php endif; ?>

            </div>
        </div>
    </div>
    <?php
}
?>

