@extends('layouts.client')


<?php $name_status=array("Pendiente","Activo","Invitación eliminada"); ?>

@section('content')
    @if($user->isAdmin() || $user->isApplicant())
    <div class="container">
        <div class="row" style="width:80%;">
            <div class="col-md-10 col-md-offset-1">

                @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 ($errors->any())
                    <div class="alert alert-danger">
                        <ul>
                            @foreach ($errors->all() as $error)
                                <li>{{ $error }}</li>
                            @endforeach
                        </ul>
                    </div>
                @endif

                <div class="panel panel-default">


                    <div class="panel-heading">Panel de {{ $user->name }} {{ $user->surname }}</div>
                    <div style="padding:16px;">

                        <form class="default-form" action="{{URL::to('/')}}/profile/update" method="post">
                            <input type="hidden" name="_token" value="{{ csrf_token() }}">
                            <input type="hidden" name="id" value="{{ $profile->id }}">

                            <div class="form-group">
                                <label for="">Área/Departamento/Carrera/Posgrado</label><br>
                                <input type="text" name="area" value=" {{ old('area',$profile->area) }}">
                            </div>
                            <div class="form-group">


                                <label for="">Nombre del responsable del Área</label><br>
                                <input type="text" name="name" value="{{ old('name', $profile->name) }}">
                            </div>
                            <div class="form-group">
                                <label for="">Apellido/s del responsable del Área</label><br>

                                <input type="text" name="surname" value="{{ old('surname',$profile->surname) }}">
                            </div>
                            <div class="form-group">
                                <label for="">Email del responsable del Área</label><br>

                                <input type="text" name="email" value=" {{ old('email',$profile->email) }}">
                            </div>
                            <div class="form-group">
                                <label for="">Estado</label><br>
                                <select name="status">
                                    <option value="1" @if ($profile->status == 1) selected @endif >Activo</option>
                                    <option value="0" @if ($profile->status == 0) selected @endif >Inactivo</option>
                                </select>
                            </div>


                            <div class="form-group" style="margin-bottom: 5px !important;">
                                <label for="">Invitar Usuarios</label><br>
                            </div>

                            <div class="row">
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <input type="text" name="email_name" id="email_name" placeholder="nombre del email" value="{{ old('email_name') }}">
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <select name="domain" id="domain">
                                            <option value="@untref.edu.ar">@untref.edu.ar</option>
                                            <option value="@bienalsur.org">@bienalsur.org</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <a style="margin-left:auto" class="btn btn-primary btn-sm" href="javascript:void(0);" onclick="addEmail();">Agregar</a>
                                    </div>
                                </div>
                            </div>

                            <table class="table" id="list">
                            @if(!empty($invitations))    
                                @foreach($invitations as $inv)
                                    <tr><td class="email">{{ $inv['email'] }}</td><td style="width:15px !important"><span style="margin-right: 16px;" ><i>{{ $name_status[$inv['status']] }}</i></span></td><td><a style="margin-left:auto" class="btn btn-primary btn-sm" href="javascript:void(0);" onclick="deleteEmail(this);">X</a></td></tr>
                                @endforeach
                            @endif
                            </table>
                            <input type="hidden" name="email_list" id="email_list" value='<?php echo (!empty($invitations))?json_encode($invitations):'' ?>'>
    
                            <div class="form-group">
                                <input onclick="form.submit()" class="btn btn-default " value="Guardar">
                            </div>

                        </form>
                    </div>

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

    </div>
    @else
        <div class="container">
            <div class="row" style="width:100%;">
                <div class="col-md-10 col-md-offset-1">
                    <div class="panel panel-default" style="width: 120%; padding: 3%">
                        <div>
                            <h5>No tienes permisos para ver este panel,<a href="{{ URL::to('/') }}/home/1" >
                                    <span class="item-text">regresar a <i>"Inicio"</i></span></a>. </h5>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    @endif

@endsection

@section('scripts')
    <script>

            jsonObj = <?php echo (!empty($invitations))?json_encode($invitations):'[]' ?>;

        function addEmail(){

            if ($("#email_name").val().trim() != '') {

                $("#list").append('<tr><td class="email">' + $("#email_name").val() + $("#domain").val() + '</td><td style="width:15px !important"><span style="margin-right: 16px;" ></span></td><td><a style="margin-left:auto" class="btn btn-primary btn-sm" href="javascript:void(0);" onclick="deleteEmail(this);">X</a></td></tr>');
                item = {};
                item ["email"] = $("#email_name").val() + $("#domain").val();
                jsonObj.push(item);
                $("#email_list").val(JSON.stringify(jsonObj));
                $("#email_name").val('');
                $("#domain").val('@untref.edu.ar');
            }
        }



        function deleteEmail(el){


                profile_id = "{{ $profile->id }}";

                $.ajax({

                    data: {email:$(el).parent().prev().prev().html(),profile_id:profile_id},
                    headers:{
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
                    },
                    dataType: 'json',
                    url:"{{ URL::to('/') }}/profile/removeinvited",
                    type:'POST',
                    success:  function (data) {

                    }
                });


                jsonOb = [];

                $.each(jsonObj, function (index, value) {
                  jsonOb.push(value.email);
                });


                $(el).parents("tr").remove();
                 jsonOb.splice($.inArray($(el).closest('tr').children('td.email').text(), jsonOb),1);
 
                var jsonArr = [];

                for (var i = 0; i < jsonOb.length; i++) {
                    jsonArr.push({
                        email: jsonOb[i]
                    });
                }

                jsonObj = jsonArr ;
                
                //  convertir a objeto

                $("#email_list").val(JSON.stringify(jsonObj));

        }

        //datatable
            var  notdatatable = true;//no toma el datatable
    </script>
@endsection
    <style>
    .default-form{width:90% !important;}    
    </style>
