<?php

if (!defined('ABSPATH')) { exit; }

class PrimerReceiptLogList {

	public array $receipt_log_array = array();

	public array $receipt_log_params_array = array();

    public function get($page_number):array {
        $receipt_log_args = array(
            'posts_per_page' => -1,
            'post_type'      => 'primer_receipt_log',
            'paged'          => $page_number,
            'post_status'    => 'publish'
        );

        $order_log = isset($_GET['order_log']) ? sanitize_text_field($_GET['order_log']) : '';
        if (!empty($order_log)) {
            $receipt_log_args['post__in'] = array($order_log);
        }

        $receipt_log_query = new WP_Query($receipt_log_args);

        if ($receipt_log_query->have_posts()) {
            while ($receipt_log_query->have_posts()) {
                $receipt_log_query->the_post();
                $get_the_id = get_the_ID();

                $receipt_log_email_error = get_post_meta($get_the_id, 'receipt_log_email_error', true);
                $receipt_log_error       = json_encode(get_post_meta($get_the_id, 'receipt_log_error', true), JSON_UNESCAPED_UNICODE);
                $receipt_log_status      = get_post_meta($get_the_id, 'receipt_log_status', true);
                $receipt_log_status_text = ($receipt_log_status === 'issued') ? 'Yes' : 'No';

                $receipt_log_email_status       = get_post_meta($get_the_id, 'receipt_log_email', true);
                $receipt_log_email_status_text  = ($receipt_log_email_status === 'sent') ? 'Yes' : 'No';

                $receipt_log_invoice_id = get_post_meta($get_the_id, 'receipt_log_invoice_id', true);
                $series                 = get_post_meta($receipt_log_invoice_id, '_primer_receipt_series', true);
                $number                 = get_post_meta($receipt_log_invoice_id, '_primer_receipt_number', true);
                $invoice_no             = ($series === "EMPTY") ? $number : "$series $number";

                $invoice_log_client               = get_post_meta($get_the_id, 'receipt_log_client', true);
                $receipt_log_order_date           = get_post_meta($get_the_id, 'receipt_log_order_date', true);
                $formatted_receipt_log_order_date = date('F j, Y', strtotime($receipt_log_order_date));

                $this->receipt_log_array[] = array(
                    'receipt_log_order_id'      => get_post_meta($get_the_id, 'receipt_log_order_id', true),
                    'receipt_log_invoice_date'  => get_post_meta($get_the_id, 'receipt_log_invoice_date', true),
                    'receipt_log_order_date'    => $formatted_receipt_log_order_date,
                    'receipt_log_invoice_id'    => $invoice_no,
                    'receipt_log_client'        => $invoice_log_client,
                    'receipt_log_status'        => $receipt_log_status_text,
                    'receipt_log_email'         => $receipt_log_email_status_text,
                    'receipt_log_error'         => $receipt_log_error,
                    'receipt_log_email_error'   => $receipt_log_email_error
                );
            }
        }

        wp_reset_postdata();
        return $this->receipt_log_array;
    }

    public function get_with_params($page_number, $receipt_log_error, $receipt_log_issue):array {
        $meta_values = array();
		$receipt_log_args = array(
			'posts_per_page' => -1,
            'paged'          => $page_number,
			'post_type'      => 'primer_receipt_log'
		);

        !empty($receipt_log_issue) && $meta_values['receipt_log_total_status'][] = 'only_issued';
        !empty($receipt_log_error) && $meta_values['receipt_log_total_status'][] = 'only_errors';

        if (!empty($meta_values)) {
            $receipt_log_args['meta_query']['relation'] = 'AND';
            $i = 0;
            foreach ( $meta_values as $key => $meta_value ) {
                $i++;
                $receipt_log_args['meta_query'][$i]['key'] = $key;
                $receipt_log_args['meta_query'][$i]['value'] = $meta_value;
            }
        }

        $receipt_log_query = new WP_Query( $receipt_log_args );

        if ($receipt_log_query->have_posts()) {
            while ($receipt_log_query->have_posts()) {
                $receipt_log_query->the_post();
                $get_the_id = get_the_ID();

                $receipt_log_email_error = get_post_meta($get_the_id, 'receipt_log_email_error', true);
                $receipt_log_error       = json_encode(get_post_meta($get_the_id, 'receipt_log_error', true), JSON_UNESCAPED_UNICODE);
                $receipt_log_status      = get_post_meta($get_the_id, 'receipt_log_status', true);
                $receipt_log_status_text = ($receipt_log_status === 'issued') ? 'Yes' : 'No';

                $receipt_log_email_status      = get_post_meta($get_the_id, 'receipt_log_email', true);
                $receipt_log_email_status_text = ($receipt_log_email_status === 'sent') ? 'Yes' : 'No';

                $receipt_log_invoice_id = get_post_meta($get_the_id, 'receipt_log_invoice_id', true);
                $series                 = get_post_meta($receipt_log_invoice_id, '_primer_receipt_series', true);
                $number                 = get_post_meta($receipt_log_invoice_id, '_primer_receipt_number', true);
                $invoice_no             = ($series === "EMPTY") ? $number : "$series $number";

                $invoice_log_client               = get_post_meta($get_the_id, 'receipt_log_client', true);
                $receipt_log_order_date           = get_post_meta($get_the_id, 'receipt_log_order_date', true);
                $formatted_receipt_log_order_date = date('F j, Y', strtotime($receipt_log_order_date));

                $this->receipt_log_params_array[] = array(
                    'receipt_log_order_id'      => get_post_meta($get_the_id, 'receipt_log_order_id', true),
                    'receipt_log_invoice_date'  => get_post_meta($get_the_id, 'receipt_log_invoice_date', true),
                    'receipt_log_order_date'    => $formatted_receipt_log_order_date,
                    'receipt_log_invoice_id'    => $invoice_no,
                    'receipt_log_client'        => $invoice_log_client,
                    'receipt_log_status'        => $receipt_log_status_text,
                    'receipt_log_email'         => $receipt_log_email_status_text,
                    'receipt_log_error'         => $receipt_log_error,
                    'receipt_log_email_error'   => $receipt_log_email_error
                );
            }
        }

        wp_reset_postdata();
        return $this->receipt_log_params_array;
    }

}
