<div class="modal fade" id="modal-default-reembolso" style="display: none;">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">Nuevo Reembolso</h4>
            </div>
            {{ form_start(formreembolso, {'attr': {'class': 'form-stacked', 'id':'formreembolsosv' } }) }}

            <div class="modal-body">
                <div id="errormodal" class="alert alert-danger alert-dismissible" style="display: none;">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                    <p id="msgerror"></p>
                </div>
                <div class="row">
                    <div class="col-md-3 clearfix">
                        <div class="form-group">
                            {% if formprovider is defined %}
                                {{ form_label(formreembolso.provreembolso_identification) }}
                                <div class="contentsv input-group">
                                    {{ form_widget(formreembolso.provreembolso_identification) }}
                                    <span class="input-group-btn">
                                        <button type="button" class="btn btn-info btn-flat"
                                                onclick="buscarProvReembolso(this)">
                                            <i class="glyphicon glyphicon-search"></i>
                                        </button>
                                    </span>
                                </div>
                                {{ form_errors(formreembolso.provreembolso_identification) }}
                            {% else %}
                                {{ form_row(formreembolso.provreembolso_identification) }}
                            {% endif %}
                        </div>
                    </div>
                    <div class="col-md-6 clearfix">
                        <div class="form-group">
                            {% if formprovider is defined %}
                                {{ form_label(formreembolso.provreembolso_name) }}
                                <div class="input-group">
                                    {{ form_widget(formreembolso.provreembolso_name, {'attr': {'class': 'text-uppercase provreembolso-autocomplete-description'}}) }}
                                    <span class="input-group-btn">
                                      <button type="button" class="btn btn-success btn-flat" data-toggle="modal"
                                              data-target="#modal-provreembolso">
                                        <i class="glyphicon glyphicon-plus"></i>
                                      </button>
                                    </span>
                                </div>
                                {{ form_errors(formreembolso.provreembolso_name) }}
                            {% else %}
                                {{ form_row(formreembolso.provreembolso_name) }}
                            {% endif %}
                            <script>
                                $(function () {
                                    addReembolsoProveedorItemAutocomplete('{{ path('item_autocomplete_provreembolso') }}');
                                });
                            </script>
                        </div>
                    </div>
                    <div class="col-md-3 clearfix">
                        {{ form_label(formreembolso.providerTipo) }}
                        {{ form_widget(formreembolso.providerTipo) }}
                        {{ form_errors(formreembolso.providerTipo) }}
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-5 clearfix">
                        <div class="form-group">
                            {{ form_label(formreembolso.tipodocreembolso) }}
                            {{ form_widget(formreembolso.tipodocreembolso) }}
                            {{ form_errors(formreembolso.tipodocreembolso) }}
                        </div>
                    </div>
                    <div class="col-md-2 clearfix">
                        <div class="form-group">
                            {{ form_label(formreembolso.serieDocumento) }}
                            {{ form_widget(formreembolso.serieDocumento) }}
                            {{ form_errors(formreembolso.serieDocumento) }}
                        </div>
                    </div>
                    <div class="col-md-2 clearfix">
                        <div class="form-group">
                            {{ form_label(formreembolso.secuencialDoc) }}
                            {{ form_widget(formreembolso.secuencialDoc) }}
                            {{ form_errors(formreembolso.secuencialDoc) }}
                        </div>
                    </div>
                    <div class="col-md-3 clearfix">
                        <div class="form-group">
                            {{ form_label(formreembolso.emisionDocumento) }}
                            {{ form_widget(formreembolso.emisionDocumento) }}
                            {{ form_errors(formreembolso.emisionDocumento) }}
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-7 clearfix">
                        <div class="form-group">
                            {{ form_label(formreembolso.autoDocumento) }}
                            {{ form_widget(formreembolso.autoDocumento) }}
                            {{ form_errors(formreembolso.autoDocumento) }}
                        </div>
                    </div>
                    <div class="col-md-2 clearfix">
                        <div class="form-group">
                            {{ form_label(formreembolso.imponible) }}
                            {{ form_widget(formreembolso.imponible) }}
                            {{ form_errors(formreembolso.imponible) }}
                        </div>
                    </div>
                    <div class="col-md-2 clearfix">
                        <div class="form-group">
                            {{ form_label(formreembolso.taxes) }}
                            {{ form_widget(formreembolso.taxes) }}
                            {{ form_errors(formreembolso.taxes) }}
                            <script type="text/javascript">
                                /*$(function(){
                                  $('td.taxes select').select2({
                                      placeholder: {
                                          id: '1', // the value of the option
                                          text: '12'
                                      }
                                  });
                                });*/


                                $("select#{{ formreembolso.taxes.vars.id }} option[value='']").remove();
                            </script>
                        </div>
                    </div>
                </div>
                {{ form_rest(formreembolso) }}
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-default pull-left" data-dismiss="modal">&nbsp;Cancelar
                </button>
                <button id="reguardar" type="submit" class="btn btn-success"><i class="fa fa-save"></i>&nbsp;Guardar
                </button>
            </div>
            {{ form_end(formreembolso) }}
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>

