<?php

use DocuSign\eSign\Client\ApiClient;

class Agro_DocuSign_Templates_TermoDadosBancariosSeguradoOrdemPagamento extends Agro_DocuSign_Template
{
    /**
     *  @param string[] $camposText
     */
    protected array $camposText = [
        "texto_nome_proponente" => '',
        "texto_numero_proposta" => '',
        "texto_banco" => '',
        "texto_agencia" => '',
        "texto_digito_agencia" => '',
        "texto_local_assinatura" => '',
        "texto_dia_assinatura" => '',
        "texto_mes_assinatura" => '',
        "texto_ano_assinatura" => ''
    ];

    protected array $camposRadioGroup = [
        "selecao_nao_sou_titular" => "Seleção1"
    ];

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

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

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

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

        $this->validate();
    }

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

        foreach($this->camposText as $chave => $valor) {
            if ($chave == 'texto_digito_agencia') {
                continue;
            }

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

        if (!empty($camposErro)) {
            $this->mensagemErros = "<b>- Termo Dados Bancários Segurado (Ordem de Pagamento):</b> " . implode( ', ', $camposErro) . ";";
        }
    }
}
