@extends('layouts.app-hr')

@section('content')
    <?php Fun::lang()?>
    @include('hr.department.confirm-delete')
    <div class="top-header">
        <ol class="breadcrumb">
            <li><a href="#">{{__('administrator.Dashboard')}}</a></li>
            <li><a href="#">{{__('administrator.department')}}</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.department')}} </h3>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6 col-xs-12">
                    <div class="title-add">
                        <div class="pull-right">
                            @can('Branch Create')
{{--                                <a href="{{route('hr-department.create')}}"--}}
{{--                                   class="button_primary">{{__('administrator.add_new')}}</a>--}}
                                <a href="#" class="button_primary btn_create_new">{{__('administrator.add_new')}}</a>
                            @endcan
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="box-body">
            <table id="datatable_department" width="100%" class="table table-striped">
                <thead>
                    <tr>
                        <th>#</th>
{{--                        <th>{{__('administrator.code') }}</th>--}}
                        <th>{{__('administrator.khmer_name')}}</th>
                        <th>{{__('administrator.english_name')}}</th>
                        <th>{{__('administrator.note')}}</th>
                        <th style="width: 200px">{{ __('administrator.action') }}</th>
                    </tr>
                </thead>
            </table>
        </div>
    </div>
@endsection
@section('js')
    <script type="text/javascript">
        $(document).ready(function () {

            $('#datatable_department').DataTable({
                ordering: false,
                serverSide: true,
                ajax: "{{ route('datatable.hr-get-department') }}",
                lengthMenu: [20, 30, 50, 100, 200, 500, 1000],
                columns:
                    [
                        {
                            data: 'DT_RowIndex',
                            orderable: false,
                            searchable: false
                        },
                        // {
                        //     data: 'code',
                        //     name: 'code'
                        // },
                        {
                            data: 'khmer_name',
                            name: 'khmer_name'
                        },
                        {
                            data: 'english_name',
                            name: 'english_name'
                        },
                        {
                            data: 'note',
                            searchable: false
                        },
                        {
                            data: 'action',
                            searchable: false
                        },
                    ],
                "initComplete": function (settings, json) {

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

        var form_content = `
            {{--<div class="form-group">--}}
            {{--    <label>{{__('administrator.code')}}</label>--}}
            {{--    <input type="text" name="code" class="form-control" placeholder="{{__('administrator.code')}}" maxlength="50">--}}
            {{--</div>--}}
            <div class="form-group">
                <label>{{__('administrator.name_en')}}</label>
                <input type="text" name="name_en" class="form-control border-red" required placeholder="{{__('administrator.name_en')}}" maxlength="50">
            </div>
            <div class="form-group">
                <label>{{__('administrator.name_kh')}}</label>
                <input type="text" name="name_kh" class="form-control border-red" required placeholder="{{__('administrator.name_kh')}}" 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-department.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-department.update',['id']) }}";

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

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




        function form(fcontent = '', btntext = '', title = '', route = '', method_ = '', code = '', name_kh = '', name_en = '', note = '',id = ''){

            $.confirm({
                title          : title + ' {{__('administrator.department')}}',
                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 department  = this;
                            let code        = department.$content.find('input[name=code]').val();
                            let name_en     = department.$content.find('input[name=name_en]').val();
                            let name_kh     = department.$content.find('input[name=name_kh]').val();
                            let note        = department.$content.find('textarea[name=note]').val();

                            department.buttons.submit.disable();

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

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

            if(code || name_kh || name_en){
                setTimeout(function () {
                    $(document).find('input[name=code]').val(code);
                    $(document).find('input[name=name_kh]').val(name_kh);
                    $(document).find('input[name=name_en]').val(name_en);
                    $(document).find('textarea[name=description]').text(note);
                });
            }
        }

        $(document).on('click', '.btn_delete_department', function () {
            var id  = $(this).data('id');
            let url = "{{ route('hr-department.destroy', ['id']) }}";

            $.confirm({
                title: 'Delete!',
                content: 'Are You want to delete this record?',
                type: 'red',
                typeAnimated: true,
                buttons: {
                    delete: {
                        text: 'Delete',
                        btnClass: 'btn-red',
                        action: function(){
                            $.ajax({
                                url: url.replace('id', id),
                                type: 'DELETE',
                                data: {
                                    "_token": "{{ csrf_token() }}",
                                    "id": id
                                },
                                success: function(response){
                                    if (response.status === 200 ) {
                                        $("#datatable_department").DataTable().ajax.reload(null, false);
                                        showSuccessToast(response.message);
                                    }

                                    if (response.status === 400 )
                                        showWarningToast(response.message);
                                }
                            });
                        }
                    },
                    close: function () {

                    }
                }
            });
        });
    </script>
@endsection
