<?php $__env->startSection('title',' Leaves'); ?>
<?php $__env->startSection('content'); ?>
<h2 class="ms-2 mb-3 card-title"><?php echo e(__('messages.leave') ." ". __('messages.list')); ?></h2>

    <div class="card  mt-3 m-lg-2">
        <div class="card-body table_card  mt-2">
            <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('leave.create')): ?>
                <div class="add-new-button-hidden">
                    <button class="btn add_new_btn" data-bs-toggle="modal" data-bs-target="#leave_add_modal"
                            ><i class="plus-ico"></i> <?php echo e(__('messages.add') ." ". __('messages.leave')); ?>

                    </button>
                </div>
            <?php endif; ?>
            <table id="myTable" class="table table-bordered dt-responsive nowrap w-100">
                <thead>
                <tr>
                    <th><?php echo e(__('messages.sl')); ?></th>
                    <th><?php echo e(__('messages.leave')); ?> <?php echo e(__('messages.date_start')); ?></th>
                    <th><?php echo e(__('messages.leave')); ?> <?php echo e(__('messages.date_end')); ?></th>
                    <th><?php echo e(__('messages.leave')); ?></th>
                    <th><?php echo e(__('messages.description')); ?>-(<?php echo e(__('messages.leave')); ?>)</th>
                    <th><?php echo e(__('messages.status')); ?></th>
                    <th style="width:25% !important"><?php echo e(__('messages.action')); ?></th>
                </tr>
                </thead>
                <tbody>
                <?php $__currentLoopData = $leaves; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key=>$item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <tr>
                        <td><?php echo e(++$key); ?></td>
                        <td><?php echo e(date('d-M-Y', strtotime($item->leave_start))); ?></td>
                        <td><?php echo e(date('d-M-Y', strtotime($item->leave_end))); ?></td>
                        <td style="width:100px !important"><?php echo e($item->leavedatess); ?></td>
                        <td><?php echo e($item->leave_description); ?></td>
                        <td><?php echo e(($item->lStatus > 1) ? 'Approved' : 'Draft'); ?></td>
                        <td class="center">
                            <?php if($item->lStatus < 2): ?>
                                <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('leave.edit')): ?>
                                    <a id="edit_btn" edit-id="<?php echo e($item->ela_id); ?>" class="btn btn-sm btn-success" data-bs-toggle="modal"
                                    data-bs-target="#leave_add_modal"><i class="edit-ico"></i> Edit</a>
                                <?php endif; ?>

                                <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('leave.approve')): ?>
                                    <a id="approve_btn" onclick="approvePost(<?php echo e($item->ela_id); ?>)" class="btn btn-sm btn-warning">
                                        <i class="ok-ico"></i> Approve

                                        <form id="approve-form-<?php echo e($item->ela_id); ?>"
                                            action="<?php echo e(route('leaves.approve', $item->ela_id)); ?>" method="POST" style="display: none;">
                                            <?php echo csrf_field(); ?>
                                        </form>
                                    </a>
                                <?php endif; ?>

                                <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('leave.delete')): ?>
                                    <a href="#" class="btn btn-sm btn-danger" onclick="deletePost(<?php echo e($item->ela_id); ?>)">
                                        <img src="<?php echo e(asset('assets/icons/svg/delete.svg')); ?>" alt="" /> Remove
                                        <form id="delete-form-<?php echo e($item->ela_id); ?>"
                                            action="<?php echo e(route('leaves.destroy', $item->ela_id)); ?>" method="POST"
                                            style="display: none;">
                                            <?php echo csrf_field(); ?>
                                            <?php echo method_field('DELETE'); ?>
                                        </form>
                                    </a>
                                <?php endif; ?>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </tbody>
            </table>
        </div>
    </div>

    
    <div class="modal fade" id="leave_add_modal" tabindex="-1" aria-labelledby="exampleModalLabel"
         aria-hidden="true">
        <div class="modal-dialog modal-lg modal-dialog-centered">
            <div class="modal-content p-5">
                <h4 class="card-title"><?php echo e(__('messages.leave') ." ". __('messages.information')); ?></h4>
                <div class="modal-body p-0 mt-3">
                    <form action="<?php echo e(route('leaves.store')); ?>" method="POST" role="form" id="form1"
                          enctype="multipart/form-data">
                        <?php echo csrf_field(); ?>
                        <div class="row">
                            <div class="col-md-6">
                                <input type="hidden" name="ela_id" id="ela_id">
                                <div class="form-group">
                                    <label class="form-label" for="rec_id"><?php echo e(__('messages.employee_name')); ?><span class="text-danger">*</span></label>
                                    <select class="form-control <?php $__errorArgs = ['rec_id'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" name="rec_id" id="rec_id" required>
                                        <option value="">-Select-</option>
                                        <?php $__currentLoopData = $employees; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $dta): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <option value="<?php echo e($dta->rec_id); ?>"><?php echo e($dta->employee_name); ?></option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="form-label" for="rec_id"><?php echo e(__('messages.leave')); ?> <?php echo e(__('messages.type')); ?><span class="text-danger">*</span></label>
                                    <select class="form-control <?php $__errorArgs = ['lTypeId'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" name="lTypeId" id="lTypeId" required>
                                        <option value="">-Select-</option>
                                        <?php $__currentLoopData = $leave_type_data; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $dta): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <option value="<?php echo e($dta->lTypeId); ?>"><?php echo e($dta->leave_type_name); ?></option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                    <span class="total_days" style="font-size:12px"></span>
                                </div>
                            </div>
                        </div>
                        <div class="row mt-3">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="form-label" for="leave_start"><?php echo e(__('messages.leave')); ?> <?php echo e(__('messages.date_start')); ?><span class="text-danger">*</span></label>
                                    <input type="date" class="form-control" name="leave_start" placeholder="<?php echo e(__('messages.enter')); ?>" id="leave_start"/>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="form-label" for="leave_end"><?php echo e(__('messages.leave')); ?> <?php echo e(__('messages.date_end')); ?><span class="text-danger">*</span></label>
                                    <input type="date" class="form-control" name="leave_end" placeholder="<?php echo e(__('messages.enter')); ?>" id="leave_end"/>
                                </div>
                            </div>
                        </div>

                        <div class="row mt-3">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="form-label" for="leave_description"><?php echo e(__('messages.leave')); ?> <?php echo e(__('messages.description')); ?><span class="text-danger">*</span></label>
                                    <textarea type="text" class="form-control <?php $__errorArgs = ['leave_description'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                           name="leave_description" placeholder="<?php echo e(__('messages.enter') ." ". __('messages.leave') ." ". __('messages.description')); ?>" required id="leave_description"/></textarea>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="form-label" for="responsible_emp_id">Responsible Persion<span class="text-danger"></span></label>
                                    <select class="form-control <?php $__errorArgs = ['responsible_emp_id'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" name="responsible_emp_id" id="responsible_emp_id" required>
                                        <option value="">-Select-</option>
                                        <?php $__currentLoopData = $employees; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $dta): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <option value="<?php echo e($dta->id); ?>"><?php echo e($dta->employee_name); ?></option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="mt-5 d-flex justify-content-end ">
                            <button type="button" class="btn btn-sm btn-danger me-2" data-bs-dismiss="modal" style="">
                                <?php echo e(__('messages.close')); ?>

                            </button>
                            <?php if (app(\Illuminate\Contracts\Auth\Access\Gate::class)->check('leave.create')): ?>
                                <button type="submit" class="btn btn-sm btn-primary" >
                                    <?php echo e(__('messages.save')); ?>

                                </button>
                            <?php endif; ?>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
