<?php
/**
 *
 * Settings Page - General Settings
 *
 * @package Alpha Insights
 * @version 5.0.0
 * @since 1.0.0
 * @author WPDavies
 * @link https://wpdavies.dev/
 *
 */
defined( 'ABSPATH' ) || exit;

$cost_defaults 								= get_option( 'wpd_ai_cost_defaults' );
$order_status 								= get_option( 'wpd_ai_order_status' );
$admin_style_override 						= get_option( 'wpd_ai_admin_style_override', 0 );
$prevent_notices 							= get_option( 'wpd_ai_prevent_wp_notices', 0 );
$admin_custom_column_settings 				= wpdai_get_admin_custom_column_settings();
$wpd_ai_use_legacy_order_admin_metaboxes	= get_option( 'wpd_ai_use_legacy_order_admin_metaboxes', 0 );
$custom_order_cost_options 					= wpdai_get_custom_order_cost_options();
$custom_product_cost_options 				= wpdai_get_custom_product_cost_options();
$analytics_settings							= wpdai_get_analytics_settings();
$allowed_roles 								= wpdai_get_authorized_user_roles_settings();
$refunded_order_costs 						= get_option( 'wpd_ai_refunded_order_costs' );
$payment_gateway_cost_settings				= wpdai_get_payment_gateway_cost_settings();
$available_payment_gateways					= wpdai_get_available_payment_gateways();
$shipping_cost_settings						= wpdai_get_shipping_cost_settings();
$available_shipping_methods					= wpdai_get_available_shipping_methods();

