<style>
.col-md-3 {
    display: inline-block;
}
</style>
<div class="row align-items-center">
    <div class="col-sm-6">
        <div class="page-title-box">
            <h4 class="font-size-18">Attendance</h4>
            <ol class="breadcrumb mb-0">
                <li class="breadcrumb-item"><a href="javascript: void(0);">Users</a></li>
                <li class="breadcrumb-item active">Manage Attendance</li>
            </ol>
        </div>
    </div>

    <div class="col-sm-6">
        <div class="float-right d-none d-md-block">

            <!-- <a class="btn btn-primary waves-effect waves-light" href="<?php echo base_url(); ?>users/manage_users/add/">
                <i class="mdi mdi-plus mr-2"></i> Add New
            </a> -->
        </div>
    </div>
</div>
<?php
if ($this->session->flashdata('alert_success')) {
    ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
        <span aria-hidden="true">×</span>
    </button>
    <strong>Success!</strong> <?php echo $this->session->flashdata('alert_success'); ?>
</div>
<?php
}

if ($this->session->flashdata('alert_danger')) {
    ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
        <span aria-hidden="true">×</span>
    </button>
    <strong>Success!</strong> <?php echo $this->session->flashdata('alert_danger'); ?>
</div>
<?php
}

if ($this->session->flashdata('alert_warning')) {
    ?>
<div class="alert alert-warning alert-dismissible fade show" role="alert">
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
        <span aria-hidden="true">×</span>
    </button>
    <strong>Success!</strong> <?php echo $this->session->flashdata('alert_warning'); ?>
</div>
<?php
}
?>

<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-body">
                <h4 class="card-title">Date Wise Report</h4>

                <form action="<?php echo base_url(); ?>users/attendance/show" method="post"
                    enctype="multipart/form-data">
                    <div class="col-md-12">
                        <div class="form-group col-md-3">
                            <label>Year</label><span class="mandatory">*</span>
                            <?php
$already_selected_value = 2021;
$earliest_year = 1950;

print '<select class="form-control" name="year" id="year">';
foreach (range(date('Y'), $earliest_year) as $x) {
    print '<option value="' . $x . '"' . ($x === $already_selected_value ? ' selected="selected"' : '') . '>' . $x . '</option>';
}
print '</select>';

?>
                            <?php if (form_error('year')) {?>
                            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                    <span aria-hidden="true">×</span>
                                </button>
                                <?php echo form_error('year'); ?>
                            </div>
                            <?php }?>
                        </div>
                        <div class="form-group col-md-3">
                            <label>Month</label>
                            <select name="month" size='1' class="form-control">
                                <?php
for ($i = 0; $i < 12; $i++) {
    $time = strtotime(sprintf('%d months', $i));
    $label = date('F', $time);
    $value = date('n', $time);
    echo "<option value='$value'>$label</option>";
}
?>
                            </select>
                            <?php if (form_error('month')) {?>
                            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                    <span aria-hidden="true">×</span>
                                </button>
                                <?php echo form_error('month'); ?>
                            </div>
                            <?php }?>
                        </div>

                        <div class="form-group col-md-3">
                            <label>Role</label>
                            <select name="role" size='1' class="form-control">
                                <option value="">Select Role</option>
                                <?php foreach ($role as $r) {?>
                                <option value="<?php echo $r->role_id; ?>"><?php echo $r->role_name; ?></option>
                                <?php }?>
                            </select>
                            <?php if (form_error('role')) {?>
                            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                    <span aria-hidden="true">×</span>
                                </button>
                                <?php echo form_error('role'); ?>
                            </div>
                            <?php }?>
                        </div>


                        <div class="form-group mb-0">
                            <div>
                                <button name="submit" type="submit"
                                    class="btn btn-primary waves-effect waves-light mr-1">
                                    Submit
                                </button>
                                <button type="reset" class="btn btn-secondary waves-effect"
                                    onclick="location.href='<?php echo base_url(); ?>users/attendance'">
                                    Cancel
                                </button>
                            </div>
                        </div>
                    </div>
                </form>

            </div>
        </div>
    </div>
</div>


<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-body">

                <table id="dmstable" class="table table-striped table-bordered dt-responsive nowrap"
                    style="border-collapse: collapse; border-spacing: 0; width: 100%;">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>USERNAME</th>
                            <th>ROLE</th>
                            <th>TOTAL WORKING DAYS</th>
                            <th>WORKED DAYS</th>
                            <th>LEAVE DAYS</th>
                            <!-- <th>ACTION</th> -->
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($result as $key => $res) {?> <tr>
                            <td><?php echo $key + 1; ?></td>
                            <td><?php echo $res['user_name']; ?></td>
                            <td><?php echo $res['role_name']; ?></td>
                            <td><?php echo $res['working_days']; ?></td>
                            <td><?php echo $res['worked_days']; ?></td>
                            <td><?php echo $res['leave_days']; ?></td>
                        </tr>
                        <?php }?>

                    </tbody>
                </table>
                <?php
if (empty($result)) {?>
                <tr>
                    <h4 style="text-align:center;">NO RESULT FOUND</h4>
                </tr>
                <?php }
?>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
// datatable_init('<?php echo base_url(); ?>', '<?php echo $datatable; ?>', [{
//         "data": "id"
//     },
//     {
//         "data": "username"
//     },
//     {
//         "data": "email"
//     },
//     {
//         "data": "mobile"
//     },
//     {
//         "data": "role_name"
//     },
//     {
//         "data": "created_at",
//         "render": function(data) {
//             if (data) {
//                 var date = new Date(data),
//                     yr = date.getFullYear(),
//                     month = date.getMonth() < 10 ? '0' + date.getMonth() : date.getMonth(),
//                     day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(),
//                     newDate = day + '-' + month + '-' + yr;
//                 return newDate;
//             }
//         }
//     },
//     {
//         "data": "action"
//     }
// ]);

function delete_item(id) {
    Swal.fire({
        title: 'Are you sure?',
        text: "You won't be able to revert this!",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, delete it!'
    }).then(isConfirmed => {
        if (isConfirmed.value) {
            $.ajax({
                url: "<?php echo base_url(); ?>users/manage_users/delete/" + id + "/",
                success: function(result) {
                    if (result) {
                        window.location('settings/region');
                    }
                }
            });

            if (isConfirmed.value) {
                Swal.fire(
                    'Deleted!',
                    'User has been deleted.',
                    'success'
                );
                $('#dmstable').DataTable().ajax.reload();
            }
        }
    });
}
</script>