<?php

class FPCrowdFundingMycampaign {

//adding tab
    public static function cf_my_account_tab($settings_tab) {
        $settings_tab['my_account'] = __('My Account Page', 'galaxyfunder');
        return $settings_tab;
    }

    public static function cf_admin_my_campaign_options() {
        global $woocommerce;
        return apply_filters('woocommerce_my_account_settings', array(array(
                'name' => __('My Account Page Settings', 'galaxyfunder'),
                'type' => 'title',
                'desc' => '',
                'id' => '_cf_my_campaign'
            ),
            array(
                'name' => __('My Campaign Table Show/Hide', 'galaxyfunder'),
                'desc' => __('This Controls the My Campaign Table Show or Hide', 'galaxyfunder'),
                'id' => 'cf_display_mycampaign_table',
                'css' => 'min-width:150px;',
                'std' => 'on', // WooCommerce < 2.0
                'default' => 'on', // WooCommerce >= 2.0
                'newids' => 'cf_display_mycampaign_table',
                'type' => 'select',
                'options' => array(
                    'on' => __('Show', 'galaxyfunder'),
                    'off' => __('Hide', 'galaxyfunder'),
                ),
            ),
            array(
                'name' => __('Title of My Campaign', 'galaxyfunder'),
                'desc' => __('Change My Campaign Title in My Account Page', 'galaxyfunder'),
                'tip' => '',
                'id' => 'cf_mycampaign_title',
                'css' => 'min-width:550px;',
                'std' => 'My Campaigns',
                'type' => 'text',
                'newids' => 'cf_mycampaign_title',
                'desc_tip' => true,
            ),
            array(
                'name' => __('S.No Label', 'galaxyfunder'),
                'desc' => __('Change S.No Caption in Single in My Campaign table by your Custom Words', 'galaxyfunder'),
                'tip' => '',
                'id' => 'cf_mycampaign_serial_number_label',
                'css' => 'min-width:550px;',
                'std' => 'S.No',
                'type' => 'text',
                'newids' => 'cf_mycampaign_serial_number_label',
                'desc_tip' => true,
            ),
            array(
                'name' => __('Campaign Label', 'galaxyfunder'),
                'desc' => __('Change Campaign Caption in My Campaign table by your Custom Words', 'galaxyfunder'),
                'tip' => '',
                'css' => 'min-width:550px;',
                'id' => 'cf_mycampaign_campaign_label',
                'std' => 'Campaign',
                'type' => 'text',
                'newids' => 'cf_mycampaign_campaign_label',
                'desc_tip' => true,
            ),
            array(
                'name' => __('Date Label', 'galaxyfunder'),
                'desc' => __('Change Date Caption in My Campaign table by your Custom Words', 'galaxyfunder'),
                'tip' => '',
                'css' => 'min-width:550px;',
                'id' => 'cf_mycampaign_date_label',
                'std' => 'Date',
                'type' => 'text',
                'newids' => 'cf_mycampaign_date_label',
                'desc_tip' => true,
            ),
            array(
                'name' => __('Goal Label', 'galaxyfunder'),
                'desc' => __('Change Goal Caption in My Campaign table by your Custom Words', 'galaxyfunder'),
                'tip' => '',
                'css' => 'min-width:550px;',
                'id' => 'cf_mycampaign_goal_label',
                'std' => 'Goal',
                'type' => 'text',
                'newids' => 'cf_mycampaign_goal_label',
                'desc_tip' => true,
            ),
            array(
                'name' => __('Fund Raised Label', 'galaxyfunder'),
                'desc' => __('Change Fund Raised Caption in My Campaign table by your Custom Words', 'galaxyfunder'),
                'tip' => '',
                'css' => 'min-width:550px;',
                'id' => 'cf_mycampaign_raised_label',
                'std' => 'Raised',
                'type' => 'text',
                'newids' => 'cf_mycampaign_raised_label',
                'desc_tip' => true,
            ),
            array(
                'name' => __('Fund Raised Percentage Label', 'galaxyfunder'),
                'desc' => __('Change Fund Raised Percentage Caption in My Campaign table by your Custom Words', 'galaxyfunder'),
                'tip' => '',
                'css' => 'min-width:550px;',
                'id' => 'cf_mycampaign_raised_percent_label',
                'std' => 'Raised %',
                'type' => 'text',
                'newids' => 'cf_mycampaign_raised_percent_label',
                'desc_tip' => true,
            ),
            array(
                'name' => __('Funders Label', 'galaxyfunder'),
                'desc' => __('Change Funders in My Campaign table by your Custom Words', 'galaxyfunder'),
                'tip' => '',
                'css' => 'min-width:550px;',
                'id' => 'cf_mycampaign_funders_label',
                'std' => 'Funders',
                'type' => 'text',
                'newids' => 'cf_mycampaign_funders_label',
                'desc_tip' => true,
            ),
            array(
                'name' => __('Status Label', 'galaxyfunder'),
                'desc' => __('Change Funders in My Campaign table by your Custom Words', 'galaxyfunder'),
                'tip' => '',
                'css' => 'min-width:550px;',
                'id' => 'cf_mycampaign_status_label',
                'std' => 'Status',
                'type' => 'text',
                'newids' => 'cf_mycampaign_status_label',
                'desc_tip' => true,
            ),
            array(
                'name' => __('Campaign Table Position', 'galaxyfunder'),
                'type' => 'radio',
                'desc' => '',
                'id' => 'cf_mycampaign_table_position',
                'options' => array('1' => 'Start of My Account', '2' => 'End of My Account'),
                'class' => 'cf_mycampaign_table_position',
                'std' => '2',
                'newids' => 'cf_mycampaign_table_position',
            ),
            
            array(
                'name'=>__('Show/Hide Your Subscribe Link', 'galaxyfunder'),
                'desc' => __('Show/Hide Your Subscribe Link if you want to display it in my account page', 'galaxyfunder'),
                'id' => 'gf_show_hide_your_subscribe_link',
                'newids' => 'gf_show_hide_your_subscribe_link',
                'class' => 'gf_show_hide_your_subscribe_link',
                'std' => '1',
                'type' => 'select',
                'options' => array(
                    '1' => __('Show', 'galaxyfunder'),
                    '2' => __('Hide', 'galaxyfunder'),
                ),
                
            ),
            
            array(
                'name'=>__('Subscribe Link Message','galaxyfunder'),
                'desc'=>__('This Message will be displayed on the option to Unsubscribe from Galaxy Funder Emails','galaxyfunder'),
                'id'=>'gf_unsubscribe_message_myaccount_page',
                'css' => 'min-width:550px;',
                'std'=>'Unsubscribe here To Stop Receiving Email',
                'type'=>'textarea',
                'newids'=>'gf_unsubscribe_message_myaccount_page',
                'class'=>'gf_unsubscribe_message_myaccount_page',
                'desc_tip' => true,
            ),
            array('type' => 'sectionend', 'id' => '_cf_my_campaign'),
        ));
    }

