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

global $campoal_options;
global $post;

use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;

$fundraising_mode 	= isset($campoal_options['conikal_fundraising_mode_field']) ? $campoal_options['conikal_fundraising_mode_field'] : 0;
$embed_width 		= isset($campoal_options['conikal_embed_width_field']) ? $campoal_options['conikal_embed_width_field'] : 380;
$embed_height 		= isset($campoal_options['conikal_embed_height_field']) ? $campoal_options['conikal_embed_height_field'] : 560;
$share_width 		= isset($campoal_options['conikal_share_embed_width_field']) ? $campoal_options['conikal_share_embed_width_field'] : 400;
$share_height 		= isset($campoal_options['conikal_share_embed_height_field']) ? $campoal_options['conikal_share_embed_height_field'] : 400;
$embed_page 		= isset($campoal_options['conikal_page_embed_field']) ? $campoal_options['conikal_page_embed_field'] : 0;
$embed_link 		= !empty($embed_page) ? get_permalink($embed_page) : '';

if(is_singular('petition')) {
    $id = $post->ID;
} else if (is_singular('update')) {
    $update_id = $post->ID;
    $id = get_post_meta($update_id, 'update_post_id', true);
} else if (is_singular('citation')) {
    $update_id = $post->ID;
    $id = get_post_meta($update_id, 'citation_post_id', true);
} else if (isset($_GET['petition_id']) && $_GET['petition_id'] != '') {
    $id = sanitize_text_field($_GET['petition_id']);
} elseif (class_exists('Give')) {
	$embed_width 	= isset($campoal_options['conikal_give_embed_width_field']) ? $campoal_options['conikal_give_embed_width_field'] : 380;
	$embed_height 	= isset($campoal_options['conikal_give_embed_height_field']) ? $campoal_options['conikal_give_embed_height_field'] : 620;
	
	$get_data     = give_clean( filter_input_array( INPUT_GET ) );
    $session      = give_get_purchase_session(); 

	if ($fundraising_mode == 1 || (!empty($get_data['donation_id']) || isset($session['donation_id'])) ) {
		$donation_id  = false;
    	$receipt_type = false;

		if ( ! empty( $get_data['donation_id'] ) ) {
			$donation_id = $get_data['donation_id'];
		} elseif ( ! empty( $get_data['action'] ) && 'view_in_browser' === $get_data['action'] ) {
			$receipt_type = 'view_in_browser';
			$donation_id  = $get_data['_give_hash'];
		} elseif ( isset( $session['donation_id'] ) ) {
			$donation_id = $session['donation_id'];
		} elseif ( ! empty( $give_receipt_args['id'] ) ) {
			$donation_id = $give_receipt_args['id'];
		}

		$payment    = new Give_Payment( $donation_id );
		$form_id    = $payment->form_id;

		// get petition id
		$id 	  	= conikal_get_petition_by_form_id($form_id);
		$id 		= !empty($id) ? $id : $form_id;
	}
} else {
	$id = '';
}
?>

<?php
if (!empty($id) && !empty($embed_link)) :
	// card iframe url
	$title = get_the_title($id);
	$embed_iframe = $embed_link ? add_query_arg(array('id' => $id, 'style' => 'card'), $embed_link) : '';

	// html version  of card
	$post_type = get_post_type($id);
	if ($post_type == 'petition') {
		$PetitionCardInline = new CssToInlineStyles();
		$card_inline = $PetitionCardInline->convert(
			conikal_embed_petition($id, 'card', 'html'),
			conikal_embed_petition($id, 'card', 'style')
		);
	} else if ($post_type == 'give_forms') {
		$FundraisingCardInline = new CssToInlineStyles();
		$card_inline = $FundraisingCardInline->convert(
			conikal_embed_fundraising($id, 'card', 'html'),
			conikal_embed_fundraising($id, 'card', 'style')
		);
	}

	$card_inline = conikal_remove_init_html_tags($card_inline);

	// share iframe url
	$share_iframe = $embed_link ? add_query_arg(array('id' => $id, 'style' => 'share'), $embed_link) : '';

	// html version of share
	$SharedInline 	= new CssToInlineStyles();
    $share_inline 	= $SharedInline->convert(
        conikal_share_embed_petition($id, 'html'),
        conikal_share_embed_petition($id, 'style')
    );

	$share_inline = conikal_remove_init_html_tags($share_inline);
