@extends('layout_forms')
@section('title', 'Asignar Propietario')
@section('style')
<style type="text/css" media="screen">
    .chosen-container{
        width: 100% !important;
    }

    .chosen-container-single .chosen-search input[type="text"]{
        width: 90%;
    }
</style>
@stop
@section('content')
<div class="col-sm-12 col-md-10 col-lg-10 col-xl-10">
    <h3 class="text-center titulo">Propietario Predio</h3>
    <div class="form-group row sin-salto">
        <div class="col-sm-2 col-md-2 col-lg-1 col-xl-1 superior">
            <a class="btn-sm btn-primary cursor" href="{{ route('infoPredio', encrypt($predio->id_unico)) }}" title="Volver"><i class="fa fa-arrow-left"></i></a>
        </div>
        <div class="col-sm-9 col-md-9 col-lg-10 col-xl-10">
            <h6 class="text-center marca-pagina">{{ "Predio: ".ucwords(mb_strtolower(empty($predio->nombre)?$predio->codigo_catastral:$predio->nombre))}}</h6>
        </div>
    </div>
    <div class="col-12 col-sm-12 col-md-12 col-lg-12 col-xl-12 superior2 tamaño-letra">
        <table class="table table-striped table-condensed table-bordered" id="table" data-toggle="dataTable" data-form="deleteForm">
            <thead>
                <tr>
                    <th width="66%">Tercero</th>
                    <th width="10%">Propietario?</th>
                    <th width="10%">Orden</th>
                    <th width="15%">%</th>
                    <th width="7%"></th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <form id="form-table" method="post" accept-charset="utf-8" action="{{ route('GuardarPropietarioPredio') }}" novalidate>
                        <input type="hidden" name="txtPredio" value="{{ encrypt($predio->id_unico) }}">
                        {{ csrf_field() }}
                        <td>
                            <select id="sltTercero" name="sltTercero" class="form-control select" title="Seleccion un tercero" required="" style="width: 100%">
                                <option value="">Tercero</option>
                                @foreach($terceros as $row)
                                 <option value="{{ $row->id_unico }}">{{ $row->nombres." ".$row->numero." ".$row->direccion  }}</option>
                                @endforeach
                            </select>
                        </td>
                        <td>
                            <div class="form-check form-check-inline">
                                <label for="optP1" id="lblOpt1" class="form-check-label label-forma text-right">
                                    <input class="form-check-input" type="radio" name="optPropietario" id="optP1" value="0" required=""> SI
                                </label>
                            </div>
                            <div class="form-check form-check-inline">
                                <label for="optP2" id="lblOpt2" class="form-check-label label-forma text-right">
                                    <input class="form-check-input" type="radio" name="optPropietario" id="optP2" value="1"> NO
                                </label>
                            </div>
                        </td>
                         <td>
                            <input class="form-control tamaño-letra" type="text" id="txtOrden" name="txtOrden" style="width: 80%" placeholder="Orden">
                        </td>
                        <td>
                            <input class="form-control tamaño-letra" type="text" id="txtParticipacion" name="txtParticipacion" style="width: 80%" placeholder="%">
                        </td>
                        <td>
                            <button type="submit" id="submit" class="btn-sin icons cursor fa fa-save"></button>
                        </td>
                    </form>
                </tr>
                @foreach($predios as $row)
                    <tr class="tamaño-letra">
                        <td>{{ mb_strtoupper($row->tercero)." ".mb_strtoupper($row->doc)}}</td>
                        <td>
                            @if($row->propietario == 0)
                                {{ "SI" }}
                            @else
                                {{ "NO" }}
                            @endif
                        </td>
                         <td>{{ $row->orden }}</td>
                        <td>{{ $row->porcentaje }}</td>
                        <td>
                            <a href="" class="fa fa-pencil icons" data-toggle="modal" data-target="#modalUpdate" data-pro="{{ $row->propietario }}" data-por="{{ $row->porcentaje }}" data-pre="{{ $predio->id_unico }}" data-cer="{{ $row->id_tercero }}" data-or="{{ $row->orden }}"></a>
                            {!! Form::model($predio, ['method' => 'post', 'route' => ['EliminarPropietarioPredio', encrypt($predio->id_unico), encrypt($row->id_tercero)], 'class' => 'form-tabla form-delete']) !!}
                            {!! Form::button(trans(''), ['class' => 'fa fa-trash icons btn-sin cursor', 'type' => 'submit']) !!}
                            {!! Form::hidden('tercero', encrypt($row->id_tercero)) !!}
                            {!! Form::hidden('predio', encrypt($predio->id_unico)) !!}
                            {!! Form::close() !!}
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</div>
@stop
@include('propietarioPredio.modalUpdate')
@include('modal-confirm')
@section('script')
<script>
    $('#table').on('click', '.form-delete', function(e){
        e.preventDefault();
        var $form=$(this);
        $('#confirm').modal({ backdrop: 'static', keyboard: false })
            .on('click', '#delete-btn', function(){
                $form.submit();
            });
    });

    $('#modalUpdate').on('show.bs.modal', function (event) {
        var btn = $(event.relatedTarget);
        var por = btn.data('por');
        var pro = btn.data('pro');
        var cer = btn.data('cer');
        var pre = btn.data('pre');
        var or =  btn.data('or');
        var modal = $(this);

        modal.find('.modal-body #txtPart').val(por);
        modal.find('.modal-body #txtCer').val(cer);
        modal.find('.modal-body #txtP').val(pre);
        modal.find('.modal-body #txtOrden').val(or);

        if(pro == 0){
            $("#optPro1").attr('checked', true);
        }else{
            $("#optPro2").attr('checked', true);
        }
    });

    $("#form-table").on('submit', function(e){
        var tercero = $("#sltTercero").val();
        var prop    = $("input[name='optPropietario']").val();
        var porce   = $("#txtParticipacion").val();

        var x = 0;

        if(tercero.length > 0){
            x+= x+1;
            $("#sltTercero_chosen").removeClass('borde-error');
        }else{
            e.preventDefault();
            $("#sltTercero_chosen").addClass('borde-error');
        }

        if($("input[name='optPropietario']").is(':checked')){
            x+= x+1;
            $("#lblOpt1").removeClass("text-danger");
            $("#lblOpt2").removeClass("text-danger");
        }else{
            e.preventDefault();
            $("#lblOpt1").addClass("text-danger");
            $("#lblOpt2").addClass("text-danger");

        }

        if(porce.length > 0){
            x+= x+1;
            $("#txtParticipacion").removeClass('is-invalid');
        }else{
            e.preventDefault();
            $("#txtParticipacion").addClass('is-invalid').removeClass('is-valid');
        }
    });
</script>
@stop