<?php
namespace CSSIgniter\Ignition\Elementor\Widget;

class Tables extends \Elementor\Widget_Base {
	public function get_style_depends() {
		wp_register_style( 'ignition-elementor-tables', plugin_dir_url( __DIR__ ) . 'assets/css/tables.min.css', array(), IGNITION_ELEMENTOR_WIDGETS_VERSION );

		return array(
			'ignition-elementor-tables',
		);

	}

	public function get_name() {
		return 'ignition-tables';
	}

	public function get_title() {
		return __( 'Tables', 'ignition-elementor-widgets' );
	}

	public function get_icon() {
		return 'eicon-table';
	}

	public function get_categories() {
		return array( 'ignition-elementor-widgets' );
	}

	protected function register_controls() {

		$this->start_controls_section(
			'section_general',
			array(
				'label' => __( 'General Options', 'ignition-elementor-widgets' ),
			)
		);

		$this->add_control(
			'responsive_tables',
			array(
				'label'     => __( 'Allow horizontal scroll on small screens', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::SWITCHER,
				'default'   => 'yes',
				'label_on'  => __( 'Yes', 'ignition-elementor-widgets' ),
				'label_off' => __( 'No', 'ignition-elementor-widgets' ),
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_header',
			array(
				'label' => __( 'Table Header', 'ignition-elementor-widgets' ),
			)
		);

		$repeater = new \Elementor\Repeater();

		$repeater->add_control(
			'text',
			array(
				'label'       => __( 'Header Text', 'ignition-elementor-widgets' ),
				'type'        => \Elementor\Controls_Manager::TEXT,
				'label_block' => true,
				'placeholder' => __( 'Header Text', 'ignition-elementor-widgets' ),
				'default'     => __( 'Header Text', 'ignition-elementor-widgets' ),
			)
		);

		$repeater->add_control(
			'colspan',
			array(
				'label'   => __( 'Column Span', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::NUMBER,
				'min'     => 1,
				'step'    => 1,
				'default' => 1,
			)
		);

		$repeater->add_control(
			'rowspan',
			array(
				'label'   => __( 'Row Span', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::NUMBER,
				'min'     => 1,
				'step'    => 1,
				'default' => 1,
			)
		);

		$repeater->add_control(
			'align',
			array(
				'label'     => __( 'Alignment', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::CHOOSE,
				'options'   => array(
					'left'    => array(
						'title' => __( 'Left', 'ignition-elementor-widgets' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center'  => array(
						'title' => __( 'Center', 'ignition-elementor-widgets' ),
						'icon'  => 'eicon-text-align-center',
					),
					'right'   => array(
						'title' => __( 'Right', 'ignition-elementor-widgets' ),
						'icon'  => 'eicon-text-align-right',
					),
					'justify' => array(
						'title' => __( 'Justified', 'ignition-elementor-widgets' ),
						'icon'  => 'eicon-text-align-justify',
					),
				),
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} table.ignition-elementor-table .ignition-elementor-table-head {{CURRENT_ITEM}}' => 'text-align: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'table_header',
			array(
				'label'         => __( 'Table Header', 'ignition-elementor-widgets' ),
				'type'          => \Elementor\Controls_Manager::REPEATER,
				'title_field'   => __( 'Table Header', 'ignition-elementor-widgets' ),
				'fields'        => $repeater->get_controls(),
				'prevent_empty' => false,
				'default'       => array(
					array(
						'text' => __( 'Header Text', 'ignition-elementor-widgets' ),
					),
					array(
						'text' => __( 'Header Text', 'ignition-elementor-widgets' ),
					),
				),
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_body',
			array(
				'label' => __( 'Table Body', 'ignition-elementor-widgets' ),
			)
		);

		$repeater = new \Elementor\Repeater();

		$repeater->add_control(
			'text',
			array(
				'label'       => __( 'Body Text', 'ignition-elementor-widgets' ),
				'type'        => \Elementor\Controls_Manager::WYSIWYG,
				'placeholder' => __( 'Type your description here', 'ignition-elementor-widgets' ),
				'default'     => __( 'Table Data', 'ignition-elementor-widgets' ),
			)
		);

		$repeater->add_control(
			'new_row',
			array(
				'label'        => __( 'New Row', 'ignition-elementor-widgets' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => __( 'On', 'your-plugin' ),
				'label_off'    => __( 'Off', 'your-plugin' ),
				'return_value' => 'yes',
				'default'      => '',
			)
		);

		$repeater->add_control(
			'colspan',
			array(
				'label'   => __( 'Column Span', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::NUMBER,
				'min'     => 1,
				'step'    => 1,
				'default' => 1,
			)
		);

		$repeater->add_control(
			'rowspan',
			array(
				'label'   => __( 'Row Span', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::NUMBER,
				'min'     => 1,
				'step'    => 1,
				'default' => 1,
			)
		);

		$this->add_control(
			'table_body',
			array(
				'label'   => __( 'Table Body', 'ignition-elementor-widgets' ),
				'type'    => \Elementor\Controls_Manager::REPEATER,
				'fields'  => $repeater->get_controls(),
				'default' => array(
					array(
						'text' => __( 'Body Text', 'ignition-elementor-widgets' ),
					),
					array(
						'text' => __( 'Body Text', 'ignition-elementor-widgets' ),
					),
				),
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style',
			array(
				'label' => __( 'Table Styles', 'ignition-elementor-widgets' ),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'cell_padding',
			array(
				'label'      => __( 'Inner Cell Padding', 'ignition-elementor-widgets' ),
				'type'       => \Elementor\Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} table.ignition-elementor-table td,{{WRAPPER}} table.ignition-elementor-table th' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_group_control(
			\Elementor\Group_Control_Border::get_type(),
			array(
				'name'     => 'table_border',
				'label'    => __( 'Border', 'ignition-elementor-widgets' ),
				'selector' => '{{WRAPPER}} table.ignition-elementor-table td,{{WRAPPER}} table.ignition-elementor-table tr,{{WRAPPER}} table.ignition-elementor-table th',
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'table_header_style',
			array(
				'label' => __( 'Table Header Style', 'ignition-elementor-widgets' ),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_responsive_control(
			'text_align',
			array(
				'label'     => __( 'Text Alignment', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::CHOOSE,
				'options'   => array(
					'left'    => array(
						'title' => __( 'Left', 'ignition-elementor-widgets' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center'  => array(
						'title' => __( 'Center', 'ignition-elementor-widgets' ),
						'icon'  => 'eicon-text-align-center',
					),
					'right'   => array(
						'title' => __( 'Right', 'ignition-elementor-widgets' ),
						'icon'  => 'eicon-text-align-right',
					),
					'justify' => array(
						'title' => __( 'Justified', 'ignition-elementor-widgets' ),
						'icon'  => 'eicon-text-align-justify',
					),
				),
				'selectors' => array(
					'{{WRAPPER}} table.ignition-elementor-table .ignition-elementor-table-head th' => 'text-align: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'header_text_color',
			array(
				'label'     => __( 'Text Color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} table.ignition-elementor-table .ignition-elementor-table-head' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			array(
				'name'     => 'header_typography',
				'selector' => '{{WRAPPER}} table.ignition-elementor-table .ignition-elementor-table-head',
				'scheme'   => \Elementor\Core\Schemes\Typography::TYPOGRAPHY_3,
			)
		);

		$this->add_control(
			'header_bg_color',
			array(
				'label'     => __( 'Background Color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} table.ignition-elementor-table .ignition-elementor-table-head' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'table_body_style',
			array(
				'label' => __( 'Table Body Style', 'ignition-elementor-widgets' ),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_responsive_control(
			'body_text_align',
			array(
				'label'     => __( 'Alignment', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::CHOOSE,
				'options'   => array(
					'left'    => array(
						'title' => __( 'Left', 'ignition-elementor-widgets' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center'  => array(
						'title' => __( 'Center', 'ignition-elementor-widgets' ),
						'icon'  => 'eicon-text-align-center',
					),
					'right'   => array(
						'title' => __( 'Right', 'ignition-elementor-widgets' ),
						'icon'  => 'eicon-text-align-right',
					),
					'justify' => array(
						'title' => __( 'Justified', 'ignition-elementor-widgets' ),
						'icon'  => 'eicon-text-align-justify',
					),
				),
				'selectors' => array(
					'{{WRAPPER}} table.ignition-elementor-table .ignition-elementor-table-body' => 'text-align: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'body_text_color',
			array(
				'label'     => __( 'Text Color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} table.ignition-elementor-table .ignition-elementor-table-body' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			array(
				'name'     => 'body_text_typography',
				'selector' => '{{WRAPPER}} table.ignition-elementor-table .ignition-elementor-table-body',
				'scheme'   => \Elementor\Core\Schemes\Typography::TYPOGRAPHY_3,
			)
		);

		$this->add_control(
			'body_bg_color',
			array(
				'label'     => __( 'Primary Background Color', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} table.ignition-elementor-table .ignition-elementor-table-body' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'body_striped_bg_color',
			array(
				'label'     => __( 'Secondary Background Color (alternating rows)', 'ignition-elementor-widgets' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} table.ignition-elementor-table .ignition-elementor-table-body tr:nth-of-type(2n)' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->end_controls_section();

	}

	protected function render() {
		$settings     = $this->get_settings();
		$table_header = $settings['table_header'];
		$table_body   = $settings['table_body'];
		$responsive   = 'yes' === $settings['responsive_tables'] ? 'ignition-elementor-table-responsive' : '';

		?>
		<table class="ignition-elementor-table <?php echo esc_attr( $responsive ); ?>">
			<?php if ( ! empty( $table_header ) ) : ?>
			<thead class="ignition-elementor-table-head">
				<tr>
					<?php
					foreach ( $table_header as $arr_index => $data ) {

						echo '<th class="elementor-repeater-item-' . esc_attr( $data['_id'] ) . '" colspan="' . intval( $data['colspan'] ) . '" rowspan="' . intval( $data['rowspan'] ) . '">' . esc_html( $data['text'] ) . '</th>';
					}
					?>
				</tr>
			</thead>
			<?php endif; ?>
			<tbody class="ignition-elementor-table-body">
				<tr>
					<?php
					foreach ( $table_body as $arr_index => $data ) {
						if ( 'yes' === $data['new_row'] ) {
							echo '</tr><tr>';
						}

						echo '<td colspan="' . intval( $data['colspan'] ) . '" rowspan="' . intval( $data['rowspan'] ) . '">' . wp_kses_post( $data['text'] ) . '</td>';
					}
					?>
				</tr>
			</tbody>
		</table>
		<?php
	}
}
