<?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['update_id'])) {
    $update_id = sanitize_text_field($_GET['update_id']);

    $args = array(
        'p' => $update_id,
        'post_type' => 'update',
        'post_status' => array('publish', 'pending')
    );

    $updates = new WP_Query($args);

    if ($updates->have_posts()) {
        while($updates->have_posts()) {
            $updates->the_post();
            $link = get_permalink($update_id);
            $title = get_the_title($update_id);
            $content = get_the_content($update_id);
            $excerpt = conikal_get_excerpt_by_id($update_id);
            $date = get_the_date('', $update_id);
            $type =  get_post_meta($update_id, 'update_type', true);
            $media =  get_post_meta($update_id, 'update_media', true);
            $gallery = get_post_meta($update_id, 'update_gallery', true);
            $images = conikal_get_gallery_images($gallery);
            $thumb_id = get_post_thumbnail_id($update_id);
            $thumbnail = wp_get_attachment_image_src($thumb_id, 'full', true);
            $video = get_post_meta($update_id, 'update_video', true);
            $thumb = get_post_meta($update_id, 'update_thumb', true);
            $notification = get_post_meta($update_id, 'update_notification', true);
            $scheduled = get_post_meta($update_id, 'update_scheduled', true);
            $comments = wp_count_comments($update_id);
            $campaign_id = get_post_meta($update_id, 'update_campaign_id', true);
            $attach_id = get_post_thumbnail_id($update_id);

            $petition_id =  get_post_meta($update_id, 'update_post_id', true);
            $p_link = get_permalink($petition_id);
            $status = get_post_meta($petition_id, 'petition_status', true);
            $decisionmakers = get_post_meta($petition_id, 'petition_decisionmakers', true);
            $decisionmakers = array_unique(explode(',', $decisionmakers));
            $form_id = $petition_id;
            $post_type = get_post_type($petition_id);
        }
    wp_reset_postdata();
    wp_reset_query();
    }
} else if (isset($_GET['form_id']) && $_GET['form_id'] != '') {
    $form_id = sanitize_text_field($_GET['form_id']);
    $petition_id = '';
    $update_id = isset($_GET['update_id']) ? sanitize_text_field($_GET['update_id']) : '';
    $p_link = get_permalink($form_id);
    $status = get_post_meta($petition_id, 'petition_status', true);
    $post_type = get_post_type($form_id);
    $type = '';
} else {
    $petition_id = isset($_GET['id']) ? sanitize_text_field($_GET['id']) : '';
    $form_id = '';
    $update_id = isset($_GET['update_id']) ? sanitize_text_field($_GET['update_id']) : '';
    $p_link = get_permalink($petition_id);
    $status = get_post_meta($petition_id, 'petition_status', true);
    $decisionmakers = get_post_meta($petition_id, 'petition_decisionmakers', true);
    $decisionmakers = array_unique(explode(',', $decisionmakers));
    $post_type = get_post_type($petition_id);
    $type = '';
}

$graph_title = get_post_meta($update_id, 'update_graph_title', true);
$graph_description =  get_post_meta($update_id, 'update_graph_description', true);
$graph_image =  get_post_meta($update_id, 'update_graph_image', true);
$graph_site =  get_post_meta($update_id, 'update_graph_site', true);

$sendinblue_list_status = isset($campoal_options['conikal_sendinblue_list_field']) ? $campoal_options['conikal_sendinblue_list_field'] : 0;
$sendinblue_key         = isset($campoal_options['conikal_sendinblue_key_field']) ? $campoal_options['conikal_sendinblue_key_field'] : '';
$email_update_supporter = isset($campoal_options['conikal_update_supporter_field']) ? $campoal_options['conikal_update_supporter_field'] : 0;
$p_media_upload         = isset($campoal_options['conikal_p_media_upload_field']) ? $campoal_options['conikal_p_media_upload_field'] : 1;

$editor_style = isset($campoal_options['conikal_editor_style_field']) ? $campoal_options['conikal_editor_style_field'] : 'markdown';
$update_type = isset($_GET['type']) ? sanitize_text_field($_GET['type']) : $type;
$petition_status = isset($_GET['status']) ? sanitize_text_field($_GET['status']) : $status;
$post_author_id = get_post_field('post_author', $petition_id);
$link_preview_placeholder = conikal_lazy_load_placeholder('link-preview-placeholder');

$campaign_id = isset($campaign_id) ? $campaign_id : '';

