<?php
/*
Template Name: Edit Petition
*/

/**
 * @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();

$cat_taxonomies = array( 
    'petition_category'
);
$cat_args = array(
    'orderby'           => 'name', 
    'order'             => 'ASC',
    'hide_empty'        => false
); 
$cat_terms = get_terms($cat_taxonomies, $cat_args);
$type_taxonomies = array( 
    'petition_topics'
);
$type_args = array(
    'orderby'           => 'name', 
    'order'             => 'ASC',
    'hide_empty'        => false
);
$type_terms = get_terms($type_taxonomies, $type_args);

$show_letter = isset($campoal_options['conikal_show_letter_section_field']) ? $campoal_options['conikal_show_letter_section_field'] : 0;
$number_sign_change_goal = isset($campoal_options['conikal_number_sign_change_field']) ? $campoal_options['conikal_number_sign_change_field'] : 0;
$receiver_step = isset($campoal_options['conikal_receiver_step_field']) ? $campoal_options['conikal_receiver_step_field'] : 1;
$editor_style = isset($campoal_options['conikal_editor_style_field']) ? $campoal_options['conikal_editor_style_field'] : 'markdown';
$p_content = isset($campoal_options['conikal_p_content_field']) ? $campoal_options['conikal_p_content_field'] : 1;
$p_content_r = isset($campoal_options['conikal_p_content_r_field']) ? $campoal_options['conikal_p_content_r_field'] : 1;
$p_category = isset($campoal_options['conikal_p_category_field']) ? $campoal_options['conikal_p_category_field'] : 1;
$p_category_r = isset($campoal_options['conikal_p_category_r_field']) ? $campoal_options['conikal_p_category_r_field'] : 1;
$maximum_category = isset($campoal_options['conikal_maximum_category_field']) ? $campoal_options['conikal_maximum_category_field'] : 1;
$p_topics = isset($campoal_options['conikal_p_topics_field']) ? $campoal_options['conikal_p_topics_field'] : 1;
$p_topics_r = isset($campoal_options['conikal_p_topics_r_field']) ? $campoal_options['conikal_p_topics_r_field'] : 1;
$p_city = isset($campoal_options['conikal_p_city_field']) ? $campoal_options['conikal_p_city_field'] : 1;
$p_city_r = isset($campoal_options['conikal_p_city_r_field']) ? $campoal_options['conikal_p_city_r_field'] : 1;
$p_coordinates = isset($campoal_options['conikal_p_coordinates_field']) ? $campoal_options['conikal_p_coordinates_field'] : 1;
$p_coordinates_r = isset($campoal_options['conikal_p_coordinates_r_field']) ? $campoal_options['conikal_p_coordinates_r_field'] : 1;
$p_address = isset($campoal_options['conikal_p_address_field']) ? $campoal_options['conikal_p_address_field'] : 1;
$p_address_r = isset($campoal_options['conikal_p_address_r_field']) ? $campoal_options['conikal_p_address_r_field'] : 1;
$p_neighborhood = isset($campoal_options['conikal_p_neighborhood_field']) ? $campoal_options['conikal_p_neighborhood_field'] : 1;
$p_neighborhood_r = isset($campoal_options['conikal_p_neighborhood_r_field']) ? $campoal_options['conikal_p_neighborhood_r_field'] : 1;
$p_zip = isset($campoal_options['conikal_p_zip_field']) ? $campoal_options['conikal_p_zip_field'] : 1;
$p_zip_r = isset($campoal_options['conikal_p_zip_r_field']) ? $campoal_options['conikal_p_zip_r_field'] : 1;
$p_state = isset($campoal_options['conikal_p_state_field']) ? $campoal_options['conikal_p_state_field'] : 1;
$p_state_r = isset($campoal_options['conikal_p_state_r_field']) ? $campoal_options['conikal_p_state_r_field'] : 1;
$p_country = isset($campoal_options['conikal_p_country_field']) ? $campoal_options['conikal_p_country_field'] : 1;
$p_country_r = isset($campoal_options['conikal_p_country_r_field']) ? $campoal_options['conikal_p_country_r_field'] : 1;
$p_receiver = isset($campoal_options['conikal_p_receiver_field']) ? $campoal_options['conikal_p_receiver_field'] : 1;
$p_receiver_r = isset($campoal_options['conikal_p_receiver_r_field']) ? $campoal_options['conikal_p_receiver_r_field'] : 1;
$p_position = isset($campoal_options['conikal_p_position_field']) ? $campoal_options['conikal_p_position_field'] : 1;
$p_position_r = isset($campoal_options['conikal_p_position_r_field']) ? $campoal_options['conikal_p_position_r_field'] : 1;
$p_goal_d = isset($conikal_prop_fields_settings['conikal_p_goal_default_field']) ? $conikal_prop_fields_settings['conikal_p_goal_default_field'] : 5000;
$p_video = isset($campoal_options['conikal_p_video_field']) ? $campoal_options['conikal_p_video_field'] : 1;
$p_media_upload = isset($campoal_options['conikal_p_media_upload_field']) ? $campoal_options['conikal_p_media_upload_field'] : 1;

$tab_active = isset($_GET['tab']) ? sanitize_text_field($_GET['tab']) : 'title';
?>
<?php 
    if (isset($_GET['id']) && $_GET['id'] != '') {
        $edit_id = sanitize_text_field($_GET['id']);

        $args = array(
            'p' => $edit_id,
            'post_type' => 'petition',
            'post_status' => array('publish', 'pending')
        );

        $query = new WP_Query($args);

        if ($query->have_posts()) {
            while($query->have_posts()) {
                $query->the_post();
                $id             = get_the_ID();
                $link           = get_permalink($id);
                $title          = get_the_title($id);
                $content        = get_the_content($id);
                $heading        = get_post_meta($id, 'petition_heading', true);
                $excerpt        = get_the_excerpt($id);
                $categories     = wp_get_post_terms($id, 'petition_category', true);
                $category_id    = ($categories && array_key_exists(0, $categories) ? $categories[0]->term_id : '');
                $topics         = wp_get_post_terms($id, 'petition_topics');
                $excerpt        = conikal_get_excerpt_by_id($id);
                $comments       = wp_count_comments($id);
                $gallery        = get_post_meta($id, 'petition_gallery', true);
                $images         = conikal_get_gallery_images($gallery);
                $video          = get_post_meta($id, 'petition_video', true);
                $address        = get_post_meta($id, 'petition_address', true);
                $city           = get_post_meta($id, 'petition_city', true);
                $state          = get_post_meta($id, 'petition_state', true);
                $neighborhood   = get_post_meta($id, 'petition_neighborhood', true);
                $zip            = get_post_meta($id, 'petition_zip', true);
                $country        = get_post_meta($id, 'petition_country', true);
                $lat            = get_post_meta($id, 'petition_lat', true);
                $lng            = get_post_meta($id, 'petition_lng', true);
                $decisionmakers = conikal_get_decistion_makers($id);
                $goal           = get_post_meta($id, 'petition_goal', true);
                $updates        = get_post_meta($id, 'petition_update', true);
                $thumb          = get_post_meta($id, 'petition_thumb', true);
                $letter         = get_post_meta($id, 'petition_letter', true);
                $status         = get_post_meta($id, 'petition_status', true);
                $sendinblue_list = get_post_meta($id, 'petition_sendinblue_list', true);
                $post_author_id = get_post_field( 'post_author', $id );
                $user_avatar    = get_the_author_meta('avatar' , get_the_author_meta('ID'));
                $attach_id      = get_post_thumbnail_id($id);
                $post_type      = get_post_type($id);
            }
        }
        
        wp_reset_postdata();
        wp_reset_query();
    } else {
        $edit_id = isset($_GET['id']) ? sanitize_text_field($_GET['id']) : '';
        $link = get_permalink($edit_id);
    }
?>
<?php if ($edit_id != '' && $post_type == 'petition' && ($post_author_id == $current_user->ID || current_user_can('editor') || current_user_can('administrator') || conikal_co_author_permission($edit_id))) { ?>
        <div class="ui container submit-petition">
            <div class="ui left aligned header submit-petition-title">
                <div class="content">
                    <?php esc_html_e('Edit Petition', 'campoal') ?>
                </div>
            </div>
            <div class="ui grid">
                <div class="sixteen wide mobile five wide tablet four wide computer column">
                    <div class="ui hidden divider"></div>
                    <div class="ui vertical secondary fluid large menu" id="petition-edit-tab">
                        <a href="#title" class="item<?php echo esc_attr($tab_active == 'title' ? ' active' : '') ?>" data-tab="title">
                            <i class="heading icon"></i>
                            <?php esc_html_e('Title', 'campoal') ?>
                        </a>
                        <?php if ($receiver_step) : ?>
                        <a href="#receiver" class="item<?php echo esc_attr($tab_active == 'receiver' ? ' active' : '') ?>" data-tab="receiver">
                            <i class="user circle icon"></i>
                            <?php esc_html_e('Receiver', 'campoal') ?>
                        </a>
                        <?php endif; ?>
                        <a href="#content" class="item<?php echo esc_attr($tab_active == 'content' ? ' active' : '') ?>" data-tab="content">
                            <i class="list alternate icon"></i>
                            <?php esc_html_e('Content', 'campoal') ?>
                        </a>
                        <?php if ($show_letter) : ?>
                        <a href="#letter" class="item<?php echo esc_attr($tab_active == 'letter' ? ' active' : '') ?>" data-tab="letter">
                            <i class="sticky note icon"></i>
                            <?php esc_html_e('Letter', 'campoal') ?>
                        </a>
                        <?php endif; ?>
                        <a href="#photo" class="item<?php echo esc_attr($tab_active == 'photo' ? ' active' : '') ?>" data-tab="photo">
                            <i class="image icon"></i>
                            <?php esc_html_e('Photo', 'campoal') ?>
                        </a>
                        <?php if (class_exists('ACF')) : ?>
                        <a href="#infomations" class="item<?php echo esc_attr($tab_active == 'infomations' ? ' active' : '') ?>" data-tab="infomations">
                            <i class="info circle icon"></i>
                            <?php esc_html_e('Infomations', 'campoal') ?>
                        </a>
                        <?php endif; ?>
                    </div>
                </div>
                <div class="sixteen wide mobile eleven wide tablet ten wide computer left aligned column">
                        <div id="save-response" class="respon-message"></div>
                        <div id="letter-response"></div>
                        <div class="ui form">
                            <input type="hidden" id="current_user" name="current_user" value="<?php echo esc_attr($post_author_id); ?>">
                            <input type="hidden" id="new_id" name="new_id" value="<?php echo esc_attr(isset($edit_id) ? esc_attr($edit_id) : ''); ?>">
                            <input type="hidden" id="sendinblue_list" name="sendinblue_list" value="<?php echo esc_attr(isset($sendinblue_list) ? esc_attr($sendinblue_list) : ''); ?>">
                            <input type="hidden" id="new_goal_d" name="new_goal_d" value="<?php echo esc_attr($goal ? $goal : $p_goal_d) ?>">
                            <input type="hidden" name="new_status" id="new_status" value="<?php echo esc_attr(isset($status) ? $status : 0); ?>">

                            <!-- STEP ONE -->
                            <div class="ui<?php echo esc_attr($tab_active == 'title' ? ' active' : '') ?> tab" data-tab="title">
                                <h2 class="ui dividing header">
                                    <div class="content"><?php esc_html_e('Write your petition title', 'campoal') ?>
                                </h2>
                                <div class="required field">
                                    <label><?php esc_html_e('Title', 'campoal') ?></label>
                                    <div class="ui large fluid input">
                                        <input type="text" id="new_title" name="new_title" placeholder="<?php esc_attr_e('What do you want to achieve?', 'campoal'); ?>" value="<?php echo esc_attr(isset($title) ? $title : ''); ?>">
                                    </div>
                                </div>
                                <div class="field">
                                    <label><?php esc_html_e('Heading for sharing', 'campoal') ?></label>
                                    <div class="ui large fluid input">
                                        <input type="text" id="new_heading" name="new_heading" placeholder="<?php esc_attr_e('Custom heading to appear on Facebook and Twitter', 'campoal'); ?>" value="<?php echo esc_attr(isset($heading) ? $heading : ''); ?>">
                                    </div>
                                    <small><?php esc_html_e('Use with variables: {title}, {supporter_name}, {supporter_number}, {decision_maker}, {author_name}', 'campoal') ?></small>
                                </div>
                                <div class="field">
                                    <label><?php esc_html_e('Excerpt', 'campoal') ?></label>
                                    <div class="ui large fluid input">
                                        <textarea type="text" id="new_excerpt" name="new_excerpt" rows="3" placeholder="<?php esc_attr_e('Short description about campaign', 'campoal'); ?>"><?php echo esc_html($excerpt) ?></textarea>
                                    </div>
                                </div>
                                <?php if($p_category != '' && $p_category == 1) { ?>
                                <div class="<?php echo esc_attr($p_category_r == 1 ? 'required ' : '') ?>field">
                                    <label><?php esc_html_e('Category', 'campoal') ?></label>                        
                                    <select class="ui dropdown large fluid input category-select" name="new_category" id="new_category"<?php echo esc_attr(isset($categories) && array_key_exists(0, $categories) && (count($categories) > 1) || ($maximum_category > 1) ? ' multiple' : '') ?>>
                                        <option value=""><?php esc_html_e('Select a category', 'campoal') ?></option>
                                        <?php foreach($cat_terms as $cat_term) { ?>
                                            <option value="<?php echo esc_attr($cat_term->term_id); ?>"><?php echo esc_html($cat_term->name); ?></option>
                                        <?php } ?>
                                    </select>
                                    <?php if (isset($categories) && array_key_exists(0, $categories) && (count($categories) > 1) || ($maximum_category > 1)) { ?>
                                        <?php foreach($categories as $category) {
                                            echo '<input type="hidden" name="category_ids[]" value="' . esc_attr($category->term_id) . '">'; 
                                        } ?>
                                    <?php } else { ?>
                                        <input type="hidden" name="category_id" id="category_id" value="<?php echo esc_attr(isset($category_id) ? esc_attr($category_id) : ''); ?>">
                                    <?php } ?>
                                </div>
                                <?php } ?>

                                <?php if(!$receiver_step && $p_address != '' && $p_address == 1) { ?>
                                <div class="<?php echo esc_attr($p_address_r == 1 ? 'required ' : '') ?>field">
                                    <label><?php esc_html_e('Add a place', 'campoal') ?></label>
                                    <div class="ui large fluid input">
                                        <input type="text" id="new_address" name="new_address" value="<?php echo esc_attr(isset($address) ? $address : ''); ?>" placeholder="<?php esc_attr_e('Add a neighborhood, state, city or country', 'campoal'); ?>">
                                    <?php if($p_city != '' && $p_city == 1) { ?>
                                        <input type="hidden" id="new_city" name="new_city" value="<?php echo esc_attr(isset($city) ? $city : ''); ?>">
                                    <?php } ?>
                                    <?php if($p_state != '' && $p_state == 1) { ?>
                                        <input type="hidden" id="new_state" name="new_state" value="<?php echo esc_attr(isset($state) ? $state : ''); ?>">
                                    <?php } ?>
                                    <?php if($p_neighborhood != '' && $p_neighborhood == 1) { ?>
                                        <input type="hidden" id="new_neighborhood" name="new_neighborhood" value="<?php echo esc_attr(isset($neighborhood) ? $neighborhood : ''); ?>">
                                    <?php } ?>
                                    <?php if($p_country != '' && $p_country == 1) { ?>
                                        <input type="hidden" id="new_country" name="new_country" value="<?php echo esc_attr(isset($country) ? $country : ''); ?>">
                                    <?php } ?>
                                    <?php if($p_zip != '' && $p_zip == 1) { ?>
                                        <input type="hidden" id="new_zip" name="new_zip" value="<?php echo esc_attr(isset($zip) ? $zip : ''); ?>">
                                    <?php } ?>
                                    <?php if($p_coordinates != '' && $p_coordinates == 1) { ?>
                                        <input type="hidden" id="new_lat" name="new_lat" value="<?php echo esc_attr(isset($lat) ? $lat : ''); ?>">
                                        <input type="hidden" id="new_lng" name="new_lng" value="<?php echo esc_attr(isset($lng) ? $lng : ''); ?>">
                                    <?php } ?>
                                    </div>
                                </div>
                                <?php } ?>
                                <div class="ui hidden divider"></div>
                            </div>


                            <!-- STEP TWO -->
                            <?php if ($receiver_step) : ?>
                            <div class="ui<?php echo esc_attr($tab_active == 'receiver' ? ' active' : '') ?> tab" data-tab="receiver">
                                <h2 class="ui dividing header">
                                    <div class="content"><?php esc_html_e('Choose a decision maker', 'campoal') ?>
                                </h2>
                                <div class="input-fields-wrap" id="new-decision-makers">
                                    <div class="ui grid search decision-search">
                                        <?php if(isset($decisionmakers[0]) && $p_receiver == 1) { ?>
                                        <div class="sixteen wide mobile eight wide tablet eight wide computer column">
                                            <div class="<?php echo esc_attr($p_receiver_r == 1 ? 'required ' : '') ?>field">
                                                <label><?php esc_html_e('Full name', 'campoal') ?></label>
                                                <div class="ui large fluid input">
                                                    <input class="prompt" type="text" id="new_receiver" name="new_receiver[]" placeholder="<?php esc_attr_e('Who can make this happen?', 'campoal'); ?>" value="<?php echo esc_attr(isset($decisionmakers[0]) ? $decisionmakers[0]['name'] : ''); ?>">
                                                    <input class="new_decisionmakers" type="hidden" name="new_decisionmakers[]" value="<?php echo esc_attr(isset($decisionmakers[0]) ? $decisionmakers[0]['id'] : ''); ?>">
                                                </div>
                                            </div>
                                        </div>
                                        <?php } ?>
                                        <?php if(isset($decisionmakers[0]) && $p_position == 1) { ?>
                                        <div class="sixteen wide mobile eight wide tablet eight wide computer column">
                                            <div class="<?php echo esc_attr($p_position_r == 1 ? 'required ' : '') ?>field">
                                                <?php if (!wp_is_mobile()) { ?>
                                                    <label><?php esc_html_e('Title or organization', 'campoal') ?></label>
                                                <?php } ?>
                                                <div class="ui large fluid input">
                                                    <input class="decision-title" type="text" id="new_position" name="new_position[]" placeholder="<?php esc_attr_e('What is their position?', 'campoal'); ?>" value="<?php echo esc_attr(isset($decisionmakers[0]) ? $decisionmakers[0]['position'] : ''); ?>">
                                                </div>
                                            </div>
                                        </div>
                                        <?php } ?>
                                        <div class="results"></div>
                                    </div>

                                    <?php if(!empty($decisionmakers) && $p_receiver == 1) { 
                                        unset($decisionmakers[0]);
                                        foreach ($decisionmakers as $decisionmaker) {
                                            $decisionmaker = (object) $decisionmaker; ?>
                                            <div class="ui grid search decision-search">
                                                <div class="sixteen wide mobile eight wide tablet eight wide computer column">
                                                    <div class="<?php echo esc_attr($p_receiver_r == 1 ? 'required ' : '') ?>field">
                                                        <div class="ui large fluid input">
                                                            <input class="prompt" type="text" id="new_receiver" name="new_receiver[]" placeholder="<?php esc_attr_e('Who can make this happen?', 'campoal'); ?>" value="<?php echo esc_attr($decisionmaker->name) ?>">
                                                            <input class="new_decisionmakers" type="hidden" name="new_decisionmakers[]" value="<?php echo esc_attr($decisionmaker->id); ?>">
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="sixteen wide mobile eight wide tablet eight wide computer column">
                                                    <div class="<?php echo esc_attr($p_position_r == 1 ? 'required ' : '') ?>field">
                                                        <div class="ui large fluid input">
                                                            <input class="decision-title" type="text" id="new_position" name="new_position[]" placeholder="<?php esc_attr_e('What is their position?', 'campoal'); ?>" value="<?php echo esc_attr($decisionmaker->position) ?>">
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="results"></div>
                                                <a href="#" class="remove-field"><i class="inverted circular close link icon"></i></a>
                                            </div>
                                    <?php
                                        }
                                    } ?>
                                </div>
                                <div class="ui vertical basic segment">
                                    <button class="ui primary tiny right floated button add-field-button"><i class="plus icon"></i><?php esc_html_e('Add', 'campoal') ?></button>
                                </div>
                                
                                <?php if($p_address != '' && $p_address == 1) { ?>
                                <div class="<?php echo esc_attr($p_address_r == 1 ? 'required ' : '') ?>field">
                                    <label><?php esc_html_e('Add a place', 'campoal') ?></label>
                                    <div class="ui large fluid input">
                                        <input type="text" id="new_address" name="new_address" value="<?php echo esc_attr(isset($address) ? $address : ''); ?>" placeholder="<?php esc_attr_e('Add a neighborhood, state, city or country', 'campoal'); ?>">
                                    <?php if($p_city != '' && $p_city == 1) { ?>
                                        <input type="hidden" id="new_city" name="new_city" value="<?php echo esc_attr(isset($city) ? $city : ''); ?>">
                                    <?php } ?>
                                    <?php if($p_state != '' && $p_state == 1) { ?>
                                        <input type="hidden" id="new_state" name="new_state" value="<?php echo esc_attr(isset($state) ? $state : ''); ?>">
                                    <?php } ?>
                                    <?php if($p_neighborhood != '' && $p_neighborhood == 1) { ?>
                                        <input type="hidden" id="new_neighborhood" name="new_neighborhood" value="<?php echo esc_attr(isset($neighborhood) ? $neighborhood : ''); ?>">
                                    <?php } ?>
                                    <?php if($p_country != '' && $p_country == 1) { ?>
                                        <input type="hidden" id="new_country" name="new_country" value="<?php echo esc_attr(isset($country) ? $country : ''); ?>">
                                    <?php } ?>
                                    <?php if($p_zip != '' && $p_zip == 1) { ?>
                                        <input type="hidden" id="new_zip" name="new_zip" value="<?php echo esc_attr(isset($zip) ? $zip : ''); ?>">
                                    <?php } ?>
                                    <?php if($p_coordinates != '' && $p_coordinates == 1) { ?>
                                        <input type="hidden" id="new_lat" name="new_lat" value="<?php echo esc_attr(isset($lat) ? $lat : ''); ?>">
                                        <input type="hidden" id="new_lng" name="new_lng" value="<?php echo esc_attr(isset($lng) ? $lng : ''); ?>">
                                    <?php } ?>
                                    </div>
                                </div>
                                <?php } ?>
                                <!--<div data-tags-input-name="decision-maker" id="decision-maker"></div>-->

                                <div class="ui hidden divider"></div>
                            </div>
                            <?php endif; ?>


                            <!-- STEP THREE -->
                            <div class="ui<?php echo esc_attr($tab_active == 'content' ? ' active' : '') ?> tab" data-tab="content">
                                <h2 class="ui dividing header">
                                    <div class="content"><?php esc_html_e('Explain the problem you want to solve', 'campoal') ?>
                                </h2>
                                <div class="required field">
                                    <label><?php esc_html_e('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'         => 15,
                                            'drag_drop_upload'      => true,
                                            'quicktags'             => false,
                                            'dfw'                   => true,
                                            'tinymce'               => true,
                                        );
                                        wp_editor($html_content, 'new_content', $settings);
                                    } else { ?>
                                        <textarea id="new_content" name="new_content"><?php echo esc_textarea($html_content) ?></textarea>
                                    <?php } ?>
                                    </div>
                                </div>

                                <?php if($p_topics != '' && $p_topics == 1) { ?>
                                <div class="<?php echo esc_attr($p_topics_r == 1 ? 'required ' : '') ?>field">
                                    <label><?php esc_html_e('Add topics', 'campoal') ?></label>
                                    <div class="ui fluid multiple search selection large dropdown" id="topics-search">
                                        <input name="new_topics" id="new_topics" type="hidden">
                                        <i class="dropdown icon"></i>
                                        <div class="default text"><?php esc_html_e('Keywords related this petition', 'campoal') ?></div>
                                        <div class="menu">
                                        </div>
                                    </div>

                                    <?php if (isset($topics)) { ?>
                                        <?php foreach($topics as $topic) { ?>
                                            <input type="hidden" name="current_topics[]" value="<?php echo esc_attr($topic->name) ?>">
                                        <?php } ?>
                                    <?php } ?>
                                </div>
                                <?php } ?>

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

                            <!-- EDIT LETTER -->
                            <div class="ui<?php echo esc_attr($tab_active == 'letter' ? ' active' : '') ?> tab" data-tab="letter">
                                <h2 class="ui dividing header">
                                    <div class="content"><?php esc_html_e('Letter content send for decision makers', 'campoal') ?>
                                </h2>
                                <div class="required field">
                                    <label><?php esc_html_e('Letter Content', 'campoal') ?></label>
                                    <div id="isDesc">
                                        <?php 
                                        if ($editor_style == 'classic') {
                                            $letter = isset($letter) ? $letter : '';
                                            $letter_settings = array(
                                                'media_buttons'         => false,
                                                'textarea_rows'         => 15,
                                                'drag_drop_upload'      => true,
                                                'quicktags'             => false,
                                                'dfw'                   => true,
                                                'tinymce'               => true,
                                            );
                                            wp_editor($letter, 'new_letter', $letter_settings);
                                        } else { ?>
                                        <textarea id="new_letter" name="new_letter"><?php echo esc_textarea($letter) ?></textarea>
                                        <?php } ?>
                                    </div>
                                </div>

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

                            <!-- STEP FOUR -->
                            <div class="ui<?php echo esc_attr($tab_active == 'photo' ? ' active' : '') ?> tab" data-tab="photo">
                                <h2 class="ui dividing header">
                                    <div class="content"><?php esc_html_e('Add a photo or video', 'campoal') ?>
                                </h2>
                                <input type="hidden" id="edit_gallery" value="<?php echo esc_attr(isset($gallery) ? esc_attr($gallery) : ''); ?>">
                                <input type="hidden" id="edit_attach" value="<?php echo esc_attr(isset($attach_id) ? esc_attr($attach_id) : ''); ?>">
                                <input type="hidden" id="edit_video" value="<?php echo esc_attr(isset($video) ? esc_attr($video) : ''); ?>">
                                <?php get_template_part('templates/gallery-upload'); ?>
                            </div>

                            <!-- ADVENCED CUSTOM FIELDS -->
                            <?php if (class_exists('ACF')) : ?>
                            <div class="ui<?php echo esc_attr($tab_active == 'infomations' ? ' active' : '') ?> tab" data-tab="infomations">
                                <h2 class="ui dividing header">
                                    <div class="content"><?php esc_html_e('Infomations', 'campoal') ?>
                                </h2>
                                <?php acf_form(array(
                                    'post_id'       => $edit_id,
                                    'post_title'    => false,
                                    'post_content'  => false,
                                    'submit_value'  => esc_html__('Save', 'campoal'),
                                    'return'        => get_permalink($edit_id),
                                )); ?>
                            </div>
                            <?php endif; ?>
                        </div>
                        
                        <div class="ui basic vertical right aligned segment">
                            <button class="ui right labeled icon primary large button finish-btn" id="finish-btn"><i class="save icon"></i><?php esc_html_e('Save', 'campoal') ?></button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
<?php wp_nonce_field('submit_petition_ajax_nonce', 'securitySubmitPetition', true); ?>
<?php } else { ?>
    <?php get_template_part('404') ?>
<?php } ?>

<?php get_footer(); ?>