
@extends('layouts.client')

<style>
    /*.taskdata {*/
        /*margin-top: 5%;*/
        /*width: 90%;*/
        /*height: 7%;*/

    /*}*/
    .containerFr {

        width: 80%;
        height: auto;
        float: left;
        padding: 3%;
    }


    .taskdata {
        margin-top: 5%;
        margin-left: 3%;

    }
    .panel-heading{
        margin-bottom: 2%;
    }


    .selecttaskdata{
        margin-left: 2%;
    }
    .contenedor-BA{
        padding: 1%;
        margin-top: 2%;

    }

    .BA-form {

        height: auto;

    }
    .form-groupBA{
        width: 80%;
        float: left;
        margin: 1%;
    }
    #bt-selectTaskdata{
        /*float: right;*/
    }
</style>

@section('content')
    <div class="container home">
        <div class="row" style="width:100%;">
            <div class="col-md-10 col-md-offset-1">
                <!--?= $page ?-->
                @if(session()->has('message'))
                    <div class="alert alert-success alert-dismissable">
                        <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>

                        {{ session()->get('message') }}
                    </div>
                @endif
                @if(session()->has('error'))
                    <div class="alert alert-danger alert-dismissable">
                        <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>

                        {{ session()->get('error') }}
                    </div>
                @endif
                @if ($errors->any())
                    <div class="alert alert-danger">
                        <ul>
                            @foreach ($errors->all() as $error)
                                <li>{{ $error }}</li>
                            @endforeach
                        </ul>
                    </div>
                @endif
                {{--@if(isset($error))--}}

                    {{--<div class="alert alert-danger alert-dismissable ">--}}
                        {{--<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>--}}

                        {{--{!!  $error !!}--}}
                    {{--</div>--}}

                {{--@endif--}}


            @if (is_countable($draft) && $user->isApplicant())
                        <span style="background:#f00;color:#fff;padding:4px;border-radius:4px;font-size:18px">Tienes {{ count($draft) }} tareas en Borrador para revisar</span>
                    @endif
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            Panel de {{ $user->name }} {{ $user->surname }}
                            @if($user->hasRole('teamleader') || $user->hasRole('administrator'))
                                <a class="btn btn-default" href="{{ URL::to('/') }}/task/create-selector">Crear tarea</a>
                            @elseif($user->hasRole('supervisor') )
                                <a class="btn btn-default" href="{{ URL::to('/') }}/tasks/teamLeader">Crear tarea</a>
                            @endif
                        </div>

                        <div class="panel-body">
                            <div class="panel-heading">
                            @foreach($status as $st)
                                    <a class="@if ($active == $st->id) btn btn-default @else btn btn-primary  @endif"  href="{{ URL::to('/') }}/home/{{ $st->id }}" >{{ $st->name }}
                                    </a>
                            @endforeach

                                @if(isset($taskdata))

                                    <form class="form-inline taskdata" action="{{URL::to('/') }}/home/select-taskdata" method="post">
                                        <input type="hidden" name="_token" value="{{ csrf_token() }}"/>
                                        <input type="hidden" name="view" value="home"/>
                                        <div class=" mx-sm-3 mb-2 ">
                                            <label for="inputaskdata" class="sr-only">Tarea</label>
                                            <select class="js-example-placeholder-single" id="selcetTaskdata1" name="taskdata_id">

                                                <option></option>
                                                @foreach ($taskdata as $td)
                                                    <option  value="{{$td->id}}">{{ $app['utils']->cutText( $td->title,40) }}</option>
                                                @endforeach

                                            </select>

                                        <button type="submit" id="bt-selectTaskdata" class="btn btn-secundary mb-2">seleccionar</button>
                                        </div>
                                    </form>
                                    <div class="contenedor-BA">
                                    <p class="selecttaskdata">
                                        <a class="text-info busquedaAvanzada" data-toggle="collapse" href="#collapseExampleBA" role="button" aria-expanded="false" aria-controls="collapseExampleBA">
                                            <i>Búsqueda avanzada</i>
                                        </a>
                                    </p>
                                    <span class="border border-info">
                                        <div class="collapse border" id="collapseExampleBA" >
                                            <nav class="navbar navbar-default">
                                                <div class="container-fluid">
                                                <!-- Brand and toggle get grouped for better mobile display -->
                                                    <div class="navbar-header">
                                                        <form class="form-inline BA-form" action="{{URL::to('/') }}/home/select-taskdata" method="post">
                                                            <div class="containerFr">
                                                                <input type="hidden" name="_token" value="{{ csrf_token() }}"/>

                                                                <input type="hidden" name="view" value="home"/>

                                                            <div class="form-groupBA">
                                                                <label for="exampleFormControlSelect1">Tareas :</label>
                                                                <select class="form-control" id="selectTaskdata2" name="taskdata_id">
                                                                    <option></option>
                                                                    @foreach ($taskdata as $td)
                                                                        <option  value="{{$td->id}}">{{ $app['utils']->cutText( $td->title,40) }}</option>
                                                                    @endforeach
                                                                </select>
                                                            </div>

                                                            <div class="form-groupBA">
                                                                <label for="exampleFormControlSelect1">Perfiles :</label>
                                                                <select class="form-control" id="selectprofile" name="id_profile">
                                                                    <option></option>
                                                                    @foreach ($profiles as $pf)
                                                                        <option  value="{{$pf->id}}">{{ $app['utils']->cutText( $pf->area,40) }}</option>
                                                                    @endforeach
                                                                </select>
                                                            </div>
                                                            <div class="form-groupBA">
                                                                <label for="exampleFormControlSelect1">Estado :</label>
                                                                <select class="form-control" id="selectstatu" name="id_statu">
                                                                    <option></option>
                                                                    @foreach ($status as $st)
                                                                        <option  value="{{$st->id}}">{{ $st->name }}</option>
                                                                    @endforeach
                                                                </select>
                                                            </div>

                                                            <div class="form-groupBA">
                                                            <button type="submit" class="btn btn-secundary mb-2 ">Buscar</button>
                                                            </div>
                                                        </div>
                                                    </form>
                                                    {{----}}
                                            </div><!-- /.navbar-collapse -->
                                          </div><!-- /.container-fluid -->
                                        </nav>
                                    </div>
                                </span>
                                </div>
                            @endif
                            </div>

                            <?php $currentStatus = $active; ?>
                            @if(is_countable($taskdata))

                            <?php $info=1; ?>
                                @foreach ($taskdata as $td)
                                    {{--si el usuario esta relacionado con el user_profile de la tarea en la tabla profile_user  o el usuario es cliente externo entonces muestra las tareas --}}
                                    @if($td->userProfileInTasdata()|| !$user->isApplicant())

                                           {{-- @if((integer)$active == $task->status_id)--}}
    {{--                                    {{dd(count($td->tasksRel))}}--}}
                                    <h4 class="accordion-toggle">
                                        <table class="table">
                                            <tr id="taskdata-{{ $td->id }}">
                                                <td>{{ $td->title }}</td>
                                            </tr>
                                        </table>
                                    </h4>

                                    <div class="accordion-content">
                                        <table id="table_<?php echo $info;?>" class="table large-only">
                                            <tr>
                                                <th>Líneas de producción</th>
                                                <th>Unidad</th>
                                                <th>Fecha de ingreso</th>
                                                <th>Estado</th>
                                                <th>Acciones</th>
                                                @if($user->hasRole('teamleader') || $user->hasRole('supervisor') || $user->hasRole('administrator'))
                                                <th>Pipeline</th>
                                                @endif

                                            </tr>

                                            @php($contPipeline = count($td->tasksRel))

                                            @foreach($td->tasksRel as $key => $task)
                                                    @if($contPipeline > 1)
                                                        @if(\Auth::user()->isApplicant())
                                                            @if($task->pipeline->first()->system == 1)
                                                                @continue
                                                            @endif
                                                        @endif
                                                    @endif
                                            <tr>

                                                <td>{{ $task->pipeline->first()->name or ' '}}</td>
                                                <td>
                                                    @if($task->unit_id == null )
                                                    @else
                                                        @if($task->unit_id != 2 )
                                                        {{ $units[$task->unit_id] }}
                                                            @endif
                                                    @endif
                                                </td>

                                                <td> {{ date('d-m-Y H:i', strtotime(str_replace('/', '-', $task->created_at))) }}</td>
                                                <td>
                                                    @if($task->unit_id == null )
                                                       <span style="background:#f00;color:#fff;padding:4px;border-radius:4px;font-size:12px;">Borrador</span>
                                                    @else
                                                        {{ $task->status->name}}
                                                    @endif
                                                </td>
                                                    {{----***Si el usuario es administrador,supervisor ó teamleader**-----}}
                                                    @if($user->hasRole('administrator') ||$user->hasRole('supervisor')||  $user->hasRole('teamleader') )

                                                        <td>
                                                        @if($task->unit_id == null )
                                                            <div style="display: flex">

                                                                <form style="margin-bottom:0 ;margin-right: 15px;width:27%;" action="{{ URL::to('/') }}/forms/{{$task->type }}/{{$task->id}}/edit">
                                                                    <input class="btn btn-primary btn-xs" type="submit" value="Editar">

                                                                </form >
                                                                <form style="margin-bottom:0;width:27%;" onclick="return confirm('Desea eliminar?')" action="{{ URL::to('/') }}/forms/{{$task->type }}/delete" method="post">
                                                                    <input type="hidden" name="_token" value="{{ csrf_token() }}">

                                                                    <input type="hidden" name="id" value="{{ $task->id }}">
                                                                    <input class="btn btn-primary btn-xs" type="submit" value="Borrar">
                                                                </form>

                                                                    {{--<form  style="margin-bottom:0;width:33%;"action="{{ URL::to('/') }}/task/publish-draft" method="post">--}}
                                                                    {{--<input type="hidden" name="_token" value="{{ csrf_token() }}">--}}
                                                                    {{--<input type="hidden" name="id" value="{{ $task->id }}">--}}

                                                                    {{--<input  class="btn btn-primary btn-xs"type="submit" value="Publicar" />--}}

                                                                    {{--</form>--}}
                                                        </div>
                                                        @else
                                                                <a href="{{URL::to('/')}}/tasks/{{ $task->id }}/unit/{{ $task->unit_id }}/show"><i class="fa fa-pencil-square-o"></i></a>
                                                        @endif

                                        </td>
                                        <td>
                                            <ul class="pipeline-timeline">

                                                @if($task->unit_id)
                                                    @if($task->pipeline->first() != null)


                                                    @foreach($task->pipeline->first()->unitsByRevision($task->pipelineRevision) as $u)
                                                            <?php
                                                            $circle = "fa-circle-o";
                                                            $active = '';
                                                            if ($u->id == $task->unit->id) {
                                                                $active = 'active';
                                                                $circle = "fa-circle";
                                                            }
                                                            ?>
                                                        <li data-unit="{{ $u->name }}" data-toggle="tooltip" title="{{ $u->name }}" class="{{ $active }}">
                                                            <a href="{{ URL::to('/') }}/tasks/{{ $task->id }}/unit/{{ $u->id }}/show">
                                                                <i style="color:{{ $u->color }};" class="fa {{ $circle }}" aria-hidden="true"></i>
                                                            </a>
                                                        </li>
                                                    @endforeach
                                                        @endif
                                                @endif
                                            </ul>
                                        </td>

                                @else {{----***entonces es clientes  => ***----}}

                                        <td>
                                            @if($task->unit_id == null )
                                                <div style="display: flex">

                                                    <form style="margin-bottom:0 ;margin-right: 15px;width:27%;" action="{{ URL::to('/') }}/forms/{{$task->type }}/{{$task->id}}/edit">
                                                        <input class="btn btn-primary btn-xs" type="submit" value="Editar">

                                                    </form >
                                                    <form style="margin-bottom:0;width:27%;" onclick="return confirm('Desea eliminar?')" action="{{ URL::to('/') }}/forms/{{$task->type }}/delete" method="post">
                                                        <input type="hidden" name="_token" value="{{ csrf_token() }}">

                                                        <input type="hidden" name="id" value="{{ $task->id }}">
                                                        <input class="btn btn-primary btn-xs" type="submit" value="Borrar">
                                                    </form>
                                                </div>
                                            @else
                                            <a href="{{URL::to('/')}}/otask/{{ $task->id }}/show">Ver tarea </a>
                                        @endif
                                        </td>
                                    </tr>
                                @endif  {{--*****Fin if roles*****--}}
                            @endforeach
                        </table>
                        <?php $info++; ?>
                                </div>
                            @endif
                        @endforeach

        @else
                No has creado ninguna tarea por el momento. <br> <br>

                @if($user->hasRole('teamleader') || $user->hasRole('supervisor') || $user->hasRole('administrator'))

                    puedes <a href="{{ URL::to('/') }}/tasks">ver todas las tareas,</a>
                    @if($user->hasRole('teamleader'))
                        <a href="{{ URL::to('/') }}/task/create-selector">crear una nueva tarea,</a>
                        <a href="{{ URL::to('/') }}/mytasks"> ver tus subtareas.</a>

                    @endif
                    @if($user->hasRole('supervisor'))
                        <a href="{{ URL::to('/') }}/tasks/teamLeader">crear una nueva tarea.</a>
                    @endif
                @endif
                @if($user->hasRole('teammember') )

                    puedes <a  style="width:300px"   href="{{ URL::to('/') }}/mytasks"> ver tus subtareas.</a>

                @endif
                <br>
                <br>
        @endif
            {{--No has creado ninguna tarea por el momento. <br> <br>--}}
                        </div>
                    </div>
                    {{--panel --}}
                    {{--old guide--}}
                    <a style="display:none" href="{{URL::to('/')}}/instructive" style="color:#000;">Guía de Uso GUNTREF</a>

        </div>
    </div>
    </div>