    public static function cf_admin_my_campaign_settings() {
        woocommerce_admin_fields(FPCrowdFundingMycampaign::cf_admin_my_campaign_options());
    }

    public static function cf_admin_my_campaign_update_settings() {
        woocommerce_update_options(FPCrowdFundingMycampaign::cf_admin_my_campaign_options());
    }

    public static function cf_default_my_account_page() {
        global $woocommerce;
        foreach (FPCrowdFundingMycampaign::cf_admin_my_campaign_options() as $setting) {
            if (isset($setting['newids']) && ($setting['std'])) {
                if (get_option($setting['newids']) == FALSE) {
                    add_option($setting['newids'], $setting['std']);
                }
            }
        }
    }

    public static function cf_my_account_campaign() {
        if (get_option('cf_display_mycampaign_table') == "on") {
            global $woocommerce;
            echo '<h2>' . get_option("cf_mycampaign_title") . '</h2>';
            $user_ID = get_current_user_id();
            $args = array('post_type' => 'product', 'posts_per_page' => '-1', 'author' => $user_ID, 'post_status' => 'draft,publish');
            ?>


            <?php

            $listmycampaign = new WP_Query($args);
            // The Loop
            if ($listmycampaign->have_posts()) {
                // echo '<table id="example">';
                echo '<p> ' . __('Search:', 'galaxyfunder') . '<input id="filter" type="text"/>  ' . __('Page Size:', 'galaxyfunder') . '
                <select id="change-page-size">

									<option value="5">5</option>
									<option value="10">10</option>
                    <option value="50">50</option>
                    <option value="100">100</option>
                </select></p>';

                echo ' <table class = "example demo shop_table my_account_orders table-bordered" data-filter = "#filter" data-page-size="5" data-page-previous-text = "prev" data-filter-text-only = "true" data-page-next-text = "next">';
                echo '<thead><tr><th data-toggle="true" data-sort-initial = "true">' . get_option('cf_mycampaign_serial_number_label') . '</th><th>' . get_option('cf_mycampaign_campaign_label') . '</th><th data-hide="phone">' . get_option('cf_mycampaign_date_label') . '</th><th data-hide = "phone">' . get_option("cf_mycampaign_goal_label") . '</th><th data-hide="phone">' . get_option("cf_mycampaign_raised_label") . '</th><th data-hide="phone">' . get_option("cf_mycampaign_raised_percent_label") . '</th><th data-hide="phone,tablet">' . get_option("cf_mycampaign_funders_label") . '</th><th data-hide="phone,tablet">' . get_option("cf_mycampaign_status_label") . '</th></tr></thead><tbody>';
                $i = 0;
                while ($listmycampaign->have_posts()) {
                    $listmycampaign->the_post();
                    if (get_post_meta(get_the_ID(), '_crowdfundingcheckboxvalue', true) == 'yes') {
                        $i++;
                        echo '<tr>';
                        echo '<td>' . $i . '</td>';
                        echo '<td><a href="' . get_permalink(get_the_ID()) . '" target="_blank">' . get_the_title() . '</a></td>';
                        echo '<td>' . get_the_date() . '</td>';
                        echo '<td>' . CrowdFunding::get_woocommerce_formatted_price(get_post_meta(get_the_ID(), '_crowdfundinggettargetprice', true)) . '</td>';
                        if (get_post_meta(get_the_ID(), '_crowdfundingtotalprice', true) == '') {
                            $total_price = CrowdFunding::get_woocommerce_formatted_price(0);
                        } else {
                            $total_price = CrowdFunding::get_woocommerce_formatted_price(get_post_meta(get_the_ID(), '_crowdfundingtotalprice', true));
                        }

                        echo '<td>' .$total_price . '</td>';
                        if (get_post_meta(get_the_ID(), '_crowdfundinggoalpercent', true) == '') {
                            $percentage_value = 0;
                        } else {
                            $percentage_value = get_post_meta(get_the_ID(), '_crowdfundinggoalpercent', true);
                        }
                        echo '<td>' . $percentage_value . '%</td>';

                        if (get_post_meta(get_the_ID(), '_update_total_funders', true) == '') {
                            $total_funders = 0;
                        } else {
                            $total_funders = get_post_meta(get_the_ID(), '_update_total_funders', true);
                        }
                        echo '<td>' . $total_funders . '</td>';
                        //same date function from backend
                        echo '<td>';
                        $datestr = get_post_meta(get_the_ID(), '_crowdfundingtodatepicker', true) . " 23:59:59"; //Your date
                        $date = strtotime($datestr); //Converted to a PHP date (a second count)
//Calculate difference
                        if (get_post_status(get_the_ID()) == 'publish') {
                            if ($date >= time()) {
                                $diff = $date - time(); //time returns current time in seconds
                                $days = floor($diff / (60 * 60 * 24)); //seconds/minute*minutes/hour*hours/day)
                                //$hours = round(($diff - $days * 60 * 60 * 24) / (60 * 60));
                                //Report
                                if ($days > 1) {
                                    //_e($days . "days to go", "galaxyfunder");
                                    echo $days . __('days to go', 'galaxyfunder');
                                } else {
                                    // _e($days . "day to go", "galaxyfunder");
                                    echo $days . __('day to go', 'galaxyfunder');
                                }
                            } else {
                                //_e("Campaign Closed", "galaxyfunder");
                                echo __('Campaign Closed', 'galaxyfunder');
                            }
                        } else {
                            $chstatus = get_post_status(get_the_ID());
                            if ($chstatus == 'draft') {
                                echo __('Pending Review', 'galaxyfunder');
                            }
                        }

                        echo '</td>';
                        echo '</tr>';
                    }
                }
                echo '</tbody>
<tfoot>
<tr style="clear:both;">
<td colspan="7">
<div class="pagination pagination-centered"></div>
</td>
</tr>
</tfoot>
</table>';
            } else {
                // no posts found
            }
            /* Restore original Post Data */
            wp_reset_postdata();
        }
    }

