<?php
/**
 * Modern Template - Front Side
 */
if ( ! defined( 'ABSPATH' ) ) { exit; }

$cloudi7_icg_org_logo      = get_option( 'cloudi7_icg_org_logo', '' );
$cloudi7_icg_org_signature = get_option( 'cloudi7_icg_org_signature', '' );
$cloudi7_icg_org_name      = get_option( 'cloudi7_icg_org_name', 'Organization Name' );
$cloudi7_icg_orientation   = get_option( 'cloudi7_icg_orientation', 'horizontal' );

$cloudi7_icg_id_number = $cloudi7_icg_card->id_number;
$cloudi7_icg_name      = $cloudi7_icg_card->name;
$cloudi7_icg_photo     = $cloudi7_icg_card->photo ?: Cloudi7_ICG_PLUGIN_URL . 'assets/images/default-avatar.png';
$cloudi7_icg_extra_data = json_decode( $cloudi7_icg_card->extra_data, true ) ?: array();

$cloudi7_icg_upload_dir = wp_upload_dir();
$cloudi7_icg_qr_url = $cloudi7_icg_upload_dir['baseurl'] . '/cloudi7-icg-qrcodes/' . $cloudi7_icg_id_number . '.png';
$cloudi7_icg_verify_url = str_replace( array( 'http://', 'https://' ), '', home_url() );
?>

<div class="cloudi7-icg-card modern <?php echo esc_attr( $cloudi7_icg_orientation ); ?> front">
    <div class="cloudi7-icg-card-header">
        <?php if ( $cloudi7_icg_org_logo ) : ?>
            <img src="<?php echo esc_url( $cloudi7_icg_org_logo ); ?>" class="cloudi7-icg-logo" alt="Logo">
        <?php endif; ?>
        <div class="cloudi7-icg-org-name"><?php echo esc_html( $cloudi7_icg_org_name ); ?></div>
    </div>
    
    <div class="cloudi7-icg-card-body">
        <div class="cloudi7-icg-photo-column">
            <div class="cloudi7-icg-photo-box">
                <img src="<?php echo esc_url( $cloudi7_icg_photo ); ?>" alt="<?php echo esc_attr( $cloudi7_icg_name ); ?>">
            </div>
            <div class="cloudi7-icg-qr-section inner-qr">
                <img src="<?php echo esc_url( $cloudi7_icg_qr_url ); ?>" class="cloudi7-icg-qr-code">
            </div>
        </div>
        <div class="cloudi7-icg-info-column">
            <div class="cloudi7-icg-user-name"><?php echo esc_html( $cloudi7_icg_name ); ?></div>
            <div class="cloudi7-icg-id-number">ID: <?php echo esc_html( $cloudi7_icg_id_number ); ?></div>
            
            <div class="cloudi7-icg-fields">
                <?php if ( ! empty( $cloudi7_icg_extra_data ) || ! empty($cloudi7_icg_card->dob) ) : 
                    $cloudi7_icg_fields_to_show = array( 'class', 'blood_group', 'father_guardian_name', 'department', 'designation', 'employee_id' );
                    
                    $cloudi7_icg_labels = array(
                        'class' => esc_html__( 'Class / Grade', 'cloudi7-id-card-generator' ),
                        'blood_group' => esc_html__( 'Blood Group', 'cloudi7-id-card-generator' ),
                        'father_guardian_name' => esc_html__( 'Guardian Name', 'cloudi7-id-card-generator' ),
                        'department' => esc_html__( 'Department', 'cloudi7-id-card-generator' ),
                        'designation' => esc_html__( 'Designation', 'cloudi7-id-card-generator' ),
                        'employee_id' => esc_html__( 'Employee ID', 'cloudi7-id-card-generator' )
                    );

                    // Add core fields to show if not empty
                    if ( ! empty( $cloudi7_icg_card->dob ) ) {
                        echo '<div class="cloudi7-icg-field"><span class="cloudi7-icg-label">' . esc_html__( 'DOB', 'cloudi7-id-card-generator' ) . ':</span> <span class="cloudi7-icg-value">' . esc_html( $cloudi7_icg_card->dob ) . '</span></div>';
                    }
                    if ( ! empty( $cloudi7_icg_card->blood_group ) ) {
                        echo '<div class="cloudi7-icg-field"><span class="cloudi7-icg-label">' . esc_html__( 'Blood', 'cloudi7-id-card-generator' ) . ':</span> <span class="cloudi7-icg-value">' . esc_html( $cloudi7_icg_card->blood_group ) . '</span></div>';
                    }
                    
                    for ( $cloudi7_icg_i = 1; $cloudi7_icg_i <= 3; $cloudi7_icg_i++ ) {
                        $cloudi7_icg_cf_label = get_option( 'cloudi7_icg_custom_field_' . $cloudi7_icg_i . '_label', '' );
                        if ( $cloudi7_icg_cf_label ) {
                            $cloudi7_icg_fields_to_show[] = 'custom_field_' . $cloudi7_icg_i;
                            $cloudi7_icg_labels['custom_field_' . $cloudi7_icg_i] = $cloudi7_icg_cf_label;
                        }
                    }

                    foreach ( $cloudi7_icg_extra_data as $cloudi7_icg_key => $cloudi7_icg_value ) : 
                        if ( in_array( $cloudi7_icg_key, $cloudi7_icg_fields_to_show ) && ! empty( $cloudi7_icg_value ) ) : 
                            $cloudi7_icg_label = isset($cloudi7_icg_labels[$cloudi7_icg_key]) ? $cloudi7_icg_labels[$cloudi7_icg_key] : str_replace( '_', ' ', ucfirst( $cloudi7_icg_key ) );
                        ?>
                        <div class="cloudi7-icg-field">
                            <span class="cloudi7-icg-label"><?php echo esc_html( $cloudi7_icg_label ); ?>:</span>
                            <span class="cloudi7-icg-value"><?php echo esc_html( $cloudi7_icg_value ); ?></span>
                        </div>
                    <?php endif; endforeach; ?>
                <?php endif; ?>

                <?php if ( ! empty( $cloudi7_icg_card->parent_name ) ) : ?>
                    <div class="cloudi7-icg-field">
                        <span class="cloudi7-icg-label"><?php esc_html_e( 'Guardian', 'cloudi7-id-card-generator' ); ?>:</span>
                        <span class="cloudi7-icg-value"><?php echo esc_html( $cloudi7_icg_card->parent_name ); ?></span>
                    </div>
                <?php endif; ?>
                <?php if ( ! empty( $cloudi7_icg_card->parent_phone ) ) : ?>
                    <div class="cloudi7-icg-field">
                        <span class="cloudi7-icg-label"><?php esc_html_e( 'Emergency Contact', 'cloudi7-id-card-generator' ); ?>:</span>
                        <span class="cloudi7-icg-value"><?php echo esc_html( $cloudi7_icg_card->parent_phone ); ?></span>
                    </div>
                <?php endif; ?>
            </div>
            
        </div>
    </div>
    
    <?php if ( $cloudi7_icg_org_signature ) : ?>
        <div class="cloudi7-icg-signature-box">
            <img src="<?php echo esc_url( $cloudi7_icg_org_signature ); ?>" class="cloudi7-icg-signature-img" alt="Authorized Signature">
            <div class="cloudi7-icg-signature-label"><?php esc_html_e( 'Authorized Signature', 'cloudi7-id-card-generator' ); ?></div>
        </div>
    <?php endif; ?>
</div>
