@extends('layouts.app')
@section('styles')

@endsection

@section('content')

    <div class="container" >
        <div class="row" style="width:100%;margin-right: 15%">
            <div class="col-md-10 col-md-offset-1">
                <div class="panel panel-default" style="width: 140%; padding: 3%; ">
                    <div >
                        <h2>Resultados de búsqueda:</h2>
                        <br>


                <div class="form-group mx-sm-3 mb-2" style="clear:both">
                    <label >Linea de Produccion:</label>
                        &nbsp;&nbsp;<select class="js-example-placeholder-single line_prod" id="line_prod" name="line_prod" style="width:250px !important;border-color: #dbd9d9;">
                        <option  value=""></option>
                        @foreach ($pipelines as $td)
                            <option  <?php  /* {{$line_prod_filter}} {{($line_prod_filter==$td->id)?"selected":""}} */ ?>         value="{{$td->id}}">{{ $td->name }}</option>
                        @endforeach
                      
                    </select>
                </div>




                <div class="form-group mx-sm-3 mb-2" style="margin-left: 34px !important;">
                    <label >Estado:</label>
                        &nbsp;&nbsp;<select class="js-example-placeholder-single select_status" readonly id="select_status" name="select_status[]" style="width:250px !important;border-color: #dbd9d9;" multiple="multiple">

                       
                        @foreach ($status as $st)
                            <option  value="{{$st->id}}">{{ $st->name }}</option>
                        @endforeach
                      
                    </select>
                </div>


                <div class="form-group mx-sm-3 mb-2" style="margin-left: 34px !important;">
                    <label >Unidad:</label>
                        &nbsp;&nbsp;<select class="js-example-placeholder-single select_unit" id="select_unit" name="select_unit[]" style="width:250px !important;border-color: #dbd9d9;" multiple="multiple">

                        @foreach ($units as $uni)
                            <option  value="{{$uni->id}}">{{ $uni->name }}</option>
                        @endforeach
                      
                    </select>
                </div>


                <div class="form-group mx-sm-3 mb-2" style="clear:both">
                    <label >Tarea creada por:</label>
                        &nbsp;&nbsp;<select class="js-example-placeholder-single user_auth" id="user_auth" name="user_auth" style="width:250px !important;border-color: #dbd9d9;">
                        <option  value=""></option>
                        @foreach ($users as $us)
                            <option  <?php /*  {{$user_auth_filter}} {{($user_auth_filter==$us->id)?"selected":""}}  */ ?>        value="{{$us->id}}">{{ $us->name." ".$us->surname }}</option>
                        @endforeach
                      
                    </select>
                </div>

                <div class="form-group mx-sm-3 mb-2" style="clear:both">
                    <label >Comentarios creado por:</label>
                        &nbsp;&nbsp;<select class="js-example-placeholder-single user_comment" id="user_comment" name="user_comment" style="width:250px !important;border-color: #dbd9d9;">
                        <option  value=""></option>
                        @foreach ($users as $us)
                            <option  <?php /* {{$user_comment_filter}} {{($user_comment_filter==$us->id)?"selected":""}}  */ ?>        value="{{$us->id}}">{{ $us->name." ".$us->surname }}</option>
                        @endforeach
                      
                    </select>
                </div>

                <div class="col-md-12">
                    <div class="form-group">
                        <label>Tareas Desde:</label>
                        <input type="text" name="fecha_cre" id="fecha_cre" readonly class="fecha_cre" class="form-control" placeholder="AAAA/MM/DD" value="">
                    </div>
                </div>
                <div class="col-md-12" style="margin-bottom:20px">
                    <div class="form-group">
                        <label>Tareas Hasta:</label>
                        <input type="text" name="fecha_end" id="fecha_end" readonly class="fecha_end" class="form-control" placeholder="AAAA/MM/DD" value="">
                    </div>
                </div>


                    <p style="margin-bottom:55px"><a href="#" class="btn btn-primary apply_filter" role="button">Aplicar Filtros</a>
                    <a href="#" class="btn btn-default reset_filter" role="button" style="margin-left: 5px;">Limpiar Filtros</a> </p>




                        @if ($user->isApplicant() == true) {{---------Es externo-----------------}}

                            @if(!empty($task))

                                <h3><i>Tareas</i></h3>

                                @foreach($task as $t)

                                    @if(!$t['descripcion'])
                                        <br>
                                        <a href="{{ URL::to('/') }}/otask/{{ $t['id']}}/show">
                                            {!!$t['title']!!} <b>{{$t['pipeline']}}</b></a>
                                    <br>
                                    @else
                                        <br>
                                        <a href="{{ URL::to('/') }}/otask/{{ $t['id']}}/show">
                                            {{$app['utils']->cutText($t['descripcion']) }} <b>{{$t['pipeline']}}</b> </a>
                                        <br>
                                    @endif
                                @endforeach

                            @endif
                            @if(!empty($attach_task))
                                <h3><i>Archivos en Tarea</i></h3>

                                @foreach($attach_task as $at)
                                    <br>
                                    <a href="{{ URL::to('/') }}/otask/{{ $at['id']}}/show"></a>
                                        {!!$at['name_file']!!}</a>
                                    <br>

                                @endforeach

                            @endif

                            @if(!empty($array_comment))
                                <h3><i>Comentarios en tareas</i></h3>

                                @foreach($array_comment as $comment)
                                    @if($comment['content_type'] == 'App\Task')

                                        <br>
                                        <a href="{{ URL::to('/') }}/otask/{{$comment['content_id']}}/show">
                                            {{ $app['utils']->cutText( $comment['comment']) }}.</a>
                                        <br>
                                    @endif
                                @endforeach

                            @endif

                        @else {{--- es interno---}}

                            @if(!empty($task))

                            <h3><i>Tareas</i></h3>

                                @foreach($task as $t)

                                    @if(!$t['descripcion'])
                                        <br>
                                        <a href="{{ URL::to('/') }}/tasks/{{ $t['id']}}/unit/{{$t['unit_id']}}/show">
                                            {!!$t['title']!!}  <b>{{$t['pipeline']}}</b></a>

                                        <br>
                                    @else
                                        <br>
                                        <a href="{{ URL::to('/') }}/tasks/{{ $t['id']}}/unit/{{$t['unit_id']}}/show">
                                            {{$app['utils']->cutText($t['descripcion']) }}  <b>{{$t['pipeline']}}</b></a>
                                        <br>
                                    @endif
                                @endforeach

                            @endif

                            @if(!empty($subtask))

                                <h3><i>Subtareas</i></h3>
                                @foreach($subtask as $st)

                                    <br>
                                    <a href="{{ URL::to('/') }}/subtasks/{{ $st['id']}}/show">
                                        {!!$st['title']!!} ... {{$app['utils']->cutText($st['description']) }}</a>
                                    <br>

                                @endforeach

                            @endif

                            @if(!empty($attach_task))
                                <h3><i>Archivos en Tarea</i></h3>

                                @foreach($attach_task as $at)
                                    <br>

                                    <a href="{{ URL::to('/') }}/tasks/{{ $at['id']}}/unit/{{$at['unit_id']}}/show">
                                        {!!$at['name_file']!!}</a>
                                    <br>

                                @endforeach

                            @endif

                            @if(!empty($attach_subtask))
                                <h3><i>Archivos en Subtareas</i></h3>

                                @foreach($attach_subtask as $ast)
                                    <br>

                                    <a href="{{ URL::to('/') }}/subtasks/{{ $ast['id']}}/show">
                                        {!!$ast['name_file']!!}</a>
                                    <br>
                                @endforeach

                            @endif

                            @if(!empty($array_comment))
                                <h3><i>Comentarios en tareas</i></h3>

                                @foreach($array_comment as $comment)
                                    @if($comment['content_type'] == 'App\Task')
                                    <?php /* buscar unidad de tarea o subtarea */ ?>
                                    <br>
                                    <a href="{{ URL::to('/') }}/tasks/{{$comment['content_id']}}/unit/{{$comment['unit_id']}}/show">
                                        {{ $app['utils']->cutText( $comment['comment']) }}.</a><br>
                                    @endif
                                @endforeach

                                @if($user->canViewTasks())
                                    <h3><i>Comentarios en subtareas</i></h3>
                                    @forelse($array_comment as $comment)

                                        @if($comment['content_type'] == 'App\Subtask')

                                            @if($user->canViewSubtask($comment['content_id']))

                                                <br>
                                                <a href="{{ URL::to('/') }}/subtasks/{{ $comment['content_id']}}/show">
                                                    {{ $app['utils']->cutText( $comment['comment']) }}.</a><br>


                                             @endif


                                        @endif
                                    @empty
                                        <p>No se encuentran resultados.</p>

                                    @endforelse
                                @endif
                            @endif
                        @endif

                    @if(empty($attach_task) && empty($task) && empty($array_comment) && empty($attach_subtask) && empty($subtask))

                            No se encontraron resultados .
                    @endif
                        <br>
                    </div>
                </div>
            </div>
        </div>
    </div>

