<?php
if (isset($block['data']['preview_image_help'])) {    /* rendering in inserter preview  */
    echo '<img src="' . $block['data']['preview_image_help'] . '" style="width:100%; height:auto;">';
} else {
    $block["class"]->load_css($block);
    $block_id = (empty($block["data"]["ftm_block_id"])) ? get_field("ftm_block_id") : $block["data"]["ftm_block_id"];

    $linkAttributes             = (!empty(get_field('table_cta_link_attributes'))) ?  get_field('table_cta_link_attributes') : '';
    $header_background_color    = (!empty(get_field('headings_background_color'))) ? get_field('headings_background_color') : acf_get_field('headings_background_color')['default_value'];
    $header_text_color          = (!empty(get_field('headings_text_color'))) ? get_field('headings_text_color') : acf_get_field('headings_text_color')['default_value'];
    $header_1                   = (!empty(get_field('table_cta_header1'))) ? get_field('table_cta_header1') : acf_get_field('table_cta_header1')['default_value'];
    $header_2                   = (!empty(get_field('table_cta_header2'))) ? get_field('table_cta_header2') : acf_get_field('table_cta_header2')['default_value'];
    $header_3                   = (!empty(get_field('table_cta_header3'))) ? get_field('table_cta_header3') : acf_get_field('table_cta_header3')['default_value'];
    $all_row_clickable          = (!empty(get_field('btn_whole_link_clickable'))) ?  get_field('btn_whole_link_clickable') : 'yes';
?>
    <table id="<?php echo $block_id; ?>" class="ftm_table_cta">
        <tbody>
            <tr>
                <th><?php echo $header_1; ?></th>
                <th><?php echo $header_2; ?></th>
                <th><?php echo $header_3; ?></th>
            </tr>
            <?php
            if (have_rows('table_cta_sites')) :
                while (have_rows('table_cta_sites')) : the_row();
                    $siteName       = get_sub_field('table_cta_site_name');
                    $siteLogo       = get_sub_field('table_cta_site_logo');
                    $siteFacts      = get_sub_field('table_cta_facts');
                    $linkText       = get_sub_field('table_cta_link_text');
                    $linkTarget     = get_sub_field('table_cta_link_target');
            ?>
                    <tr>
                        <td>
                            <img src="<?php echo $siteLogo["url"]; ?>" class="aligncenter" alt="<?php echo $siteLogo["alt"]; ?>" width="200" height="80">
                            <strong><?php echo $siteName; ?></strong>
                        </td>
                        <td>
                            <?php
                            echo $siteFacts;
                            ?>
                        </td>
                        <td>
                            <a href="<?php echo $linkTarget; ?>" <?php echo $linkAttributes; ?>>
                                <button>
                                    <?php echo $linkText; ?>
                                </button>
                            </a>
                        </td>
                        <?php
                        if ($all_row_clickable == "yes") {
                        ?>
                            <td><a href="<?php echo $linkTarget; ?>" <?php echo $linkAttributes; ?>></a></td>
                        <?php
                        }
                        ?>
                    </tr>
            <?php
                endwhile;
            else :
            // no rows found
            endif;
            ?>
        </tbody>
    </table>
<?php
}
?>