<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
use Sifency\Classes\Sifency_Addons_Widget_Base;
use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Typography;
use \Elementor\Group_Control_Border;
use \Elementor\Group_Control_Box_Shadow;
use \Elementor\Group_Control_Background;
use \Elementor\Icons_Manager;
use \Elementor\Repeater;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use \Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Help_Controls;


class Sifency_Comparative_Table extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Section_Header;
	use Sifency_Addons_Help_Controls;
	public function __construct( $data = [], $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = [
        'name'       => 'sifency_comparative_table',
        'title'      => 'Sifency Comparative Table',
        'icon'       => 'eicon-table',
        'category' 	=> [ 'sifency-business-widgets' ],
        'script_depends'    => [ 'sifency-comparative-table-script' ],
        'style_depends'     => [ 'sifency-comparative-table-style', 'sifency-typo-style', 'sifency-icons', 'sifency-frontend-style' ],
		'keywords'   => ['comparative table', 'feature comparison', 'pricing table', 'feature table', 'comparison chart'],
    ];}
	protected function register_controls() {
		$this->add_sifency_addons_section_header_controls();
		$this->start_controls_section(
			'section_settings',
			[
				'label' => esc_html__( 'Table Settings', 'sifency-addons' ),
			]
		);
		$this->add_control(
			'layout_style',
			[
				'label'   => esc_html__( 'Pre-built Layout', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'style-1',
				'options' => [
					'style-1' => esc_html__( 'Style 1: Clean Grid', 'sifency-addons' ),
					'style-2' => esc_html__( 'Style 2: Shadow Cards', 'sifency-addons' ),
					'style-3' => esc_html__( 'Style 3: Minimal Rows', 'sifency-addons' ),
					'style-4' => esc_html__( 'Style 4: Dark/Contrast', 'sifency-addons' ),
				],
				'prefix_class' => 'sifency-comp-table-layout-',
			]
		);
		$this->add_control(
			'highlight_col',
			[
				'label' => esc_html__( 'Highlight Column', 'sifency-addons' ),
				'type' => Controls_Manager::SELECT,
				'default' => '2',
				'options' => [
					'none' => esc_html__( 'None', 'sifency-addons' ),
					'2'    => esc_html__( 'Column 1 (After Label)', 'sifency-addons' ),
					'3'    => esc_html__( 'Column 2', 'sifency-addons' ),
					'4'    => esc_html__( 'Column 3', 'sifency-addons' ),
					'5'    => esc_html__( 'Column 4', 'sifency-addons' ),
				],
			]
		);
		$this->add_control(
			'sticky_header',
			[
				'label' => esc_html__( 'Sticky Header', 'sifency-addons' ),
				'type' => Controls_Manager::SWITCHER,
				'default' => 'no',
				'prefix_class' => 'sifency-table-sticky-',
			]
		);
		$this->add_control(
			'highlight_badge',
			[
				'label' => esc_html__( 'Highlight Badge Text', 'sifency-addons' ),
				'type' => Controls_Manager::TEXT,
				'default' => 'MOST POPULAR',
				'condition' => [ 'highlight_col!' => 'none' ],
			]
		);
		$this->add_control(
			'show_footer',
			[
				'label' => esc_html__( 'Show Footer Buttons', 'sifency-addons' ),
				'type' => Controls_Manager::SWITCHER,
				'default' => 'yes',
			]
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		// --- SECTION: HEADERS ---
		$this->start_controls_section(
			'section_header_data',
			[
				'label' => esc_html__( 'Table Headers', 'sifency-addons' ),
			]
		);
		$this->add_control( 'label_text', [
			'label' => esc_html__( 'Corner Label (Feature Name Column)', 'sifency-addons' ),
			'type' => Controls_Manager::TEXT,
			'default' => 'Key Features',
		]);

		$this->add_control( 'col_1_head', [
			'label' => esc_html__( 'Column 1 Title', 'sifency-addons' ),
			'type' => Controls_Manager::TEXT,
			'default' => 'Our Company',
		]);

		$this->add_control( 'col_2_head', [
			'label' => esc_html__( 'Column 2 Title', 'sifency-addons' ),
			'type' => Controls_Manager::TEXT,
			'default' => 'Competitor A',
		]);

		$this->add_control( 'col_3_head', [
			'label' => esc_html__( 'Column 3 Title (Optional)', 'sifency-addons' ),
			'type' => Controls_Manager::TEXT,
		]);

		$this->add_control( 'col_4_head', [
			'label' => esc_html__( 'Column 4 Title (Optional)', 'sifency-addons' ),
			'type' => Controls_Manager::TEXT,
		]);

		$this->end_controls_section();

		// --- SECTION: COMPARISON ROWS ---
		$this->start_controls_section(
			'section_rows',
			[
				'label' => esc_html__( 'Comparison Rows', 'sifency-addons' ),
			]
		);
		$repeater = new Repeater();

		$repeater->add_control(
			'row_title',
			[
				'label' => esc_html__( 'Feature Name', 'sifency-addons' ),
				'type' => Controls_Manager::TEXT,
				'default' => 'Feature Name',
				'label_block' => true,
			]
		);

		for ( $i = 1; $i <= 4; $i++ ) {
			$repeater->add_control( "c{$i}_type", [
				'label'     => "Col {$i} Value Type",
				'type'      => Controls_Manager::CHOOSE,
				'options'   => [
					'icon' => [ 'title' => 'Icon', 'icon' => 'eicon-check' ],
					'text' => [ 'title' => 'Text', 'icon' => 'eicon-t-letter' ],
				],
				'default'   => 'icon',
				'separator' => 'before',
			]);

			$repeater->add_control( "c{$i}_icon", [
				'label'     => "Col {$i} Icon",
				'type'      => Controls_Manager::ICONS,
				'default'   => [ 'value' => ($i == 1 ? 'fas fa-check' : 'fas fa-times'), 'library' => 'fa-solid' ],
				'condition' => [ "c{$i}_type" => 'icon' ],
			]);

			$repeater->add_control( "c{$i}_text", [
				'label'     => "Col {$i} Text",
				'type'      => Controls_Manager::TEXT,
				'condition' => [ "c{$i}_type" => 'text' ],
			]);
		}

		$this->add_control(
			'rows',
			[
				'label' => esc_html__( 'Comparison Features', 'sifency-addons' ),
				'type' => Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
				'default' => [
					[ 'row_title' => 'Unlimited Projects', 'c1_text' => 'Yes', 'c2_text' => 'No' ],
					[ 'row_title' => '24/7 Priority Support', 'c1_text' => 'Yes', 'c2_text' => 'No' ],
				],
				'title_field' => '{{{ row_title }}}',
			]
		);
		$this->end_controls_section();

		// --- SECTION: FOOTER ACTIONS ---
		$this->start_controls_section(
			'section_footer_actions',
			[ 
				'label' => esc_html__( 'Footer Buttons', 'sifency-addons' ),
				'condition' => [ 'show_footer' => 'yes' ]
			]
		);

		for ($i = 1; $i <= 4; $i++) {
			$this->add_control( "btn_{$i}_text", [
				'label' => "Button {$i} Text",
				'type' => Controls_Manager::TEXT,
				'default' => 'Get Started',
				'separator' => 'before',
			]);

			$this->add_control( "btn_{$i}_link", [
				'label' => "Link {$i}",
				'type' => Controls_Manager::URL,
				'placeholder' => 'https://example.com',
			]);
		}

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_table_box',
			[
				'label' => esc_html__( 'Table Container & Cells', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);
		$this->add_responsive_control(
			'cell_padding',
			[
				'label' => esc_html__( 'Cell Padding', 'sifency-addons' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%', 'em' ],
				'selectors' => [
					'{{WRAPPER}} .sifency-comp-table th, {{WRAPPER}} .sifency-comp-table td' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);
		$this->add_control(
			'row_border_color',
			[
				'label' => esc_html__( 'Border Color', 'sifency-addons' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .sifency-comp-table, {{WRAPPER}} .sifency-comp-table td, {{WRAPPER}} .sifency-comp-table th' => 'border-color: {{VALUE}};',
				],
			]
		);
		$this->end_controls_section();

		// --- STYLE: HIGHLIGHT COLUMN ---
		$this->start_controls_section( 
			'style_highlight', 
			[
				'label' => esc_html__( 'Highlight Column Style', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
				'condition' => [ 'highlight_col!' => 'none' ],
			]
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name'     => 'high_column_bg',
				'selector' => '{{WRAPPER}} .sifency-ct-highlight',
			]
		);
		$this->add_responsive_control(
			'high_scale',
			[
				'label' => esc_html__( 'Scale (Enlarge)', 'sifency-addons' ),
				'type' => Controls_Manager::SLIDER,
				'range' => [ 'px' => [ 'min' => 1, 'max' => 1.2, 'step' => 0.01 ] ],
				'selectors' => [
					'{{WRAPPER}} .sifency-ct-highlight' => 'transform: scale({{SIZE}}); z-index: 5;',
				],
			]
		);
		$this->add_control(
			'high_badge_heading',
			[
				'label'     => esc_html__( 'Badge Styling', 'sifency-addons' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
			]
		);
		$this->add_control(
			'badge_text_color',
			[
				'label' => esc_html__( 'Badge Text Color', 'sifency-addons' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [ '{{WRAPPER}} .sifency-ct-badge' => 'color: {{VALUE}};' ],
			]
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name'     => 'badge_bg',
				'selector' => '{{WRAPPER}} .sifency-ct-badge',
			]
		);
		$this->end_controls_section();

		// --- TRAIT STYLES ---
		$this->add_sifency_addons_item_style_controls([
			'prefix'   => 'table_wrapper_style',
			'label'    => 'Wrapper',
			'selector' => '.sifency-comp-table',
			'exclude'         => ['dimensions'],
		]);
		$this->add_sifency_addons_master_typography_controls([
			'prefix'   => 'header_style',
			'label'    => 'Header Typography',
			'selector' => '.sifency-comp-table thead tr th',
			'exclude' => ['bg', 'radius', 'stroke', 'shadow', 'tabs', 'transition']
		]);

		$this->add_sifency_addons_master_typography_controls([
			'prefix'   => 'cell_text_style',
			'label'    => 'Data Cell Text',
			'selector' => '.sifency-ct-data',
			'exclude' => ['bg', 'radius', 'stroke', 'shadow', 'tabs', 'transition']
		]);

		$this->add_sifency_addons_master_typography_controls([
			'prefix'   => 'title_style',
			'label'    => 'Feature Name Text',
			'selector' => '.sifency-ct-row-title',
			'exclude' => ['bg', 'radius', 'stroke', 'shadow', 'tabs', 'transition']
		]);
		$this->add_sifency_addons_master_typography_controls([
			'prefix'   => 'footer_style',
			'label'    => 'Footer',
			'selector' => '.sifency-ct-footer-cell, td.sifency-ct-footer-empty',
			'exclude'  => ['stroke','shadow','box_model','alignment','typography','transition','transition'],
		]);

		$this->add_sifency_addons_icon_style_controls([
			'prefix'          => 'table_icon_',
			'label'           => esc_html__( 'Table Icons', 'sifency-addons' ),
			'selector'        => '{{WRAPPER}} .sifency-ct-data i, {{WRAPPER}} .sifency-ct-data svg',
		]);

		$this->add_Sifency_Addons_Button_Style_Controls([ 
			'prefix' => 'footer_btn', 
			'label' => 'Footer Buttons', 
			'selector' => '.sifency-ct-btn',
			'show_dimensions' => true,
		]);

		$this->add_Sifency_Addons_Animation_Controls();
	}
	protected function render_dom( array $context ) {
	$wrapper_id = esc_attr( $context['dom_id'] );
		$settings = $this->get_settings_for_display();
		
		$active_cols = 2;
		if ( ! empty( $settings['col_3_head'] ) ) $active_cols = 3;
		if ( ! empty( $settings['col_4_head'] ) ) $active_cols = 4;

		$highlight = $settings['highlight_col'];
		?>
		<?php $this->render_sifency_section_header_elements( $settings ); ?>
		<div id="<?php echo esc_attr ( $wrapper_id ); ?>" class="sifency-comp-table-wrapper">
			<table class="sifency-comp-table">
				<thead>
					<tr>
						<th class="sifency-ct-head-label">
							<?php echo esc_html($settings['label_text']); ?>
						</th>
						<?php for($i=1; $i<=$active_cols; $i++): 
							$col_idx = $i + 1; // Col 1 is actually the 2nd <th>
							$is_high = ( (string)$highlight === (string)$col_idx );
							$h_class = $is_high ? 'sifency-ct-highlight' : '';
						?>
							<th class="<?php echo esc_attr($h_class); ?>">
								<?php if($is_high && !empty($settings['highlight_badge'])): ?>
									<span class="sifency-ct-badge"><?php echo esc_html($settings['highlight_badge']); ?></span>
								<?php endif; ?>
								<div class="sifency-ct-header-content">
									<?php echo esc_html($settings["col_{$i}_head"]); ?>
								</div>
							</th>
						<?php endfor; ?>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $settings['rows'] as $row ) : ?>
					<tr>
						<td class="sifency-ct-row-title"><?php echo esc_html( $row['row_title'] ); ?></td>
						<?php for ( $i = 1; $i <= $active_cols; $i++ ) : 
							$col_idx = $i + 1;
							$h_class = ( (string)$highlight === (string)$col_idx ) ? 'sifency-ct-highlight' : '';
						?>
						<td class="sifency-ct-data <?php echo esc_attr($h_class); ?>">
							<?php if ( 'icon' === $row["c{$i}_type"] ) : 
								Icons_Manager::render_icon( $row["c{$i}_icon"], [ 'aria-hidden' => 'true' ] ); 
							else : ?>
								<span class="sifency-ct-text-val"><?php echo esc_html( $row["c{$i}_text"] ); ?></span>
							<?php endif; ?>
						</td>
						<?php endfor; ?>
					</tr>
					<?php endforeach; ?>
				</tbody>

				<?php if ( 'yes' === $settings['show_footer'] ) : ?>
				<tfoot>
					<tr>
						<td class="sifency-ct-footer-empty"></td>
						<?php for ( $i = 1; $i <= $active_cols; $i++ ) : 
							$col_idx = $i + 1;
							$h_class = ( (string)$highlight === (string)$col_idx ) ? 'sifency-ct-highlight' : '';
							
							$btn_key = "button_" . $i;
							if ( ! empty( $settings["btn_{$i}_link"]['url'] ) ) {
								$this->add_link_attributes( $btn_key, $settings["btn_{$i}_link"] );
							}
							$this->add_render_attribute( $btn_key, 'class', 'sifency-ct-btn text_underline_animation' );
						?>
						<td class="sifency-ct-footer-cell <?php echo esc_attr($h_class); ?>">
							<?php if ( ! empty( $settings["btn_{$i}_text"] ) ) : ?>
								<a <?php $this->print_render_attribute_string( $btn_key ); ?>>
									<span><?php echo esc_html( $settings["btn_{$i}_text"] ); ?></span>
									<i class="icon-right-open-mini"></i>
								</a>
							<?php endif; ?>
						</td>
						<?php endfor; ?>
					</tr>
				</tfoot>
				<?php endif; ?>
			</table>
		</div>
		<?php
	}
}