<?php
setlocale(LC_MONETARY, 'en_US');
$data = Input::all();
$serial_scy = $data['serial_scy'];
$totalToPayStudents = 0;
$protocol = 'http://';
$serialPcg = '';
if ($_SERVER['SERVER_PORT'] === '443') {
    $protocol = 'https://';
}
$totalToPay = 0;
$document_root = $protocol . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . DOCUMENT_ROOT_LOGIC;
$global_months_full_name = GlobalVars::$global_months_full_name;
?>
<?php echo HTML::script('https://cdn.paymentez.com/ccapi/sdk/payment_checkout_3.0.0.min.js'); ?>

<div class="form-row">
    <?php echo Form::open(array('url' => '', 'class' => 'form-signin', 'method' => 'POST','id'=>'frmSolicitudPago')); ?>

    <div class="row">
        <div class="col-md-7 col-md-offset-2">
            <h4 class="text-muted text-center">Confirmación de datos y pago</h4>
            <hr width="100%">
        </div>
    </div>
    <div class="row">
        <div class="col-md-7 col-md-offset-2">
            <h3 class="text-muted text-center">PAGO DE RUBROS EN CORRIENTE</h3>
        </div>
    </div>
    <div class="row">
        <div class="col-md-7 col-md-offset-2">
            <table class="table table-responsive table-bordered table-hover table-sm results">
                <thead>
                    <tr>
                        <th>
                            ESTUDIANTE
                        </th>
                        <th>CARTA DE PAGO</th>
                        <th>MES</th>
                        <th>VALOR</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($data['serial_std'] as $serial_std=> $cps): ?>
                    <?php
                    $uData = $data['student'][$serial_std];
                    $student = Student::find($serial_std);
                    $paymentManageController = new PaymentManageController();
                    $valueToPay = 0;
                    $invoiceExplodeContgroller = new InvoiceExplodeController();
                    $invoiceExplodeContgroller->getCheckIsAgroped($data['serial_scy'], $student->serial_std);
					$valueToPayByCp=0;
					 $valueToPayByCp = 0;
					 end($cps);
					$cpEndToChekDAteTo= key($cps);
					$startDatedeb = $paymentManageController->getMonthByCpNumber($data['serial_scy'], $cpEndToChekDAteTo - 1);   
                    ?>
                    <?php foreach($cps as $cp => $totalInvoice): ?>
                    <?php
					$valueToPayByCp = 0;
                    $startDatedeb = $paymentManageController->getMonthByCpNumber($data['serial_scy'], $cp - 1);
                    $studentYearDebts = new StudentYearDebts();
                    $totalInvoices = $studentYearDebts->getTotalInvoicesByStudentStartDateDebSchoolYear($student->serial_std, $startDatedeb, $data['serial_scy'], 'NO', 'NO');
                    $datesToPayCurrent = new DatesToPayCurrentPayments();
                    $datesToPayCurrent = $datesToPayCurrent->getMaxMonthsToPay($data['serial_scy'], $startDatedeb, 1);
					//Debug::print_r(array($startDatedeb,$datesToPayCurrent));
                    ?>
                    <?php foreach($totalInvoices as $totalInvoice): ?>
                    <?php
                    $totalInvoiceData = TotalInvoice::find($totalInvoice->serial_ttl);
                    $sumPaymentDetails = $totalInvoiceData->getSumPaymentDetail();
                    $yearLevelSpecialtyPaymentConcept = YearLevelSpecialtyPaymentConcept::find($totalInvoice->serial_ypc);
                    $invoiceExplodeContgroller = new InvoiceExplodeController();
                    $invoiceExplodeContgroller->getCheckIsAgroped($yearLevelSpecialtyPaymentConcept->serial_scy, $student->serial_std);
                    $paymentConcept = PaymentConcept::find($yearLevelSpecialtyPaymentConcept->serial_pct);
                    $serialPcg = $yearLevelSpecialtyPaymentConcept->serial_pcg;
                    $paymetnConceptGroup = PaymentConceptGroup::find($serialPcg);
                    $value = bcsub($totalInvoice->total_ttl, $sumPaymentDetails, 2);
                    $valueToPay += bcsub($totalInvoice->total_ttl, $sumPaymentDetails, 2);
                    $valueToPayByCp += bcsub($totalInvoice->total_ttl, $sumPaymentDetails, 2);
                    ?>
                    <?php
                    $totalToPay+=$value;
                    ?>
                    <tr>
                        <td>
                            <label class="text-muted "><?php echo $student->last_name_std; ?> <?php echo $student->first_name_std; ?></label>
                            <input type="hidden" name="student[<?php echo $serial_std; ?>][serial_std]" id="serial_sdt_<?php echo $student->serial_std; ?>" value="<?php echo $student->serial_std; ?>" >
                        </td>
                        <td>
                            <label class="text-muted "><?php echo $paymetnConceptGroup->name; ?> </label>
                            <input type="hidden" name="student[<?php echo $serial_std; ?>][serial_ypc]" id="serial_sdt_<?php echo $student->serial_std; ?>" value="<?php echo $yearLevelSpecialtyPaymentConcept->serial_ypc; ?>" >
                        </td>
                        <td>
                            <label class="text-muted "><?php echo $global_months_full_name[Date('M',strtotime($totalInvoice->start_date_deb))]; ?></label>
                            <input type="hidden" name="student[<?php echo $serial_std; ?>][serial_dts]" id="serial_sdt_<?php echo $student->serial_std; ?>" value="<?php echo $datesToPayCurrent->serial_dts; ?>" >
                            <input type="hidden" name="student[<?php echo $serial_std; ?>][start_date_deb]" id="start_date_deb_serial_sdt_<?php echo $student->serial_std; ?>" value="<?php echo $startDatedeb; ?>" >
                            <input type="hidden" name="debtsData[<?php echo $student->serial_std; ?>][<?php echo $yearLevelSpecialtyPaymentConcept->serial_ypc; ?>][<?php echo $datesToPayCurrent->serial_dts; ?>]" id="serial_sdt_<?php echo $student->serial_std; ?>" value="<?php echo $valueToPayByCp; ?>" >
                        </td>
                        <td class="text-right col-md-3">
                            <label class="text-muted "><?php echo money_format('%(#10n',$value); ?></label>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    <?php endforeach; ?>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
    <div class="row">
        <div class="col-md-7 col-md-offset-2">
            <table>
                <tbody>
                    <tr>
                        <td class="col-md-6">
                            <h4 style="color:black">SUBTOTAL</h4>
                        </td>
                        <td class="col-md-6 text-right">
                            <h4 style="color:black"><?php echo money_format('%(#10n',$totalToPay); ?></h4>
                        </td>
                    </tr>
                    <tr>
                        <td class="col-md-6">
                            <h4 style="color:black">IMPUESTOS</h4>
                        </td>
                        <td class="col-md-6 text-right">
                            <h4 style="color:black"><?php echo money_format('%(#10n',0); ?></h4>
                        </td>
                    </tr>
                    <tr>
                        <td class="col-md-6">
                            <h4 style="color:black">TOTAL</h4>
                        </td>
                        <td class="col-md-6 text-right">
                            <h4 style="color:black"><?php echo money_format('%(#10n',$totalToPay); ?></h4>
                            <input type="hidden" name="quota_amount_cdf" id="quota_amount_cdf" value="<?php echo $totalToPayStudents; ?>" >
                        </td>
                    </tr>
                </tbody>
            </table>

        </div>
    </div>
    <div class="row">
        <div class="form-horizontal">
            <div class="col-md-offset-3 col-md-5">
                <div class="form-group">
                    <label for="documentType" class="col-md-6 control-label">* Tipo de documento:</label>
                    <div class="col-md-6">
                        <?php echo Form::select('documentType', array('CI'=>'Cédula','RUC'=>'RUC','PPN'=> 'Pasaporte'), '', array('class' => 'form-control', 'id' => 'documentType')); ?>

                        <input type="hidden" name="serial_cdd" id="serial_cdd" value="" >
                    </div>
                </div>
                <div class="form-group">
                    <label for="document" class="col-md-6 control-label">* Documento:</label>
                    <div class="col-md-6">
                        <?php echo Form::text('document', '', array('class' => 'form-control', 'id' => 'document')); ?>

                    </div>
                </div>
                <div class="form-group">
                    <label for="txtPaymentName" class="col-md-6 control-label">* Nombre del titular de la tarjeta:</label>
                    <div class="col-md-6">
                        <?php echo Form::text('txtPaymentName', '', array('class' => 'form-control', 'id' => 'txtPaymentName')); ?>

                    </div>
                </div>
                <div class="form-group" id='divsurname'>
                    <label for="surname" class="col-md-6 control-label">* Apellido del titular de la tarjeta:</label>
                    <div class="col-md-6">
                        <?php echo Form::text('surname', '', array('class' => 'form-control', 'id' => 'surname')); ?>

                    </div>
                </div>
                <div class="form-group">
                    <label for="txtKinship" class="col-md-6 control-label">* Parentesco con estudiante(s):</label>
                    <div class="col-md-6">
                        <?php echo Form::select('txtKinship', array(''=>'Seleccione','Padre'=>'Padre','Madre'=>'Madre','Abuelo(a)'=>'Abuelo(a)','Tio(a)'=>'Tío(s)','Otro(a)'=>'Otro(a)'), '', array('class' => 'form-control', 'id' => 'txtKinship')); ?>

                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php
    $paymentButtons = PaymentButton::getCurrentPaymentButtonByPaymentConceptGroup($serialPcg, 'CURRENTPAYMENTS');
    ?>
    <div class='row text-muted '>
        <div class="col-md-offset-2 col-md-7">
            <p align="justify"><b>Para confirmar el pago en línea dar clic en el botón de pagos Placetopay/Payphone, luego acepte los términos y condiciones y finalmente dar clic en el botón Pagar.</b></p>  
            <?php foreach($paymentButtons as $paymentButton): ?>
            <?php echo $paymentButton->info_pbn; ?>

            <?php endforeach; ?>
        </div>
    </div>
    <div class="row">
        <div class="form-horizontal">
            <div class="col-md-offset-3 col-md-6">
                <table>
                    <tr>
                        <?php foreach($paymentButtons as $pbn): ?>
                        <td>
                            <div id="div_btn_<?php echo $pbn->serial_pbn; ?>">
                                <label for='radPayment_<?php echo $pbn->serial_pbn; ?>'>
                                    <input class="classbtnpay radPayment" type="radio" style="top:-2.55em" value="<?php echo $pbn->serial_gpb; ?>"
                                           id="radPayment_<?php echo $pbn->serial_gpb; ?>"
                                           name="serial_gpb"/>
                                    <img id="btnPayment_<?php echo $pbn->serial_gpb; ?>" width='150px'
                                         name="btnPayment_<?php echo $pbn->serial_gpb; ?>" 
                                         src="<?php echo $pbn->path_logo; ?>" style="cursor:pointer;"
                                         alt="Alignet"
                                         for="radPayment_<?php echo $pbn->serial_gpb; ?>"
                                         url="<?php echo $pbn->vpos_url_pbn; ?>" type_pbn="<?php echo $pbn->type_pbn; ?>"
                                         pathterms="<?php echo $pbn->path_terms_conditions; ?>"/> <br/>
                                    <!--información del botón-->
                                </label>
                            </div>
                            <br>
                            <div class="span-5 classterm"
                                 id="termsContainer_<?php echo $pbn->serial_pbn; ?>"
                                 style="display: none">
                                <!-- Se verifica si tiene path de terminos y condiciones-->
                                <?php if($pbn->path_terms_conditions): ?>
                                <input type="checkbox" name="aceptTerms_<?php echo $pbn->serial_pbn; ?>"
                                       id="aceptTerms_<?php echo $pbn->serial_pbn; ?>" class="aceptTerms"
                                       seleccionado="no">
                                <input type="hidden"
                                       id="chekValidator_<?php echo $pbn->serial_pbn; ?>"
                                       title="'Debe aceptar los t&eacute;rminos y condiciones'" value="">
                                <a id="termsDialogLauncher_<?php echo $pbn->serial_pbn; ?>" class="aceptTermsLink" path_terms_conditions='<?php echo $pbn->path_terms_conditions; ?>'
                                   style="cursor: pointer">T&eacute;rminos y Condiciones</a>
                                <?php endif; ?>
                                <div class="prepend-2 span-3 last line">
                                    <br>
                                    <input type="button" class="btn btn-primary btn-pay"
                                           name="btnVpos_<?php echo $pbn->serial_pbn; ?>"
                                           id="btnVpos_<?php echo $pbn->serial_pbn; ?>"
                                           value="Pagar"/>
                                </div>

                                <div id="termsButtonDialog_<?php echo $pbn->serial_pbn; ?>"
                                     style="display: none">
                                </div>
                            </div>
                        </td>
                        <td width="10%"></td>
                        <?php endforeach; ?>
                    </tr>
                </table>
            </div>
        </div>
    </div>

    <?php echo Form::close(); ?>

    <div class="col-md-7 col-md-offset-2">
        <div class="text-center">  
            <br/> 
            <div class="form-inline">
                <button type="button" id="btn-back" class="btn btn-primary"> << Atras</button>
            </div>
        </div>
    </div>
</div>
<?php echo HTML::script('js/paymentstm/currentpayments/loads/paymentButtons.js'); ?>