{% include 'header.twig' %}

<div class="wrapper-content__body">
    <div class="appContent">
        <div class="hiddenTitle">
            <div id="appTt">Child Panel</div>
            <div id="appTx">We're the best!</div>
        </div>

        <section class="dashboard-head-wrapper">
            <div class="container-fluid">
                <div class="dashboard-head card-border">
                    <div class="dhead-body">
                        <h2 class="title">Rent a Child Panel</h2>
                        <p class="text">
                            We charge {{site_base_currency_symbol}}{{" "}}{{site["childpanel_price"]}}/month for a child panel.<br>
                            Special discounts are available if you spend more than $500/month.
                        </p>
                    </div>
                    <div class="dhead-img-wrapper">
                        <img src="https://cdn.mypanel.link/6f7slz/hjd38x9quo3byspn.png" class="dhead-img" alt="Panel">
                    </div>
                </div>
            </div>
        </section>

        <section>
            <div class="container-fluid">
                <div class="row">
                    <div class="col-lg-4 mb-4 mb-lg-0">
                        <div class="card">
                            {% if error %}
                            <div class="alert alert-dismissible alert-danger">
                                <button type="button" class="close" data-dismiss="alert">×</button>
                                {{ errorText }}
                            </div>
                            {% endif %} 

                            {% if data["childpanel"]["success"] == 1 or data["childpanel"]["success"] == 2 %}
                            <div class="alert alert-dismissible alert-success" style="background-color: #f0fdf4; border: 1px solid #bbf7d0; border-left: 4px solid #22c55e; color: #166534; margin:15px;">
                                <strong> Order Id: </strong> {{ data['childpanel']['id'] }} <br>
                                <strong> Domain: </strong> {{ data['childpanel']['link'] }} <br>
                                <strong> Duration: </strong> {{ data['childpanel']['quantity'] }} Month(s)<br>
                                <strong> Charge: </strong> {{ data['childpanel']['price'] }} <br>
                            </div>
                            {% endif %}
                            
                            <div class="card-header">
                                <div class="row align-items-center">
                                    <div class="col-md">
                                        <h3 class="card-title">Set Your Nameservers</h3>
                                    </div>
                                    <div class="col-md-auto mt-2">
                                        <button class="btn btn-secondary btn-rounded mb-2">{{ site['ns1'] }}</button>
                                        <button class="btn btn-secondary btn-rounded mb-2">{{ site['ns2'] }}</button>
                                    </div>
                                </div>
                            </div>

                            <div class="card-body">
                                <form action="/child-panels" method="post">
                                    <div class="row">
                                        <div class="col-md-12">
                                            <div class="form-group">
                                                <label class="control-label">Domain</label>
                                                <input type="text" class="form-control" name="panel_domain" placeholder="example.com" required>
                                            </div>
                                        </div>
                                        <div class="col-md-12">
                                            <div class="form-group">
                                                <label class="control-label">Currency</label>
                                                <select class="form-control" name="panel_currency" required>
                                                    {{ child_panel_currencies }}
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div class="row">
                                        <div class="col-md-12">
                                            <div class="form-group">
                                                <label class="control-label">Plan Duration</label>
                                                <select class="form-control" name="panel_months" id="panel_months" onchange="calculatePrice()" required>
                                                    <option value="1">1 Month</option>
                                                    <option value="2">2 Months</option>
                                                    <option value="3">3 Months</option>
                                                    <option value="6">6 Months</option>
                                                    <option value="12">1 Year (12 Months)</option>
                                                </select>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="row">
                                        <div class="col-md-12">
                                            <div class="form-group">
                                                <label class="control-label">Admin username</label>
                                                <input type="text" class="form-control" name="admin_username" required>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div class="row">
                                        <div class="col-md-12">
                                            <div class="form-group">
                                                <label class="control-label">Admin password</label>
                                                <input type="password" class="form-control" name="admin_password" required>
                                            </div>
                                        </div>
                                        <div class="col-md-12">
                                            <div class="form-group">
                                                <label class="control-label">Confirm password</label>
                                                <input type="password" class="form-control" name="admin_confirm_password" required>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label">Total Price</label>
                                        <input type="text" class="form-control" id="price" value="{{site_base_currency_symbol}} {{site["childpanel_price"]}}" readonly style="font-weight:bold; color:#10b981;">
                                    </div>
                                    <button type="submit" class="btn btn-primary btn-100">Submit order</button>
                                </form>
                            </div>
                        </div>
                    </div>
                    
                    <div class="col-lg-8">
                        <div class="card">
                            <div class="card-header">
                                <h3 class="card-title">Your Child Panels History</h3>
                            </div>
                            <div class="card-body p-0">
                                <div class="table-responsive">
                                    <table class="table table-striped table-bordered" style="margin-bottom: 0;">
                                        <thead style="background-color: #f8f9fa;">
                                            <tr>
                                                <th>ID</th>
                                                <th>Domain</th>
                                                <th>Price & Plan</th>
                                                <th>Status</th>
                                                <th>Created</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            {% if data['panel_logs'] %}
                                                {% for log in data['panel_logs'] %}
                                                <tr>
                                                    <td>#{{ log.id }}</td>
                                                    <td><strong>{{ log.domain }}</strong></td>
                                                    <td>
                                                        {{ log.charged_amount }} {{ log.child_panel_currency }}<br>
                                                        <small class="text-muted">{{ log.duration_months }} Month(s)</small>
                                                    </td>
                                                    <td>
                                                        {% if log.child_panel_status == 'Pending' %}
                                                            <span class="badge" style="background:#f59e0b; color:#fff;">Pending</span>
                                                        {% elseif log.child_panel_status == 'Active' %}
                                                            <span class="badge" style="background:#10b981; color:#fff;">Active</span>
                                                        {% elseif log.child_panel_status == 'Suspended' or log.child_panel_status == 'Frozen' %}
                                                            <span class="badge" style="background:#ef4444; color:#fff;">{{ log.child_panel_status }}</span>
                                                        {% else %}
                                                            <span class="badge" style="background:#6b7280; color:#fff;">{{ log.child_panel_status }}</span>
                                                        {% endif %}
                                                    </td>
                                                    <td>{{ log.created_on|date("d M Y") }}</td>
                                                    <td>
                                                        {% if log.child_panel_status == 'Active' or log.child_panel_status == 'Suspended' %}
                                                        <form action="/child-panels" method="post" style="margin:0;">
                                                            <input type="hidden" name="renew_id" value="{{ log.id }}">
                                                            <input type="hidden" name="renew" value="1">
                                                            <button type="submit" class="btn btn-xs btn-primary" onclick="return confirm('Renew this Child Panel for 1 Month?');">Renew</button>
                                                        </form>
                                                        {% else %}
                                                            <span class="text-muted">-</span>
                                                        {% endif %}
                                                    </td>
                                                </tr>
                                                {% endfor %}
                                            {% else %}
                                                <tr>
                                                    <td colspan="6" class="text-center text-muted py-4">No child panels found. Your orders will appear here.</td>
                                                </tr>
                                            {% endif %}
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>

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

<script>
// JS for Dynamic Price Calculation
function calculatePrice() {
    let basePrice = {{ site["childpanel_price"] }};
    let months = parseInt(document.getElementById("panel_months").value);
    let total = basePrice * months;
    document.getElementById("price").value = "{{ site_base_currency_symbol }} " + total.toFixed(2);
}
</script>

{% include 'footer.twig' %}