<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
use Sifency\Classes\Sifency_Addons_Widget_Base;
use \Elementor\Widget_Base;
use \Elementor\Controls_Manager;
use \Elementor\Group_Control_Border;
use \Elementor\Group_Control_Box_Shadow;
use \Elementor\Group_Control_Typography;
use \Elementor\Icons_Manager;
use \Elementor\Repeater;
use \Elementor\Group_Control_Background;
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_Section_Header;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Data_Table extends Sifency_Addons_Widget_Base {
    use Sifency_Addons_Master_Typography;
    use Sifency_Addons_Image_Style_Controls;
    use Sifency_Addons_Item_Style_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_data_table',
        'title'      => 'Sifency Data Table',
        'icon'       => 'eicon-table',
        'category' => [ 'sifency-addons-widgets' ],
        'style_depends'     => [ 'sifency-data-table-style', 'sifency-frontend-style', 'sifency-icons', 'sifency-typo-style' ],
		'keywords'      => ['data table', 'table', 'pricing table', 'comparison table', 'product table', 'responsive table'],
    ];}

	protected function register_controls() {
		$this->add_sifency_addons_section_header_controls();
		$this->start_controls_section(
			'header_content',
			[
				'label' => esc_html__( 'Table Headers', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);

		$repeater_head = new Repeater();

		$repeater_head->add_control(
			'title',
			[
				'label' => esc_html__( 'Column Title', 'sifency-addons' ),
				'type' => Controls_Manager::TEXT,
				'default' => 'Header',
				'label_block' => true,
			]
		);

		$repeater_head->add_control(
			'icon',
			[
				'label' => esc_html__( 'Icon', 'sifency-addons' ),
				'type' => Controls_Manager::ICONS,
			]
		);

        $repeater_head->add_control(
			'image',
			[
				'label' => esc_html__( 'Image', 'sifency-addons' ),
				'type' => Controls_Manager::MEDIA,
			]
		);

		$repeater_head->add_control(
			'is_highlighted_col',
			[
				'label' => esc_html__( 'Highlight Column', 'sifency-addons' ),
				'type' => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
			]
		);

		$repeater_head->add_responsive_control(
			'col_width',
			[
				'label' => esc_html__( 'Column Width', 'sifency-addons' ),
				'type' => Controls_Manager::SLIDER,
				'size_units' => [ 'px', '%' ],
				'selectors' => [
					'{{WRAPPER}} {{CURRENT_ITEM}}' => 'width: {{SIZE}}{{UNIT}}',
				],
			]
		);
		$this->add_control(
			'headers',
			[
				'label' => esc_html__( 'Manage Columns', 'sifency-addons' ),
				'type' => Controls_Manager::REPEATER,
				'fields' => $repeater_head->get_controls(),
				'default' => [
					[ 'title' => 'Product' ],
					[ 'title' => 'Description' ],
					[ 'title' => 'Price' ],
				],
				'title_field' => '{{{ title }}}',
			]
		);
		$this->end_controls_section();

		$this->start_controls_section(
			'section_body_content',
			[
				'label' => esc_html__( 'Table Rows', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);

		$repeater_body = new Repeater();

		$repeater_body->add_control(
			'row_content',
			[
				'label' => esc_html__( 'Row Data (Separated by |)', 'sifency-addons' ),
				'type' => Controls_Manager::TEXTAREA,
				'default' => 'Cell 1 | Cell 2 | Cell 3',
				'description' => esc_html__( 'Use "|" to separate cells for each column.', 'sifency-addons' ),
			]
		);

        $repeater_body->add_control(
			'body_image',
			[
				'label' => esc_html__( 'Row Image (Optional)', 'sifency-addons' ),
				'type' => Controls_Manager::MEDIA,
				'description' => esc_html__( 'Shows in the first column.', 'sifency-addons' ),
			]
		);

		$repeater_body->add_control(
			'is_highlighted',
			[
				'label' => esc_html__( 'Highlight this Row', 'sifency-addons' ),
				'type' => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
			]
		);
		$this->add_control(
			'rows',
			[
				'label' => esc_html__( 'Manage Rows', 'sifency-addons' ),
				'type' => Controls_Manager::REPEATER,
				'fields' => $repeater_body->get_controls(),
				'default' => [
					[ 'row_content' => 'Item A | Quality | $50' ],
					[ 'row_content' => 'Item B | Value | $40' ],
				],
				'title_field' => '{{{ row_content }}}',
			]
		);
		$this->end_controls_section();

		$this->start_controls_section(
			'section_table_settings',
			[
				'label' => esc_html__( 'Table Settings', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);
		$this->add_control(
			'table_layout',
			[
				'label' => esc_html__( 'Table Layout', 'sifency-addons' ),
				'type' => Controls_Manager::SELECT,
				'default' => 'auto',
				'options' => [
					'auto'  => esc_html__( 'Auto', 'sifency-addons' ),
					'fixed' => esc_html__( 'Fixed', 'sifency-addons' ),
				],
				'selectors' => [
					'{{WRAPPER}} .sifency-data-table' => 'table-layout: {{VALUE}};',
				],
			]
		);
		$this->add_responsive_control(
			'table_scroll_mobile',
			[
				'label' => esc_html__( 'Responsive Behavior', 'sifency-addons' ),
				'type' => Controls_Manager::SELECT,
				'default' => 'scroll',
				'options' => [
					'scroll' => esc_html__( 'Horizontal Scroll', 'sifency-addons' ),
					'stack'  => esc_html__( 'Stack Rows (Mobile)', 'sifency-addons' ),
				],
				'prefix_class' => 'sifency-table-res-',
			]
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
        $this->add_sifency_addons_item_style_controls([
            'prefix'   => 'header_item',
            'label'    => 'Header Cell',
            'selector' => '.sifency-data-table thead th',
        ]);

        $this->add_sifency_addons_master_typography_controls([
            'prefix'   => 'header_text',
            'label'    => 'Header Typography',
            'selector' => '.sifency-heading-style',
			'exclude' => ['bg', 'radius', 'stroke', 'shadow', 'tabs', 'transition']
        ]);

		$this->start_controls_section(
			'section_style_header',
			[
				'label' => esc_html__( 'Header Style', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);
		$this->add_control(
			'header_icon_heading',
			[
				'label' => esc_html__( 'Icons & Images', 'sifency-addons' ),
				'type' => Controls_Manager::HEADING,
				'separator' => 'before',
			]
		);
        $this->add_control(
			'header_icon_color',
			[
				'label' => esc_html__( 'Icon Color', 'sifency-addons' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .sifency-dt-header-icon i' => 'color: {{VALUE}};',
					'{{WRAPPER}} .sifency-dt-header-icon svg' => 'fill: {{VALUE}};',
				],
			]
		);
		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_body_rows',
			[
				'label' => esc_html__( 'Body & Rows', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);

		$this->start_controls_tabs('tabs_row_colors');

		$this->start_controls_tab('tab_row_normal', ['label' => 'Normal']);
		
		$this->add_control(
			'row_bg_odd',
			[
				'label' => esc_html__( 'Odd Row Background', 'sifency-addons' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [ '{{WRAPPER}} .sifency-data-table tbody tr:nth-child(odd)' => 'background-color: {{VALUE}};' ],
			]
		);
		$this->add_control(
			'row_bg_even',
			[
				'label' => esc_html__( 'Even Row Background', 'sifency-addons' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [ '{{WRAPPER}} .sifency-data-table tbody tr:nth-child(even)' => 'background-color: {{VALUE}};' ],
			]
		);

		$this->end_controls_tab();

		$this->start_controls_tab('tab_row_hover', ['label' => 'Hover']);

		$this->add_control(
			'row_bg_hover',
			[
				'label' => esc_html__( 'Hover Background', 'sifency-addons' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [ '{{WRAPPER}} .sifency-data-table tbody tr:hover' => 'background-color: {{VALUE}};' ],
			]
		);

		$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->add_responsive_control(
			'cell_padding',
			[
				'label' => esc_html__( 'Cell Padding', 'sifency-addons' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em' ],
				'selectors' => [
					'{{WRAPPER}} .sifency-data-table td, {{WRAPPER}} .sifency-data-table th' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
				'separator' => 'before',
			]
		);
		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_highlight_col',
			[
				'label' => esc_html__( 'Highlighted Column', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name'     => 'highlight_col_bg',
				'label'    => esc_html__( 'Background', 'sifency-addons' ),
				'types'    => [ 'classic', 'gradient' ],
				'selector' => '{{WRAPPER}} .sifency-dt-col-highlight',
			]
		);
		$this->add_control(
			'highlight_col_text',
			[
				'label' => esc_html__( 'Text Color', 'sifency-addons' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .sifency-dt-col-highlight' => 'color: {{VALUE}};',
				],
			]
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name' => 'highlight_col_border',
				'selector' => '{{WRAPPER}} .sifency-dt-col-highlight',
			]
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[
				'name' => 'highlight_col_shadow',
				'selector' => '{{WRAPPER}} .sifency-dt-col-highlight',
			]
		);
		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_highlight_row',
			[
				'label' => esc_html__( 'Highlighted Row', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);
		$this->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name'     => 'highlight_row_bg',
				'label'    => esc_html__( 'Background', 'sifency-addons' ),
				'types'    => [ 'classic', 'gradient' ],
				'selector' => '{{WRAPPER}} tr.sifency-dt-row-highlight td',
			]
		);
		$this->add_control(
			'highlight_row_text',
			[
				'label' => esc_html__( 'Text Color', 'sifency-addons' ),
				'type'  => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} tr.sifency-dt-row-highlight' => 'color: {{VALUE}};',
				],
			]
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'highlight_row_typography',
				'selector' => '{{WRAPPER}} tr.sifency-dt-row-highlight',
			]
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name' => 'highlight_row_border',
				'selector' => '{{WRAPPER}} tr.sifency-dt-row-highlight',
			]
		);
		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[
				'name' => 'highlight_row_shadow',
				'selector' => '{{WRAPPER}} tr.sifency-dt-row-highlight',
			]
		);
		$this->add_control(
			'highlight_row_radius',
			[
				'label' => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'  => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'selectors' => [
					'{{WRAPPER}} tr.sifency-dt-row-highlight td:first-child' => 'border-radius: {{TOP}}{{UNIT}} 0 0 {{LEFT}}{{UNIT}};',
					'{{WRAPPER}} tr.sifency-dt-row-highlight td:last-child'  => 'border-radius: 0 {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} 0;',
				],
			]
		);
		$this->end_controls_section();

        $this->add_sifency_addons_item_style_controls([
            'prefix'   => 'row_item',
            'label'    => 'Row Item',
            'selector' => '.sifency-data-table tbody td',
        ]);
        $this->add_sifency_addons_item_style_controls([
            'prefix'   => 'col_item',
            'label'    => 'Column Item',
            'selector' => '.sifency-data-table tbody tr',
        ]);
        $this->add_sifency_addons_master_typography_controls([
            'prefix'   => 'cell_text',
            'label'    => 'Cell Text',
            'selector' => '.sifency-content-style',
			'exclude' => ['bg', 'radius', 'stroke', 'shadow', 'tabs', 'transition']
        ]);
        $this->add_sifency_addons_image_style_controls([
            'prefix'   => 'row_image',
            'label'    => 'Row Image',
            'selector' => '.sifency-dt-row-img',
			'show_overlay'   => false,
        ]);
        $this->add_sifency_addons_image_style_controls([
            'prefix'   => 'col_image',
            'label'    => 'Column Image',
            'selector' => '.sifency-dt-img',
			'show_overlay'   => false,
        ]);
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		$settings = $this->get_settings_for_display();

		if ( empty( $settings['headers'] ) ) return;
		?>
		<?php $this->render_sifency_section_header_elements( $settings ); ?>
		<div id="<?php echo esc_attr ( $wrapper_id ); ?>" class="sifency-data-table-wrapper">
			<table class="sifency-data-table">
				<thead>
					<tr>
						<?php foreach ( $settings['headers'] as $index => $head ) : 
							$header_key = $this->get_repeater_setting_key( 'title', 'headers', $index );
							$classes = [ 'elementor-repeater-item-' . $head['_id'] ];
							if ( 'yes' === ($head['is_highlighted_col'] ?? '') ) $classes[] = 'sifency-dt-col-highlight';
							$this->add_render_attribute( $header_key, 'class', $classes );
						?>
							<th <?php echo wp_kses_post( $this->get_render_attribute_string( $header_key )); ?>>
								<div class="sifency-dt-cell-inner">
									<?php if ( ! empty( $head['icon']['value'] ) ) : ?>
										<span class="sifency-dt-header-icon"><?php Icons_Manager::render_icon( $head['icon'] ); ?></span>
									<?php endif; ?>

									<?php if ( ! empty( $head['image']['url'] ) ) : ?>
										<img class="sifency-dt-img" src="<?php echo esc_url( $head['image']['url'] ); ?>" alt="">
									<?php endif; ?>

									<span class="sifency-dt-title sifency-heading-style"><?php echo esc_html( $head['title'] ); ?></span>
								</div>
							</th>
						<?php endforeach; ?>
					</tr>
				</thead>

				<tbody>
					<?php foreach ( $settings['rows'] as $row ) : 
						$cells = explode( '|', $row['row_content'] );
						$row_class = ( 'yes' === ($row['is_highlighted'] ?? '') ) ? 'sifency-dt-row-highlight' : '';
					?>
						<tr class="<?php echo esc_attr( $row_class ); ?>">
							<?php foreach ( $settings['headers'] as $col_index => $header ) : 
								$cell_class = ( 'yes' === ($header['is_highlighted_col'] ?? '') ) ? 'sifency-dt-col-highlight' : '';
							?>
								<td class="<?php echo esc_attr( $cell_class ); ?>" data-title="<?php echo esc_attr($header['title']); ?>">
									<div class="sifency-dt-cell-inner">
										<?php if ( $col_index === 0 && ! empty( $row['body_image']['url'] ) ) : ?>
											<img class="sifency-dt-row-img" src="<?php echo esc_url( $row['body_image']['url'] ); ?>" alt="">
										<?php endif; ?>

										<span class="sifency-dt-cell-text sifency-content-style">
											<?php echo isset( $cells[ $col_index ] ) ? wp_kses_post( trim($cells[ $col_index ]) ) : ''; ?>
										</span>
									</div>
								</td>
							<?php endforeach; ?>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		</div>
		<?php
	}
}