<?php

namespace GriffinForms\Frontend\Html\Forms;

class PageRow extends \GriffinForms\Frontend\Html\Forms\Format
{
    protected $bs_pagerow_container_classes = '';
    protected $bs_pagerow_heading_classes = '';
    protected $bs_pagerow_description_classes = '';
    protected $bs_pagerow_columns_container_classes = '';

    public function html()
    {
        $is_theme_set = $this->data['is_theme_set'] ?? false;
        $description = $this->item->getProp('description');
        $margin_class = empty($description) ? 'mb-2' : 'mb-4 gf-pagerow-has-description';
        $container_classes = 'griffinforms-pagerow-container row ' . esc_attr($margin_class);
        if (!$is_theme_set && !empty($this->bs_pagerow_container_classes)) {
            $container_classes .= ' ' . esc_attr($this->bs_pagerow_container_classes);
        }
        $data_attr = $is_theme_set ? ' data-gf-row="container"' : '';
        $html = '<div id="' . esc_attr($this->html_ids['container']) . '" class="' . esc_attr($container_classes) . '"' . $data_attr . '>';
        $html .= $this->pageRowHeading($is_theme_set);
        $html .= $this->rowColumns($is_theme_set);
        $html .= $this->pageRowDescription($is_theme_set);
        $html .= '</div>';
        return $html;
    }
    
    private function pageRowHeading($is_theme_set = false)
    {
        $heading = $this->item->getProp('heading');
        if (empty($heading)) {
            return '';
        }
        $heading_classes = 'griffinforms-pagerow-heading col-md-12';
        if (!$is_theme_set && !empty($this->bs_pagerow_heading_classes)) {
            $heading_classes .= ' ' . esc_attr($this->bs_pagerow_heading_classes);
        }
        $html = '<div class="' . esc_attr($heading_classes) . '" data-gf-element="row-heading">';
        $html .= '<div class="h6 mb-2">' . esc_html($heading) . '</div>';
        $html .= '</div>';
        return $html;
    }
    
    private function pageRowDescription($is_theme_set = false)
    {
        $description = $this->item->getProp('description');
        if (empty($description)) {
            return '';
        }
        $description_classes = 'griffinforms-pagerow-description small col-md-12';
        if (!$is_theme_set && !empty($this->bs_pagerow_description_classes)) {
            $description_classes .= ' ' . esc_attr($this->bs_pagerow_description_classes);
        }
        $html = '<div class="' . esc_attr($description_classes) . '" data-gf-element="row-description">';
        $html .= '<div class="fw-semibold">' . esc_html($description) . '</div>';
        $html .= '</div>';
        return $html;
    }
    
    private function rowColumns($is_theme_set = false)
    {
        $rowcolumn_ids = $this->item->getProp('rowcolumns');

        if (empty($rowcolumn_ids) or !is_array($rowcolumn_ids)) {
            return;
        }

        $columns_container_classes = 'griffinforms-pagerow-children-container col-md-12';
        if (!$is_theme_set && !empty($this->bs_pagerow_columns_container_classes)) {
            $columns_container_classes .= ' ' . esc_attr($this->bs_pagerow_columns_container_classes);
        }
        $html = '<div class="' . esc_attr($columns_container_classes) . '">';
        $html .= '<div class="row">';

        foreach ($rowcolumn_ids as $rowcolumn_id) {

            if (!$this->sql->itemExists('rowcolumn', $rowcolumn_id)) {
                continue;
            }

            $rowcolumn = new RowColumn($rowcolumn_id, $this->data);
            $html .= $rowcolumn->html();
        }
        $html .= '</div>';
        $html .= '</div>';
        return $html;
    }
}