<?php $__env->stopSection(); ?>
<?php $__env->startPush('scripts'); ?>
    <script type="text/javascript">


        function approvePost(id) {
            swal({
                title: 'Are you sure?',
                text: "You won't be able to revert this!",
                type: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Yes, approve it!',
                cancelButtonText: 'No, cancel!',
                confirmButtonClass: 'btn btn-warning ms-2',
                cancelButtonClass: 'btn btn-success',
                buttonsStyling: false,
                reverseButtons: true
            }).then((result) => {
                if (result.value) {
                    event.preventDefault();
                    document.getElementById('approve-form-' + id).submit();
                }
            })
        }
        $(document).ready(function ($) {
            $(document).on('click', '#edit_btn', function () {
                let id = $(this).attr('edit-id');
                $.ajax({
                    type: "GET",
                    url: "<?php echo e(url('system/leaves')); ?>/" + id + "/edit",
                    success: function (res) {
                        $('#ela_id').val(res.ela_id);
                        $('#rec_id').val(res.rec_id);
                        $('#lTypeId').val(res.lTypeId);
                        $('#leave_start').val(res.leave_start);
                        $('#leave_end').val(res.leave_end);
                        $('#leave_description').val(res.leave_description);
                        $('#responsible_emp_id').val(res.responsible_emp_id);
                        $('#total_days').val(res.total_days);

                        let rec_id = res.rec_id;
                        let lTypeId = res.lTypeId;
                        $.ajax({
                            type: "POST",
                            url: "<?php echo e(url('system/leaves/availableLeaveDays')); ?>",
                            data:{'rec_id' : rec_id, 'lTypeId' : lTypeId, 'empOrOthers' : 1, '_token' : '<?php echo e(csrf_token()); ?>'},
                            dataType:'json',
                            success: function (response) {
                                $('.total_days').html('Available Leave Days : ' + response.availableLeaveDays);
                            },
                            error: function (response) {
                                console.log(response);
                            }
                        });

                    },
                    error: function (res) {
                        console.log(res);
                    }
                });
            });


            $('.add_new_btn').click(function () {
                $('.total_days').html('');
                document.getElementById("form1").reset();
            });

            $(document).on('change', '#lTypeId, #rec_id', function () {
                let rec_id = $('#rec_id').val();
                let lTypeId = $('#lTypeId').val();
                // let id = $(this).data('id');
                // axios.post("<?php echo e(url('system/leaves/availableLeaveDays')); ?>", {
                //     rec_id: rec_id,
                //     lTypeId: lTypeId,
                //     empOrOthers: 1
                // })
                // .then(function (response) {
                //     $('.total_days').html('Available Leave Days : ' + response.availableLeaveDays);
                // }).catch(function (error) {
                //     console.log(error)
                // })


                $.ajax({
                    type: "POST",
                    url: "<?php echo e(url('system/leaves/availableLeaveDays')); ?>",
                    data:{'rec_id' : rec_id, 'lTypeId' : lTypeId, 'empOrOthers' : 1, '_token' : '<?php echo e(csrf_token()); ?>'},
                    dataType:'json',
                    success: function (response) {
                        $('.total_days').html('Available Leave Days : ' + response.availableLeaveDays);
                    },
                    error: function (response) {
                        console.log(response);
                    }
                });
            });
        });

        // $("#leave_start").datepicker({
        //     autoHide: true,
        //     endDate: "today",
        //     // format: 'dd-mm-yyyy',
        //     format: 'yyy-mm-dd',
        // });
        // $("#leave_end").datepicker({
        //     autoHide: true,
        //     endDate: "today",
        //     // format: 'dd-mm-yyyy',
        //     format: 'yyy-mm-dd',
        // });
    </script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('backend.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH D:\laragon\www\hr_and_att\resources\views/backend/hr/attend/leave.blade.php ENDPATH**/ ?>