<?php /* var_dump($permission); */ ?>
<?php (empty($permission) ? redirect(base_url("error404")) : ""); ?>
<?php if (!empty($this->app->is_can($permission, "view"))) { /* Can View */ ?>
    <!-- SweetAlert2 -->
    <link rel="stylesheet" href="<?php echo base_url(); ?>vendor/sweetalert2-theme-bootstrap-4/bootstrap-4.min.css">
    <!-- Toastr -->
    <link rel="stylesheet" href="<?php echo base_url(); ?>vendor/toastr/toastr.min.css">
    <!-- DataTables -->
    <link rel="stylesheet" href="<?php echo base_url(); ?>vendor/datatables-bs4/css/dataTables.bootstrap4.min.css">
    <link rel="stylesheet" href="<?php echo base_url(); ?>vendor/datatables-rowreorder/css/rowReorder.bootstrap4.min.css">
    <!-- Bootstrap-Switch  & Plugins -->
    <link rel="stylesheet" href="<?php echo base_url(); ?>vendor/bootstrap-switch/css/bootstrap3/bootstrap-switch.min.css">
    <style>
        .btn-xs {
            height: 22px;
            padding: 2px 5px;
            font-size: 12px;
            line-height: 1.5;
            /* If Placeholder of the input is moved up, rem/modify this. */
            border-radius: 3px;
        }

        .IsSmall {
            width: 15px;
            text-align: center;
        }
    </style>

    <!-- ============================================================== -->
    <!-- Start Page Content -->
    <!-- ============================================================== -->

    <div class="col-12 pt-3">
        <div class="row">
            <div class="col-6">
                <div class="text-left">
                    <a class="btn btn-xs mb-2" href="<?php echo base_url(); ?>sys/company">
                        <h5><i class="fa fa-backward"></i> <?php echo $company->company_name; ?></h5>
                    </a>
                </div>
            </div>
            <div class="col-6">

            </div>
        </div>
        <div class="table-responsive m-t-40">
            <div class="text-right">
                <?php if ($this->app->is_can($permission, "add")) { ?>
                    <a id="addBtnLeavetype" class="btn btn-primary btn-xs mb-2" data-toggle="modal" data-backdrop="static" data-keyboard="false" data-target="#addModalLeavetype" href="javascript:void(0);"><span><i class="fa fa-plus"></i> ประเภทการลา</span></a>
                <?php } ?>
            </div>
            <table id="mTable" class="table table-bordered table-striped table-hover" style="width:100%">
                <thead>
                    <tr>
                        <th class="IsSmall">NO.</th>
                        <th>ประเภทการลา</th>
                        <th>จำนวนวันลา/ปี</th>
                        <th>จำนวนวันลาติดกันสูงสุด/ครั้ง</th>
                        <th>ระยะเวลาวันที่ต้องแจ้งล่วงหน้าก่อนลา</th>
                        <th>จำนวนวันลาสะสมสูงสุด/ปี</th>
                        <?php if ($this->app->is_can($permission, "edit")) { ?>
                            <th class="IsSmall"><i class="fa fa-edit"></i></th>
                        <?php } ?>
                        <?php if ($this->app->is_can($permission, "delete")) { ?>
                            <th class="IsSmall"><i class="fa fa-trash-alt"></i></th>
                        <?php } ?>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $sql = "SELECT company_leavetype.*, leavetype.leavetype_name, leavetype.leavetype_description FROM company_leavetype 
                        INNER JOIN leavetype ON leavetype.id = company_leavetype.leavetype_id
                        WHERE company_leavetype.company_id ='" . $this->uri->segment(4) . "' 
                        ORDER BY leavetype.sort";
                    if ($this->db->query($sql)->num_rows() > 0) {
                        foreach ($this->db->query($sql)->result() as $index => $item) {
                    ?>
                            <tr>
                                <td class="IsSmall"><?php echo ($index + 1); ?></td>
                                <td><?php echo $item->leavetype_name; ?></th>
                                <td><?php echo $item->num; ?></td>
                                <td><?php echo $item->per_round; ?></i></td>
                                <td><?php echo $item->advance_notice_day; ?></i></td>
                                <td><?php echo $item->collect; ?></td>
                                <?php if ($this->app->is_can($permission, "edit")) { ?>
                                    <td class="IsSmall">
                                        <button type="button" class="btn btn-info btn-xs editBtnLeavetype" data-id="<?php echo $item->id; ?>" data-toggle="modal" data-backdrop="static" data-keyboard="false" data-target="#editModalLeavetype"><i class="fas fa-pencil-alt"></i></button>
                                    </td>
                                <?php } ?>
                                <?php if ($this->app->is_can($permission, "delete")) { ?>
                                    <td class="IsSmall">
                                        <button type="button" class="btn btn-danger btn-xs confirmDelete" data-href="<?php echo base_url('sys/company/company_leavetype_delete/' . $item->id); ?>"> <i class=" fas fa-trash-alt"></i></button>
                                    </td>
                                <?php } ?>
                            </tr>
                    <?php
                        }
                    } else {
                        echo '<tr class="text-center"><td colspan="8">ยังไม่ตั้งประเภทการลา</td>';
                    }
                    ?>
                </tbody>
            </table>
        </div>
    </div>

    <?php if ($this->app->is_can($permission, "add")) { ?>
        <div class="modal fade" id="addModalLeavetype" tabindex="-1" role="dialog">
            <form id="addFormLeavetype" method="post" action="<?php echo base_url('sys/company/company_leavetype_add'); ?>">
                <input type="hidden" name="sort">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h4 class="modal-title">เพิ่มประเภทการลา</h4>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        </div>
                        <div class="modal-body">
                            <input type="hidden" name="company_id" value="<?php echo $this->uri->segment(4); ?>">
                            <div class="form-group">
                                <label class="control-label">ประเภทการลา<span class="text-danger">*</span></label>
                                <div class="controls">
                                    <select name="leavetype_id" class="form-control select2-add" required>
                                        <option value="">เลือกประเภทการลา</option>
                                        <?php
                                        $sql = "SELECT leavetype.* FROM leavetype WHERE leavetype.is_active ='1' 
                                        AND (SELECT COUNT(id) FROM company_leavetype WHERE company_id ='" . $this->uri->segment(4) . "' AND leavetype_id  = leavetype.id) =0
                                        ORDER BY leavetype.sort";
                                        foreach ($this->db->query($sql)->result() as $item) {
                                            echo '<option value="' . $item->id . '">' . $item->leavetype_name . '</option>';
                                        }
                                        ?>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label">จำนวนวันลา/ปี<span class="text-danger">*</span></label>
                                <div class="controls">
                                    <div class="input-group">
                                        <input type="number" name="num" class="form-control" autocomplete="off" placeholder="หากไม่จำกัดวันลาต่อปี ระบุจำนวนเป็น 0" required>
                                        <div class="input-group-append">
                                            <span class="input-group-text input-group-addon">วัน</span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label">ระยะเวลาวันที่ต้องแจ้งล่วงหน้าก่อนลา<span class="text-danger">*</span></label>
                                <div class="controls">
                                    <div class="input-group">
                                        <input type="number" name="advance_notice_day" class="form-control" autocomplete="off" required>
                                        <div class="input-group-append">
                                            <span class="input-group-text input-group-addon">วัน</span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label">จำนวนวันลาสูงสุด/ครั้ง<span class="text-danger">*</span></label>
                                <div class="controls">
                                    <div class="input-group">
                                        <input type="number" name="per_round" class="form-control" autocomplete="off" required>
                                        <div class="input-group-append">
                                            <span class="input-group-text input-group-addon">วัน</span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label">จำนวนวันลาสะสมสูงสุด/ปี<span class="text-danger">*</span></label>
                                <div class="controls">
                                    <div class="input-group">
                                        <input type="number" name="collect" class="form-control" autocomplete="off" required>
                                        <div class="input-group-append">
                                            <span class="input-group-text input-group-addon">วัน</span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="reset" class="btn btn-xs btn-default" data-dismiss="modal">ยกเลิก</button>
                            <button type="submit" class="btn btn-xs btn-primary">บันทึก</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
        <!-- /.modal -->
    <?php } ?>

    <?php if ($this->app->is_can($permission, "edit")) { ?>
        <div class="modal fade" id="editModalLeavetype" tabindex="-1" role="dialog">
            <form id="editFormLeavetype" method="post" action="<?php echo base_url('sys/company/company_leavetype_edit'); ?>" novalidate>
                <input type="hidden" name="id">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h4 class="modal-title">แก้ประเภทการลา</h4>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        </div>
                        <div class="modal-body">
                            <input type="hidden" name="company_id" value="<?php echo $this->uri->segment(4); ?>">
                            <div class="form-group">
                                <label class="control-label">ประเภทการลา</label>
                                <div class="controls">
                                    <select name="leavetype_id" class="form-control select2-add" disabled>
                                        <option value="">เลือกประเภทการลา</option>
                                        <?php
                                        $sql = "SELECT * FROM leavetype WHERE is_active ='1' ORDER BY sort";
                                        foreach ($this->db->query($sql)->result() as $item) {
                                            echo '<option value="' . $item->id . '">' . $item->leavetype_name . '</option>';
                                        }
                                        ?>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label">จำนวนวันลา/ปี<span class="text-danger">*</span></label>
                                <div class="controls">
                                    <div class="input-group">
                                        <input type="number" name="num" class="form-control" autocomplete="off" placeholder="หากไม่จำกัดวันลาต่อปี ระบุจำนวนเป็น 0" required>
                                        <div class="input-group-append">
                                            <span class="input-group-text input-group-addon">วัน</span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label">ระยะเวลาวันที่ต้องแจ้งล่วงหน้าก่อนลา<span class="text-danger">*</span></label>
                                <div class="controls">
                                    <div class="input-group">
                                        <input type="number" name="advance_notice_day" class="form-control" autocomplete="off" required>
                                        <div class="input-group-append">
                                            <span class="input-group-text input-group-addon">วัน</span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label">จำนวนวันลาสูงสุด/ครั้ง<span class="text-danger">*</span></label>
                                <div class="controls">
                                    <div class="input-group">
                                        <input type="number" name="per_round" class="form-control" autocomplete="off" required>
                                        <div class="input-group-append">
                                            <span class="input-group-text input-group-addon">วัน</span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label">จำนวนวันลาสะสมสูงสุด/ปี<span class="text-danger">*</span></label>
                                <div class="controls">
                                    <div class="input-group">
                                        <input type="number" name="collect" class="form-control" autocomplete="off" required>
                                        <div class="input-group-append">
                                            <span class="input-group-text input-group-addon">วัน</span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="reset" class="btn btn-xs btn-default" data-dismiss="modal">ยกเลิก</button>
                            <button type="submit" class="btn btn-xs btn-primary">บันทึก</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
        <!-- /.modal -->
    <?php } ?>

    <!-- jQuery -->
    <script src="<?php echo base_url(); ?>vendor/jquery/jquery.min.js"></script>
    <!-- jQuery UI 1.11.4 -->
    <script src="<?php echo base_url(); ?>vendor/jquery-ui/jquery-ui.min.js"></script>
    <!-- Select2 -->
    <script src="<?php echo base_url(); ?>vendor/select2/js/select2.full.min.js"></script>
    <!-- SweetAlert2 -->
    <script src="<?php echo base_url(); ?>vendor/sweetalert2/sweetalert2.min.js"></script>
    <!-- Toastr -->
    <script src="<?php echo base_url(); ?>vendor/toastr/toastr.min.js"></script>
    <!-- DataTables  & Plugins -->
    <script src="<?php echo base_url(); ?>vendor/datatables/jquery.dataTables.min.js"></script>
    <script src="<?php echo base_url(); ?>vendor/datatables-bs4/js/dataTables.bootstrap4.min.js"></script>
    <script src="<?php echo base_url(); ?>vendor/datatables-responsive/js/dataTables.responsive.min.js"></script>
    <script src="<?php echo base_url(); ?>vendor/datatables-responsive/js/responsive.bootstrap4.min.js"></script>
    <script src="<?php echo base_url(); ?>vendor/datatables-buttons/js/dataTables.buttons.min.js"></script>
    <script src="<?php echo base_url(); ?>vendor/datatables-buttons/js/buttons.bootstrap4.min.js"></script>
    <script src="<?php echo base_url(); ?>vendor/datatables-rowreorder/js/dataTables.rowReorder.min.js"></script>
    <script src="<?php echo base_url(); ?>vendor/datatables-rowreorder/js/rowReorder.bootstrap4.min.js"></script>
    <!-- Bootstrap-Switch  & Plugins -->
    <script src="<?php echo base_url(); ?>vendor/bootstrap-switch/js/bootstrap-switch.min.js"></script>
    <!-- jquery-validation -->
    <script src="<?php echo base_url(); ?>vendor/jquery-validation/jquery.validate.min.js"></script>
    <script src="<?php echo base_url(); ?>vendor/jquery-validation/additional-methods.min.js"></script>

    <script type="text/javascript">
        var timestamp = '<?php echo $timestamp; ?>';

        ! function(window, document, $) {
            "use strict";

        }(window, document, jQuery);

        $(document).ready(function() {
            <?php if ($this->app->is_can($permission, "add")) { ?>
                $('#addBtnLeavetype').click(function() {
                    var $form = $('#addFormLeavetype');
                    $form.trigger("reset");
                    $form.find('.is-invalid,.is-valid').css('border-color', '').removeClass('is-invalid').removeClass('is-valid');
                    $form.find('.error.invalid-feedback').html('');
                });
            <?php } ?>

            <?php if ($this->app->is_can($permission, "edit")) { ?>
                $('.editBtnLeavetype').click(function() {
                    var $form = $('#editFormLeavetype');
                    $form.find('.is-invalid,.is-valid').css('border-color', '').removeClass('is-invalid').removeClass('is-valid');
                    $form.find('.error.invalid-feedback').html('');
                    $.post("<?php echo base_url('sys/company/company_leavetype_ajax_data'); ?>", {
                            id: $(this).data('id')
                        },
                        function(data) {
                            $form.find('[name="id"]').val(data['id']);
                            $form.find('[name="company_id"]').val(data['company_id']).trigger('change');
                            $form.find('[name="leavetype_id"]').val(data['leavetype_id']);
                            $form.find('[name="num"]').val(data['num']);
                            $form.find('[name="advance_notice_day"]').val(data['advance_notice_day']);
                            $form.find('[name="per_round"]').val(data['per_round']);
                            $form.find('[name="collect"]').val(data['collect']);
                        }, "json");
                });
            <?php } ?>

        });

        $('#addFormLeavetype').validate({
            rules: {},
            messages: {},
            errorElement: 'span',
            errorPlacement: function(error, element) {
                error.addClass('invalid-feedback');
                element.closest('.form-group').append(error);
            },
            highlight: function(element, errorClass, validClass) {
                $(element).addClass('is-invalid');
            },
            unhighlight: function(element, errorClass, validClass) {
                $(element).removeClass('is-invalid');
            }
        });

        $('#editFormLeavetype').validate({
            rules: {},
            messages: {},
            errorElement: 'span',
            errorPlacement: function(error, element) {
                error.addClass('invalid-feedback');
                element.closest('.form-group').append(error);
            },
            highlight: function(element, errorClass, validClass) {
                $(element).addClass('is-invalid');
            },
            unhighlight: function(element, errorClass, validClass) {
                $(element).removeClass('is-invalid');
            }
        });

        /* Conform delete data in table */
        $('.confirmDelete').on('click', function() {
            Swal.fire({
                title: 'คุณต้องการลบรายการนี้หรือไม่?',
                width: 300,
                showDenyButton: false,
                showCancelButton: true,
                confirmButtonText: 'ตกลง',
                confirmButtonColor: '#dc3545',
            }).then((result) => {
                /* Read more about isConfirmed, isDenied below */
                if (result.isConfirmed) {
                    location.href = $(this).data('href');
                }
            })
        });
    </script>
<?php
} /* Can View */ ?>