<script>

    $(document).ready(function () {
        $body = $("body");
        var collectionHolder = $('#invoice-like-itemsreembolsos tbody');
        collectionHolder.data('index', 0);

        $('#formreembolsosv').submit(function (e) {
            e.preventDefault();
            //var collectionHolder = $('#invoice-like-itemsreembolsos tbody');
            var prototype = collectionHolder.attr('data-prototype');
            var index = collectionHolder.data('index');
            form = prototype.replace(/__name__/g, index);
            collectionHolder.append(form);
            collectionHolder.data('index', index + 1);

            $.ajax({
                url: '{{ path('provider_searchprovider') }}',

                data: {
                    term: $('input[name="reembolso[provreembolso_identification]"]').val()
                },
                error: function (ui) {
                    alert(ui.responseText);
                },
                async: true,
                beforeSend: function () {
                    $('div.modal').block({
                        message: '<h3><img src="{{ asset('loading.gif') }}" />&nbsp;&nbsp;Procesando...</h3>',
                        css: {border: '1px solid #a00'}
                    });
                },
                complete: function () {
                    $('div.modal').unblock();
                },
                success: function (ui) {
                    $('input[name$="[' + index + '][provreembolso_identification]"]').val($('input[name="reembolso[provreembolso_identification]"]').val());
                    $('input[name$="[' + index + '][provreembolso_name]"]').val($('input[name="reembolso[provreembolso_name]"]').val());
                    $('input[name$="[' + index + '][providerTipo]"]').val($('input[name="reembolso[providerTipo]"]').val());
                    $('input[name$="[' + index + '][tipodocreembolso]"]').val($('input[name="reembolso[tipodocreembolso]"]').val());
                    $('input[name$="[' + index + '][serieDocumento]"]').val($('input[name="reembolso[serieDocumento]"]').val());
                    $('input[name$="[' + index + '][secuencialDoc]"]').val($('input[name="reembolso[secuencialDoc]"]').val());
                    $('input[name$="[' + index + '][emisionDocumento]"]').val($('input[name="reembolso[emisionDocumento]"]').val());
                    $('input[name$="[' + index + '][autoDocumento]"]').val($('input[name="reembolso[autoDocumento]"]').val());
                    $('input[name$="[' + index + '][imponible]"]').val($('input[name="reembolso[imponible]"]').val());
                    $('input[name$="[' + index + '][taxes]"]').val($('input[name="reembolso[taxes]"]').val());

                    $('#modal-default-reembolso').modal('hide');
                    document.getElementById("formreembolsosv").reset();

                },
                type: 'GET'
            });

        });

    });

    function buscarProvReembolso(current) {

        var ubi = $(current).closest("div.contentsv");
        var identificacion = ubi.find("input[name$='[provreembolso_identification]']");
        var elemento = identificacion.attr("name");
        $.ajax({
            url: '{{ path('provider_searchprovider') }}',

            data: {
                term: identificacion.val()
            },
            error: function (ui) {
                alert(ui.responseText);
            },
            async: true,
            beforeSend: function () {
                //$body.addClass("loading");
                $('div.modal').block({
                    message: '<h3><img src="{{ asset('loading.gif') }}" />&nbsp;&nbsp;Buscando...</h3>',
                    css: {border: '1px solid #a00'}
                });
            },
            complete: function () {
                //$body.removeClass("loading");
                $('div.modal').unblock();
            },
            success: function (ui) {
                var nombre = elemento.replace('provreembolso_identification', 'provreembolso_name');
                $("input[name='" + nombre + "']").val(ui.item.name);
                identificacion.val(ui.item.identification);
                //identificacion.val(ui.item.name);
                //    $('input[name$="[shipping_address]"]').val(ui.item.shipping_address);

            },
            type: 'GET'
        });

    }

</script>
