{!! Form::open(['url'=>'leave-apply-edit','id'=>'leaveApply']) !!}
{!! Form::hidden('leaveId',$leave->id) !!}

<div class="content">
    <div class="row">
        <div class="content">
            <div class="row">
                <div class="col-md-12">
                    <div class="form-group col-md-5" style="margin-bottom: 4px;">
                        <label class="control-label">From Date</label>
                        <input type="text" name="from_date" value="{{$leave->from_date}}" class="form-control datepicker" required="true">
                    </div>
                    <div class="form-group col-md-5" style="margin-bottom: 4px;">
                        <label class="control-label">To Date</label>
                        <input type="text" name="to_date" value="{{$leave->to_date}}" class="form-control datepicker" required="true">
                    </div>
                    <div class="form-group col-md-12">
                        <label class="control-label">Leave Type</label>
                        {!! Form::select('leave_type',$leaveType,$leave->client_leave_type_id,['class'=>'form-control','required'=>'true']) !!}
                    </div>

                    <div class="form-group col-md-12">
                        <label class="control-label">Message</label>
                        <textarea name="message" class="form-control" required="true" id="editor">{{$leave->message}}</textarea>
                    </div>

                </div>

            </div>
        </div>
    </div>
</div>
<div class="footer">

    <button type="submit" class="btn btn-info btn-fill pull-right">Apply</button>
    <button type="button" class="btn btn-default btn-simple pull-right" data-dismiss="modal">Close</button>
    <div class="clearfix"></div>

    <div id="errorList" style="color:red">

    </div>
</div>
{!! Form::close() !!}

<script type="text/javascript">
    CKEDITOR.replace( 'editor',{
        toolbarGroups : [
            { name: 'basicstyles', groups: [ 'Bold', 'Italic' ] },
            { name: 'paragraph',   groups: [ 'list' ] },
        ]
    });

    $(document).ready(function () {
        $('#leaveApply').validate({
            ignore: [],
            invalidHandler: function() {
                $("#errorList").empty();
            },
            submitHandler: function() {
                submitForm();
            }
        });
        $(".modal-title").text("Leave Apply Edit");
        $(".modal-dialog").addClass("modal-lg");
    });
    $('.datepicker').datetimepicker({
        format: 'YYYY-MM-DD',
        icons: {
            time: "fa fa-clock-o",
            date: "fa fa-calendar",
            up: "fa fa-chevron-up",
            down: "fa fa-chevron-down",
            previous: 'fa fa-chevron-left',
            next: 'fa fa-chevron-right',
            today: 'fa fa-screenshot',
            clear: 'fa fa-trash',
            close: 'fa fa-remove'
        }
    });

    function remove(ele) {
        $(ele).closest('.test').remove();
    }
    function submitForm(){
        var editorText = CKEDITOR.instances.editor.getData();
        $.ajax({
            url: "{{URL::to('leave-apply-edit')}}",
            type: 'POST',
            data: $("#leaveApply").serialize()+ '&edit_message=' + encodeURIComponent(editorText),
            dataType: 'json',
            success:function (data) {
                if(data.error){
                    $("#errorList").html(data.error);
                }else if(data.success=='true') {
                    window.location.href="{{URL::to('user-leave')}}";
                }
            },
            error: function (data) {
                alert("Something wrong!");
            }
        });
    }

</script>