{{--    @if(session()->has('task'))--}}
{{--        @section('modals')--}}
{{--        <div id="taskdata" class="modal fade " role="dialog">--}}
{{--            <div class="modal-dialog modal-lg">--}}

{{--                <!-- Modal content-->--}}
{{--                <div class="modal-content">--}}
{{--                    <div class="modal-header">--}}
{{--                        <button type="button" class="close" data-dismiss="modal">&times;</button>--}}
{{--                        <h4 class="modal-title">Tarea Creada</h4>--}}
{{--                    </div>--}}
{{--                    <div class="modal-body">--}}
{{--                        La tarea se creó exitosamente.<br>--}}
{{--                        <a href="{{ URL::to('/') }}/tasks/{{ $_GET['t'] }}/unit/{{ $_GET['u'] }}/show">{{ URL::to('/') }}/tasks/{{ $_GET['t'] }}/unit/{{ $_GET['u'] }}/show</a> <br>--}}
{{--                        Recomendamos visitar GUNTREF frecuentemente para verificar el progreso de la tarea, gracias!--}}
{{--                    </div>--}}
{{--                </div>--}}

{{--            </div>--}}
{{--        </div>--}}
{{--        @endsection--}}
    <script>
        $(document).ready(function() {
            $("#taskdata").modal("show");
        })
    </script>
{{--    @endif--}}

