<link href="<?= $assets ?>plugins/bootstrap-toggle/bootstrap-toggle.min.css" rel="stylesheet">
<script src="<?= $assets ?>plugins/bootstrap-toggle/bootstrap-toggle.min.js"></script>
<link rel="stylesheet" href="<?= $assets ?>dist/css/pages/customers.css">

<?php

// echo json_encode($customer);
// echo json_encode($customer['id']);


?>

<section class="content">
    <span class="modal-title" style="font-size: 22px; " id="titlename">Cliente - <?= ($customer['person_type'] == 1) ? $customer['cf1'] : $customer['cf2'] ?></span>

    <div class="row">
        <div class="col-xs-12">
            <div class="box" style="border-top: 2.5px solid #004785;">
                <div class="box-body">
                    <div class="col-lg-12">
                        <?php echo form_open("customers/edit/" . $customer['id']); ?>


                        <div class="row">
                            <input type="hidden" name="idclienteedit" value="<?php echo $customer['id']; ?>" id="idclienteedit">
                            <!-- Numero de documento / Consulta de DNI -->
                            <div class="col-md-4" id="div-cf1">
                                <div class="form-group">
                                    <label class="control-label" for="cf1"><?= $this->lang->line("ccf1"); ?></label>
                                    <div class="input-group">
                                        <?php if ($customer['person_type'] == 1 || $form_data['person'] == 1) : ?>
                                            <?= form_input('cf1', set_value('cf1', (isset($form_data['cf1']) ? $form_data['cf1'] : $customer['cf1'])), 'class="form-control" id="cf1" readonly maxlength="15"'); ?>
                                        <?php elseif ($customer['person_type'] == 2 || $form_data['person'] == 2) : ?>
                                            <?= form_input('cf1', set_value('cf1', (isset($form_data['cf2']) ? $form_data['cf2'] : $customer['cf2'])), 'class="form-control" id="cf1" maxlength="11" readonly'); ?>
                                        <?php endif; ?>

                                        <div class="input-group-btn">
                                            <div id="con-num-doc" class="btn btn-info" tabindex="0" data-toggle="tooltip" title="Consulta de numero de documento"><i class="fa fa-search"></i></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <!-- Radio button para Persona Natura y Juridica -->
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label class="control-label" for="type_persona"><?= $this->lang->line("type_person"); ?> *</label>
                                    <div class="radio-group">
                                        <div class="radio-inline">
                                            <input type="radio" <?= ($customer['person_type'] == 1 || (isset($form_data['person']) && $form_data['person'] == 2)) ? 'checked="true" disabled' : 'disabled'; ?> name="person" id="person-natural" value="1" class="radio-person-type mg">
                                            <label class="custom-control-label"><?= $this->lang->line("person1"); ?></label>
                                        </div>
                                        <div class="radio-inline">
                                            <input type="radio" <?= ($customer['person_type'] == 2 || (isset($form_data['person']) && $form_data['person'] == 2)) ? 'checked="true" disabled' : 'disabled'; ?> name="person" id="person-juridica" value="2" class="radio-person-type">
                                            <label class="custom-control-label"><?= $this->lang->line("person2"); ?></label>
                                        </div>
                                    </div>
                                    <!-- Agregar un campo oculto para almacenar el valor seleccionado -->
                                    <input type="hidden" id="input_hidden_person_type" name="person" value="<?= (isset($form_data['person']) ? $form_data['person'] : $customer['person_type']) ?>">
                                </div>
                            </div>

                            <div class="col-md-4" id="div-document_type">
                                <div class="form-group">
                                    <div class="form-group" >
                                    <?= lang('document_type', 'document_type'); ?> *
                                    <select name="document_type" id="document-type" class="form-control" readonly>
                                        <option value=""><?= lang("select") . " " . lang("document_type") ?></option> 
                                    <?php
                                        foreach ($document_type as $doc_type) { ?>
                                            <option value="<?= $doc_type->id ?>" <?=($doc_type->id == set_value("document_type",(isset($form_data['document_type'])?$form_data['document_type']:$customer['document_type_id'])))?'selected':''?>><?= $doc_type->document_type; ?></option> 
                                        <?php }?>
                                    </select>
                                </div></div>
                            </div>
                            
                            <div class="col-md-4">
                                <div class="form-group" id="div-cf2">
                                    <label class="control-label" for="cf2"><?= $this->lang->line("ccf2"); ?> </label>
                                    <div class="input-group">
                                        <?= form_input('cf2', set_value('cf2', (isset($form_data['cf2']) ? $form_data['cf2'] : $customer['cf2'])), 'class="form-control" id="cf2" maxlength="11" oninput="this.value = this.value.replace(/[^0-9.]/g, \'\').replace(/(\..*)\./g, \'$1\');"'); ?>
                                        <div class="input-group-btn">
                                            <div id="con-ruc" class="btn btn-info" tabindex="0" data-toggle="tooltip" title="Consulta de RUC"><i class="fa fa-search"></i></div>
                                        </div>
                                    </div>
                                    <div id="cargando" class="col-md-2" style="display:none;"><img style="width: 30px;height:auto;" src="https://pa1.narvii.com/6558/d6738388bae69543478eb78d0545bf0475ef05a6_hq.gif"></div>
                                </div>
                            </div>

                            <div class="col-md-12"></div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="control-label" id="nombre-persona" for="code"><?= $this->lang->line("name"); ?></label>
                                    <?= form_input('name', set_value('name', (isset($form_data['name']) ? $form_data['name'] : $customer['name'])), 'class="form-control" id="name" readonly'); ?>
                                </div>
                            </div>
                            <div class="col-md-6">

                                <div class="form-group">
                                    <?= lang('customers_type', 'customers_type'); ?>
                                    <?php
                                    $ctv[0] = lang("select") . " " . lang("customers_type");

                                    foreach ($customer_type as $cust_type) {
                                        $ctv[$cust_type['id']] = $cust_type['customers_type'];
                                    }
                                    ?>
                                    <?= form_dropdown('customer_type', $ctv, set_value("customers_type", (isset($form_data['customer_type']) ? $form_data['customer_type'] : $customer['customers_type_id'])), 'class="form-control tip select2" id="customer_type"  required="required" style="width:100%;"'); ?>
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label class="control-label" for="direccion"><?= $this->lang->line("address"); ?> </label>
                                    <?= form_input('direccion', set_value('direccion', (isset($form_data['direccion']) ? $form_data['direccion'] : $customer['direccion'])), 'class="form-control" id="cdireccion" maxlength="255"'); ?>
                                </div>
                            </div>
                           
                            <div class="col-md-6">
									<div class="form-group">
										<label class="control-label" for="phone"><?= $this->lang->line("phone_movil"); ?></label>
										<div class="input-group" style="width: 100%;">
											<select id="codigoPais" name="codigoPais" class="form-control" style="width: 20%; margin-right: 5px;">
												<option value="51"><img src="<?= $assets ?>images/peru.png"/>(PE) +51</option>
											</select>
											<?= form_input('phone', set_value('phone', (isset($form_data['phone']) ? $form_data['phone'] : preg_replace('/^51/', '',$customer['phone']))),  'class="form-control" style="width: 70%;" oninput="this.value = this.value.replace(/[^0-9.]/g, \'\').replace(/(\..*)\./g, \'$1\');" id="phone" maxlength="15"'); ?>
										</div>
									</div>
								</div>
                            <div class="col-md-6">

                                <div class="form-group">
                                    <label class="control-label" for="email_address"><?= $this->lang->line("email_address"); ?></label>
                                    <?= form_input('email', set_value('email', (isset($form_data['email']) ? $form_data['email'] : $customer['email'])), 'class="form-control" id="email_address" maxlength="100"'); ?>
                                </div>
                            </div>
                            <?php if ($this->session->userdata('retencion') == 1) { ?>

                                <!--********START TRJ143 - DANIEL PADILLA - 20/12/2022*****************-->
                                <div class="col-md-12">
                                    <div class="form-group row">
                                        <label class="col-sm-1 col-form-label">
                                            <?= lang('is_retainer_text', 'is_retainer_text'); ?>
                                        </label>
                                        <div class="col-sm-1">
                                            <input type="checkbox" <?= $customer['is_retainer'] ? 'checked' : '' ?> id="is_retainer_checkbox" name="is_retainer_checkbox" data-offstyle="danger" data-style="slow" data-onstyle="success" data-toggle="toggle" data-on="Si" data-off="No" data-size="mini">
                                            <input type="hidden" name="is_retainer" id="is_retainer" value="<?= (isset($form_data['is_retainer']) ? $form_data['is_retainer'] : $customer['is_retainer']); ?>">
                                        </div>
                                        <?php
                                        $styleForRetainer = $customer['is_retainer'] ? '' : 'style="display:none;"'
                                        ?>

                                        <label class="col-sm-2 col-form-label retainer_percentage pt-1" <?= $styleForRetainer ?>>
                                            <?= lang('retainer_percentage', 'retainer_percentage'); ?>
                                        </label>
                                        <div class="col-sm-1 retainer_percentage" <?= $styleForRetainer ?>>
                                            <input class="form-control" type='text' id='retainer_percentage' name='retainer_percentage' for='retainer_percentage' value="<?= $customer['retainer_percentage']; ?>" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');" maxlength="2" max="99" />
                                        </div>
                                    </div>
                                </div>
                                <!--********END TRJ143 - DANIEL PADILLA - 20/12/2022*****************-->
                            <?php } ?>

                            <div class="col-md-12">

                                <?php switch ($this->session->userdata('negocio')) {
                                    case 0: ?>
                                    <?php break;
                                    case 1: ?>
                                        <div class="form-group">
                                            <label class="control-label" for="custom_field_1"><?= $this->lang->line("digemid"); ?></label>
                                            <?= form_input('custom_field_1', set_value("custom_field_1", $customer->custom_field_1), 'class="form-control" id="custom_field_1"'); ?>
                                        </div>
                                <?php break;
                                } ?>
                                <div class="row">
                                    <div class="col-md-12 text-right">
                                        <?php echo form_submit('edit_customer', $this->lang->line("accept"), 'class="btn btn-primary"'); ?>
                                        <a id="btn-cancel" class="btn btn-danger"><?= lang("cancel") ?></a>
                                    </div>
                                </div>
                            </div>
                            <?php echo form_close(); ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>


