<?= $this->extend('layout/admin') ?>
<?= $this->section('title') ?>Manage <?= esc($domain->full_domain) ?><?= $this->endSection() ?>

<?= $this->section('content') ?>
<div class="page-header" style="margin-bottom: 1rem;">
    <h1 style="font-size: 1.5rem; font-weight: 700;">Manage <?= esc($domain->full_domain) ?></h1>
    <a href="<?= base_url('dashboard') ?>" class="btn btn-outline">Back</a>
</div>

<div style="display: grid; grid-template-columns: 2fr 1fr; gap: 2rem;">
    
    <!-- Left Column: DNS Records -->
    <div>
        <div class="card" style="margin-bottom: 2rem;">
            <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 1.5rem;">
                <h3 style="font-size: 1.125rem; font-weight: 600;">DNS Records</h3>
            </div>
            
            <div class="table-wrapper" style="box-shadow: none; border: 1px solid var(--border-color);">
                <table>
                    <thead style="background: white;">
                        <tr>
                            <th>Type</th>
                            <th>Name</th>
                            <th>Content</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if(empty($records)): ?>
                            <tr><td colspan="4" style="text-align: center; color: var(--text-muted); padding: 2rem;">No DNS records found.</td></tr>
                        <?php else: ?>
                            <?php foreach($records as $rec): ?>
                            <tr>
                                <td><strong><?= esc($rec->type) ?></strong></td>
                                <td><?= esc($rec->name) ?></td>
                                <td><?= esc($rec->content) ?></td>
                                <td>
                                    <a href="<?= base_url('dashboard/delete-record/' . $rec->id) ?>" style="color: var(--error); font-size: 0.875rem;" onclick="return confirm('Delete this record?')">Delete</a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>

        <div class="card">
            <h3 style="font-size: 1.125rem; font-weight: 600; margin-bottom: 1rem;">Add New Record</h3>
            <form action="<?= base_url('dashboard/add-record') ?>" method="POST" style="display: grid; grid-template-columns: 1fr 2fr 3fr auto; gap: 1rem; align-items: end;">
                <input type="hidden" name="domain_id" value="<?= esc($domain->id) ?>">
                
                <div class="form-group" style="margin-bottom: 0;">
                    <label class="form-label">Type</label>
                    <select name="type" class="form-control" required>
                        <option value="A">A</option>
                        <option value="AAAA">AAAA</option>
                        <option value="CNAME">CNAME</option>
                        <option value="TXT">TXT</option>
                        <option value="MX">MX</option>
                    </select>
                </div>
                
                <div class="form-group" style="margin-bottom: 0;">
                    <label class="form-label">Name</label>
                    <input type="text" name="name" class="form-control" placeholder="@ or www" required>
                </div>
                
                <div class="form-group" style="margin-bottom: 0;">
                    <label class="form-label">Content</label>
                    <input type="text" name="content" class="form-control" placeholder="192.168.1.1" required>
                </div>
                
                <button type="submit" class="btn btn-primary" style="height: 42px;">Add</button>
            </form>
        </div>
    </div>

    <!-- Right Column: Custom Nameservers -->
    <div>
        <div class="card" style="border-top: 4px solid var(--primary);">
            <h3 style="font-size: 1.125rem; font-weight: 600; margin-bottom: 0.5rem;">Custom Nameservers</h3>
            <p style="color: var(--text-muted); font-size: 0.875rem; margin-bottom: 1.5rem;">
                Delegating your subdomain to external nameservers. Setting these will override your individual DNS records above.
            </p>
            
            <form action="<?= base_url('dashboard/save-ns') ?>" method="POST">
                <input type="hidden" name="domain_id" value="<?= esc($domain->id) ?>">
                
                <div class="form-group">
                    <label class="form-label">Nameserver 1</label>
                    <input type="text" name="custom_ns1" class="form-control" placeholder="ns1.example.com" value="<?= esc($domain->custom_ns1) ?>">
                </div>
                
                <div class="form-group">
                    <label class="form-label">Nameserver 2</label>
                    <input type="text" name="custom_ns2" class="form-control" placeholder="ns2.example.com" value="<?= esc($domain->custom_ns2) ?>">
                </div>
                
                <button type="submit" class="btn btn-primary" style="width: 100%; justify-content: center;">Save Nameservers</button>
            </form>
        </div>
    </div>
</div>
<?= $this->endSection() ?>
