<?php
/**
 * Template One - Professional CV Layout
 * File: templates/templateThree/templateThree.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_three( $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">
        <div class="wpcv-temp2-content <?php echo esc_attr( $waterMarkCanvas ); ?>">
            <?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">
                    <!-- <h1 class="wpcv-name">Esther Howard, Finance Manager</h1> -->
                        <?php if ( !empty( $firstName ) || !empty( $lastName ) ): ?>
                            <h1 class="wpcv-name">
                                <?php if ( !empty( $firstName ) ): ?>
                                    <?php echo esc_html( $firstName ); ?>
                                <?php endif; ?>
                                 <?php if ( !empty( $lastName ) ): ?>
                                    <?php echo esc_html( $lastName ); ?>
                                <?php endif; ?>
                                <?php if ( !empty( $designation ) ): ?>
                                    <?php echo esc_html( $designation ); ?>
                                <?php endif; ?>
                            </h1>
                        <?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( $email ) ): ?>
                                <li><a href="mailto:<?php echo esc_attr( $email ); ?>"><?php echo esc_html( $email ); ?></a></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( $phoneNumber ) ): ?>
                                <li><a href="tel:<?php echo esc_attr( $phoneNumber ); ?>"><?php echo esc_html( $phoneNumber ); ?></a></li>
                            <?php endif; ?>
                            <?php if ( !empty( $city ) ): ?>
                                <li><span><?php echo esc_html( $city ); ?></span></li>
                            <?php endif; ?>
                            <?php if ( !empty( $postalCode ) ): ?>
                                <li><span><?php echo esc_html( $postalCode ); ?></span></li>
                            <?php endif; ?>
                    </ul>
                </div>
            </div>

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

                    <?php if ( isset( $section['type'] ) && $section['type'] === 'about' ) : ?>
                        <hr />
                        <div class="wpcv-about-content d-flex">
                            <div class="wpcv-section-title">
                                <h3><?php echo esc_html( $section['title'] ); ?></h3>
                            </div>
                            <div>
                               <?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>
                        </div>
                    <?php endif; ?>

                    <?php if ( isset( $section['type'] ) && $section['type'] === 'education' && ! empty( $section['items'] ) && is_array( $section['items'] ) ) : ?>
                        <hr />
                        <div class="wpcv-education-content">
                            <div class="wpcv-section-title">
                                <h3><?php echo esc_html( $section['title'] ); ?></h3>
                            </div>
                            <?php foreach ( $section['items'] as $item ) : ?>
                                <div class="wpcv-education d-flex">
                                <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>
                                    <?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">
                                            <ul>
                                                <li>
                                                    <span>
                                                        <?php echo esc_html( $item['name'] ); ?>
                                                        <?php if ( !empty( $item['cgpa'] ) ): ?>
                                                            <span><?php echo esc_html( $item['cgpa'] ); ?></span>
                                                        <?php endif; ?>
                                                        <?php if ( !empty( $item['cgpaValue'] ) ): ?>
                                                            <span><?php echo esc_html( $item['cgpaValue'] ); ?></span>
                                                        <?php endif; ?>
                                                    </span>
                                                </li>
                                            </ul>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                            <?php endforeach; ?>
                            
                        </div>
                    <?php endif; ?>

                    <?php if ( isset( $section['type'] ) && $section['type'] === 'experience' && ! empty( $section['items'] ) && is_array( $section['items'] ) ) : ?>
                        <hr />
                        <div class="wpcv-experience-content">
                            <div class="wpcv-section-title">
                                <h3><?php echo esc_html( $section['title'] ); ?></h3>
                            </div>
                            <?php foreach ( $section['items'] as $item ) : ?>
                                <div class="wpcv-experience d-flex">
                                    <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>
                                    <div>
                                        <div class="wpcv-old-company">
                                            Finance Manager, Skyline Technologies Inc. — Austin, TX
                                        </div>
                                        <?php if ( ! empty( $item['name'] ) ) : ?>
                                            <div class="wpcv-old-company">
                                                <?php echo esc_html( $item['name'] ); ?>
                                            </div>
                                        <?php endif; ?>
                                        <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'] === 'skills' && ! empty( $section['items'] ) && is_array( $section['items'] ) ) : ?>
                        <div class="wpcv-competencies-content d-flex">
                            <div class="wpcv-section-title">
                                <h3><?php echo esc_html( $section['title'] ); ?></h3>
                            </div>
                            <div class="wpcv-competencies">
                                <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'] === 'awards' && ! empty( $section['items'] ) && is_array( $section['items'] ) ) : ?>
                        <div class="wpcv-achievements-content d-flex">
                            <div class="wpcv-section-title">
                                <h3><?php echo esc_html( $section['title'] ); ?></h3>
                            </div>
                            <div class="wpcv-achievements">
                                <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'] === 'tools' && ! empty( $section['items'] ) && is_array( $section['items'] ) ) : ?>
                        <div class="wpcv-tools-content d-flex">
                            <div class="wpcv-section-title">
                                <h3><?php echo esc_html( $section['title'] ); ?></h3>
                            </div>
                            <div class="wpcv-tools">
                                <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'] === 'languages' && ! empty( $section['items'] ) && is_array( $section['items'] ) ) : ?>
                        <div class="wpcv-languages-content d-flex">
                            <div class="wpcv-section-title">
                                <h3><?php echo esc_html( $section['title'] ); ?></h3>
                            </div>
                            <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-contact-content d-flex">
                            <div class="wpcv-section-title">
                                <h3><?php echo esc_html( $section['title'] ); ?></h3>
                            </div>
                            <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'] === 'hobbies' && ! empty( $section['items'] ) && is_array( $section['items'] ) ) : ?>
                        <div class="wpcv-hobbies-content d-flex">
                            <div class="wpcv-section-title">
                                <h3><?php echo esc_html( $section['title'] ); ?></h3>
                            </div>
                            <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 d-flex">
                            <div class="wpcv-section-title">
                                <h3><?php echo esc_html( $section['title'] ); ?></h3>
                            </div>
                            <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
}
?>