@extends('layouts.app-hr')

@section('content')
    <?php Fun::lang()?>
    @include('hr.employment-status.confirm-delete')
    <div class="top-header">
        <ol class="breadcrumb">
            <li><a href="#">{{__('administrator.Dashboard')}}</a></li>
            <li><a href="#">{{__('administrator.employment_status')}}</a></li>
        </ol>
    </div>
    <div class="page">
        <div class="top-content">
            <div class="row">
                <div class="col-sm-6 col-xs-12">
                    <div class="title-add">
                        <div class="pull-left">
                            <h3>{{__('administrator.employment_status')}} </h3>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6 col-xs-12">
                    <div class="title-add">
                        <div class="pull-right">
                            @if(Gate::check('Employment Status Create'))
{{--                            <a href="{{route('hr-employment-status.create')}}"--}}
{{--                               class="button_primary">{{__('administrator.add_new')}}</a> --}}
                                <a href="#" class="button_primary btn_create_new"> {{__('administrator.add_new')}}</a>
                            @endif
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="box-body">
            <table id="datatable_products" width="100%" class="table table-striped">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>{{ __('administrator.name') }}</th>
                        <th>{{__('administrator.note')}}</th>
                        <th style="width: 200px;">{{ __('administrator.action') }}</th>
                    </tr>
                </thead>
                <tbody>

                </tbody>
            </table>
        </div>
    </div>
@endsection
@section('js')
    <script type="text/javascript">
        $(document).ready(function () {
            $(document).on('click', '.btn_del_emp_status', function(){
                var id = $(this).data('id');
                $('#delete-id').val(id);
                $('#form-delete').modal('show');
            });
            $('#datatable_products').DataTable({
                ordering: false,
                serverSide: true,
                ajax: "{{ route('datatable.hr-get-employment-status') }}",
                lengthMenu: [[20, 30, 50, 100, 1000], [20, 30, 50, 100, 1000]],
                columns:
                    [
                        {
                            data: 'DT_RowIndex',
                            orderable: false,
                            searchable: false
                        },
                        {
                            data: 'name'
                        },
                        {
                            data: 'note'
                        },
                        {
                            data: 'action'
                        },
                    ],
                "initComplete": function (settings, json) {

                }

            });

            $.fn.dataTable.ext.errMode = 'throw';
        });

        var form_content = `
            <div class="form-group">
                <label>{{__('administrator.name')}}</label>
                <input type="text" name="name" class="form-control border-red" required placeholder="1 ខែ, 3 ខែ, 6ខែ ..." maxlength="50">
            </div>
            <div class="form-group">
                <label>{{__('administrator.note')}}</label>
                <textarea name="note" rows="5" maxlength="100" class="no-resize form-control" placeholder="{{__('administrator.note')}}" ></textarea>
            </div>`;

        $(document).on('click','.btn_create_new',function(){
            form(form_content,'{{__('administrator.save')}}','{{__('administrator.create')}}','{{ route('hr-employment-status.store') }}','POST');
        });

        $(document).on('click', '.btn_edit', function () {
            let url = $(this).data('url');
            $.ajax({
                url      : url,
                datatype : 'json',
                method   : 'get',
            }).done(function(response){
                let this_id  = response.id;
                let url      = "{{ route('hr-employment-status.update',['id']) }}";

                form(form_content,'{{__('administrator.update')}}','{{__('administrator.edit')}}', url.replace('id',this_id), 'PATCH', response);

            }).fail(function (error) {
                console.log(error);
            });
        });




        function form(fcontent = '', btntext = '', title = '', route = '', method_ = '', response = ''){

            $.confirm({
                title          : title + ' {{__('administrator.employment_status')}}',
                content        : fcontent,
                theme          : 'modern',
                columnClass    : 'medium',
                type           : 'blue',
                typeAnimated   : true,
                closeAnimation : 'zoom',
                buttons: {
                    cancel: {
                        text     : '{{__('administrator.cancel')}}',
                        btnClass : 'btn-red close_form',
                        keys     : ['esc'],
                        action   : function () {

                        }
                    },
                    submit: {
                        text: btntext,
                        btnClass: 'btn-blue',
                        action: function () {
                            let this_  = this;
                            let name   = this_.$content.find('input[name=name]').val();
                            let note   = this_.$content.find('textarea[name=note]').val();

                            this_.buttons.submit.disable();

                            $.ajax({
                                url      : route,
                                dataType : 'json',
                                method   : method_,
                                data: {
                                    "_token"    : "{{ csrf_token() }}",
                                    name        : name,
                                    note        : note,
                                },
                            }).done(function (response) {
                                if (response.status === 200) {
                                    $('#datatable_products').DataTable().ajax.reload(null, false);
                                    $(document).find('.close_form').trigger('click');
                                    showSuccessToast(response.message);
                                }
                            }).fail(function (xhr) {
                                alertErrorFormController(xhr);
                                this_.buttons.submit.enable();

                            });
                            return false;
                        }
                    }
                }
            });

            if(response){
                setTimeout(function () {
                    $(document).find('input[name=name]').val(response.name);
                    $(document).find('textarea[name=note]').text(response.note);
                });
            }
        }
    </script>
@endsection