// check sendiblue campaign status sent or not
if (!empty($campaign_id)) {
    $campaign_status = conikal_sendinblue_get_campaign($campaign_id);
    if ($campaign_status != false) {
        $campaign_status = $campaign_status['status'];
        switch ($campaign_status) {
            case 'sent':
                $campaign_status = false;
                break;

            case 'archive':
                $campaign_status = false;
                break;

            case 'sent and archived':
                $campaign_status = false;
                break;
            
            default:
                $campaign_status = true;
                break;
        }
    }
} else {
    $campaign_status = true;
}

// type of default accordition
if (empty($notification)) {
    $notification = 'schedule';
} else {
    if ($notification == 'now') {
        $notification = 'not';
    } elseif ($notification == 'schedule' && $campaign_status == false) {
        $notification = 'not';
    }
}

?>
<?php if (($petition_id != '' || $update_id != '' || $form_id != '') && ($post_author_id == $current_user->ID || current_user_can('editor') || current_user_can('administrator') || in_array($current_user->ID, $decisionmakers) || conikal_co_author_permission($petition_id))) { ?>
        <div class="ui container add-update">
            <div class="ui center aligned header add-update-title">
                <div class="content">
                    <?php esc_html_e('Add Update', 'campoal') ?>
                </div>
            </div>
            <div class="ui centered left aligned grid">
                <div class="sixteen wide mobile fourteen wide tablet ten wide computer column">
                    <div class="respon-message" id="update-response"></div>
                    <form class="ui form">
                        <input type="hidden" name="update_id" id="update_id" value="<?php echo esc_attr($update_id ? $update_id : ''); ?>">
                        <input type="hidden" name="petition_id" id="petition_id" value="<?php echo esc_attr($petition_id ? $petition_id : $form_id); ?>">
                        <input type="hidden" name="petition_status" id="petition_status" value="<?php echo esc_attr($petition_status); ?>">
                        <input type="hidden" name="update_type" id="update_type" value="<?php echo esc_attr($update_type ? $update_type : 'update'); ?>">
                        <input type="hidden" name="campaign_id" id="campaign_id" value="<?php echo esc_attr($campaign_id ? $campaign_id : ''); ?>">
                        <input type="hidden" name="campaign_status" id="campaign_status" value="<?php echo esc_attr($campaign_status && $campaign_status == true ? 1 : 0); ?>">
                        <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($graph_title ? $graph_title : '') ?>">
                        <input type="hidden" id="graph_description" name="graph_description" value="<?php echo esc_attr($graph_description ? $graph_description : '') ?>">
                        <input type="hidden" id="graph_image" name="graph_image" value="<?php echo esc_url($graph_image ? $graph_image : '') ?>">
                        <input type="hidden" id="graph_site" name="graph_site" value="<?php echo esc_attr($graph_site ? $graph_site : '') ?>">
                        <div class="required field">
                            <label><?php esc_html_e('Title', 'campoal') ?></label>
                            <div class="ui large input">
                                <input class="ui large input" type="text" name="update_title" id="update_title" value="<?php echo esc_attr($update_id != '' ? $title : ($petition_status == 1 ? esc_html__('Victory!', 'campoal') : '')); ?>">
                            </div>
                        </div>
                        <div class="required field">
                            <label><?php esc_html_e('Your latest development', 'campoal') ?></label>
                            <div id="isDesc">
                            <?php 
                            $html_content = isset($content) ? $content : '';
                            if ($editor_style == 'classic') {
                                $settings = array(
                                    'media_buttons'         => $p_media_upload,
                                    'textarea_rows'         => 20,
                                    'drag_drop_upload'      => true,
                                    'quicktags'             => false,
                                    'dfw'                   => true,
                                    'tinymce'               => true,
                                );
                                wp_editor($html_content, 'update_content', $settings);
                            } else { ?>
                                <textarea id="update_content" name="update_content"><?php echo esc_textarea($html_content) ?></textarea>
                            <?php } ?>
                            </div>
                        </div>
                        <div class="field">
                            <label><?php esc_html_e('URL for an article', 'campoal') ?></label>
                            <div class="ui large input">
                                <input type="text" name="update_media" id="update_media" placeholder="<?php esc_attr_e('http://', 'campoal') ?>" value="<?php echo esc_attr(isset($media) ? $media : '') ?>">
                            </div>
                        </div>
                        <div class="field<?php echo esc_attr(empty($media) ? ' display none' : '') ?>" id="article-link-preview">
                        <?php if (empty($media)) : ?>
                            <div class="ui secondary loading segment">
                                <img src="<?php echo esc_url($link_preview_placeholder) ?>">
                            </div>
                        <?php else : ?>
                            <div class="ui secondary segment">
                                <div class="ui items">
                                    <div class="item">
                                        <div class="ui tiny image"><img src="<?php echo esc_url($graph_image) ?>"></div>
                                        <div class="content"><a class="header"><?php echo esc_html($graph_title) ?></a>
                                            <div class="meta"><span></span></div>
                                            <div class="description"><?php echo esc_html($graph_description) ?></div>
                                            <div class="extra"><?php echo esc_html($graph_site) ?></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        <?php endif; ?>
                        </div>   

                        <div class="ui section divider"></div>

                        <div class="field">
                            <label><?php esc_html_e('Add a photo or video', 'campoal') ?></label>
                            <input type="hidden" id="edit_gallery" value="<?php echo esc_attr(isset($gallery) ? $gallery : ''); ?>">
                            <input type="hidden" id="edit_attach" value="<?php echo esc_attr(isset($attach_id) ? $attach_id : ''); ?>">
                            <input type="hidden" id="edit_video" value="<?php echo esc_attr(isset($video) ? $video : ''); ?>">
                            <?php get_template_part('templates/gallery-upload'); ?>
                        </div>

                        <?php if ($email_update_supporter == 1 && $sendinblue_list_status == 1 && $sendinblue_key != '') : ?>
                            <div class="ui section divider"></div>

                            <h2 class="ui header">
                                <div class="content"><?php esc_html_e('Schedule Update Notification', 'campoal') ?>
                            </h2>
                            <div class="respon-message" id="campaign-response"></div>
                            <div class="field">
                                <div class="ui styled fluid accordion">
                                    <div class="<?php echo esc_attr($notification == 'schedule' ? 'active ' : '') ?>title">
                                        <div class="ui radio <?php echo esc_attr($campaign_status == false ? 'disabled ' : '') ?>checkbox">
                                            <input type="radio" name="update_notification" value="schedule"<?php echo esc_attr($notification == 'schedule' ? ' checked' : '') ?>>
                                            <label><i class="calendar icon"></i><?php echo esc_html__('Schedule for a specific time', 'campoal') ?></label>
                                        </div>
                                    </div>
                                    <div class="<?php echo esc_attr($notification == 'schedule' ? 'active ' : '') ?>content">
                                        <p><?php esc_html_e('Schedule your update of campaign to be sent in the future.', 'campoal') ?></p>
                                        <div class="ui calendar" id="update_scheduled_calendar">
                                            <div class="ui large right icon labeled input">
                                                <i class="calendar outline icon"></i>
                                                <input type="text" id="update_scheduled" placeholder="<?php esc_attr_e('Select your specific time', 'campoal') ?>" value="<?php echo esc_attr(isset($scheduled) ? $scheduled : '') ?>">
                                            </div>
                                        </div>
                                    </div>
                                    <div class="<?php echo esc_attr($notification == 'now' ? 'active ' : '') ?>title">
                                        <div class="ui radio <?php echo esc_attr($campaign_status == false ? 'disabled ' : '') ?>checkbox">
                                            <input type="radio" name="update_notification" value="now"<?php echo esc_attr($notification == 'now' ? ' checked' : '') ?>>
                                            <label><i class="time icon"></i><?php echo esc_html__('Send immediately', 'campoal') ?></label>
                                        </div>
                                    </div>
                                    <div class="<?php echo esc_attr($notification == 'now' ? 'active ' : '') ?>content">
                                        <p><?php esc_html_e('Send your update of campaign for supporters right now.', 'campoal') ?></p>
                                    </div>
                                    <div class="<?php echo esc_attr($notification == 'not' ? 'active ' : '') ?>title">
                                        <div class="ui radio checkbox">
                                            <input type="radio" name="update_notification" value="not"<?php echo esc_attr($notification == 'not' ? ' checked' : '') ?>>
                                            <label><i class="bell slash icon"></i><?php echo esc_html__('Not send', 'campoal') ?></label>
                                        </div>
                                    </div>
                                    <div class="<?php echo esc_attr($notification == 'not' ? 'active ' : '') ?>content">
                                        <p><?php esc_html_e('Not send email update of campaign for supporters.', 'campoal') ?></p>
                                        <?php if ($campaign_status == false) : ?>
                                        <div class="ui warning message display block">
                                            <i class="circle warning icon"></i>
                                            <?php esc_html_e('Email has been sent to the supporters, so you can not sending again.', 'campoal') ?>
                                        </div>
                                    <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        <?php endif; ?>

                        <div class="field">
                            <a class="ui right labeled icon primary large right floated button" id="submitUpdate"><i class="paper plane icon"></i><?php esc_html_e('Post Update', 'campoal') ?></a>
                        </div>
                        <?php wp_nonce_field('update_petition_ajax_nonce', 'securityUpdate', true); ?>
                    </form>
                </div>
            </div>
        </div>
<?php } else { ?>
    <?php get_template_part('404') ?>
<?php } ?>