@endsection


@section('scripts')


     <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
        <script>





            var disableddates = ["12-25-2018","1-1-2019","5-1-2018","5-25-2018","7-9-2018"];//mes,dia,año ,acà se agrega días feriados inamovibles


            $.datepicker.regional['es'] = {
                closeText: 'Cerrar',
                prevText: '<Ant',
                nextText: 'Sig>',
                currentText: 'Hoy',
                monthNames: ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'],
                monthNamesShort: ['Ene','Feb','Mar','Abr', 'May','Jun','Jul','Ago','Sep', 'Oct','Nov','Dic'],
                dayNames: ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'],
                dayNamesShort: ['Dom','Lun','Mar','Mié','Juv','Vie','Sáb'],
                dayNamesMin: ['Do','Lu','Ma','Mi','Ju','Vi','Sá'],
                weekHeader: 'Sm',
                dateFormat: 'yy-mm-dd',
            };
            $.datepicker.setDefaults($.datepicker.regional['es']);




  


        </script>


<script>

setTimeout(function(){ 

$("document").ready(function(){

    $(".apply_filter").on("click",function(){

        $(".button_form_principal").trigger("click");

    });



    $(".reset_filter").on("click",function(){


        $(".line_prod").val('').trigger('change');

        $(".select_status").val('').trigger('change');

        $(".select_unit").val('').trigger('change');

        $(".user_auth").val('').trigger('change');

        $(".user_comment").val('').trigger('change');

  //     $(".user_auth option[value='']").attr("selected", true);

   //     $(".user_comment option[value='']").attr("selected", true);

        $('#fecha_cre').val("");

        $('#fecha_end').val("");

    });


    $('#fecha_cre').datepicker();
    $('#fecha_end').datepicker();

    $('#fecha_cre').val("{{$fecha_cre_filter}}");
    $('#fecha_end').val("{{$fecha_end_filter}}");

    $('#select_status').select2({
    placeholder: 'Seleccione un estado'
    });

    $('#select_unit').select2({
    placeholder: 'Seleccione un unidad'
    });


    $('#user_auth').select2({
    placeholder: 'Seleccione un usuario'
    });

    $('#user_comment').select2({
    placeholder: 'Seleccione un usuario'
    });


    $('#line_prod').select2({
    placeholder: 'Seleccione un linea de producción'
    });




<?php  if(!empty($select_status_filter)){ ?>

    $("#select_status").val(<?php echo json_encode($select_status_filter)?>).trigger('change');

<?php  } ?>

<?php  if(!empty($select_unit_filter)){ ?>

    $("#select_unit").val(<?php echo json_encode($select_unit_filter)?>).trigger('change');

<?php  } ?>


/// user_auth_filter   user_comment_filter

<?php  if(!empty($user_auth_filter)){ ?>

    $("#user_auth").val(<?php echo json_encode($user_auth_filter)?>).trigger('change');

<?php  } ?>

<?php  if(!empty($user_comment_filter)){ ?>

    $("#user_comment").val(<?php echo json_encode($user_comment_filter)?>).trigger('change');

<?php  } ?>


<?php  if(!empty($line_prod_filter)){ ?>

    $("#line_prod").val(<?php echo json_encode($line_prod_filter)?>).trigger('change');

<?php  } ?>



});


 }, 1000);

</script>

@endsection