    public static function crowdfunding_enqueue_scripts() {
        wp_register_script('jqueryfootable', plugins_url('gf/js/footable.js'));
        wp_register_script('jqueryfootablesort', plugins_url('gf/js/footable.sort.js'));
        wp_register_script('jqueryfootablepaging', plugins_url('gf/js/footable.paginate.js'));
        wp_register_script('jqueryfootablefilter', plugins_url('gf/js/footable.filter.js'));
        wp_register_style('jqueryfootablecss', plugins_url('gf/css/footable.core.css'));
        wp_register_style('bootstrapnavigation', plugins_url('gf/css/bootstrap.css'));
        wp_register_style('progressstyle2css', plugins_url('gf/css/mystyle.css'));
        wp_enqueue_script('jquery');
        wp_enqueue_script('jqueryfootable');
        wp_enqueue_script('jqueryfootablepaging');
        wp_enqueue_script('jqueryfootablefilter');
        wp_enqueue_style('jqueryfootablecss');
        wp_enqueue_style('bootstrapnavigation');
        wp_enqueue_style('progressstyle2css');
        wp_enqueue_script('jqueryfootablesort');
    }

    public static function add_script_to_head() {
        if (!is_product()) {
            ?>
            <script type="text/javascript">
                jQuery(document).ready(function() {
                    jQuery('.example').footable().bind('footable_filtering', function(e) {
                        var selected = jQuery('.filter-status').find(':selected').text();
                        if (selected && selected.length > 0) {
                            e.filter += (e.filter && e.filter.length > 0) ? ' ' + selected : selected;
                            e.clear = !e.filter;
                        }
                    });
                    jQuery('#change-page-size').change(function(e) {
                        e.preventDefault();
                        var pageSize = jQuery(this).val();
                        jQuery('.footable').data('page-size', pageSize);
                        jQuery('.footable').trigger('footable_initialized');
                    });

                });
            </script>
            <?php

        }
    }

}