</section>
<script src="<?= $assets ?>dist/js/pages/Customers.js"></script>

<script>
    var personValue = <?php echo json_encode(isset($form_data['person']) ? $form_data['person'] : $customer['person']); ?>;

    if (personValue == 1) {

        $("#person-natural").prop("checked", true);

    }
    if (personValue == 2) {

        $("#person-juridica").prop("checked", true);

    }
</script>


<script language="javascript">
    $(document).ready(function() {

        $('form').keypress(function(e) {
            if (e == 13) {
                return false;
            }
        });

        $('input').keypress(function(e) {
            if (e.which == 13) {
                return false;
            }
        });
        $('#document-type').click(function(){
            $('#document-type option').hide();
        });

    });
</script>

<!--********START TRJ143 - DANIEL PADILLA - 20/12/2022*****************-->
<script language="javascript">
    $(document).ready(function() {
        $("#is_retainer_checkbox").on('change', function() {
            var isChecked = document.getElementById('is_retainer_checkbox').checked;
            if (isChecked) {
                $('#is_retainer').val(1);
                $('.retainer_percentage').show(600);
            } else {
                $('#is_retainer').val(0);
                $('.retainer_percentage').hide(600);
            }
        });
    });
</script>




<!--********END TRJ143 - DANIEL PADILLA - 20/12/2022*****************-->