<?php
if (session_status() === PHP_SESSION_NONE) session_start();

$package_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$user_id = (int)($_SESSION['userid'] ?? 0);

if ($package_id <= 0) {
    echo '<div class="container" style="margin-top:20px;">
            <div class="alert alert-danger" style="border-radius:12px;">
              <strong>Error:</strong> Invalid package id.
            </div>
          </div>';
    exit;
}

$package = $obj->details_by_cond('re_tbl_package', "package_id='$package_id'");

if (!$package) {
    echo '<div class="container" style="margin-top:20px;">
            <div class="alert alert-danger" style="border-radius:12px;">
              <strong>Error:</strong> Package not found.
            </div>
          </div>';
    exit;
}

$updated = false;

if (isset($_POST['update']) && $_POST['update'] == '1') {

    $bill_amount = isset($_POST['bill_amount']) ? preg_replace('/[^0-9.]/', '', $_POST['bill_amount']) : '0';
    
    $bill_amount = ($bill_amount === '' ? '0' : $bill_amount);

    $obj->updateData(
        're_tbl_package',
        ['bill_amount' => $bill_amount],
        ['package_id' => (int)$package_id]
    );
    // get updated package data for log
    $package = $obj->rawSqlSingle("SELECT * FROM re_tbl_package WHERE package_id='$package_id'");

    // activity log
    $result = $obj->resellerHistoryLog([
        're_id'        => $user_id,
        'header'       => 'Package Update',
        'descriptions' => 'Update Package Name: "'.$package['package_name'].'" And Customer bill amount: "'.$package['bill_amount'].'" Updated.',
        'type'         => 'update',
    ]);

    $updated = true;

    // refresh package data
    $package = $obj->details_by_cond('re_tbl_package', "package_id='$package_id'");
}
?>

<style>
    :root {
        --bg: #f6f8fb;
        --card: #ffffff;
        --text: #111827;
        --muted: #6b7280;
        --border: #e5e7eb;
        --shadow: 0 10px 25px rgba(17, 24, 39, .06);
        --radius: 14px;
    }

    .page-wrap {
        padding: 14px 0;
        background: transparent;
    }

    .cardx {
        background: var(--card);
        border: 1px solid var(--border);
        border-radius: var(--radius);
        box-shadow: var(--shadow);
        overflow: hidden;
    }

    .cardx-h {
        padding: 14px 16px;
        background: linear-gradient(135deg, #0b9444, #57606f);
        color: #fff;
        display: flex;
        justify-content: space-between;
        align-items: center;
        flex-wrap: wrap;
        gap: 10px;
    }

    .title {
        margin: 0;
        font-size: 18px;
        font-weight: 900;
        display: flex;
        align-items: center;
        gap: 10px;
    }

    .subtitle {
        margin: 4px 0 0;
        font-size: 12px;
        opacity: .9;
    }

    .badge-soft {
        display: inline-block;
        padding: 7px 10px;
        border-radius: 999px;
        font-weight: 900;
        font-size: 12px;
        background: rgba(255, 255, 255, .18);
        border: 1px solid rgba(255, 255, 255, .35);
        color: #fff;
    }

    .cardx-b {
        padding: 16px;
    }

    .form-control {
        border-radius: 10px;
        border: 1px solid var(--border);
        height: 38px;
        box-shadow: none;
    }

    .help {
        font-size: 12px;
        color: var(--muted);
        margin-top: 6px;
    }

    .hr-soft {
        border-top: 1px dashed var(--border);
        margin: 12px 0;
    }

    .footer {
        display: flex;
        gap: 10px;
        justify-content: flex-end;
        flex-wrap: wrap;
    }

    .btn {
        border-radius: 10px !important;
        font-weight: 900;
    }

    @media (max-width:767px) {
        .footer .btn {
            width: 100%;
        }
    }
</style>

<div class="container page-wrap">
    <div class="cardx">

        <div class="cardx-h">
            <div>
                <h6 class="title"><i class="fa fa-edit"></i> Edit Package</h6>
                <div class="subtitle">Update customer bill amount</div>
            </div>
            <div class="badge-soft">Package ID: <?php echo (int)$package_id; ?></div>
        </div>

        <div class="cardx-b">

            <?php if ($updated) { ?>
                <div class="alert alert-success" style="border-radius:12px;">
                    <strong>Success!</strong> Package updated successfully.
                </div>
            <?php } ?>

            <form method="post" id="editPackageForm" autocomplete="off">

                <input type="hidden" name="update" value="1">

                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label>Package Name</label>
                            <input type="text" class="form-control" readonly value="<?php echo htmlspecialchars($package['package_name']); ?>">
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label>Customer Bill Amount</label>
                            <input type="text"
                                class="form-control"
                                id="bill_amount"
                                name="bill_amount"
                                onkeypress="return numbersOnly(event)"
                                required
                                value="<?php echo htmlspecialchars($package['bill_amount']); ?>">
                            <div class="help">Only numeric value allowed</div>
                        </div>
                    </div>
                </div>

                <div class="hr-soft"></div>

                <div class="row">
                    

                    <div class="col-md-6">
                        <div class="form-group">
                            <label>Reseller Bill Amount</label>
                            <input type="text" class="form-control" readonly value="<?php echo htmlspecialchars($package['re_bill_amount']); ?>">
                            <div class="help">This field is readonly here</div>
                        </div>
                    </div>
                </div>

                <div class="footer">
                    <a href="?page=reseller-list-package" class="btn btn-default">
                        <i class="fa fa-arrow-left"></i> Back
                    </a>
                    <button type="submit" class="btn btn-primary"  id="updateBtn">
                        <i class="fa fa-save"></i> Update
                    </button>
                </div>

            </form>

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

<?php $obj->start_script(); ?>
<script>
    function numbersOnly(e) {
        var unicode = e.charCode ? e.charCode : e.keyCode;
        if (unicode != 8) {
            if ((unicode < 2534 || unicode > 2543) && (unicode < 48 || unicode > 57)) return false;
        }
    }

    $(document).ready(function() {
        $('#editPackageForm').on('submit', function() {
            $('#updateBtn').prop('disabled', true).text('Updating...');
        });

        <?php if ($updated) { ?>
            setTimeout(function() {
                window.location.href = "?page=reseller-list-package";
            }, 900);
        <?php } ?>
    });
</script>
<?php $obj->end_script(); ?>