<?php 
/**
 * Class ST_Proqyz_Admin_Page_Groups
 */
defined( 'ABSPATH' ) || exit;

class ST_ProQyz_Admin_Page_Groups {
    /**
    * Hook in tabs.
    */
    public function __construct() {
        $this->hooks();
        $this->includes();
    }

    /**
      * register hooks
      */
    public function hooks() { }

    /**
      * includes
      */
    public function includes() { }

    public static function get_page_layout(){
        require_once ST_PROQYZ_ABSPATH . "/includes/templates/admin/groups/groups.php"; // checked
    }

    /**
     * @var action_url
     */
    public static function action_url($params = [], $post_id = null, $url_type = 'list') {
        $params         = (object) $params;
        $current_page   = isset($params->current_page) ? max(1, intval($params->current_page)) : 1;
        $per_page       = isset($params->per_page) ? max(1, intval($params->per_page)) : 100;
        $page_slug      = isset($params->page_slug) ? $params->page_slug : 'st-proqyz-groups';
        $post_action    = isset($params->post_action) ? $params->post_action : 'trash';
        $post_status    = isset($params->post_status) ? $params->post_status : 1;
        $post_status    = $post_status == 1? 'publish' : 'trash';


        $url            = admin_url("admin.php?page=$page_slug&post_status=$post_status&current_page=$current_page&per_page=$per_page&post_action=$url_type&post_id=$post_id");
        return $url;
    }

    /**
     * @var table_action_url
     */
    public static function get_table_action_url( $params = [], $url_type = 'publish') {
        $params         = (object) $params;
        $current_page   = isset($params->current_page) ? max(1, intval($params->current_page)) : 1;
        $per_page       = isset($params->per_page) ? max(1, intval($params->per_page)) : 100;
        $page_slug      = isset($params->page_slug) ? $params->page_slug : 'st-proqyz-groups';

        $url            = admin_url("admin.php?page=$page_slug&post_status=$url_type&current_page=$current_page&per_page=$per_page");
        return $url;
    }


    public static function get_groups_list($params = []) {
        global $wpdb, $table_proqyz_groups;
        $params = (object) $params;

        // Default parameters
        $current_page = isset($params->current_page) ? max(1, intval($params->current_page)) : 1;
        $per_page = isset($params->per_page) ? max(1, intval($params->per_page)) : 100;
        $status = isset($params->post_status) ? intval($params->post_status) : 1;

        // Calculate offset for pagination
        $offset = ($current_page - 1) * $per_page;

        // Prepare the base query for getting groups
        $query = "SELECT * FROM {$table_proqyz_groups} WHERE `status` = %d ORDER BY `date_of_publish` DESC LIMIT %d, %d";
        $get_groups = $wpdb->get_results($wpdb->prepare($query, $status, $offset, $per_page));

        // Queries for total published and trashed items
        $total_published_items_query = "SELECT COUNT(*) FROM {$table_proqyz_groups} WHERE `status` = 1";
        $total_trashed_items_query = "SELECT COUNT(*) FROM {$table_proqyz_groups} WHERE `status` = 0";

        // Execute the queries to get the counts
        $total_published = $wpdb->get_var($total_published_items_query);
        $total_trashed = $wpdb->get_var($total_trashed_items_query);

        if($status == 1 || $status == 0){
            if(count($get_groups) <= 0 ) {
                
            }
        }

        // Return the results
        return (object) [
            "rows"      => (array) $get_groups,
            "published" => intval($total_published),
            "trashed"   => intval($total_trashed)
        ];
    }


    public static function process_trash($params = [], $post_id = null ) {
        global $wpdb, $table_proqyz_groups;
        $params         = (object) $params;
        $current_page   = isset($params->current_page) ? max(1, intval($params->current_page)) : 1;
        $per_page       = isset($params->per_page) ? max(1, intval($params->per_page)) : 100;
        $page_slug      = isset($params->page_slug) ? $params->page_slug : 'st-proqyz-groups';
        $post_action    = isset($params->post_action) ? $params->post_action : 'trash';
        $post_status    = isset($params->post_status) ? $params->post_status : 1;
        $post_status    = $post_status == 1? 'publish' : 'trash';

        if( $post_id !== null ) {
            $wpdb->update($table_proqyz_groups, [
                "status" => 0
            ], [
                "ID" => $post_id
            ]);
        }

        $url            = admin_url("admin.php?page=$page_slug&post_status=$post_status&current_page=$current_page&per_page=$per_page&post_action=list");
        // Redirect to the specified URL
        wp_redirect($url);
        exit; // Ensures that the script stops executing after the redirect
    }

    public static function process_restore($params = [], $post_id = null ) {
        global $wpdb, $table_proqyz_groups;
        $params         = (object) $params;
        $current_page   = isset($params->current_page) ? max(1, intval($params->current_page)) : 1;
        $per_page       = isset($params->per_page) ? max(1, intval($params->per_page)) : 100;
        $page_slug      = isset($params->page_slug) ? $params->page_slug : 'st-proqyz-groups';
        $post_action    = isset($params->post_action) ? $params->post_action : 'trash';
        $post_status    = isset($params->post_status) ? $params->post_status : 1;
        $post_status    = $post_status == 1? 'publish' : 'trash';

        if( $post_id !== null ) {
            $wpdb->update($table_proqyz_groups, [
                "status" => 1
            ], [
                "ID" => $post_id
            ]);
        }

        $url            = admin_url("admin.php?page=$page_slug&post_status=$post_status&current_page=$current_page&per_page=$per_page&post_action=list");
        // Redirect to the specified URL
        wp_redirect($url);
        exit; // Ensures that the script stops executing after the redirect
    }

    public static function process_delete($params = [], $post_id = null ) {
        global $wpdb, $table_proqyz_groups;
        $params         = (object) $params;
        $current_page   = isset($params->current_page) ? max(1, intval($params->current_page)) : 1;
        $per_page       = isset($params->per_page) ? max(1, intval($params->per_page)) : 100;
        $page_slug      = isset($params->page_slug) ? $params->page_slug : 'st-proqyz-groups';
        $post_action    = isset($params->post_action) ? $params->post_action : 'trash';
        $post_status    = isset($params->post_status) ? $params->post_status : 1;
        $post_status    = $post_status == 1? 'publish' : 'trash';

        if( $post_id !== null ) {
            $wpdb->delete($table_proqyz_groups, [
                "ID" => $post_id
            ]);
        }

        $url            = admin_url("admin.php?page=$page_slug&post_status=$post_status&current_page=$current_page&per_page=$per_page&post_action=list");
        // Redirect to the specified URL
        wp_redirect($url);
        exit; // Ensures that the script stops executing after the redirect
    }

}
