<?php $__env->startSection('content'); ?>
    <?php Fun::lang()?>
    <?php echo $__env->make('hr.job-benefit.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.benefit')); ?></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.benefit')); ?> </h3>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6 col-xs-12">
                    <div class="title-add">
                        <div class="pull-right">
                            <?php if(Gate::check('Benefit type 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.kh_name')); ?></th>
                        <th><?php echo e(__('administrator.en_name')); ?></th>
                        <th><?php echo e(__('administrator.amount')); ?></th>
                        <th><?php echo e(__('administrator.note')); ?></th>
                        <th style="width: 200px;"><?php echo e(__('administrator.action')); ?></th>
                    </tr>
                </thead>
                <tbody>

                </tbody>
            </table>
        </div>
    </div>
<?php $__env->stopSection(); ?>
<?php $__env->startSection('script'); ?>
    <script type="text/javascript">
        $(document).ready(function () {
            $(document).on('click', '.btn_job_benefit', function () {
                var id = $(this).data('id');
                $('#delete-id').val(id);
                $('#form-delete').modal('show');
            });
            $('#datatable_products').DataTable({
                ordering: false,
                serverSide: true,
                ajax: "<?php echo e(route('datatable.hr-get-job-benefit')); ?>",
                lengthMenu: [
                    [20, 30, 50, 100, 1000],
                    [20, 30, 50, 100, 1000]
                ],
                columns:
                    [
                        {
                            data: 'DT_RowIndex',
                            orderable: false,
                            searchable: false
                        },
                        {
                            data: 'name_kh'
                        },
                        {
                            data: 'name'
                        },

                        {
                            data: 'amount'
                        },
                        {
                            data: 'note'
                        },
                        {
                            data: 'action'
                        },
                    ],
                "initComplete": function (settings, json) {
                    $('#datatable_products tbody :not(script)').contents().filter(function () {
                        return this.nodeType === 3;
                    }).replaceWith(function () {
                        return this.nodeValue.replace(/full_day/g, '<?php echo e(__('administrator.full_day')); ?>').replace(/half_day/g, '<?php echo e(__('administrator.half_day')); ?>').replace(/day_off/g, '<?php echo e(__('administrator.day_off')); ?>');
                    });
                }
            });

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

        var form_content = `
            <div class="form-group">
                <label><?php echo e(__('administrator.name_kh')); ?></label>
                <input type="text" name="name_kh" class="form-control border-red"  placeholder="<?php echo e(__('administrator.name_kh')); ?>" maxlength="50">
            </div>
            <div class="form-group">
                <label><?php echo e(__('administrator.name_en')); ?></label>
                <input type="text" name="name_en" class="form-control border-red"  placeholder="<?php echo e(__('administrator.name_en')); ?>" maxlength="50">
            </div>
            <div class="form-group">
                <label><?php echo e(__('administrator.amount')); ?></label>
                <input type="text" name="amount" class="form-control border-red number-only"  placeholder="<?php echo e(__('administrator.amount')); ?>" maxlength="10">
            </div>
            <div class="form-group">
                <label><?php echo e(__('administrator.note')); ?></label>
                <textarea name="note" rows="5" maxlength="100" class="no-resize form-control" placeholder="<?php echo e(__('administrator.note')); ?>" ></textarea>
            </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-job-benefit.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-job-benefit.update',['id'])); ?>";

                form(form_content,'<?php echo e(__('administrator.update')); ?>','<?php echo e(__('administrator.edit')); ?>', url.replace('id',this_id), 'PATCH', response);

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




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

            $.confirm({
                title          : title + ' <?php echo e(__('administrator.benefit_type')); ?>',
                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 name_en     = this_.$content.find('input[name=name_en]').val();
                            let name_kh     = this_.$content.find('input[name=name_kh]').val();
                            let amount      = this_.$content.find('input[name=amount]').val();
                            let note        = this_.$content.find('textarea[name=note]').val();

                            this_.buttons.submit.disable();

                            $.ajax({
                                url      : route,
                                dataType : 'json',
                                method   : method_,
                                data: {
                                    "_token"    : "<?php echo e(csrf_token()); ?>",
                                    name_en     : name_en,
                                    name_kh     : name_kh,
                                    amount      : amount,
                                    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){
                alert
                setTimeout(function () {
                    $(document).find('input[name=name_kh]').val(response.name_kh);
                    $(document).find('input[name=name_en]').val(response.name);
                    $(document).find('input[name=amount]').val(response.amount);
                    $(document).find('textarea[name=note]').text(response.note);
                });
            }
        }
    </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/job-benefit/index.blade.php ENDPATH**/ ?>