<?php

use DocuSign\eSign\Client\ApiClient;

class Agro_DocuSign_Templates_TermoSubvencaoPR extends Agro_DocuSign_Template
{
    protected string $idDev = 'b7231c12-e592-4675-bd26-3e8269c52f03';
    protected string $idProd = '8a323739-1fa6-411d-8ffe-db960d35f61e';

    /**
     *  @param string[] $camposText
     */
    protected array $camposText = [
        "texto_nome_produtor_rural" => '',
        "texto_cadpro_produtor_rural" => '',
        "texto_qualificacao_civil_produtor_rural" => '',
        "texto_municipio_produtor_rural" => '',
        "texto_municipio_propriedade_rural" => '',
        "texto_cobertura" => '',
        "texto_cultura" => '',
        "texto_ano_exercicio" => '',
        "texto_local_assinatura2" => '',
        "texto_dia_assinatura2" => '',
        "texto_mes_descrito_assinatura2" => '',
        "texto_ano_assinatura2" => '',
        "texto_cpf_produtor_rural" => '',
        "texto_rg_produtor_rural" => '',
        "texto_rua_produtor_rural" => '',
        "texto_numero_produtor_rural" => '',
        "texto_cep_produtor_rural" => ''
    ];

    public function __construct(ApiClient $apiClient, ?array $parametros = null)
    {
        $this->id = (new Agro_DocuSign_Services_BuscarIdTemplateTermoSubvencaoPR())->executar();
        parent::__construct($apiClient, $parametros);
    }

    protected function preencherCampos()
    {
        if (empty($this->parametros['id_proposta'])) {
            throw new Exception("ID da proposta não informado.");
        }

        $this->camposText['texto_mes_descrito_assinatura2'] = strtoupper(Agro_Util::formatDate(date('Y-m-d'), Zend_Date::MONTH_NAME));

        $propostas = new Seguro_Model_Propostas();
        $row = $propostas->getDadosTermoSubvencaoPR($this->parametros['id_proposta']);

        foreach ($row as $key => $value) {
            if (array_key_exists($key, $this->camposText)) {
                $this->camposText[$key] = $value;
            }
        }

        $this->validate();
    }

    private function validate() 
    {
        $camposErro = [];

        foreach($this->camposText as $chave => $valor) {

            if ($chave == 'texto_qualificacao_civil_produtor_rural') {
                continue;
            }

            if ($chave == 'texto_rg_produtor_rural') {
                continue;
            }

            if ($chave == 'texto_numero_produtor_rural' && $valor == 0) {
                continue;
            }

            if (empty($valor)) {
                $camposErro[] = $chave;
            }
        }

        if (!empty($camposErro)) {
            $this->mensagemErros = "<b>- Termo Subvenção PR:</b> " . implode( ', ', $camposErro) . ";";
        }
    }
}
