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

global $campoal_options;
global $post;
global $current_user;

// Check user is loggedin or not, if not redirect to login page
conikal_redirect_login();

if (isset($_GET['citation_id'])) {
    $citation_id = sanitize_text_field($_GET['citation_id']);

    $editor_style = isset($campoal_options['conikal_editor_style_field']) ? $campoal_options['conikal_editor_style_field'] : 'markdown';
	$p_media_upload = isset($campoal_options['conikal_p_media_upload_field']) ? $campoal_options['conikal_p_media_upload_field'] : 1;
	$p_media_upload = $p_media_upload === 1 ? true : false; 

	$citation_button_text = isset($campoal_options['conikal_citation_button_text_field']) ? $campoal_options['conikal_citation_button_text_field'] : __('Send Citation', 'campoal');
	$link_preview_placeholder = conikal_lazy_load_placeholder('link-preview-placeholder');


    $args = array(
        'p' => $citation_id,
        'post_type' => 'citation',
        'post_status' => array('publish', 'pending')
    );

    $citation = new WP_Query($args);

    if($citation->have_posts()) {
        while ( $citation->have_posts() ) {
            $citation->the_post();
            $id = get_the_ID();
            $link = get_permalink($id);
            $title = get_the_title($id);
            $status = get_post_status($id);
            $excerpt = conikal_get_excerpt_by_id($id);
            $content = get_the_content($id);
            $date = get_the_date('', $id);
            $time = sprintf(esc_html__('%s ago', 'campoal'), human_time_diff(get_the_time('U', $id), current_time('timestamp')));
            $opinion =  get_post_meta($id, 'citation_opinion', true);
            $petition_id =  get_post_meta($id, 'citation_post_id', true);
            $reference =  get_post_meta($id, 'citation_reference', true);
            $reference_title = get_post_meta($citation_id, 'citation_reference_title', true);
		    $reference_description =  get_post_meta($citation_id, 'citation_reference_description', true);
		    $reference_image =  get_post_meta($citation_id, 'citation_reference_image', true);
		    $reference_site =  get_post_meta($citation_id, 'citation_reference_site', true);
            $gallery = get_post_meta($id, 'citation_gallery', true);
            $images = conikal_get_gallery_images($gallery);
            $thumb_video = get_post_meta($id, 'citation_thumb_small', true);
            $comments = wp_count_comments($id);

            if(has_post_thumbnail()) {
                $thumb_id = get_post_thumbnail_id();
                $thumbnail = wp_get_attachment_image_src($thumb_id, 'thumbnail', true);
                $thumbnail = $thumbnail[0];
            } elseif ($thumb_video) {
                $thumbnail = $thumb_video;
            } elseif ($gallery) {
                $thumbnail = $images[1];
            } elseif ($reference_image) {
                $thumbnail = $reference_image;
            } else {
                $thumbnail = '';
            }
    	}

	    wp_reset_postdata();
	    wp_reset_query();
    }
?>

<div id="wrapper" class="wrapper petition">
	<div class="color silver">
        <div class="ui large secondary pointing grey menu" id="control-menu">
            <div class="ui container">
                    <!-- Campaign link -->
                    <a href="<?php echo get_permalink($petition_id) ?>" class="item" data-bjax><i class="chevron left icon"></i><?php esc_html_e('Back to Campaign', 'campoal') ?></a>
            </div>
        </div>
    </div>
	<div class="ui container add-update">
	    <div class="ui center aligned header add-update-title">
	        <div class="content">
	            <?php esc_html_e('Edit Citation', 'campoal') ?>
	        </div>
	    </div>
	    <div class="ui centered left aligned grid">
	        <div class="sixteen wide mobile fourteen wide tablet ten wide computer column">
		        <div id="citation-response" class="respon-message"></div>
		    	<form class="ui form submit-contribute">
		            <?php wp_nonce_field('citation_ajax_nonce', 'securityCitation', true); ?>
		            <?php wp_nonce_field('opengraph_ajax_nonce', 'securityOpenGraph', true); ?>
		        	<div class="field required">
		        		<label><?php esc_html_e('Citation title', 'campoal') ?></label>
		        		<div class="ui large fluid icon input">
		        			<input type="url" id="citation_title" name="citation_title" placeholder="<?php esc_attr_e('Enter citation title', 'campoal') ?>" value="<?php echo esc_attr($title) ?>">
		        		</div>
		        	</div>
		           	<div class="field required">
		                <label><?php esc_html_e('Citation content', 'campoal') ?></label>
		                <div id="isDesc">
		                <?php 
		                $html_content = isset($content) ? $content : '';
		                if ($editor_style == 'classic') {
		                    $settings = array(
		                        'media_buttons'         => $p_media_upload,
		                        'textarea_rows'         => 10,
		                        'drag_drop_upload'      => true,
		                        'quicktags'             => false,
		                        'dfw'                   => true,
		                        'editor_height'         => '300px',
		                        );
		                    wp_editor($html_content, 'form_content', $settings);
		                } else { ?>
		                    <textarea id="citation_content" name="citation_content"><?php echo esc_textarea($html_content) ?></textarea>
		                <?php } ?>
		                </div>
		            </div>
		            <div class="field">
		                <label><?php esc_html_e('Reference link', 'campoal') ?></label>
		                <div class="ui large fluid left icon input">
		                    <input type="url" id="citation_reference" name="citation_reference" placeholder="<?php esc_attr_e('Enter reference link', 'campoal') ?>" value="<?php echo esc_attr($reference) ?>">
		                    <i class="linkify icon"></i>
		                </div>
		            </div>
		            <div class="field" id="citation-link-preview">
					    <div class="ui secondary segment">
					        <div class="ui items">
					            <div class="item">
					                <div class="ui tiny image"><img src="<?php echo esc_url($reference_image) ?>"></div>
					                <div class="content"><a class="header"><?php echo esc_html($reference_title) ?></a>
					                    <div class="meta"><span></span></div>
					                    <div class="description"><?php echo esc_html($reference_description) ?></div>
					                    <div class="extra"><?php echo esc_html($reference_site) ?></div>
					                </div>
					            </div>
					        </div>
					    </div>
					</div>
		            <input type="hidden" id="citation_id" name="citation_id" value="<?php echo esc_attr($id) ?>">
		            <input type="hidden" name="petition_id" id="petition_id" value="<?php echo esc_attr($petition_id); ?>">
		            <input type="hidden" id="graph_title" name="graph_title" value="<?php echo esc_attr($reference_title) ?>">
		            <input type="hidden" id="graph_description" name="graph_description" value="<?php echo esc_attr($reference_description) ?>">
		            <input type="hidden" id="graph_image" name="graph_image" value="<?php echo esc_url($reference_image) ?>">
		            <input type="hidden" id="graph_site" name="graph_site" value="<?php echo esc_attr($reference_site) ?>">
		        	<div class="field">
		        		<a href="javascript:void(0);" class="ui fluid primary big button submitCitation" data-id="<?php echo esc_attr($petition_id); ?>"><?php echo conikal_custom_icon('citation') ?><?php echo esc_html($citation_button_text != '' ? $citation_button_text : __('Send Citation', 'campoal')) ?></a>
		        	</div>
		        </form>
		    </div>
		</div>
	</div>
</div>
<?php } else { ?>
    <?php get_template_part('404') ?>
<?php } ?>