<div class="modal-header">
    <h5 class="modal-title"><?= $title ?></h5>
    <button class="close" type="button" data-dismiss="modal" aria-label="Close"><span class="font-weight-light" aria-hidden="true">&times;</span></button>
</div>
<div class="modal-body text-left">
    <form id="add_submit">
        <input type="hidden" name="action" value="<?= $action ?>" />
        <input type="hidden" name="id" value="<?php if (isset($detail['id'])) echo $detail['id']; ?>" />

        <!-- Nama Server -->
        <label>Nama Server:</label>
        <input type="text" name="server_name" value="<?php if (isset($detail['server_name'])) echo $detail['server_name']; ?>" class="form form-control mb-3" />

        <label>Kepemilikan Server:</label>
        <select name="kepemilikan_dropdown" id="kepemilikan_dropdown" class="form-control mb-3" onchange="toggleKepemilikanFields()">
            <option value="Dinas Komunikasi dan Informatika Kota Medan" <?= (isset($detail['kepemilikan_dropdown']) && $detail['kepemilikan_dropdown'] == 'Dinas Komunikasi dan Informatika Kota Medan') ? 'selected' : '' ?>>Dinas Komunikasi dan Informatika Kota Medan</option>
            <option value="lainnya" <?= (isset($detail['kepemilikan_dropdown']) && $detail['kepemilikan_dropdown'] == 'lainnya') ? 'selected' : '' ?>>Lainnya</option>
        </select>

        <div id="kepemilikan_fields" style="display: none;">
            Kepemilikan:
            <input type="text" name="kepemilikan_lainnya" value="<?php if (isset($detail['kepemilikan_lainnya'])) echo $detail['kepemilikan_lainnya']; ?>" class="form-control mb-2" />

            Tanggal Serah Terima:
            <input type="date" name="serah_terima" value="<?php if (isset($detail['serah_terima'])) echo $detail['serah_terima']; ?>" class="form-control mb-2" />

            Penanggung Jawab:
            <input type="text" name="penanggung_jawab" id="penanggung_jawab" value="Dinas Komunikasi dan Informatika Kota Medan" class="form-control mb-2" />
        </div>


        <!-- Spesifikasi -->
        <label>Spesifikasi:</label>
        <div class="row">
            <div class="col-md-6">
                Core CPU:
                <input type="number" name="cpu_core" value="<?php if (isset($detail['cpu_core'])) echo $detail['cpu_core']; ?>" class="form-control mb-2" />
            </div>
            <div class="col-md-6">
                RAM (GB):
                <input type="number" name="ram" value="<?php if (isset($detail['ram'])) echo $detail['ram']; ?>" class="form-control mb-2" />
            </div>
            <div class="col-md-6">
                Storage (GB):
                <input type="number" name="storage" value="<?php if (isset($detail['storage'])) echo $detail['storage']; ?>" class="form-control mb-2" />
            </div>
            <div class="col-md-6">
                Status:
                <select name="status" class="form-control mb-2">
                    <option value="Active" <?= isset($detail['status']) && $detail['status'] == 'Active' ? 'selected' : '' ?>>Active</option>
                    <option value="Maintanance" <?= isset($detail['status']) && $detail['status'] == 'Maintanance' ? 'selected' : '' ?>>Maintanance</option>
                    <option value="Idle" <?= isset($detail['status']) && $detail['status'] == 'Idle' ? 'selected' : '' ?>>Idle</option>
                    <option value="Development" <?= isset($detail['status']) && $detail['status'] == 'Development' ? 'selected' : '' ?>>Development</option>
                    <option value="Retired" <?= isset($detail['status']) && $detail['status'] == 'Retired' ? 'selected' : '' ?>>Retired</option>
                    <option value="Inventory" <?= isset($detail['status']) && $detail['status'] == 'Inventory' ? 'selected' : '' ?>>Inventory</option>
                </select>
            </div>
            <div class="col-md-12">
                Sistem Operasi:
                <input type="text" name="operating_system" value="<?php if (isset($detail['operating_system'])) echo $detail['operating_system']; ?>" class="form-control mb-2" />
            </div>
        </div>
        <label>Tanggal:</label>
        <div class="row">
            <div class="col-md-6">
                Tanggal Server Mulai Diaktifkan:
                <input type="date" name="server_date" value="<?php if (isset($detail['server_date'])) echo $detail['server_date']; ?>" class="form-control mb-2" />
            </div>
            <div class="col-md-6">
                Tanggal Terakhir Dievaluasi:
                <input type="date" name="last_evaluated" value="<?php if (isset($detail['last_evaluated'])) echo $detail['last_evaluated']; ?>" class="form-control mb-2" onchange="generateMaintenanceDate()" />
            </div>
            <div class="col-md-6">
                Jadwal Pemeliharaan:
                <input type="date" name="next_maintenance" id="next_maintenance" value="<?php if (isset($detail['next_maintenance'])) echo $detail['next_maintenance']; ?>" class="form-control mb-2" />
            </div>
        </div>

        <input type="submit" name="submit" value="<?= $tombol ?>" class="btn btn-success mt-3" />
    </form>
