<?php

loadController('base/module');

class ControllerModuleCategory extends ControllerBaseModule {

    protected $referencePathFile = 'module/category';

    protected $arrayJson = array('category_module');

    private $post  = [];

    protected function loadScripts() {
        parent::loadScripts();
        $this->document->addScript(array(
          'view/javascript/module.category.js'
        ));
    }

    private $error = array();

    protected function loadModel() {
        parent::loadModel();
        $this->load->dmodel('design/layout');
    }

	public function loadData() {
        $this->load->type('tipocomum');
        $this->load->helper('ftr_geral');

        $this->dataVue['Type_Status'] = Type_Status::getEnumList();

        $this->dataVue['TypesForJs'] = array(
            Type_IdLayout::getTypeForJs(),
            Type_StyleCategory::getTypeForJs(),
        );

        $this->dataVue['Type_PositionColumn'] = Type_PositionColumn::getRemoveArray([
            Type_PositionColumn::Tp_Top,
            Type_PositionColumn::Tp_Bottom,
        ]);

        $this->dataVue['layouts'] = $this->dmodel_design_layout->getLayouts();

        $category_module = $this->config->get('category_module');

        $this->dataVue['configurations'] = [
            'category_module' => !empty($category_module) ? array_values($category_module) : [],
          ];
	}

	protected function validate() {
        $this->load->type('tipocomum');
        extract($this->request->post);

        foreach ($category_module as $key => $value) {
            $errors = $this->validate->execute([
                "category_module[$key][position]" => [
                    'value' => $value['position'],
                    'validates' => ['isEmpty'],
                    'condition' => "category_module[$key][position]" == Type_PositionColumn::Tp_Refinar && $layout_id != Type_IdLayout::Tp_Idcategory && $layout_id != Type_IdLayout::Tp_IdSearch
                ],
                "category_module[$key][layout]" => [
                    'value' => $value['layout_id'],
                    'validates' => ['isEmpty'],
                ],
                "category_module[$key][status]" => [
                    'value' => $value['status'],
                    'validates' => ['isEmpty'],
                ],
            ]);
        }

        return $errors;
    }

}
