<?php
/**
 * Template One - Professional CV Layout
 * File: templates/templateOne/templateOne.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_one( $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 <?php echo esc_attr( $waterMarkCanvas ); ?>">
            <div class="wpcv-temp1-content">
                <?php if ( $showQRCode ): ?>
                <div class="cv-qrWrapper">
                    <?php bwdcv_output_qr_canvas( $linkedin_url, $qrWidth='100' ); ?>
                </div>

                <?php endif; ?>
                <div class="wpcv-header d-flex justify-content-between align-items-center">
                    <div class="wpcv-header-photo">
                        <?php
                        if ( !empty( $profile_image ) ) {
                            echo '<img src="' . esc_url( $profile_image ) . '" alt="Profile Image" />';
                        } else {
                            echo '<img src="' . esc_url( $placeholder_image_url ) . '" alt="Profile Image" />';
                        }
                        ?>
                    </div>
                    <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($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>
                            <?php if (!empty($email)) : ?>
                                <li>
                                    <a href="<?php echo esc_url('mailto:' . $email); ?>">
                                        <?php echo esc_html($email); ?>
                                    </a>
                                </li>
                            <?php endif; ?>

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

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

                            <?php if (!empty($address)) : ?>
                                <li>
                                    <span><?php echo esc_html($address); ?></span>
                                </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['sectionItems26'] ) && is_array( $attributes['sectionItems26'] ) ) : ?>
                    <?php foreach ( $attributes['sectionItems26'] 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>
                            <hr />
                        <?php endif; ?>

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

                                                <?php if ( ! empty( $item['name'] ) ) : ?>
                                                    <div class="wpcv-experience-company">
                                                        <?php echo esc_html( $item['name'] ); ?>
                                                    </div>
                                                <?php endif; ?>

                                                <?php if ( ! empty( $item['fromExpDate'] ) ) : ?>
                                                    <div class="wpcv-experience-date">
                                                        <?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'] ) );
                                                        }
                                                        ?>
                                                    </div>
                                                <?php endif; ?>
                                            </div>

                                            <?php if ( ! empty( $item['experienceDescription'] ) ) : ?>
                                                <div>
                                                    <p class="wpcv-experience-description">
                                                        <?php echo esc_html( wp_strip_all_tags($item['experienceDescription'] ) ); ?>
                                                    </p>
                                                </div>
                                            <?php endif; ?>
                                        </div>
                                    <?php endforeach; ?>
                                </div>
                            </div>
                        <?php endif; ?>

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

                                                            <?php if ( !empty( $item['degree'] ) && !empty( $item['name'] ) ): ?>
                                                                <span> &nbsp;-&nbsp; </span>
                                                            <?php endif; ?>

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

                                                    <?php if ( !empty( $item['fromDate'] ) || !empty( $item['toDate'] ) || !empty( $item['current'] ) ): ?>
                                                        <li>
                                                        <span>
                                                            <?php
                                                                $fromYear = !empty($item['fromDate']) && is_numeric($item['fromDate']) && strlen($item['fromDate']) === 4
                                                                    ? esc_html($item['fromDate'])
                                                                    : '';

                                                                $toYear = !empty($item['toDate']) && is_numeric($item['toDate']) && strlen($item['toDate']) === 4
                                                                    ? esc_html($item['toDate'])
                                                                    : '';

                                                                $isPresent = !empty($item['current']);

                                                                if ( $fromYear ) {
                                                                    echo $fromYear;
                                                                }

                                                                if ( ($toYear || $isPresent) && $fromYear ) {
                                                                    echo ' – ';
                                                                }

                                                                if ( $isPresent ) {
                                                                    echo esc_html__('Present', 'cv-builder');
                                                                } elseif ( $toYear ) {
                                                                    echo $toYear;
                                                                }

                                                                if ( ($fromYear || $toYear || $isPresent) && !empty( $item['name'] ) ) {
                                                                    echo ' , ';
                                                                }
                                                            ?>

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

                                                            <?php if ( !empty( $item['cgpa'] ) && !empty( $item['cgpaValue'] ) ): ?>
                                                                <span> &nbsp;-&nbsp; </span>
                                                            <?php endif; ?>

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

                                                    <?php endif; ?>
                                                </ul>
                                            </div>
                                        </div>
                                    <?php endforeach; ?>
                                </div>
                            </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>
                                    <div class="wpcv-competencies d-flex">
                                        <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'] === 'awards' && ! empty( $section['items'] ) && is_array( $section['items'] ) ) : ?>
                            <div class="wpcv-award-content d-flex">
                                    <div class="wpcv-section-title">
                                    <h3><?php echo esc_html( $section['title'] ); ?></h3>
                                </div>
                                <div>
                                    <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>
                            </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>
                                    <div class="wpcv-languages">
                                        <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="section">
                            <div class="wpcv-tools-content d-flex">
                                <div class="wpcv-section-title">
                                    <h3><?php echo esc_html( $section['title'] ); ?></h3>
                                </div>
                                <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>
                            </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>
                                    <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>
                            </div>
                        <?php endif; ?>

                        
                        <?php if ( isset( $section['type'] ) && $section['type'] === 'contact' && ! empty( $section['items'] ) && is_array( $section['items'] ) ) : ?>
                            <div class="wpcv-contact-content d-flex">
                                <div class="wpcv-section-title">
                                    <h3><?php echo esc_html( $section['title'] ); ?></h3>
                                </div>
                                <div>
                                    <div class="wpcv-contact">
                                        <ul>
                                            <?php if ( ! empty( $section['phoneNumber'] ) ) : ?>
                                                <li><span><?php echo esc_html( $section['phoneNumber'] ); ?></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['address'] ) ) : ?>
                                                <li><span><?php echo esc_html( $section['address'] ); ?></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>
                            </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>
                                    <?php foreach ( $section['items'] as $item ) : ?>
                                        <div class="wpcv-reference d-flex">
                                            <?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
}
?>