if (get_option('cf_mycampaign_table_position') === '2') {
    add_action('woocommerce_after_my_account', array('FPCrowdFundingMycampaign', 'cf_my_account_campaign'));
} else if (get_option('cf_mycampaign_table_position') === '1') {
    add_action('woocommerce_before_my_account', array('FPCrowdFundingMycampaign', 'cf_my_account_campaign'));
}


add_action('wp_head', array('FPCrowdFundingMycampaign', 'add_script_to_head'));

/**
 * Adding the setting tab my account
 */
add_filter('woocommerce_cf_settings_tabs_array', array('FPCrowdFundingMycampaign', 'cf_my_account_tab'), 103);
add_action('woocommerce_cf_settings_tabs_my_account', array('FPCrowdFundingMycampaign', 'cf_admin_my_campaign_settings'));
add_action('woocommerce_update_options_my_account', array('FPCrowdFundingMycampaign', 'cf_admin_my_campaign_update_settings'));
add_action('init', array('FPCrowdFundingMycampaign', 'cf_default_my_account_page'));

add_action('wp_enqueue_scripts', array('FPCrowdFundingMyCampaign', 'crowdfunding_enqueue_scripts'));
add_action('admin_enqueue_scripts', array('FPCrowdFundingMyCampaign', 'crowdfunding_enqueue_scripts'));
?>