<!-- home.php -->
<?php $this->extend('admin/layout/main') ?>

<?php $this->section('content') ?>
<!-- Page content goes here -->

<div class="card mb-3">
    <div class="bg-holder d-none d-lg-block bg-card" style="background-image:url(<?= base_url() ?>assets/img/illustrations/corner-4.png);">
    </div>
    <!--/.bg-holder-->
    <div class="card-body">
        <div class="row">
            <div class="col-lg-8">
                <h4 class="mb-0">List Server</h4>
            </div>
        </div>
    </div>
</div>
<div class="card mb-3">
    <div class="card-header">
        <div class="row flex-between-center">
            <div class="col">
                <button class="btn btn-primary" onclick="adddata()"><i class="fas fa-plus-square"></i> Tambah Server</button>
            </div>
        </div>
    </div>
    <div class="card-body px-0 pt-0">
        <div class="row">
            <div class="col-12">
                <div class="dashboard-data-table">
                    <table class="table table-sm table-dashboard data-table no-wrap mb-0 fs--1 w-100">
                        <thead class="bg-200">
                            <tr>
                                <th class="sort">Jadwal Pemeliharaan</th>
                                <th>Name</th>
                                <th class="sort">Kepemilikan</th>
                                <th class="sort">Core CPU</th>
                                <th class="sort">RAM (GB)</th>
                                <th class="sort">Storage (GB)</th>
                                <th class="sort">Status</th>
                                <th class="sort">OS</th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody class="bg-white">
                            <?php foreach ($servers as $server): ?>
                                <?php
                                // Determine ownership
                                $ownership = !empty($server['kepemilikan_lainnya'])
                                    ? $server['kepemilikan_lainnya']
                                    : $server['kepemilikan_dropdown'];

                                // Format dates
                                $maintenance_date = date('d-m-Y', strtotime($server['next_maintenance']));

                                // Status badge classes
                                $status_classes = [
                                    'Active' => 'badge-soft-success',
                                    'Maintanance' => 'badge-soft-warning',
                                    'Idle' => 'badge-soft-secondary',
                                    'Development' => 'badge-soft-info',
                                    'Retired' => 'badge-soft-danger',
                                    'Inventory' => 'badge-soft-dark'
                                ];

                                // Status icons
                                $status_icons = [
                                    'Active' => 'fa-check',
                                    'Maintanance' => 'fa-wrench',
                                    'Idle' => 'fa-pause-circle',
                                    'Development' => 'fa-flask',
                                    'Retired' => 'fa-power-off',
                                    'Inventory' => 'fa-ban'
                                ];
                                ?>
                                <tr>
                                    <td><?= $maintenance_date ?></td>
                                    <td><?= $server['server_name'] ?></td>
                                    <td><?= $ownership ?></td>
                                    <td><?= $server['cpu_core'] ?></td>
                                    <td><?= $server['ram'] ?></td>
                                    <td><?= $server['storage'] ?></td>
                                    <td class="align-middle text-left fs-0">
                                        <span class="badge badge rounded-capsule <?= $status_classes[$server['status']] ?>">
                                            <?= $server['status'] ?>
                                            <span class="ml-1 fas <?= $status_icons[$server['status']] ?>" data-fa-transform="shrink-2"></span>
                                        </span>
                                    </td>
                                    <td><?= $server['operating_system'] ?></td>

                                    <td class="align-middle white-space-nowrap">
                                        <div class="dropdown text-sans-serif">
                                            <button class="btn btn-link text-600 btn-sm dropdown-toggle btn-reveal mr-3" type="button" id="dropdown1" data-toggle="dropdown" data-boundary="html" aria-haspopup="true" aria-expanded="false">
                                                <span class="fas fa-ellipsis-h fs--1"></span>
                                            </button>
                                            <div class="dropdown-menu dropdown-menu-right border py-0" aria-labelledby="dropdown1">
                                                <div class="bg-white py-2">
                                                    <a class="dropdown-item" href="#!">View</a>
                                                    <div class="dropdown-divider"></div>
                                                    <a class="dropdown-item text-warning" href="#!">Edit</a>
                                                    <a class="dropdown-item text-danger" href="#!">Delete</a>
                                                </div>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<?php $this->endsection() ?>
<?php $this->section('script') ?>
<script>
    function deletedata(iddata) {
        $('#alert_modal').modal('show');
        $("#click_yes").off("click").on("click", function() {
            $.ajax({
                type: 'DELETE',
                url: "<?= site_url('admin/admin/data') ?>/" + iddata,
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                success: function(response) {
                    $('#alert_modal').modal('hide');
                    showToast('success', response.message);
                    dataindex(); // Refresh the table
                },
                error: function(xhr, status, error) {
                    showToastError(error, xhr.responseJSON);
                }
            });
        });
    }

    function editdata(iddata) {
        $.get("<?= site_url('admin/admin/edit') ?>/" + iddata, function(data, status) {
            $("#editor_add").html(data);
            $('#add').modal('toggle');
        });
    }

    function adddata() {
        $('#editor_add').load('<?= site_url('admin/server/add') ?>', function() {
            $('#add').modal({
                show: true
            });
        });
    }
</script>

<?php $this->endsection() ?>