@endsection

        <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
        <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {

                for (var i = 0; i < <?php echo count($taskdata); ?>; i++) {
                    $('#table_'+(i+1)).stacktable({myClass:'stacktable small-only'});
                }


                $('.panel-body').find('.accordion-toggle').click(function(){
                    $(this).next().slideToggle('fast');
                    $(".accordion-content").not($(this).next()).slideUp('fast');
                    $(".accordion-toggle").css('background','#d5d1d0');
                    $(this).css('background','#ffffff');

                });
            });



            //no toma el datatable
            notdatatable = true;

            $(document).ready(function() {


                $('.js-example-placeholder-single').select2({
                    placeholder: 'Seleccione una tarea'
                });
                $('#selectTaskdata2').select2({
                    placeholder: 'Seleccione una tarea'
                });
                $('#selectprofile').select2({
                    placeholder: 'Seleccione un perfil'
                });
                $('#selectstatu').select2({
                    placeholder: 'Seleccione un estado'
                });

                $('.busquedaAvanzada').click(function (e) {

                    if($("#collapseExampleBA").is(":visible"))
                    {
                        $('.taskdata').show();
                    }
                    else
                    {
                        $('.taskdata').hide();
                    }

                })


            });
        </script>

        <style>
            .accordion-toggle {cursor: pointer; background: #d5d1d0; }
            .accordion-content {display: none;}
        </style>
