<?php
/**
 * @package WordPress
 * @subpackage Campoal
 */

global $campoal_options;
global $post;
global $current_user;
$show_letter = isset($campoal_options['conikal_show_letter_section_field']) ? $campoal_options['conikal_show_letter_section_field'] : 0;
$receiver_step = isset($campoal_options['conikal_receiver_step_field']) ? $campoal_options['conikal_receiver_step_field'] : 1;


$petition_id = $post->ID;
$letter = get_post_meta($petition_id, 'petition_letter', true);

// Class used to render markdown content into HTML
$Parsedown = new Parsedown();
?>

<?php if ($show_letter == 1) : ?>
	<div class="ui piled secondary segment" id="letter<?php echo esc_attr($petition_id) ?>">
	    <div class="ui grid">
	        <div class="twelve wide column">
	            <div class="letter-title text grey"><?php esc_html_e('Letter to', 'campoal') ?></div>
	        </div>
	        <div class="four wide right aligned column">
	            <div class="letter-title text grey">
	                <!-- Edit petition link -->
	                <?php if ( is_user_logged_in() && (get_the_author_meta('ID') == $current_user->ID || current_user_can('administrator') || current_user_can('editor') || conikal_co_author_permission($petition_id)) ) {
	                	$page_dashboard_petition = isset($campoal_options['conikal_page_dashboard_petition_field']) ? $campoal_options['conikal_page_dashboard_petition_field'] : 0;
						$page_edit_petition = $page_dashboard_petition ? add_query_arg(array('section' => 'edit_petition', 'tab' => 'letter', 'id' => $petition_id), get_permalink($page_dashboard_petition)) : ''; ?>
	                    <a class="font medium" href="<?php echo esc_url($page_edit_petition); ?>" data-bjax><i class="pencil icon"></i></a>
	                <?php } ?>
	            </div>
	        </div>
	    </div>
	    <?php 
	    $all_decisionmakers = conikal_get_decistion_makers($petition_id);
	    if (!empty($all_decisionmakers) && $receiver_step == 1) { 
	        foreach ($all_decisionmakers as $decisionmaker) {
	            $decisionmaker = (object) $decisionmaker ?>
	            <div class="text grey letter-receiver"><?php echo esc_html($decisionmaker->position ? $decisionmaker->position . ', ' : '') ?><strong><?php echo esc_html($decisionmaker->name ? $decisionmaker->name : '') ?></strong></div>
	        <?php }
	    }
	    ?>
	    <div class="ui hidden divider"></div>
	    <div class="content-letter" id="content-letter">
	        <?php echo wp_kses_post($Parsedown->text($letter ? $letter : $title)); ?>
	    </div>
	</div>
<?php endif; ?>