<?php $__env->startSection('content'); ?>
    <?php Fun::lang()?>
    <?php echo $__env->make('hr.absent-policy.confirm-delete', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
    <div class="top-header">
        <ol class="breadcrumb">
            <li><a href="#"><?php echo e(__('administrator.Dashboard')); ?></a></li>
            <li><a href="#"><?php echo e(__('administrator.absent_policy')); ?></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><?php echo e(__('administrator.absent_policy')); ?> </h3>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6 col-xs-12">
                    <div class="title-add">
                        <div class="pull-right">
                            <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('Absent Policy Create')): ?>


                                <a href="#" class="button_primary btn_create_new"><?php echo e(__('administrator.add_new')); ?></a>
                            <?php 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><?php echo e(__('administrator.name')); ?></th>
                    <th><?php echo e(__('administrator.deduct_money')); ?></th>
                    <th><?php echo e(__('administrator.date')); ?></th>
                    <th><?php echo e(__('administrator.note')); ?></th>
                    <th style="width: 200px"><?php echo e(__('administrator.action')); ?></th>
                </tr>
                </thead>
            </table>
        </div>
    </div>
<?php $__env->stopSection(); ?>
<?php $__env->startSection('js'); ?>
    <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_products').DataTable({
                ordering: false,
                serverSide: true,
                lengthMenu: [
                    [20, 50, 75, 100, 1000],
                    [20, 50, 75, 100, 1000]
                ],
                ajax: "<?php echo e(route('hr-absent-policy.index')); ?>",
                columns:
                    [
                        {
                            data: 'DT_RowIndex',
                            orderable: false,
                            searchable: false
                        },
                        {
                            data: 'name'
                        },
                        {
                            data: 'deduct_money'
                        },
                        {
                            data: 'date'
                        },
                        {
                            data: 'note'
                        },
                        {
                            data: 'action',
                            orderable: false,
                            searchable: false
                        },
                    ],
                "initComplete": function (settings, json) {

                }
            });

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

        var form_content = `
            <div class="form-group">
                <label class=""  for="name"><?php echo e(__('administrator.department')); ?></label>
                <?php echo Form::select('department_id', $department, null, array('placeholder' => __('administrator.please_select'),'class' => 'form-control select2', 'autocomplete'=>'off')); ?>

            </div>
            <div class="form-group">
                <label class=""  for="name"><?php echo e(__('administrator.position')); ?></label>
                <?php echo Form::select('position_id', $position, null, array('placeholder' => __('administrator.please_select'),'class' => 'form-control select2', 'autocomplete'=>'off',)); ?>

            </div>
            <div class="form-group">
                <label class="required"  for="name"><?php echo e(__('administrator.absent_policy')); ?> <?php echo e(__('administrator.name')); ?></label>
                <?php echo Form::text('name', null, array('placeholder' => __('administrator.name'),'class' => 'form-control', 'autocomplete'=>'off','required')); ?>

            </div>
            <div class="form-group">
                <label class="required"  for="name"><?php echo e(__('administrator.deduct_money_day')); ?></label>
                <?php echo Form::text('deduct_money', null, array('placeholder' => __('administrator.deduct_money'),'class' => 'form-control number-only', 'autocomplete'=>'off','required')); ?>

            </div>
            <div class="form-group">
                <label class="required"  for="name"><?php echo e(__('administrator.date')); ?></label>
                <?php echo Form::text('date', null, array('placeholder' => __('administrator.date'),'class' => 'form-control date_picker', 'autocomplete'=>'off','required')); ?>

            </div>
            <div class="form-group">
                <label class=""  for="name"><?php echo e(__('administrator.note')); ?></label>
                <?php echo Form::text('note', null, array('placeholder' => __('administrator.note'),'class' => 'form-control', 'autocomplete'=>'off')); ?>

            </div>`;

        $(document).on('click','.btn_create_new',function(){
            form(form_content,'<?php echo e(__('administrator.save')); ?>','<?php echo e(__('administrator.create')); ?>','<?php echo e(route('hr-absent-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      = "<?php echo e(route('hr-absent-policy.update',['id'])); ?>";
                form(form_content,'<?php echo e(__('administrator.update')); ?>','<?php echo e(__('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 + ' <?php echo e(__('administrator.absent_policy')); ?>',
                content        : fcontent,
                theme          : 'modern',
                columnClass    : 'medium',
                type           : 'blue',
                typeAnimated   : true,
                closeAnimation : 'zoom',
                buttons: {
                    cancel: {
                        text     : '<?php echo e(__('administrator.cancel')); ?>',
                        btnClass : 'btn-red close_form',
                        keys     : ['esc'],
                        action   : function () {

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


                            this_.buttons.submit.disable();

                            $.ajax({
                                url      : route,
                                dataType : 'json',
                                method   : method_,
                                data: {
                                    "_token"            : "<?php echo e(csrf_token()); ?>",
                                    department_id       : department_id,
                                    position_id         : position_id,
                                    name                : name,
                                    deduct_money        : deduct_money,
                                    date                : date,
                                    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;
                        }
                    }
                },
                onContentReady: function () {
                    var jc = this;
                    jc.$content.find(".date_picker").datepicker({
                        format: 'dd-mm-yyyy',
                        todayHighlight: true,
                        autoclose: true,
                    });

                    jc.$content.find('.select2').select2({
                        dropdownParent: $('.jconfirm')
                    }).next("span").css( "text-align", "left");

                    this.$content.find('form').on('submit', function (e) {
                        e.preventDefault();
                    });
                }
            });

            if(response){
                let date = DateFormat(response.date);
                setTimeout(function () {
                    $(document).find('select[name=department_id]').val(response.department_id).trigger('change');
                    $(document).find('select[name=position_id]').val(response.position_id).trigger('change');
                    $(document).find('input[name=name]').val(response.name);
                    $(document).find('input[name=deduct_money]').val(response.deduct_money);
                    $(document).find('input[name=date]').val(date);
                    $(document).find('input[name=note]').val(response.note);
                });
            }
        }

        function DateFormat(date){
            var items = date.split('-');
            return items[2] +'-'+ items[1] +'-'+ items[0];
        }
    </script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app-hr', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/nsowos/public_html/shr-brand/resources/views/hr/absent-policy/index.blade.php ENDPATH**/ ?>