<?php
use Codexpert\WC_Affiliate\Helper;

// $_is_active = $plugin['license']->_is_active();

$from = isset( $_GET['from'] ) && '' !== $_GET['from'] 
    ? sanitize_text_field( $_GET['from'] ) 
    : date( 'F d, Y', current_time( 'timestamp' ) - Helper::date_range_diff() );

$to = isset( $_GET['to'] ) && '' !== $_GET['to'] 
    ? sanitize_text_field( $_GET['to'] ) 
    : date( 'F d, Y', current_time( 'timestamp' ) );

$affiliates_count = Helper::item_count( 'affiliates' );
$visits_count     = Helper::item_count( 'visits' );
$referrals_count  = Helper::item_count( 'referrals' );
$referral_amounts = Helper::get_transactions_amount();
$currency         = get_wc_currency_symbol();

$amounts = array(
    'paid'   => 0,
    'unpaid' => 0,
);

foreach ( $referral_amounts as $key => $referral ) {
    if ( ! isset( $amounts[ $referral->status ] ) ) {
        $amounts[ $referral->status ] = 0;
    }

    $amounts[ $referral->status ] += $referral->amount;
}

global $wpdb;
$paid_commission   = $amounts['paid'];
$unpaid_commission = Helper::get_referrals_amount( 'approved' );
$total_commission  = $paid_commission + $unpaid_commission;

$reports = apply_filters(
    'wc-affiliate-reports',
    array(
        'visits'                    => __( 'Visits', 'wc-affiliate' ),
        'referrals'                 => __( 'Referrals', 'wc-affiliate' ),
        'earnings'                  => __( 'Earnings', 'wc-affiliate' ),
        'visits-referrals-earnings' => __( 'Visits vs. Referrals vs. Earnings', 'wc-affiliate' ),
        'conversions'               => __( 'Conversions', 'wc-affiliate' ),
        'products'                  => __( 'Top Seller Products', 'wc-affiliate' ),
        'affiliates'                => __( 'Top Affiliates', 'wc-affiliate' ),
        'landingpages'              => __( 'Top Landing Pages', 'wc-affiliate' ),
        'referralurls'              => __( 'Top Referrer URLs', 'wc-affiliate' ),
    )
);

