<?php
$loader = $this->registry->get('load');
$loader->controller('base/base');

class ControllerCheckoutInformation extends ControllerBaseBase
{

    protected $referencePathFile = 'checkout/information';
    protected $debug = false;

    protected function loadStyle()
    {
        $this->document->addStyle("public/css/minimalist.css");
    }

    protected function loadModel()
    {
        parent::loadModel();
        $this->load->model('account/customer');
    }

    public function index()
    {

        if (!$this->customer->isLogged()) {
            $this->redirect($this->url->link('checkout/checkout'));
        }

        if (count(array_filter($this->customer->getMandatoryInfo())) >= 1) {
            $this->redirect($this->url->link('checkout/checkout'));
        }

        $this->app();

        parent::index();

        if ($this->request->server['REQUEST_METHOD'] == 'POST') {
            if (!$this->validate()) {
                return $this->response->setOutputJson(['errors' => $this->errors]);
            }

            $this->model_account_customer->updateMandatory($this->request->post);

            return $this->response->setOutputJson(['redirect' => $this->url->link('checkout/checkout')]);
        }

        $this->dataVue['name'] = $this->customer->getFirstname(). ' '.$this->customer->getLastname();

        if($this->customer->getTipo_pessoa() == Type_CadastroPessoa::Tp_Fisico){
            $this->dataVue['document'] = ftr_geral::mask($this->customer->getCpf(), "###.###.###-##");
        }else{
            $this->dataVue['document'] = ftr_geral::mask($this->customer->getCnpj(),"##.###.###/####-##");
        }

        $this->response->setOutput($this->render($this->referencePathFile));
    }

    protected function actionUrls()
    {
        $this->dataVue['action'] = $this->url->link($this->referencePathFile, '', 'SSL');
    }

    protected function loadData()
    {
        parent::loadData();

        $this->dataVue['TypesForJs'] = [
            Type_CadastroPessoa::getTypeForJs()
        ];
    }

    protected function childrens()
    {
        if(!$this->ftrgeralregistry->isApp()){
            $this->children = [
                'common/header_minimalist'
            ];
        }else{
            parent::childrens();
        }
    }

    protected function validate()
    {
        extract($this->request->post);

        $this->errors = $this->validate->execute([
            'celular' => [
                'value' => $celular,
                'validates' => ['isEmpty', 'isCellPhone']
            ]
        ]);

        return !$this->errors;
    }

    public function app()
    {
        if($this->ftrgeralregistry->isApp()){
            $this->document->addScriptApp(array(
                'catalog/view/theme/appmobile/public/js/mainheaderform.js',
                'catalog/view/theme/appmobile/public/js/'.$this->referencePathFile.'.js',
            ));
        }
    }


}