@extends('layouts.admin')

@php
    if($create){
        $title = 'Dodavanje administratora';
        $formpar = array('route' => 'admin.admin.store');
    } else {
        $title = 'Izmena administratora';
        $formpar = array('route' => array('admin.admin.update', $admin->id), 'method' => 'PUT');
    }

    $tr = [
        'dep id'  => 'Sektor',
        'username' => 'username',
        'last name' => 'Prezime',
        'name' => 'Ime',
        'password' => 'Lozinka',
        'newpassword' => 'Nova lozinka',
        'phone' => 'Telefon',
        'Polje email već postoji.' => 'Ovaj email je vec u upotrebi',
        'Polje roles je obavezno' => 'Morate odabrati bar jedan nivo pristupa'
    ];
@endphp

@section('title'){{$title}}@endsection
@section('header'){{$title}}@endsection
@section('header_desc') @endsection
@section('breadcumbs') @endsection

@section('content')
    <!-- Main content -->
    <section class="content">



        <div class="row">

            <div class="col-xs-12">
                @if (session('error'))
                    <div class="alert alert-danger alert-dismissible">
                        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                        <i class="icon fa fa-exclamation-triangle"></i> <strong>Info: </strong> &nbsp;  &nbsp;
                        {{ session('error') }}
                    </div>
                @endif
            </div>

            {!! Form::open($formpar) !!}

            <div class="col-xs-12">
                <div class="box">


                <!-- /.box-header -->
                    <div class="box-body">
                        <div class="row tp30">

                            <div class="col-xs-12 col-sm-6 col-md-4 pbb">
                                {{Form::label('name', 'Ime:')}}
                                {{Form::text('name', old('name', $admin->name), ['class' => 'form-control'])}}
                                <small class="error">{{ strtr($errors->first('name'),$tr) }}</small>
                            </div>


                            <div class="col-xs-12 col-sm-6 col-md-4 pbb">
                                {{Form::label('last_name', 'Prezime:')}}
                                {{Form::text('last_name', old('last_name', $admin->last_name), ['class' => 'form-control'])}}
                                <small class="error">{{ strtr($errors->first('last_name'),$tr) }}</small>
                            </div>

                            {{--<div class="col-xs-12 col-sm-6 col-md-4 pbb">
                                {{Form::label('username', 'username:')}}
                                {{Form::text('username', old('username', $admin->username), ['class' => 'form-control'])}}
                                <small class="error">{{ strtr($errors->first('username'),$tr) }}</small>
                            </div>--}}

                            <div class="col-xs-12 col-sm-6 col-md-4 pbb">
                                @php $cgId = old('dep_id', $admin->dep_id); @endphp
                                {{Form::label('dep_id', 'Sektor:')}}
                                {{Form::select('dep_id', $departments, $cgId, ['placeholder' => 'Odaberite sektor', 'class' => 'form-control', 'id' => 'group-select'])}}
                                <small class="error">{{ strtr($errors->first('dep_id'),$tr) }}</small>
                            </div>

                            <div class="col-xs-12 col-sm-6 col-md-4 pbb">
                                {{Form::label('email', 'email:')}}
                                {{Form::text('email', old('email', $admin->email), ['class' => 'form-control'])}}
                                <small class="error">{{ strtr($errors->first('email'),$tr) }}</small>
                            </div>

                            @if($create)
                            <div class="col-xs-12 col-sm-6 col-md-4 pbb">
                                {{Form::label('password', 'Lozinka:')}}
                                {{Form::password('password', ['class' => 'form-control', 'autocomplete' => 'off'])}}  {{--old('password', $admin->password),--}}
                                <small class="error">{{ strtr($errors->first('password'),$tr) }}</small>
                            </div>

                            <div class="col-xs-12 col-sm-6 col-md-4 pbb">
                                {{Form::label('password_confirmation', 'Ponovite lozinku:')}}
                                {{Form::password('password_confirmation',  ['class' => 'form-control', 'autocomplete' => 'off'])}} {{--old('password_confirmation', null),--}}
                                <small class="error">{{ strtr($errors->first('password_confirmation'),$tr) }}</small>
                            </div>

                            @endif





                            <div class="col-xs-12 col-sm-6 col-md-4 pbb">
                                {{Form::label('phone', 'Telefon:')}}
                                {{Form::text('phone', old('phone', $admin->phone), ['class' => 'form-control'])}}
                                <small class="error">{{ strtr($errors->first('phone'),$tr) }}</small>
                            </div>








                            <div class="col-xs-12 col-sm-6 col-md-4 pbb">
                                {{Form::label('status', 'Aktivan:')}}<br>
                                @php
                                    $cv = old('status', $admin->status);
                                    $actval = !is_null($cv) ? $cv : 1;
                                @endphp
                                <label class="radio-inline"><input type="radio" name="status" value="1" {{ $actval== 1 ? 'checked': ''}} > Da</label>
                                <label class="radio-inline"><input type="radio" name="status" value="0" {{ $actval== 0 ? 'checked': ''}} > Ne</label>
                                <small class="error">{{ strtr($errors->first('status'),$tr) }}</small>
                            </div>

                            <div class="col-xs-12 col-sm-6 col-md-4 pbb">
                                {{Form::label('is_super_admin', 'Super admin:')}}<br>
                                @php
                                    $cv = old('is_super_admin', $admin->is_super_admin);
                                    $saval = !is_null($cv) ? $cv : 0;
                                @endphp
                                <label class="radio-inline"><input type="radio" name="is_super_admin" value="1" {{ $saval== 1 ? 'checked': ''}} > Da</label>
                                <label class="radio-inline"><input type="radio" name="is_super_admin" value="0" {{ $saval== 0 ? 'checked': ''}} > Ne</label>
                                <br>
                                <small>NAPOMENA: Super admin automatski ima sva moguća ovlašćenja čak iako nema nijedan nivo pristupa</small>
                                <small class="error">{{ strtr($errors->first('is_super_admin'),$tr) }}</small>
                            </div>




                            <div class="col-xs-12">
                                <hr>
                            </div>

                        </div><!-- /.row -->

                        @if($create == 0)
                        <div class="row">
                                <div class="col-xs-12">
                                    <strong>Promena lozinke:</strong><br>
                                    <span class="text-red">
                                        <strong>popuniti samo ukoliko menjate trenutno vazecu lozinku !</strong>
                                    </span>
                                    <br><br><br>
                                </div>


                                <div class="col-xs-12 col-sm-6 col-md-4 pbb">
                                    {{Form::label('newpassword', 'Nova lozinka:')}}
                                    {{Form::password('newpassword', ['class' => 'form-control', 'autocomplete' => 'off'])}}  {{--old('password', $admin->password),--}}
                                    <small class="error">{{ strtr($errors->first('newpassword'),$tr) }}</small>
                                </div>

                                <div class="col-xs-12 col-sm-6 col-md-4 pbb">
                                    {{Form::label('newpassword_confirmation', 'Ponovite novu lozinku:')}}
                                    {{Form::password('newpassword_confirmation',  ['class' => 'form-control', 'autocomplete' => 'off'])}} {{--old('password_confirmation', null),--}}
                                    <small class="error">{{ strtr($errors->first('newpassword_confirmation'),$tr) }}</small>
                                </div>


                        </div>
                        @endif

                        <div class="row">

                            <div class="col-xs-12 tp30">
                                <strong>Odaberite nivoe pristupa za ovog administratora:</strong><br>
                                <small class="error">{{ strtr($errors->first('roles'),$tr)}}</small>
                                <br><br>
                            </div>


                            @foreach($roles as $role)

                                <div class="col-xs-6 col-md-3">
                                    <div class="form-check form-check-inline">
                                        @php $checked = !is_null(coalesce(old('roles.'.$role->id), in_array($role->id, $curentroles), null)); @endphp
                                        {{ Form::checkbox('roles['.$role->id.']', $role->id, $checked, ['class' => 'form-check-label', 'id' => "roles[$role->id]"]) }}
                                        {{ Form::label("roles[$role->id]", $role->name, ['class' => 'custom-label noselect']) }}
                                    </div>
                                    <small class="error">{{ preg_replace('/(roles)\.(\d+)/i', '', $errors->first('roles.'.$role->id))}}</small>
                                </div>

                            @endforeach
                        </div>
                    </div>
                    <!-- /.box-body -->
                </div>
                <!-- /.box -->
            </div>
            <!-- /.col -->

            <div class="col-xs-12">
                @php $btntext = ($create) ? 'Unesi administratora' : 'Sačuvajte izmene'@endphp
                {{Form::submit($btntext, ['class' => 'btn btn-success pull-right'])}}
            </div>

            {!! Form::close() !!}

        </div>
        <!-- /.row -->

    </section>
@endsection

@push('css')
    <link rel="stylesheet" href="{{asset('assets/components/bootstrap-datepicker/dist/css/bootstrap-datepicker.min.css')}}">
@endpush


@push('scripts')
    <script src="{{asset('assets/components/bootstrap-datepicker/dist/js/bootstrap-datepicker.min.js')}}"></script>
    <script>
        //Date picker
        $('#datepicker').datepicker({
            format: 'yyyy-mm-dd',
            autoclose: true
        })
    </script>
@endpush