<?php
/**
 * Settings: Admin Product Reports
 *
 * @package     WCVendors_Pro
 */
class WCVendors_Pro_Admin_Product_Reports {

    /**
     * Constructor
     */
    public function __construct() {
        add_action( 'update_option_wcvendors_pro_product_reports_enabled', array( $this, 'reload_on_toggle_option' ), 10, 2 );
        add_action( 'update_option_wcvendors_pro_product_reports_reasons', array( $this, 'process_save_reasons' ), 10, 2 );
    }

    /**
     * Output the report table
     */
    public function reports_table() {
        $product_reports_table = new WCVendors_Pro_Product_Reports_Table();

        wc_get_template(
            'html-product-reports-table.php',
            array( 'product_reports_table' => $product_reports_table ),
            'wc-vendors/admin/product-reports/',
            WCV_PRO_ABSPATH_ADMIN . 'views/product-reports/'
        );
    }

    /**
     * Add the report product menu item
     */
    public function add_product_reports_menu() {
        $hook = add_submenu_page(
            'wc-vendors',
            __( 'Product Reports', 'wcvendors-pro' ),
            __( 'Product Reports', 'wcvendors-pro' ),
            'manage_woocommerce',
            'wcv-reports-product',
            array( $this, 'reports_table' ),
        );
        add_action( "load-$hook", array( $this, 'add_options' ) );
    }

    /**
     * Product Reports table page screen options
     */
    public function add_options() {
        $option = 'per_page';
        $args   = array(
            'label'   => __( 'Product Reports', 'wcvendors-pro' ),
            'default' => 10,
            'option'  => 'product_reports_per_page',
        );
        add_screen_option( $option, $args );
    }

    /**
     * Set the screen options for the report product table
     *
     * @param  string     $status  Status of the screen option.
     * @param  string     $option  option name.
     * @param  string|int $value option value.
     */
    public function set_screen_option( $status, $option, $value ) {
        if ( 'product_reports_per_page' === $option ) {
            return $value;
        }

        return $status;
    }

    /**
     * Reload on toggle option
     *
     * @param  string $old_value  Old value.
     * @param  string $new_value  New value.
     */
    public function reload_on_toggle_option( $old_value, $new_value ) {
        if ( $old_value === $new_value ) {
            return;
        }

        if ( isset( $_SERVER['REQUEST_URI'] ) ) {
            wp_safe_redirect( esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
            exit();
        }
    }

    /**
     * Process save reasons
     *
     * @param array $old_value Old value.
     * @param array $new_value New value.
     */
    public function process_save_reasons( $old_value, $new_value ) {

        $reasons = array_map( 'sanitize_text_field', $new_value );

        $reasons = array_filter( $reasons, 'strlen' );

        if ( empty( $reasons ) ) {
            delete_option( 'wcvendors_pro_product_reports_reasons' );
        }

        update_option( 'wcvendors_pro_product_reports_reasons', $reasons );
    }
}
