@extends('layouts.app-hr')

@section('content')
    <?php Fun::lang()?>
    @include('hr.overtime-policy.confirm-delete')
    <div class="top-header">
        <ol class="breadcrumb">
            <li><a href="#">{{__('administrator.Dashboard')}}</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.overtime_policy')}} </h3>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6 col-xs-12">
                    <div class="title-add">
                        <div class="pull-right">
                            @can('OT Policy Create')
{{--                                <a href="{{route('overtime-policy.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_overtime_policy" width="100%" class="table table-striped">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>{{__('administrator.name')}}</th>
                        <th>{{__('administrator.rate_normal_ot')}}</th>
                        <th>{{__('administrator.rate_sunday_ot')}}</th>
                        <th>{{__('administrator.rate_holiday_ot')}}</th>
                        <th style="width: 200px;">{{ __('administrator.action') }}</th>
                    </tr>
                </thead>
                <tbody>

                </tbody>
            </table>
        </div>
    </div>
@endsection
@section('script')
    <script type="text/javascript">
        $(document).ready(function () {

            $(document).on('click', '.btn_delete', function () {
                var id = $(this).data('id');
                $('#delete-id').val(id);
                $('#form-delete').modal('show');
            });

            $('#datatable_overtime_policy').DataTable({
                ordering: false,
                serverSide: true,
                ajax: "{{ route('overtime-policy.index') }}",
                lengthMenu: [
                    [20, 30, 50, 100, 1000],
                    [20, 30, 50, 100, 1000]
                ],
                columns:
                    [
                        {
                            data: 'DT_RowIndex',
                            orderable: false,
                            searchable: false
                        },
                        {
                            data: 'name'
                        },
                        {
                            data: 'normal_ot_rate'
                        },
                        {
                            data: 'sunsat_ot_rate'
                        },
                        {
                            data: 'holiday_ot_rate'
                        },
                        {
                            data: 'action',
                            orderable: false,
                            searchable: false
                        },
                    ],
                "initComplete": function (settings, json) {

                }
            });

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

        var form_content = `
            <div class="form-group">
                <label class="required"  for="name">{{__('administrator.overtime_policy')}} {{__('administrator.name')}}</label>
                {!! Form::text('name', null, array('placeholder'=>'Name', 'class' => 'form-control','required', 'autocomplete' => 'off')) !!}
            </div>
            <div class="form-group">
                <label class="required"  for="name">{{__('administrator.rate_normal_ot')}}</label>
                {!! Form::text('rate_normal_ot', null, array('placeholder' => 'type 1 = 100%, 1.5 = 150%, 2 = 200%..','class' => 'form-control number-only','required')) !!}

            </div>
            <div class="form-group">
                <label class="required"  for="name">{{__('administrator.rate_sunday_ot')}}</label>
                {!! Form::text('rate_sunday_ot', null, array('placeholder' => 'type 1 = 100%, 1.5 = 150%, 2 = 200%..','class' => 'form-control number-only','required')) !!}

            </div>
            <div class="form-group">
                <label class="required"  for="name">{{__('administrator.rate_holiday_ot')}}</label>
                {!! Form::text('rate_holiday_ot', null, array('placeholder' => 'type 1 = 100%, 1.5 = 150%, 2 = 200%..','class' => 'form-control number-only','required')) !!}
            </div>
            <div class="form-group">
                <label class="required"  for="name">{{__('administrator.effect_date')}}</label>
                {!! Form::text('effect_date', null, array('placeholder' => __('administrator.effect_date'),'class' => 'form-control date_picker','required')) !!}
            </div>
            `;

        $(document).on('click','.btn_create_new',function(){
            form(form_content,'{{__('administrator.save')}}','{{__('administrator.create')}}','{{ route('overtime-policy.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('overtime-policy.update',['id']) }}";

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

            }).fail(function (error) {
                alertErrorFormController(error);
            });
        });




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

            $.confirm({
                title          : title + ' {{__('administrator.overtime_policy')}}',
                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 rate_normal_ot      = this_.$content.find('input[name=rate_normal_ot]').val();
                            let rate_sunday_ot      = this_.$content.find('input[name=rate_sunday_ot]').val();
                            let rate_holiday_ot     = this_.$content.find('input[name=rate_holiday_ot]').val();
                            let effect_date         = this_.$content.find('input[name=effect_date]').val();


                            this_.buttons.submit.disable();

                            $.ajax({
                                url      : route,
                                dataType : 'json',
                                method   : method_,
                                data: {
                                    "_token"            : "{{ csrf_token() }}",
                                    name                : name,
                                    rate_normal_ot      : rate_normal_ot,
                                    rate_sunday_ot      : rate_sunday_ot,
                                    rate_holiday_ot     : rate_holiday_ot,
                                    effect_date         : effect_date,
                                },
                            }).done(function (response) {
                                if (response.status === 200) {
                                    $('#datatable_overtime_policy').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;
                        }
                    }
                },
                onContentReady: function () {
                    var jc = this;
                    jc.$content.find(".date_picker").datepicker({
                        format: 'dd-mm-yyyy',
                        todayHighlight: true,
                        autoclose: true,
                    });
                    this.$content.find('form').on('submit', function (e) {
                        e.preventDefault();
                    });
                }
            });

            if(response){
                let effect_date = DateFormat(response.effect_date);
                setTimeout(function () {
                    $(document).find('input[name=name]').val(response.name);
                    $(document).find('input[name=rate_normal_ot]').val(response.normal_ot_rate);
                    $(document).find('input[name=rate_sunday_ot]').val(response.sunsat_ot_rate);
                    $(document).find('input[name=rate_holiday_ot]').val(response.holiday_ot_rate);
                    $(document).find('input[name=effect_date]').val(effect_date);
                });
            }
        }

        function DateFormat(date){
            var items = date.split('-');
            return items[2] +'-'+ items[1] +'-'+ items[0];
        }
    </script>
@endsection