if ( isset( $_GET['affiliate'] ) && '' !== sanitize_text_field( $_GET['affiliate'] ) ) {
    unset( $reports['affiliates'] );
}
?>
<div class="wrap wca-wrap">
    <?php
    $admin_url = admin_url( 'admin.php' );
    if ( isset( $_GET['affiliate'] ) && ( $_user = get_userdata( (int) sanitize_text_field( $_GET['affiliate'] ) ) ) !== false ) :
        ?>
        <h2>
            <?php
            printf(
                esc_html__( 'Affiliate #%1$d: %2$s', 'wc-affiliate' ),
                absint( $_user->ID ),
                esc_html( $_user->display_name )
            );
            ?>
            <a href="<?php echo esc_url( add_query_arg( 'page', 'affiliates', $admin_url ) ); ?>" class="button wf-al-reports">
                <?php esc_html_e( 'All Affiliates', 'wc-affiliate' ); ?>
            </a>
        </h2>
    <?php endif; ?>

    <div class="wf-summary-header">
        <p class="wf-summary-title"><?php esc_html_e( 'Summary', 'wc-affiliate' ); ?></p>
        <div>
            <?php 
			$active_plugins = get_option( 'active_plugins' );
			if ( ! in_array( 'wc-affiliate-pro/wc-affiliate-pro.php', $active_plugins ) ) {
			?>
            <a target="_blank" href="<?php echo esc_url( 'https://wcaffiliate.com/pricing/?utm_source=inplugin&utm_medium=button&utm_campaign=year-end' ); ?>" class="wf-summary-button pro-btn">
                <?php esc_html_e( 'Enjoy Pro Features', 'wc-affiliate' ); ?>
            </a>
            <?php } ?>
            <a target="_blank" href="https://wcaffiliate.com/docs/" class="wf-summary-button">
                <?php esc_html_e( 'Documentation', 'wc-affiliate' ); ?>
            </a>
            <a target="_blank" href="https://help.pluggable.io/new-ticket/" class="wf-summary-button">
                <?php esc_html_e( 'Get Help', 'wc-affiliate' ); ?>
            </a>
        </div>
    </div>

    <div class="wf-transaction-summary-panel">
        <div class="cx-row">
            <div class="cx-col-sm-12">
                <div class="wf-dashboards" id="wf-referral-earning">
                    <div class="cx-row">
                        <?php
                        // Dashboard data.
                        $dashboard_items = array(
                            array(
                                'class' => 'active-affiliates',
                                'icon'  => 'active-affiliates.png',
                                'value' => (int) $affiliates_count,
                                'info'  => __( 'Active Affiliates', 'wc-affiliate' ),
                            ),
                            array(
                                'class' => 'total-visits',
                                'icon'  => 'total-visits.png',
                                'value' => (int) $visits_count,
                                'info'  => __( 'Total Visits', 'wc-affiliate' ),
                            ),
                            array(
                                'class' => 'total-referrals',
                                'icon'  => 'total-referrals.png',
                                'value' => (int) $referrals_count,
                                'info'  => __( 'Total Referrals', 'wc-affiliate' ),
                            ),
                            array(
                                'class' => 'total-commissions',
                                'icon'  => 'total-commissions.png',
                                'value' => sprintf( '%s%.2f', esc_html( $currency ), esc_html( $total_commission ) ),
                                'info'  => __( 'Total Commissions', 'wc-affiliate' ),
                            ),
                            array(
                                'class' => 'paid-commissions',
                                'icon'  => 'paid-commissions.png',
                                'value' => sprintf( '%s%.2f', esc_html( $currency ), esc_html( $paid_commission ) ),
                                'info'  => __( 'Paid Commissions', 'wc-affiliate' ),
                            ),
                            array(
                                'class' => 'unpaid-commissions',
                                'icon'  => 'unpaid-commissions.png',
                                'value' => sprintf( '%s%.2f', esc_html( $currency ), esc_html( $unpaid_commission ) ),
                                'info'  => __( 'Unpaid Commissions', 'wc-affiliate' ),
                            ),
                        );

                        foreach ( $dashboard_items as $item ) {
                            ?>
                            <div class="cx-col-sm-6 cx-col-md-2">
                                <div class="wf-dashboard-content">
                                    <div class="wf-dashboard-content-col <?php echo esc_attr( $item['class'] ); ?>">
                                        <div class="wf-icon">
                                            <img src="<?php echo esc_url( plugins_url( 'assets/img/' . $item['icon'], WCAFFILIATE ) ); ?>" alt="">
                                        </div>
                                        <div class="wf-value"><?php echo wp_kses_post( $item['value'] ); ?></div>
                                        <div class="wf-info"><?php echo esc_html( $item['info'] ); ?></div>
                                    </div>
                                </div>
                            </div>
                            <?php
                        }
                        ?>
                    </div>
                </div>
            </div>
        </div>
    </div>

	<div id="wf-filter-wrap">
		<form method="get">
			<input type="hidden" name="page" value="wc-affiliate" />
			<select name='affiliate' class="input-hero">
				<option value=""><?php _e( 'All Affiliates', 'wc-affiliate' ) ?></option>
				<?php 
				foreach ( Helper::get_affiliates_raw( [ 'active', 'rejected' ] ) as $affiliate ) {
					$user = get_userdata( $affiliate->user_id );
					echo "<option value='{$user->ID}' " . selected( isset( $_GET['affiliate'] ) ? (int)sanitize_text_field( $_GET['affiliate'] ) : '', $user->ID ) . ">{$user->display_name}</option>";
				}
				?>
			</select>
			<input type="text" name="from" value="<?php echo esc_html( $from ); ?>" class="input-hero datepicker" />
			<input type="text" name="to" value="<?php echo esc_html( $to ); ?>" class="input-hero datepicker" />
			<input type="submit" value="<?php _e( 'Filter', 'wc-affiliate' ) ?>" class="button button-primary button-hero wf-button" />
		</form>
	</div>

	<div class="wf-reports meta-box-sortables ui-sortable">
		<?php
		// $pro_overlay = $_is_active != false ? '' : "<a href='" . admin_url( 'admin.php?page=wc-affiliate-settings#wc_affiliate_license' ) . "'><div class='wf-pro-overlay'></div></a>";
		foreach ( $reports as $key => $label ) {
			echo "
			<div id='wf-{$key}-wrap' class='postbox wf-report'>
				<div class='hndle ui-sortable-handle'><span>{$label}</span></div>
				<div id='wf-{$key}' class='inside'></div>
				"; 
			do_action( 'wc-affiliate-after_chart', $key, $label );
			echo "</div>";
		}
		?>
	</div>
</div>
