<?php 
$this->load->view('layouts/header');
$this->load->helper('layout');
?>

    <!-- App Header -->
    <div class="appHeader bg-primary text-light">
        <div class="left">
            <a href="<?php echo base_url('admin/card/' . $card->id); ?>" class="headerButton">
                <ion-icon name="chevron-back-outline"></ion-icon>
            </a>
        </div>
        <div class="pageTitle">Edit Card</div>
        <div class="right">
            <a href="#" class="headerButton" data-bs-toggle="modal" data-bs-target="#adminSidebar">
                <ion-icon name="menu-outline"></ion-icon>
            </a>
        </div>
    </div>

    <!-- App Capsule -->
    <div id="appCapsule">
        
        <?php if ($this->session->flashdata('success')): ?>
        <div class="section mt-2">
            <div class="alert alert-success"><?php echo $this->session->flashdata('success'); ?></div>
        </div>
        <?php endif; ?>

        <!-- Card Info -->
        <div class="section mt-2 text-center">
            <div class="card">
                <div class="card-body py-3">
                    <p class="text-muted mb-1">Card</p>
                    <h4 class="mb-1"><?php echo $card->card_number_masked; ?></h4>
                    <p class="small text-muted mb-0">
                        <?php echo ucfirst($card->card_type); ?> <?php echo ucfirst($card->card_brand); ?> - 
                        <?php echo htmlspecialchars($card_user->first_name . ' ' . $card_user->last_name); ?>
                    </p>
                </div>
            </div>
        </div>

        <div class="section mt-2 mb-3">
            <form action="<?php echo base_url('admin/edit_card/' . $card->id); ?>" method="POST">
                <input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">
                
                <!-- Basic Info -->
                <div class="card">
                    <div class="card-header">Card Information</div>
                    <div class="card-body">
                        <div class="form-group basic">
                            <label class="label">Cardholder Name</label>
                            <input type="text" name="cardholder_name" class="form-control" 
                                   value="<?php echo htmlspecialchars($card->cardholder_name ?? strtoupper($card_user->first_name . ' ' . $card_user->last_name)); ?>"
                                   style="text-transform: uppercase;">
                        </div>
                        <div class="form-group basic">
                            <label class="label">Status</label>
                            <select name="status" class="form-control">
                                <option value="active" <?php echo $card->status == 'active' ? 'selected' : ''; ?>>Active</option>
                                <option value="inactive" <?php echo $card->status == 'inactive' ? 'selected' : ''; ?>>Inactive</option>
                                <option value="frozen" <?php echo $card->status == 'frozen' ? 'selected' : ''; ?>>Frozen</option>
                                <option value="blocked" <?php echo $card->status == 'blocked' ? 'selected' : ''; ?>>Blocked</option>
                            </select>
                        </div>
                        <div class="form-group basic">
                            <label class="label">Balance (<?php echo $card->currency; ?>)</label>
                            <input type="number" name="balance" class="form-control" step="0.01" min="0" 
                                   value="<?php echo $card->balance ?? 0; ?>">
                            <small class="text-muted">Adjust card balance directly</small>
                        </div>
                        <div class="row">
                            <div class="col-6">
                                <div class="form-group basic">
                                    <label class="label">Expiry Month</label>
                                    <select name="expiry_month" class="form-control">
                                        <?php for($m = 1; $m <= 12; $m++): ?>
                                        <option value="<?php echo str_pad($m, 2, '0', STR_PAD_LEFT); ?>" 
                                                <?php echo $card->expiry_month == str_pad($m, 2, '0', STR_PAD_LEFT) ? 'selected' : ''; ?>>
                                            <?php echo str_pad($m, 2, '0', STR_PAD_LEFT); ?>
                                        </option>
                                        <?php endfor; ?>
                                    </select>
                                </div>
                            </div>
                            <div class="col-6">
                                <div class="form-group basic">
                                    <label class="label">Expiry Year</label>
                                    <select name="expiry_year" class="form-control">
                                        <?php for($y = date('Y'); $y <= date('Y') + 10; $y++): ?>
                                        <option value="<?php echo $y; ?>" <?php echo $card->expiry_year == $y ? 'selected' : ''; ?>>
                                            <?php echo $y; ?>
                                        </option>
                                        <?php endfor; ?>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Limits -->
                <div class="card mt-2">
                    <div class="card-header">Spending Limits</div>
                    <div class="card-body">
                        <div class="form-group basic">
                            <label class="label">Daily Limit (<?php echo $card->currency; ?>)</label>
                            <input type="number" name="daily_limit" class="form-control" step="0.01" min="0" 
                                   value="<?php echo $card->daily_limit ?? 5000; ?>">
                        </div>
                        <div class="form-group basic">
                            <label class="label">Monthly Limit (<?php echo $card->currency; ?>)</label>
                            <input type="number" name="monthly_limit" class="form-control" step="0.01" min="0" 
                                   value="<?php echo $card->monthly_limit ?? 50000; ?>">
                        </div>
                        <div class="form-group basic">
                            <label class="label">Per Transaction Limit (<?php echo $card->currency; ?>)</label>
                            <input type="number" name="single_transaction_limit" class="form-control" step="0.01" min="0" 
                                   value="<?php echo $card->single_transaction_limit ?? 2500; ?>">
                        </div>
                    </div>
                </div>

                <!-- Controls -->
                <div class="listview-title mt-2">Card Controls</div>
                <ul class="listview image-listview text inset">
                    <li>
                        <div class="item">
                            <div class="in">
                                <div>
                                    Online Payments
                                    <div class="text-muted">Allow online/e-commerce transactions</div>
                                </div>
                                <div class="form-check form-switch ms-2">
                                    <input class="form-check-input" type="checkbox" name="is_online_enabled" value="1" 
                                           id="onlineEnabled" <?php echo ($card->is_online_enabled ?? 1) ? 'checked' : ''; ?>>
                                    <label class="form-check-label" for="onlineEnabled"></label>
                                </div>
                            </div>
                        </div>
                    </li>
                    <li>
                        <div class="item">
                            <div class="in">
                                <div>
                                    POS Payments
                                    <div class="text-muted">Allow point-of-sale transactions</div>
                                </div>
                                <div class="form-check form-switch ms-2">
                                    <input class="form-check-input" type="checkbox" name="is_pos_enabled" value="1" 
                                           id="posEnabled" <?php echo ($card->is_pos_enabled ?? 1) ? 'checked' : ''; ?>>
                                    <label class="form-check-label" for="posEnabled"></label>
                                </div>
                            </div>
                        </div>
                    </li>
                    <li>
                        <div class="item">
                            <div class="in">
                                <div>
                                    ATM Withdrawals
                                    <div class="text-muted">Allow ATM cash withdrawals</div>
                                </div>
                                <div class="form-check form-switch ms-2">
                                    <input class="form-check-input" type="checkbox" name="is_atm_enabled" value="1" 
                                           id="atmEnabled" <?php echo ($card->is_atm_enabled ?? 0) ? 'checked' : ''; ?>>
                                    <label class="form-check-label" for="atmEnabled"></label>
                                </div>
                            </div>
                        </div>
                    </li>
                    <li>
                        <div class="item">
                            <div class="in">
                                <div>
                                    International Transactions
                                    <div class="text-muted">Allow transactions outside home country</div>
                                </div>
                                <div class="form-check form-switch ms-2">
                                    <input class="form-check-input" type="checkbox" name="is_international_enabled" value="1" 
                                           id="intlEnabled" <?php echo ($card->is_international_enabled ?? 0) ? 'checked' : ''; ?>>
                                    <label class="form-check-label" for="intlEnabled"></label>
                                </div>
                            </div>
                        </div>
                    </li>
                </ul>

                <!-- Read-only Info -->
                <div class="card mt-2">
                    <div class="card-header">Card Details (Read Only)</div>
                    <div class="card-body">
                        <div class="form-group basic">
                            <label class="label">Card Number</label>
                            <input type="text" class="form-control" value="<?php echo $card->card_number_masked; ?>" readonly disabled>
                        </div>
                        <div class="form-group basic">
                            <label class="label">Card Type</label>
                            <input type="text" class="form-control" value="<?php echo ucfirst($card->card_type); ?>" readonly disabled>
                        </div>
                        <div class="form-group basic">
                            <label class="label">Card Brand</label>
                            <input type="text" class="form-control" value="<?php echo ucfirst($card->card_brand); ?>" readonly disabled>
                        </div>
                        <div class="form-group basic">
                            <label class="label">Currency</label>
                            <input type="text" class="form-control" value="<?php echo $card->currency; ?>" readonly disabled>
                        </div>
                        <div class="form-group basic">
                            <label class="label">Created</label>
                            <input type="text" class="form-control" value="<?php echo date('M d, Y H:i', strtotime($card->created_at)); ?>" readonly disabled>
                        </div>
                    </div>
                </div>

                <div class="section mt-2">
                    <button type="submit" class="btn btn-primary btn-block btn-lg">
                        <ion-icon name="save-outline"></ion-icon> Save Changes
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- Admin Sidebar -->
    <?php $this->load->view('admin/partials/sidebar'); ?>

<?php $this->load->view('layouts/footer'); ?>