?>
<div class="wpd-wrapper">
	<div class="wpd-section-heading wpd-inline">
		<?php esc_html_e( 'General Settings', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?>
		<?php submit_button( __('Save Changes', 'alpha-insights-sales-report-builder-analytics-for-woocommerce'), 'primary pull-right', 'submit', false); ?>
	</div>
</div>
<div class="wpd-wrapper">
	<table class="wpd-table fixed widefat">
		<thead>
			<tr>
				<th colspan="2"><?php esc_html_e( 'Initial Configuration - Default Costs', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td>
					<label><?php esc_html_e( 'Default Product Cost Price', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?> (%)</label>
					<div class="wpd-meta"><?php esc_html_e( 'This will be a fallback setting for products in which you haven\'t entered a cost price. This is calculated as a percentage of the given product\'s retail price. Use the configure COGS Per Product to manage costs per product (recommended).', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></div>
					<div class="wpd-meta">Our cost price hierarchy works as follows:</div>
					<div class="wpd-meta">1. Value saved for a product in the Alpha Insights Cost of Goods Manager (recommended)</div>
					<div class="wpd-meta">2. Fall back to WooCommerce Native COGS if set -> WooCommerce 10.0+</div>
					<div class="wpd-meta">3. Fall back to Parent Variable Product meta if a variation and value found in parent product</div>
					<div class="wpd-meta">4. Fall back to Default Cost Price (This Setting) e.g. 30% of RRP (if no other value is found)</div>
				</td>
				<td>
					<table class="wpd-table fixed" width="100%">
						<thead>
							<tr>
								<td>Percent Of RRP</td>
								<td colspan="2">Actions</td>
							</tr>
						</thead>
						<tbody>
							<tr>
								<td><input class="wpd-input" type="number" name="wpd_ai_cost_defaults[default_product_cost_percent]" value="<?php echo esc_attr( $cost_defaults['default_product_cost_percent'] ); ?>" step="0.01" placeholder="Percent of RRP"></td>
								<td colspan="2"><a href="<?php echo esc_url( wpdai_admin_page_url('cost-of-goods-manager') ); ?>" target="_blank" class="button btn wpd-input"><?php esc_html_e( 'Configure COGS Per Product', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ) ?></a></td>
							</tr>
						</tbody>
					</table>
				</td>
			</tr>
			<tr>
				<td>
					<label for="wpd_ai_payment_gateway_costs"><?php esc_html_e( 'Payment Gateway Costs', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></label>
					<div class="wpd-meta"><?php esc_html_e( 'This will define the costs associated with each of your payment gateways. Your orders will start with this cost, but you can override it on the order admin page. If we detect that your payment plugin has returned the actual fee we will use that instead.', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></div>
				</td>
				<td>
					<table class="wpd-table fixed" width="100%">
						<thead>
							<tr>
								<td>Payment Gateway</td>
								<td>Percent Of Order Value</td>
								<td>Static Fee</td>
							</tr>
						</thead>
						<tbody>
							<?php if ( is_array($available_payment_gateways) && ! empty($available_payment_gateways) ) : ?>
								<?php foreach( $available_payment_gateways as $payment_gateway_id => $payment_gateway_data ) : ?>
									<tr>
										<td><?php echo esc_html( $payment_gateway_data['title'] ); ?></td>
										<td>
											<input class="wpd-input" type="number" name="wpd_ai_payment_gateway_costs[<?php echo esc_attr( $payment_gateway_id ); ?>][percent_of_sales]" value="<?php echo esc_attr( (float) ( $payment_gateway_cost_settings[$payment_gateway_id]['percent_of_sales'] ?? 0 ) ); ?>" step="0.01" placeholder="Percent Of Order Value">
										</td>
										<td>
											<input class="wpd-input" type="number" name="wpd_ai_payment_gateway_costs[<?php echo esc_attr( $payment_gateway_id ); ?>][static_fee]" value="<?php echo esc_attr( (float) ( $payment_gateway_cost_settings[$payment_gateway_id]['static_fee'] ?? 0 ) ); ?>" step="0.01" placeholder="Static Fee">
										</td>
									</tr>
								<?php endforeach; ?>
							<?php endif; ?>
						</tbody>
					</table>
				</td>
			</tr>
			<tr>
				<td>
					<label for="wpd_ai_general_settings"><?php esc_html_e( 'Default Shipping Cost', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></label>
					<div class="wpd-meta"><?php esc_html_e( 'This will be a fallback setting for the shipping fees you pay to your carrier. Your orders will start with this cost, but you can override it as the fee is finalised.', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></div>
				</td>
				<td>
					<table class="wpd-table fixed" width="100%">
						<thead>
							<tr>
								<td>Shipping Method</td>
								<td>Percent Of Order Value</td>
								<td>Percent Of Shipping Charged</td>
								<td>Static Fee</td>
							</tr>
						</thead>
						<tbody>
							<?php if ( is_array($available_shipping_methods) && ! empty($available_shipping_methods) ) : ?>
								<?php foreach( $available_shipping_methods as $shipping_method_instance_id => $shipping_method_data ) : ?>
									<tr>
										<td><?php echo esc_html( $shipping_method_data['title'] ); ?><div class="wpd-meta">Instance ID: <?php echo esc_attr( $shipping_method_data['instance_id'] ); ?></div></td>
										<td><input class="wpd-input" type="number" name="wpd_ai_shipping_costs[<?php echo esc_attr( $shipping_method_instance_id ); ?>][percent_of_order_value]" value="<?php echo esc_attr( $shipping_cost_settings[$shipping_method_instance_id]['percent_of_order_value'] ); ?>" step="0.01" max="100" placeholder="Percent Of Order Value"></td>
										<td><input class="wpd-input" type="number" name="wpd_ai_shipping_costs[<?php echo esc_attr( $shipping_method_instance_id ); ?>][percent_of_shipping_charged]" value="<?php echo esc_attr( $shipping_cost_settings[$shipping_method_instance_id]['percent_of_shipping_charged'] ); ?>" step="0.01" max="100" placeholder="Percent Of Shipping Charged"></td>
										<td><input class="wpd-input" type="number" name="wpd_ai_shipping_costs[<?php echo esc_attr( $shipping_method_instance_id ); ?>][static_fee]" value="<?php echo esc_attr( $shipping_cost_settings[$shipping_method_instance_id]['static_fee'] ); ?>" step="0.01" placeholder="Static Fee"></td>
									</tr>
								<?php endforeach; ?>
							<?php endif; ?>
						</tbody>
					</table>

				</td>
			</tr>
		</tbody>
	</table>
</div>
<div class="wpd-wrapper">
	<table class="wpd-table fixed widefat">
		<thead>
			<tr>
				<th colspan="2"><?php esc_html_e( 'Report Settings', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td>
					<label><?php esc_html_e( 'Paid Order Status For Reporting', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></label>
					<div class="wpd-meta"><?php echo wp_kses_post( __( 'These are the order statuses that we will look at when reviewing your profitability.<br>These statuses are the ones that are considered paid for and will be used in your report calculations.<br>Refund status is required.', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ) ); ?></div>
				</td>
				<td>
					<select class="wpd-input wpd-combo-select" name="wpd_ai_order_status[]" value="" multiple="multiple">
						<?php 
							$chosen_status 	= wpdai_paid_order_statuses();
							$order_status 	= wc_get_order_statuses();
							foreach( $order_status as $key => $value ) {
								$selected = '';
								if ( in_array( $key, $chosen_status ) ) {
									$selected = 'selected="selected"';
								}
								echo '<option value="' . esc_attr( $key ) . '" ' . esc_attr( $selected ) . '>' . esc_html( $value ) . '</option>';
							}

						?>
					</select>
				</td>
			</tr>
			<tr>
				<td>
					<label><?php esc_html_e( 'Cache Build Batch Size', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></label>
					<div class="wpd-meta"><?php echo wp_kses_post( __( 'This will define the batch size for the cache build process. This is the number of orders that will be processed at a time.<br>Lowering this value if you are having errors in the cache building process, but this may increase processing time.', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ) ); ?></div>
				</td>
				<td>
					<input class="wpd-input" type="number" name="wpd_ai_cache_build_batch_size" value="<?php echo esc_attr( get_option( 'wpd_ai_cache_build_batch_size', 250 ) ); ?>" step="1" placeholder="Batch Size" min="1" max="10000">
				</td>
			</tr>
			<tr>
				<td>
					<label for="wpd_ai_report_logo_attachment_id"><?php esc_html_e( 'Whitelabel Report Logo', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></label>
					<div class="wpd-meta"><?php echo wp_kses_post( __( 'This logo appears on public-facing report outputs only: exported PDFs and Live Share links. It will not appear in the WordPress admin.', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ) ); ?></div>
				</td>
				<td>
					<?php
					$report_logo_attachment_id = get_option( 'wpd_ai_report_logo_attachment_id', '' );
					$report_logo_url           = '';
					if ( ! empty( $report_logo_attachment_id ) ) {
						$report_logo_data = wp_get_attachment_image_src( (int) $report_logo_attachment_id, 'thumbnail' );
						$report_logo_url  = is_array( $report_logo_data ) ? $report_logo_data[0] : '';
					}
					?>
					<input type="hidden" name="wpd_ai_report_logo_attachment_id" id="wpd_ai_report_logo_attachment_id" value="<?php echo esc_attr( $report_logo_attachment_id ); ?>">
					<div class="wpd-report-logo-preview-wrap" style="margin-bottom: 10px; min-height: 60px;">
						<?php if ( $report_logo_url ) : ?>
							<img class="wpd-report-logo-preview-img" src="<?php echo esc_url( $report_logo_url ); ?>" alt="" style="max-width: 200px; max-height: 60px; display: block;">
						<?php else : ?>
							<span class="wpd-report-logo-placeholder" style="display: block; color: #646970;"><?php esc_html_e( 'No logo selected', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></span>
						<?php endif; ?>
					</div>
					<button type="button" class="button wpd-report-logo-upload-btn"><?php echo $report_logo_attachment_id ? esc_html__( 'Change Logo', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ) : esc_html__( 'Select Logo', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></button>
					<button type="button" class="button wpd-report-logo-remove" style="margin-left: 5px;<?php echo ! $report_logo_attachment_id ? ' display: none;' : ''; ?>"><?php esc_html_e( 'Remove Logo', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></button>
				</td>
			</tr>
		</tbody>
	</table>
</div>
<div class="wpd-wrapper">
	<table class="wpd-table fixed widefat">
		<thead>
			<tr>
				<th colspan="2"><?php esc_html_e( 'Profit Calculation Settings', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ) ?></th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td>
					<label><?php esc_html_e( 'Costs to include when an order is fully refunded', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></label>
					<div class="wpd-meta"><?php echo wp_kses_post( __( 'By default, all costs are set to 0 when an order is fully refunded.<br>You can use these settings to adjust which costs are included in your profit calculation when an order is fully refunded.<br>*Partially refunded orders have exemption calculations, per item refunded..', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ) ); ?></div>
				</td>
				<td>
					<!-- Ensure a value is always set -->
					<input type="hidden" name="wpd-refunded-order-costs[__none]" value="0">
					<?php wpdai_checkbox( 'wpd-refunded-order-costs[total_product_cost_of_goods]', $refunded_order_costs['total_product_cost_of_goods'], __( 'Product Cost Of Goods', 'alpha-insights-sales-report-builder-analytics-for-woocommerce') ); ?>
					<?php wpdai_checkbox( 'wpd-refunded-order-costs[total_product_custom_costs]', $refunded_order_costs['total_product_custom_costs'], __( 'Product Custom Costs', 'alpha-insights-sales-report-builder-analytics-for-woocommerce') ); ?>
					<?php wpdai_checkbox( 'wpd-refunded-order-costs[total_shipping_cost]', $refunded_order_costs['total_shipping_cost'], __( 'Shipping Costs', 'alpha-insights-sales-report-builder-analytics-for-woocommerce') ); ?>
					<?php wpdai_checkbox( 'wpd-refunded-order-costs[payment_gateway_cost]', $refunded_order_costs['payment_gateway_cost'], __( 'Payment Gateway Fees', 'alpha-insights-sales-report-builder-analytics-for-woocommerce') ); ?>
					<?php wpdai_checkbox( 'wpd-refunded-order-costs[total_custom_order_costs]', $refunded_order_costs['total_custom_order_costs'], __( 'Custom Order Costs', 'alpha-insights-sales-report-builder-analytics-for-woocommerce') ); ?>
				</td>
			</tr>
		</tbody>
	</table>
</div>
<!-- Custom Order Costs -->
<div class="wpd-wrapper">
	<table class="wpd-table fixed widefat">
		<thead>
			<tr>
				<th colspan="2"><?php esc_html_e( 'Custom Order & Product Costs', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td>
					<label for="wpd_ai_custom_order_cost"><?php esc_html_e( 'Create Custom Order Costs', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></label>
					<div class="wpd-meta">
						<?php
						$custom_order_cost_doc_url = wpdai_wpdavies_url( '/documentation/alpha-insights/additional-features/custom-order-costs/setting-up-custom-order-costs/', 'Alpha Insights Settings - Custom Order Costs Documentation');
						echo wp_kses_post(
							sprintf(
								__( 'You can use this setting to create additional order costs with default values for each order.<br>Every new cost field you add here will show up on the order edit page in the admin area.<br>You can override the default cost value for each order. <a href="%1$s" target="_blank">Click Here</a> for documentation.', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ),
								esc_url( $custom_order_cost_doc_url )
							)
						);
						?></div>
				</td>
				<td>
					<table class="wpd-table fixed" width="100%">
						<thead>
							<tr>
								<td colspan="2">Name</td>
								<td colspan="2">% Of Order Value</td>
								<td colspan="2">Static Fee</td>
								<td>Action</td>
							</tr>
						</thead>
						<tbody>
							<?php if ( is_array($custom_order_cost_options) && ! empty($custom_order_cost_options) ) : ?>
								<?php foreach( $custom_order_cost_options as $slug => $data ) : ?>
									<tr class="wpd-custom-order-cost-row">
										<td colspan="2">
											<input class="wpd-input" type="text" name="wpd_ai_custom_order_cost[<?php echo esc_attr( $slug ); ?>][label]" placeholder="E.g. Commission" value="<?php echo esc_attr( $data['label'] ); ?>">
											<label for="wpd_ai_custom_order_cost[<?php echo esc_attr( $slug ); ?>][label]" class="wpd-meta wpd-block-label">Unique Key: <?php echo esc_html( $slug ); ?></label>
										</td>
										<td colspan="2">
											<input class="wpd-input" type="number" name="wpd_ai_custom_order_cost[<?php echo esc_attr( $slug ); ?>][percent_of_order_value]" placeholder="0" value="<?php echo esc_attr( $data['percent_of_order_value'] ); ?>" step="0.01" >
											<label for="wpd_ai_custom_order_cost[<?php echo esc_attr( $slug ); ?>][percent_of_order_value]" class="wpd-meta wpd-block-label">Percent Of Order Value</label>
										</td>
										<td colspan="2">
											<input class="wpd-input" type="number" name="wpd_ai_custom_order_cost[<?php echo esc_attr( $slug ); ?>][static_fee]" placeholder="0" value="<?php echo esc_attr( $data['static_fee'] ); ?>" step="0.01" >
											<label for="wpd_ai_custom_order_cost[<?php echo esc_attr( $slug ); ?>][static_fee]" class="wpd-meta wpd-block-label">+ Static Fee</label>
										</td>
										<td><div class="wpd-delete wpd-delete-custom-order-cost" style="color: red; cursor: pointer; text-decoration: underline; margin-bottom: 25px;">Delete</div></td>
										<input type="hidden" name="wpd_ai_custom_order_cost[<?php echo esc_attr( $slug ); ?>][slug]" value="<?php echo esc_attr( $slug ); ?>">
									</tr>
								<?php endforeach; ?>
							<?php endif; ?>
							<tr class="wpd-custom-order-cost-row-new">
								<td colspan="2">
									<input class="wpd-input" type="text" name="wpd_ai_custom_order_cost[new][label]" placeholder="E.g. Commission" value="">
									<label for="wpd_ai_custom_order_cost[new][label]" class="wpd-meta wpd-block-label">Unique Key: N/A</label>
								</td>
								<td colspan="2">
									<input class="wpd-input" type="number" name="wpd_ai_custom_order_cost[new][percent_of_order_value]" placeholder="0" value="" step="0.01" >
									<label for="wpd_ai_custom_order_cost[new][percent_of_order_value]" class="wpd-meta wpd-block-label">Percent Of Order Value</label>
								</td>
								<td colspan="2">
									<input class="wpd-input" type="number" name="wpd_ai_custom_order_cost[new][static_fee]" placeholder="0" value="" step="0.01" >
									<label for="wpd_ai_custom_order_cost[new][static_fee]" class="wpd-meta wpd-block-label">+ Static Fee</label>
								</td>
								<td></td>
							</tr>
						</tbody>
					</table>
				</td>
			</tr>
			<tr>
				<td>
					<label for="wpd_ai_custom_product_cost"><?php esc_html_e( 'Create Custom Product Costs', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></label>
					<div class="wpd-meta">
						<?php
						$custom_product_cost_doc_url = wpdai_wpdavies_url( '/documentation/alpha-insights/additional-features/custom-product-costs/setting-up-custom-product-costs/', 'Alpha Insights Settings - Custom Product Costs Documentation');
						echo wp_kses_post(
							sprintf(
								__( 'You can use this setting to create additional product costs with default values for each product.<br>Every new cost field you add here will show up on the product edit page and in the order admin area.<br>You can override the default cost value for each product & each order. <a href="%1$s" target="_blank">Click Here</a> for documentation.', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ),
								esc_url( $custom_product_cost_doc_url )
							)
						);
						?></div>
				</td>
				<td>
					<table class="wpd-table fixed" width="100%">
						<thead>
							<tr>
								<td colspan="2">Name</td>
								<td colspan="2">% Of Sell Price</td>
								<td colspan="2">Static Fee</td>
								<td>Action</td>
							</tr>
						</thead>
						<tbody>
							<?php if ( is_array($custom_product_cost_options) && ! empty($custom_product_cost_options) ) : ?>
								<?php foreach( $custom_product_cost_options as $slug => $data ) : ?>
									<tr class="wpd-custom-order-cost-row">
										<td colspan="2">
											<input class="wpd-input" type="text" name="wpd_ai_custom_product_cost[<?php echo esc_attr( $slug ); ?>][label]" placeholder="E.g. Packing Fees" value="<?php echo esc_attr( $data['label'] ); ?>">
											<label for="wpd_ai_custom_product_cost[<?php echo esc_attr( $slug ); ?>][label]" class="wpd-meta wpd-block-label">Unique Key: <?php echo esc_html( $slug ); ?></label>
										</td>
										<td colspan="2">
											<input class="wpd-input" type="number" name="wpd_ai_custom_product_cost[<?php echo esc_attr( $slug ); ?>][percent_of_sell_price]" placeholder="0" value="<?php echo esc_attr( $data['percent_of_sell_price'] ); ?>" step="0.01" >
											<label for="wpd_ai_custom_product_cost[<?php echo esc_attr( $slug ); ?>][percent_of_sell_price]" class="wpd-meta wpd-block-label">Percent Of Sell Price</label>
										</td>
										<td colspan="2">
											<input class="wpd-input" type="number" name="wpd_ai_custom_product_cost[<?php echo esc_attr( $slug ); ?>][static_fee]" placeholder="0" value="<?php echo esc_attr( $data['static_fee'] ); ?>" step="0.01" >
											<label for="wpd_ai_custom_product_cost[<?php echo esc_attr( $slug ); ?>][static_fee]" class="wpd-meta wpd-block-label">+ Static Fee</label>
										</td>
										<td><div class="wpd-delete wpd-delete-custom-order-cost" style="color: red; cursor: pointer; text-decoration: underline; margin-bottom: 25px;">Delete</div></td>
										<input type="hidden" name="wpd_ai_custom_product_cost[<?php echo esc_attr( $slug ); ?>][slug]" value="<?php echo esc_attr( $slug ); ?>">
									</tr>
								<?php endforeach; ?>
							<?php endif; ?>
							<tr class="wpd-custom-order-cost-row-new">
								<td colspan="2">
									<input class="wpd-input" type="text" name="wpd_ai_custom_product_cost[new][label]" placeholder="E.g. Packing Fees" value="">
									<label for="wpd_ai_custom_product_cost[new][label]" class="wpd-meta wpd-block-label">Unique Key: N/A</label>
								</td>
								<td colspan="2">
									<input class="wpd-input" type="number" name="wpd_ai_custom_product_cost[new][percent_of_sell_price]" placeholder="0" value="" step="0.01" >
									<label for="wpd_ai_custom_product_cost[new][percent_of_sell_price]" class="wpd-meta wpd-block-label">Percent Of Sell Price</label>
								</td>
								<td colspan="2">
									<input class="wpd-input" type="number" name="wpd_ai_custom_product_cost[new][static_fee]" placeholder="0" value="" step="0.01" >
									<label for="wpd_ai_custom_product_cost[new][static_fee]" class="wpd-meta wpd-block-label">+ Static Fee</label>
								</td>
								<td></td>
							</tr>
						</tbody>
					</table>
				</td>
			</tr>
		</tbody>
	</table>
</div>
<div class="wpd-wrapper">
	<table class="wpd-table fixed widefat">
		<thead>
			<tr>
				<th colspan="2"><?php esc_html_e( 'Alpha Analytics & Event Tracking', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?><div class="wpd-meta">Full WooCommerce Analytics suite for event tracking & session data</div></th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td>
					<label><?php esc_html_e( 'Enable Woocommerce Event Tracking', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></label>
					<div class="wpd-meta"><?php esc_html_e( 'This will enable product analytics which will add additional tracking to monitor things like product clicks, add to carts and purchases - utilising this will add a small additional load to your server.', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></div>
				</td>
				<td>
					<select class="wpd-input" name="wpd_ai_analytics[enable_woocommerce_analytics]">
						<option value="1" <?php echo esc_attr( wpdai_selected_option( '1', $analytics_settings['enable_woocommerce_analytics'] ) ); ?> ><?php esc_html_e( 'True', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></option>
						<option value="0" <?php echo esc_attr( wpdai_selected_option( '0', $analytics_settings['enable_woocommerce_analytics'] ) ); ?> ><?php esc_html_e( 'False', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></option>
					</select>
				</td>
			</tr>
			<tr>
				<td>
					<label><?php esc_html_e( 'Exclude These Roles From Tracking', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></label>
					<div class="wpd-meta"><?php esc_html_e( 'This will prevent these user roles from being tracked on your website.', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></div>
				</td>
				<td>
					<select class="wpd-input wpd-combo-select" name="wpd_ai_analytics[exclude_roles][]" value="" multiple="multiple" placeholder="Select Role Type(s) To Exclude">
						<?php 
							$analytics_excluded_roles 	= ( ! empty($analytics_settings['exclude_roles']) ) ? $analytics_settings['exclude_roles'] : array();
							$analytics_all_roles 		= wpdai_get_available_store_roles();
							foreach( $analytics_all_roles as $analytics_role ) {
								$analytics_selected = '';
								$analytics_role = 'exclude_' . $analytics_role; // Prevent collisions with other settings?
								if ( in_array( $analytics_role, $analytics_excluded_roles ) ) {
									$analytics_selected = 'selected="selected"';
								}
								echo '<option value="' . esc_attr( $analytics_role ) . '" ' . esc_attr( $analytics_selected ) . '>' . esc_html( $analytics_role ) . '</option>';
							}
						?>
					</select>
				</td>
			</tr>
			<tr>
				<td>
					<label><?php esc_html_e( 'Only Track & Report On Engaged Sessions', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></label>
					<div class="wpd-meta"><?php esc_html_e( 'This feature will only track & report on engaged sessions.<br>Page views will only be tracked if there\'s mouse movement or clicks & Alpha Insights will only report sessions that are marked as engaged or greater than 0 seconds in duration.', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></div>
				</td>
				<td>
					<select class="wpd-input" name="wpd_ai_analytics[only_track_engaged_sessions]">
						<option value="1" <?php echo esc_attr( wpdai_selected_option( '1', $analytics_settings['only_track_engaged_sessions'] ) ); ?>><?php esc_html_e( 'True', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></option>
						<option value="0" <?php echo esc_attr( wpdai_selected_option( '0', $analytics_settings['only_track_engaged_sessions'] ) ); ?>><?php esc_html_e( 'False', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></option>
					</select>
				</td>
			</tr>
			<tr>
				<td>
					<label><?php esc_html_e( 'Attribution Window Timeout (days)', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></label>
					<div class="wpd-meta"><?php esc_html_e( 'This is the amount of time in days that the attribution window will be open for. The attribution window will store the landing page and referral source for the session for this many days.', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></div>
				<td>	
					<select class="wpd-input" name="wpd_ai_analytics[attribution_timeout_in_days]">
						<option value="1" <?php echo esc_attr( wpdai_selected_option( '1', $analytics_settings['attribution_timeout_in_days'] ) ); ?>><?php esc_html_e( '1 Day', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></option>
						<option value="2" <?php echo esc_attr( wpdai_selected_option( '2', $analytics_settings['attribution_timeout_in_days'] ) ); ?>><?php esc_html_e( '2 Days', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></option>
						<option value="3" <?php echo esc_attr( wpdai_selected_option( '3', $analytics_settings['attribution_timeout_in_days'] ) ); ?>><?php esc_html_e( '3 Days', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></option>
						<option value="4" <?php echo esc_attr( wpdai_selected_option( '4', $analytics_settings['attribution_timeout_in_days'] ) ); ?>><?php esc_html_e( '4 Days', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></option>
						<option value="5" <?php echo esc_attr( wpdai_selected_option( '5', $analytics_settings['attribution_timeout_in_days'] ) ); ?>><?php esc_html_e( '5 Days', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></option>
						<option value="6" <?php echo esc_attr( wpdai_selected_option( '6', $analytics_settings['attribution_timeout_in_days'] ) ); ?>><?php esc_html_e( '6 Days', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></option>
						<option value="7" <?php echo esc_attr( wpdai_selected_option( '7', $analytics_settings['attribution_timeout_in_days'] ) ); ?>><?php esc_html_e( '7 Days', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></option>
						<option value="14" <?php echo esc_attr( wpdai_selected_option( '14', $analytics_settings['attribution_timeout_in_days'] ) ); ?>><?php esc_html_e( '14 Days', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></option>
						<option value="30" <?php echo esc_attr( wpdai_selected_option( '30', $analytics_settings['attribution_timeout_in_days'] ) ); ?>><?php esc_html_e( '30 Days', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></option>
					</select>
				</td>
			</tr>
			<tr>
				<td>
					<label><?php esc_html_e( 'Cache-Safe Event Tracking (Beta)', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></label>
					<div class="wpd-meta"><?php esc_html_e( 'Uses client-side session storage and does not set PHP cookies, improving full-page cache compatibility. When disabled, the existing tracking system is used unchanged.', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></div>
				</td>
				<td>
					<select class="wpd-input" name="wpd_ai_analytics[enable_cache_safe_tracking_beta]">
						<option value="1" <?php echo esc_attr( wpdai_selected_option( '1', $analytics_settings['enable_cache_safe_tracking_beta'] ?? 0 ) ); ?>><?php esc_html_e( 'True', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></option>
						<option value="0" <?php echo esc_attr( wpdai_selected_option( '0', $analytics_settings['enable_cache_safe_tracking_beta'] ?? 0 ) ); ?>><?php esc_html_e( 'False', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></option>
					</select>
				</td>
			</tr>
			<tr>
				<td>
					<label><?php esc_html_e( 'Cache-Safe Cookie Storage Mode', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></label>
					<div class="wpd-meta"><?php esc_html_e( 'Checkout Only keeps analytics data in localStorage until cart/checkout for best cache performance. Immediate writes cookies on first visit (legacy-like behavior). Only applies when Cache-Safe Event Tracking is enabled.', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></div>
				</td>
				<td>
					<select class="wpd-input" name="wpd_ai_analytics[cookie_storage_mode]">
						<option value="checkout_only" <?php echo esc_attr( wpdai_selected_option( 'checkout_only', $analytics_settings['cookie_storage_mode'] ?? 'checkout_only' ) ); ?>><?php esc_html_e( 'Checkout Only (Recommended)', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></option>
						<option value="immediate" <?php echo esc_attr( wpdai_selected_option( 'immediate', $analytics_settings['cookie_storage_mode'] ?? 'checkout_only' ) ); ?>><?php esc_html_e( 'Immediate', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></option>
					</select>
				</td>
			</tr>
		</tbody>
	</table>
</div>
<div class="wpd-wrapper">
	<table class="wpd-table fixed widefat">
		<thead>
			<tr>
				<th colspan="2">
					<?php esc_html_e( 'WordPress Admin Display Extensions', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?>
					<div class="wpd-meta">These checkboxes will display / hide any extensions to the standard WP Admin columns.</div>
					<?php $admin_custom_column_defaults = wpdai_get_admin_custom_column_defaults(); ?>
					<!-- Hidden input allows for saving empty values across the board due to empty multi-select not passing into _POST -->
					<input type="hidden" name="wpd_ai_admin_custom_columns[]" value="" />
				</th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td>
					<label><?php esc_html_e( 'Product Admin Columns', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></label>
					<div class="wpd-meta"><?php esc_html_e( 'Select which columns you would like to display in the Product Admin List section in your WP Dashboard.', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></div>
				</td>
				<td>
					<select class="wpd-input wpd-combo-select" name="wpd_ai_admin_custom_columns[products][]" value="" multiple="multiple" placeholder="Select Columns">
						<?php 
							$chosen_columns 	= ( isset($admin_custom_column_settings['products']) ) ? $admin_custom_column_settings['products'] : array();
							$product_columns 	= $admin_custom_column_defaults['products'];
							foreach( $product_columns as $key => $value ) {
								$selected = '';
								if ( array_key_exists( $key, $chosen_columns) ) {
									$selected = 'selected="selected"';
								}
								echo '<option value="' . esc_attr( $key ) . '" ' . esc_attr( $selected ) . '>' . esc_html( $value ) . '</option>';
							}

						?>
					</select>
				</td>			
			</tr>
			<tr>
				<td>
					<label><?php esc_html_e( 'Order Admin Columns', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></label>
					<div class="wpd-meta"><?php esc_html_e( 'Select which columns you would like to display in the Order Admin List section in your WP Dashboard.', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></div>
				</td>
				<td>
					<select class="wpd-input wpd-combo-select" name="wpd_ai_admin_custom_columns[orders][]" value="" multiple="multiple" placeholder="Select Columns">
						<?php 
							$chosen_columns = (isset($admin_custom_column_settings['orders'])) ? $admin_custom_column_settings['orders'] : array();
							$order_columns 	= $admin_custom_column_defaults['orders'];
							foreach( $order_columns as $key => $value ) {
								$selected = '';
								if ( array_key_exists( $key, $chosen_columns) ) {
									$selected = 'selected="selected"';
								}
								echo '<option value="' . esc_attr( $key ) . '" ' . esc_attr( $selected ) . '>' . esc_html( $value ) . '</option>';
							}

						?>
					</select>
				</td>			
			</tr>
			<tr>
				<td>
					<label><?php esc_html_e( 'User Admin Columns', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></label>
					<div class="wpd-meta"><?php esc_html_e( 'Select which columns you would like to display in the Users List section in your WP Dashboard.', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></div>
				</td>
				<td>
					<select class="wpd-input wpd-combo-select" name="wpd_ai_admin_custom_columns[users][]" value="" multiple="multiple" placeholder="Select Columns">
						<?php 
							$chosen_columns 	= (isset($admin_custom_column_settings['users'])) ? $admin_custom_column_settings['users'] : array();
							$user_columns 		= $admin_custom_column_defaults['users'];
							foreach( $user_columns as $key => $value ) {
								$selected = '';
								if ( array_key_exists( $key, $chosen_columns) ) {
									$selected = 'selected="selected"';
								}
								echo '<option value="' . esc_attr( $key ) . '" ' . esc_attr( $selected ) . '>' . esc_html( $value ) . '</option>';
							}

						?>
					</select>
				</td>			
			</tr>
		</tbody>
	</table>
</div>
<div class="wpd-wrapper">
	<table class="wpd-table fixed widefat">
		<thead>
			<tr>
				<th colspan="2"><?php esc_html_e( 'Other Settings', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td>
					<label><?php esc_html_e( 'Plugin Access Permissions', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></label>
					<div class="wpd-meta"><?php echo wp_kses_post( __( 'Choose which user roles can view and use this plugin. Those who are denied access will not see any part of the plugin.<br>Administrators will always have access.', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ) ); ?></div>
				</td>
				<td>
					<select class="wpd-input wpd-combo-select" name="wpd_ai_plugin_visibility[]" multiple="multiple" placeholder="Select Role Type(s) To Include">
						<?php

						    $all_roles = wpdai_get_available_store_roles();

							foreach( $all_roles as $role ) {

								$selected = '';

								if ( in_array( $role, $allowed_roles ) ) {
									$selected = 'selected="selected"';
								}

								if ( $role === 'administrator' ) {
									$selected = 'selected="selected"';
								}

								echo '<option value="' . esc_attr( $role ) . '" ' . esc_attr( $selected ) . '>' . esc_html( $role ) . '</option>';
							}

						?>
					</select>
				</td>
			</tr>
			<tr>
				<td>
					<label><?php esc_html_e( 'Load Modern WP Admin Skin', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></label>
					<div class="wpd-meta"><?php esc_html_e( 'Load our custom stylesheet which will override core admin appearance settings to help modernize your admin.', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></div>
				</td>
				<td>
					<select class="wpd-input" name="wpd_ai_admin_style_override">
						<option value="0" <?php echo esc_attr( wpdai_selected_option( '0', $admin_style_override ) ); ?> ><?php esc_html_e( 'False', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></option>
						<option value="1" <?php echo esc_attr( wpdai_selected_option( '1', $admin_style_override ) ); ?> ><?php esc_html_e( 'True', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></option>
					</select>
				</td>
			</tr>
			<tr>
				<td>
					<label><?php esc_html_e( 'Prevent annoying WordPress notices', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></label>
					<div class="wpd-meta"><?php esc_html_e( 'This will prevent the annoying update notices, license notices and whatever else rubbish people like to clutter your screen with.', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></div>
				</td>
				<td>
					<select class="wpd-input" name="wpd_ai_prevent_wp_notices">
						<option value="0" <?php echo esc_attr( wpdai_selected_option( '0', $prevent_notices ) ); ?> ><?php esc_html_e( 'False', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></option>
						<option value="1" <?php echo esc_attr( wpdai_selected_option( '1', $prevent_notices ) ); ?> ><?php esc_html_e( 'True', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></option>
					</select>
				</td>
			</tr>
		</tbody>
	</table>
</div>
<div class="wpd-wrapper">
	<table class="wpd-table fixed widefat">
		<thead>
			<tr>
				<th colspan="2"><?php esc_html_e( 'Debugging, Tools & Cache', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></th>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td>
					<label><?php esc_html_e( 'Delete All Report Caches', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></label>
					<div class="wpd-meta"><?php echo wp_kses_post( __( 'We store order and product data in a cache in order to run your reports faster.<br>This function will delete all caches to force the most recent data in your reports.<br>This is non-destructive and recommended for displaying the latest data.', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ) ); ?></div>
				</td>
				<td>
					<a class="button btn wpd-input" id="wpd-delete-cache" data-wpd-ajax-action="wpd_delete_all_cache"><?php esc_html_e( 'Delete Cache', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ) ?></a>
				</td>
			</tr>
			<tr>
				<td>
					<label><?php esc_html_e( 'Delete All Order Calculation Overrides', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></label>
					<div class="wpd-meta"><?php echo wp_kses_post( __( 'In your order admin page you are able to override the default calculations by entering values manually.<br>This tool will remove all of these overrides. It will not effect your WooCommerce order data in any way.<br><span style="color: red;">This tool will permanetly delete order overrides.</span>', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ) ); ?></div>
				</td>
				<td>
					<a class="button btn wpd-input" id="wpd-reset-order-meta" data-wpd-ajax-action="wpd_reset_order_meta"><?php esc_html_e( 'Delete All Order Calculation Overrides', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ) ?></a>
				</td>
			</tr>
			<tr>
				<td>
					<label><?php esc_html_e( 'Delete All Order Line Item COGS Overrides', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></label>
					<div class="wpd-meta"><?php echo wp_kses_post( __( 'If you have overridden the Cost of Goods for a product at the line item level on an order, this is saved and used for calculations.<br>This tool will remove all of these overrides. It will not effect your WooCommerce order data in any way.<br><span style="color: red;">This tool will permanetly delete line item COGS overrides.</span>', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ) ); ?></div>
				</td>
				<td>
					<a class="button btn wpd-input" id="wpd-delete-order-line-item-cogs" data-wpd-ajax-action="wpd_delete_order_line_item_cogs"><?php esc_html_e( 'Delete All Order Line Item COGS', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ) ?></a>
				</td>
			</tr>
			<tr>
				<td>
					<label><?php esc_html_e( 'Upgrade Database', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></label>
					<div class="wpd-meta"><?php esc_html_e( 'Run this function to manually update your database to the latest version.', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></div>
					<div class="wpd-meta"><?php esc_html_e( 'Installed Version', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?>: <?php echo esc_html( get_option('wpd_ai_db_version') ); ?></div>
					<div class="wpd-meta"><?php esc_html_e( 'Required Version', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?>: <?php echo esc_html( WPD_AI_DB_VERSION ); ?></div>
				</td>
				<td>
					<a class="button btn wpd-input" id="wpd-update_db_manually" data-wpd-ajax-action="wpd-update_db_manually"><?php esc_html_e( 'Update Database', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ); ?></a>
				</td>
			</tr>
		</tbody>
	</table>
</div>
<script>
jQuery( function( $ ) {
	var $input = $( '#wpd_ai_report_logo_attachment_id' );
	var $previewWrap = $( '.wpd-report-logo-preview-wrap' );
	var $uploadBtn = $( '.wpd-report-logo-upload-btn' );
	var $removeBtn = $( '.wpd-report-logo-remove' );
	if ( ! $input.length || typeof wp === 'undefined' || ! wp.media ) return;
	$uploadBtn.on( 'click', function() {
		var frame = wp.media( {
			title: '<?php echo esc_js( __( 'Select Report Logo', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ) ); ?>',
			button: { text: '<?php echo esc_js( __( 'Use as logo', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ) ); ?>' },
			library: { type: 'image' },
			multiple: false
		} );
		frame.on( 'select', function() {
			var attachment = frame.state().get( 'selection' ).first().toJSON();
			$input.val( attachment.id );
			$previewWrap.find( '.wpd-report-logo-preview-img' ).remove();
			$previewWrap.find( '.wpd-report-logo-placeholder' ).remove();
			var url = attachment.sizes && attachment.sizes.thumbnail ? attachment.sizes.thumbnail.url : attachment.url;
			$previewWrap.prepend( '<img class="wpd-report-logo-preview-img" src="' + url + '" alt="" style="max-width: 200px; max-height: 60px; display: block;">' );
			$uploadBtn.text( '<?php echo esc_js( __( 'Change Logo', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ) ); ?>' );
			$removeBtn.show();
		} );
		frame.open();
	} );
	$removeBtn.on( 'click', function() {
		$input.val( '' );
		$previewWrap.find( '.wpd-report-logo-preview-img' ).remove();
		$previewWrap.find( '.wpd-report-logo-placeholder' ).remove();
		$previewWrap.append( '<span class="wpd-report-logo-placeholder" style="display: block; color: #646970;"><?php echo esc_js( __( 'No logo selected', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ) ); ?></span>' );
		$uploadBtn.text( '<?php echo esc_js( __( 'Select Logo', 'alpha-insights-sales-report-builder-analytics-for-woocommerce' ) ); ?>' );
		$( this ).hide();
	} );
} );
</script>
<div class="wpd-inline">
	<?php submit_button( __('Save Changes', 'alpha-insights-sales-report-builder-analytics-for-woocommerce'), 'primary pull-right', 'submit', false); ?>
</div>