</div>
<script>
    $(document).ready(function() {
        $('.custom-file-input').on('change', function(e) {
            var $this = $(e.currentTarget);
            var fileName = $this.val().split('\\').pop();
            $this.next('.custom-file-label').addClass('selected').html(fileName);
        });

        $('#add_submit').submit(function(e) {
            e.preventDefault(); // Prevent the default form submission

            var form = $(this)[0]; // Get the raw HTML form element
            var formData = new FormData(form); // Create a new FormData object

            $.ajax({
                type: 'POST',
                url: "<?= site_url('admin/server/data') ?>",
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                data: formData, // Use the FormData object as the data
                processData: false, // Prevent jQuery from processing the data
                contentType: false, // Prevent jQuery from setting the content type
                success: function(response) {
                    $('#add_submit input[type="text"]').val('');
                    $('#add_submit textarea').val('');
                    $('#add').modal('hide');
                    dataindex();
                    showToast("success", response.message);
                },
                error: function(xhr, status, error) {
                    var response = xhr.responseJSON;
                    showToastError('Error', response);
                }
            });
        });

        $('#add').on('hidden.bs.modal', function() {
            dataindex();
            $('#report_edit').html('');
        });
    });
</script>
<script>
    function generateMaintenanceDate() {
        const lastEvalInput = document.querySelector('input[name="last_evaluated"]');
        const maintenanceInput = document.getElementById('next_maintenance');
        if (lastEvalInput.value) {
            const evalDate = new Date(lastEvalInput.value);
            evalDate.setMonth(evalDate.getMonth() + 3);
            const yyyy = evalDate.getFullYear();
            const mm = String(evalDate.getMonth() + 1).padStart(2, '0');
            const dd = String(evalDate.getDate()).padStart(2, '0');
            maintenanceInput.value = `${yyyy}-${mm}-${dd}`;
        }
    }

    // Auto set jadwal jika last_evaluated sudah terisi saat load
    window.onload = function() {
        const lastEval = document.querySelector('input[name="last_evaluated"]').value;
        const nextMaint = document.querySelector('input[name="next_maintenance"]').value;
        if (lastEval && !nextMaint) {
            generateMaintenanceDate();
        }
    };
</script>
<script>
    function toggleKepemilikanFields() {
        const dropdown = document.getElementById('kepemilikan_dropdown');
        const fields = document.getElementById('kepemilikan_fields');
        const pj = document.getElementById('penanggung_jawab');

        if (dropdown.value === 'lainnya') {
            fields.style.display = 'block';
            pj.value = 'Dinas Komunikasi dan Informatika Kota Medan';
        } else {
            fields.style.display = 'none';
            pj.value = '';
        }
    }

    // Auto tampilkan jika sebelumnya "lainnya" disimpan
    window.onload = function() {
        toggleKepemilikanFields();
    };
</script>