@extends('layouts.client')



@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>
                    @if($type)
                        <h3 style="margin-left: 10px">Debe crear un perfil para continuar</h3>
                    @endif
                    <div style="padding:25px;width: 180%">

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

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


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

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

                            <input type="text" name="email" value=" {{ old('email') }}">
                        </div>

                        <div class="form-group">
                            <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" 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">

                        </table>
                        <input type="hidden" name="email_list" id="email_list" value="">

                        <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>

    //datatable
    var  notdatatable = true;//no toma el datatable


jsonObj = [];

function addEmail(){
    
    if ($("#email_name").val().trim() != '') {
        $("#list").append('<tr><td class="email">' + $("#email_name").val() + $("#domain").val() + '</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){



        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));


}

</script>
@endsection