<?php
/**
 * Invoices Configuration template class
 *
 * @package MultiPOS - Point of Sale for WooCommerce
 * @version 1.0.0
 */

namespace DDWCMultiPOS\Templates\Admin\Invoices;

use DevDiggers\Framework\Includes\DDFW_Layout;

defined( 'ABSPATH' ) || exit();

if ( ! class_exists( 'DDWCPOS_Invoices_Template' ) ) {
	/**
	 * Free invoice template list.
	 *
	 * The receipt composer is a Pro-only feature; its code is not shipped in the free
	 * build. The "Compose" action routes to an upgrade notice only.
	 */
	class DDWCPOS_Invoices_Template {
		/**
		 * Configuration variable.
		 *
		 * @var array
		 */
		protected $ddwcpos_configuration;

		/**
		 * Construct.
		 *
		 * @param array $ddwcpos_configuration Configuration.
		 */
		public function __construct( $ddwcpos_configuration ) {
			$this->ddwcpos_configuration = $ddwcpos_configuration;

			// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only admin routing input, no state change.
			$action = ! empty( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : '';

			if ( 'compose' === $action ) {
				$this->ddwcpos_get_invoice_composer_upgrade();
				return;
			}

			$this->ddwcpos_get_invoice_template();
		}

		/**
		 * Render the Pro upgrade notice for the invoice composer.
		 *
		 * @return void
		 */
		public function ddwcpos_get_invoice_composer_upgrade() {
			ddfw_upgrade_to_pro_section(
				[
					'image_url'     => DDWCPOS_PLUGIN_URL . 'assets/images/pro-pages/customize-invoice.webp',
					'heading'       => esc_html__( 'Design Your Own Receipts', 'devdiggers-multipos-for-woocommerce' ),
					'description'   => esc_html__( 'The free version ships one translatable receipt template. Upgrade to Pro to fully compose and customize receipts and manage multiple templates.', 'devdiggers-multipos-for-woocommerce' ),
					'list_features' => [
						esc_html__( 'Drag-and-drop receipt composer', 'devdiggers-multipos-for-woocommerce' ),
						esc_html__( 'Add your logo, store details, and custom blocks', 'devdiggers-multipos-for-woocommerce' ),
						esc_html__( 'Create and manage multiple receipt templates', 'devdiggers-multipos-for-woocommerce' ),
						esc_html__( 'Per-outlet receipt branding', 'devdiggers-multipos-for-woocommerce' ),
						esc_html__( 'Show or hide tax, discount, and payment rows', 'devdiggers-multipos-for-woocommerce' ),
					],
					'upgrade_url'   => '//devdiggers.com/product/multipos-point-of-sale-for-woocommerce/',
				]
			);
		}

		/**
		 * Render invoice template list.
		 *
		 * @return void
		 */
		public function ddwcpos_get_invoice_template() {
			$compose_url  = admin_url( 'admin.php?page=ddwcpos-dashboard&menu=invoices&action=compose' );
			$default_name = esc_html__( 'Default Invoice', 'devdiggers-multipos-for-woocommerce' );

			ob_start();
			?>
			<div class="ddwcpos-invoices-grid-wrapper">
				<div class="ddwcpos-invoices-grid">
					<div class="ddwcpos-invoice-card">
						<div class="ddwcpos-invoice-card-header">
							<div class="ddwcpos-card-icon">
								<span class="dashicons dashicons-media-text"></span>
							</div>
							<div class="ddwcpos-card-actions">
								<span class="ddwcpos-status-badge active"><?php esc_html_e( 'Active', 'devdiggers-multipos-for-woocommerce' ); ?></span>
							</div>
						</div>

						<div class="ddwcpos-card-body">
							<div class="ddwcpos-form-group">
								<input type="text" class="regular-text ddwcpos-full-width" value="<?php echo esc_attr( $default_name ); ?>" readonly title="<?php echo esc_attr( $default_name ); ?>" />
							</div>
							<div class="ddwcpos-form-group">
								<input type="text" class="regular-text ddwcpos-full-width readonly-slug" value="default-invoice" readonly title="default-invoice" />
							</div>
						</div>
						<div class="ddwcpos-card-footer">
							<a href="<?php echo esc_url( $compose_url ); ?>" class="button ddfw-upgrade-to-pro-tag-wrapper"><?php esc_html_e( 'Customize', 'devdiggers-multipos-for-woocommerce' ); ?></a>
						</div>
					</div>

					<a href="<?php echo esc_url( $compose_url ); ?>" class="ddwcpos-invoice-card create-new ddfw-upgrade-to-pro-tag-wrapper">
						<div class="ddwcpos-create-content">
							<span class="dashicons dashicons-plus"></span>
							<h3><?php esc_html_e( 'Create New Template', 'devdiggers-multipos-for-woocommerce' ); ?></h3>
						</div>
					</a>
				</div>
			</div>
			<?php
			$custom_html = ob_get_clean();

			$args = [
				[
					'header'            => [
						'heading'     => esc_html__( 'Invoice Templates', 'devdiggers-multipos-for-woocommerce' ),
						'description' => esc_html__( 'Translatable receipt template. Compose and manage extra templates in Pro.', 'devdiggers-multipos-for-woocommerce' ),
					],
					'after_header_html' => $custom_html,
					'fields'            => [],
				],
			];

			$layout = new DDFW_Layout();
			$layout->get_form_section_layout( $args, '', '', 'ddwcpos-invoices-container' );
		}
	}
}