?>

	<div class="ui tiny modal embed-modal" id="embed-modal<?php echo esc_attr($id) ?>">
		<div class="content">
			<div class="ui secondary pointing large menu embed-tab" id="embed-tab<?php echo esc_attr($id) ?>">
				<div class="item embed-header"><h3 class="ui header"><?php esc_html_e('Embed Campaign', 'campoal') ?></h3></div>
				<div class="right menu">
					<a class="active item" data-tab="card<?php echo esc_attr($id) ?>">
						<i class="pen square icon"></i>
						<?php esc_html_e('Card', 'campoal') ?>
					</a>
					<a class="item" data-tab="share<?php echo esc_attr($id) ?>">
						<i class="share alternate square icon"></i>
						<?php esc_html_e('Share', 'campoal') ?>
					</a>
				</div>
			</div>
			<div class="ui active tab" data-tab="card<?php echo esc_attr($id) ?>">
				<div class="ui large form">
					<div class="field">
						<label><i class="expand icon"></i><?php esc_html_e('Iframe card', 'campoal') ?></label>
						<textarea rows="3" id="embed-code<?php echo esc_attr($id) ?>" readonly><?php echo esc_textarea('<iframe src="' . esc_url($embed_iframe) . '" width="' . esc_attr($embed_width) . 'px" height="' . esc_attr($embed_height) . 'px" scrolling="auto" frameborder="0"></iframe>') ?></textarea>
					</div>
					<div class="field alignright">
						<div class="ui primary labeled icon button copy-embed" id="copy-embed<?php echo esc_attr($id) ?>" data-id="<?php echo esc_attr($id) ?>"><i class="copy outline icon"></i><?php esc_html_e('Copy', 'campoal') ?></div>
					</div>
					<div class="field">
						<label><i class="html5 icon"></i><?php esc_html_e('HTML card', 'campoal') ?></label>
						<textarea rows="3" id="embed-html-code<?php echo esc_attr($id) ?>" readonly><?php echo esc_textarea($card_inline); ?></textarea>
					</div>
					<div class="field alignright">
						<div class="ui primary labeled icon button copy-html-embed" id="copy-html-embed<?php echo esc_attr($id) ?>" data-id="<?php echo esc_attr($id) ?>"><i class="copy outline icon"></i><?php esc_html_e('Copy', 'campoal') ?></div>
					</div>
					<div class="field" id="msg-embed-copy<?php echo esc_attr($id) ?>"></div>
				</div>
			</div>
			<div class="ui tab" data-tab="share<?php echo esc_attr($id) ?>">
				<div class="ui large form">
					<div class="field">
						<label><i class="expand icon"></i><?php esc_html_e('Iframe share', 'campoal') ?></label>
						<textarea rows="3" id="embed-code-share<?php echo esc_attr($id) ?>" readonly><?php echo esc_textarea('<iframe src="' . esc_url($share_iframe) . '" width="' . esc_attr($share_width) . 'px" height="' . esc_attr($share_height) . 'px" scrolling="auto" frameborder="0"></iframe>') ?></textarea>
					</div>
					<div class="field alignright">
						<div class="ui primary labeled icon button copy-embed-share" id="copy-embed-share<?php echo esc_attr($id) ?>" data-id="<?php echo esc_attr($id) ?>"><i class="copy outline icon"></i><?php esc_html_e('Copy', 'campoal') ?></div>
					</div>
					<div class="field">
						<label><i class="html5 icon"></i><?php esc_html_e('HTML share', 'campoal') ?></label>
						<textarea rows="3" id="embed-html-code-share<?php echo esc_attr($id) ?>" readonly><?php echo esc_textarea($share_inline); ?></textarea>
					</div>
					<div class="field alignright">
						<div class="ui primary labeled icon button copy-html-embed-share" id="copy-html-embed-share<?php echo esc_attr($id) ?>" data-id="<?php echo esc_attr($id) ?>"><i class="copy outline icon"></i><?php esc_html_e('Copy', 'campoal') ?></div>
					</div>
					<div class="field" id="msg-embed-copy-share<?php echo esc_attr($id) ?>"></div>
				</div>
			</div>
		</div>
		<div class="actions">
			<div class="ui cancel button" data-id="<?php echo esc_attr($id) ?>"><?php esc_html_e('Close', 'campoal') ?></div>
			<div class="ui cancel primary labeled icon button" data-id="<?php echo esc_attr($id) ?>"><i class="check icon"></i><?php esc_html_e("I'm done!", 'campoal') ?></div>
		</div>
	</div>
<?php endif; ?>