@extends('adminlte::page')

@section('content')
    <?php Fun::lang() ?>
    <?php
    $rand_id = rand(1, 1000) . time() . rand(1, 1000);
    $id = isset($id) ? $id : null;
    $entry = null;
    if ($id > 0) {
        $entry = \App\Models\EmpLeave::find($id);
    }
    ?>
    @include('hr.page-breadcrumb',['l1'=>'Manage HR','l2'=>'ATTENDANCE','l3'=>'Take Leave'])
    <div id="page" class="page">
        @include('hr.top-content',['l1'=>'Take Leave',
        'create'=> 0,
        'route' => 'take-leave-authorize',
        'btn_custom' => 0,
        'search' => 0,

        ])
        <?php
        $d = isset($d) ? $d : null;
        ?>
        @if($d != null)
            <form class="form" role="form" method="POST" action="{{ route('take-leave-authorize.update', $d->id) }}">
                @csrf
                @method('PATCH')
                @else
                    {!! Form::open(array('route' => 'take-leave.store','method'=>'POST','enctype'=>'multipart/form-data')) !!}
                @endif
                <div class="row">

                    <div class="col-xs-12 col-sm-3 col-md-3 ">
                        <div class="form-group">
                            <label class="required">Employee Name</label>
                            <select id="select2_ajax_emp_id" class="select2 form-control" name="select2_ajax_emp_id">
                                @if($d !=null)
                                    <option value="{{optional($d->employee)->id}}"
                                            selected="selected">{{optional($d->employee)->getName()}}111
                                    </option>
                                @endif
                            </select>
                        </div>
                    </div>

                    <div class="col-xs-12 col-sm-3 col-md-3 ">
                        <div class="form-group">
                            <label class="required">Form date</label>
                            <input type="text" name="from_date" class="form-control date_picker" placeholder="From date"
                                   autocomplete="off"
                                   value="{{$d!=null? date('Y-m-d', strtotime($d->from_date)):date('Y-m-d')}}">
                        </div>
                    </div>
                    <div class="col-xs-12 col-sm-3 col-md-3 ">
                        <div class="form-group">
                            <label class="required">To date</label>
                            <input type="text" name="to_date" class="form-control date_picker" placeholder="To date"
                                   autocomplete="off"
                                   value="{{$d!=null?date('Y-m-d', strtotime($d->to_date)):date('Y-m-d')}}">
                        </div>
                    </div>
                    <div class="col-xs-12 col-sm-3 col-md-3 ">
                        <div class="form-group">
                            <label>Request date</label>
                            <input type="text" name="request_date" class="form-control date_picker"
                                   placeholder="Request date"
                                   autocomplete="off"
                                   value="{{$d!=null? date('Y-m-d', strtotime($d->request_date)):date('Y-m-d')}}">
                        </div>
                    </div>
                    <div class="col-xs-12 col-sm-3 col-md-3 ">
                        <div class="form-group">
                            <label class="required">Leave Type</label>
                            <select id="leave_type" class="select2 form-control" name="leave_type">
                                @if($d !=null)
                                    <option value="{{optional(\App\Models\LeaveType::find($d->leave_type))->id}}"
                                            selected="selected">{{str_replace('_',' ',optional(\App\Models\LeaveType::find($d->leave_type))->name)}}
                                    </option>
                                @endif
                            </select>
                        </div>
                    </div>

                    <div class="col-xs-12 col-sm-3 col-md-3 ">
                        <div class="form-group">
                            <label class="required">Request To</label>
                            <select id="request_to" class="select2 form-control" name="request_to">
                                @if($d !=null)
                                    <option value="{{optional(\App\Models\Employee::where('id',$d->request_to)->first())->id}}"
                                            selected="selected">{{optional(\App\Models\Employee::where('id',$d->request_to)->first())->getName()}}
                                    </option>
                                @endif
                            </select>
                        </div>
                    </div>

                    <div class="col-xs-12 col-sm-12 col-md-12 ">
                        <div class="form-group">
                            <label>Reason</label>
                            <textarea class="form-control" rows="2" placeholder="Enter Your Reason" name="reason">
                                    @if($d != null)
                                    {!! $d->reason !!}
                                @endif
                            </textarea>
                        </div>
                    </div>
                    <div class="col-xs-12 col-sm-3 col-md-3 ">
                        <div class="form-group">
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" title="Check it to approve authorize!" name="authorize" value="
                                   1
                                    "
                                    @if($d != null) {{optional($d)->approve > 0? 'checked="checked"': ''}} @endif

                                    > Authorize

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

                    @include('hr.save_back')

                </div>
                @if($d != null)
            </form>
        @else
            {!! Form::close() !!}
        @endif
    </div>

@endsection



@section('js')

    <script type="text/javascript" src="{{asset('assets/controller/location.js')}}"></script>
    <script>
        $(function () {
            $('#select2_ajax_emp_id').select2({
                placeholder: 'Select Category',
                ajax: {
                    url: '{{url('api/employee')}}',
                    dataType: 'json',
                    delay: 0,
                    processResults: function (data) {
                        return {
                            results: $.map(data, function (item) {
                                return {
                                    text: item.user_firstname + ' ' + item.user_lastname,
                                    id: item.id
                                }
                            })
                        };
                    },
                    cache: true
                },
                minimumInputLength: 0,
                autoClear: false,
                autoclose: true,
            });
            $('#request_to').select2({
                placeholder: 'Select Employee',
                ajax: {
                    url: '{{url('api/employee')}}',
                    dataType: 'json',
                    delay: 0,
                    processResults: function (data) {
                        return {
                            results: $.map(data, function (item) {
                                return {
                                    text: item.user_firstname + ' ' + item.user_lastname,
                                    id: item.id
                                }
                            })
                        };
                    },
                    cache: true
                },
                minimumInputLength: 0,
                autoClear: false,
                autoclose: true,
            });

            $('#leave_type').select2({
                placeholder: 'Select Leave Type',
                ajax: {
                    url: '{{url('api/leave-type')}}',
                    dataType: 'json',
                    delay: 0,
                    processResults: function (data) {
                        return {
                            results: $.map(data, function (item) {
                                return {
                                    text: item.name.substr(0, 1).toUpperCase() + item.name.substr(1),
                                    id: item.id
                                }
                            })
                        };
                    },
                    cache: true
                },
                minimumInputLength: 0,
                autoClear: false,
                autoclose: true,
            });

        });
    </script>


@endsection

