<?= message_box('success'); ?>
<?= message_box('error'); ?>
<?php
$edited = can_action('131', 'edited');
$deleted = can_action('131', 'deleted');
$edit_id = $this->uri->segment(5);
$action_url = base_url() . 'admin/settings/payment_method/update_payment_method';
$submit_label = lang('add');
$method_name = '';
if (!empty($edit_id) && !empty($method_info)) {
    $action_url = base_url() . 'admin/settings/payment_method/update_payment_method/' . $method_info->payment_methods_id;
    $submit_label = lang('update');
    $method_name = $method_info->method_name;
}
$total_methods = !empty($all_method_info) ? count($all_method_info) : 0;
$selected_method_name = !empty($method_name) ? $method_name : 'New payment method';
?>

<div class="space-y-6">
    <div class="overflow-hidden rounded-3xl border border-border bg-card shadow-sm">
        <div class="relative px-6 py-6 md:px-8">
            <div class="absolute inset-0 bg-[radial-gradient(circle_at_top_right,rgba(59,130,246,0.10),transparent_32%),radial-gradient(circle_at_bottom_left,rgba(16,185,129,0.10),transparent_28%)]"></div>
            <div class="relative flex flex-col gap-6 xl:flex-row xl:items-start xl:justify-between">
                <div class="space-y-3">
                    <p class="text-xs font-semibold uppercase tracking-[0.2em] text-muted-foreground"><?= lang('settings') ?></p>
                    <div class="space-y-2">
                        <h1 class="text-2xl font-semibold tracking-tight text-foreground"><?= lang('payment_method') ?></h1>
                        <p class="max-w-3xl text-sm leading-6 text-muted-foreground">
                            Maintain internal ERP payment method labels used across transactions, deposits, expenses, invoices and purchase payment records.
                        </p>
                    </div>
                    <div class="flex flex-wrap gap-2">
                        <span class="inline-flex items-center rounded-full border border-border bg-background/80 px-3 py-1 text-xs font-semibold text-foreground">
                            <?= $total_methods ?> configured methods
                        </span>
                        <?php if (!empty($edit_id)) { ?>
                            <span class="inline-flex items-center rounded-full border border-indigo-200 bg-indigo-50 px-3 py-1 text-xs font-semibold text-indigo-700 dark:border-indigo-500/20 dark:bg-indigo-500/10 dark:text-indigo-300">
                                Editing: <?= html_escape($selected_method_name) ?>
                            </span>
                        <?php } ?>
                    </div>
                </div>
                <div class="grid w-full gap-3 sm:grid-cols-2 2xl:grid-cols-3 xl:max-w-xl">
                    <div class="rounded-2xl border border-border bg-background/80 px-4 py-4">
                        <div class="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Method count</div>
                        <div class="mt-2 text-2xl font-semibold text-foreground"><?= $total_methods ?></div>
                        <div class="text-xs text-muted-foreground">Available payment labels</div>
                    </div>
                    <div class="rounded-2xl border border-border bg-background/80 px-4 py-4">
                        <div class="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Current mode</div>
                        <div class="mt-2 text-2xl font-semibold text-foreground"><?= !empty($edit_id) ? 'Edit' : 'Create' ?></div>
                        <div class="text-xs text-muted-foreground"><?= !empty($edit_id) ? 'Updating an existing method' : 'Adding a new payment method' ?></div>
                    </div>
                    <div class="rounded-2xl border border-border bg-background/80 px-4 py-4">
                        <div class="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Usage</div>
                        <div class="mt-2 text-2xl font-semibold text-foreground">ERP</div>
                        <div class="text-xs text-muted-foreground">Transactions, invoices and purchases</div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="grid gap-6 xl:grid-cols-[380px_minmax(0,1fr)]">
        <?php if (!empty($edited)) { ?>
            <div class="space-y-6">
                <div class="rounded-3xl border border-border bg-card shadow-sm">
                    <div class="border-b border-border px-6 py-5">
                        <h2 class="text-lg font-semibold text-foreground"><?= !empty($edit_id) ? lang('update') . ' ' . lang('payment_method') : lang('new') . ' ' . lang('payment_method') ?></h2>
                        <p class="text-sm text-muted-foreground">Use short, clear method names so accounting teams can identify payment sources quickly.</p>
                    </div>
                    <form method="post" action="<?= $action_url ?>" class="space-y-5 p-6">
                        <?= form_input_group('method_name', lang('method_name'), $method_name, 'text', true, array('placeholder' => lang('method_name'))); ?>
                        <div class="rounded-2xl border border-border bg-background/60 p-4">
                            <div class="flex items-start gap-3">
                                <div class="mt-0.5 shrink-0 inline-flex h-9 w-9 items-center justify-center rounded-xl bg-indigo-100 text-indigo-700 dark:bg-indigo-500/15 dark:text-indigo-300">
                                    <i data-lucide="circle-dollar-sign" class="h-4 w-4"></i>
                                </div>
                                <div class="space-y-1">
                                    <div class="text-sm font-semibold text-foreground">Naming guidance</div>
                                    <p class="text-sm leading-6 text-muted-foreground">
                                        Examples: Cash, Bank Transfer, Mobile Banking, Stripe Card, Cheque, POS Terminal.
                                    </p>
                                </div>
                            </div>
                        </div>
                        <div class="flex flex-wrap items-center justify-end gap-2 border-t border-border pt-5">
                            <?php if (!empty($edit_id)) { ?>
                                <a href="<?= base_url('admin/settings/payment_method/') ?>" class="inline-flex items-center gap-2 rounded-lg border border-border bg-background px-4 py-2 text-sm font-medium text-foreground hover:bg-muted">
                                    <i data-lucide="arrow-left" class="h-4 w-4"></i>
                                    <?= lang('cancel') ?>
                                </a>
                            <?php } ?>
                            <button type="submit" class="inline-flex items-center gap-2 rounded-xl bg-primary px-5 py-2.5 text-sm font-semibold text-primary-foreground shadow-sm transition hover:bg-primary/90">
                                <i data-lucide="<?= !empty($edit_id) ? 'save' : 'plus' ?>" class="h-4 w-4"></i>
                                <?= $submit_label ?>
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        <?php } ?>

        <div class="rounded-3xl border border-border bg-card shadow-sm">
            <div class="flex flex-col gap-4 border-b border-border px-6 py-5 md:flex-row md:items-center md:justify-between">
                <div>
                    <h2 class="text-lg font-semibold text-foreground"><?= lang('payment_method') . ' ' . lang('list') ?></h2>
                    <p class="text-sm text-muted-foreground">Manage the payment options available to accounting entries and invoice payment forms.</p>
                </div>
                <div class="inline-flex items-center gap-2 rounded-full border border-border bg-background px-3 py-1.5 text-xs font-semibold text-muted-foreground whitespace-nowrap">
                    <i data-lucide="credit-card" class="h-4 w-4"></i>
                    <?= $total_methods ?> methods
                </div>
            </div>
            <div class="p-6">
                <?php if (!empty($all_method_info)) { ?>
                    <div class="grid gap-4 md:grid-cols-1 2xl:grid-cols-2">
                        <?php foreach ($all_method_info as $index => $v_method_info) { ?>
                            <div id="payment_methods_<?= $v_method_info->payment_methods_id ?>" class="group rounded-2xl border border-border bg-background/60 p-5 shadow-sm transition hover:-translate-y-0.5 hover:shadow-md">
                                <div class="flex items-start justify-between gap-4">
                                    <div class="min-w-0">
                                        <div class="inline-flex h-11 w-11 items-center justify-center rounded-2xl bg-slate-100 text-slate-700 dark:bg-slate-800 dark:text-slate-200">
                                            <i data-lucide="<?= $index % 3 === 0 ? 'wallet' : ($index % 3 === 1 ? 'building-2' : 'credit-card') ?>" class="h-5 w-5"></i>
                                        </div>
                                        <h3 class="mt-4 truncate text-base font-semibold text-foreground"><?= html_escape($v_method_info->method_name) ?></h3>
                                        <p class="mt-1 text-sm text-muted-foreground">Available for ERP transaction and payment allocation forms.</p>
                                    </div>
                                    <span class="inline-flex items-center rounded-full border border-border bg-card px-2.5 py-1 text-[11px] font-semibold text-muted-foreground">
                                        #<?= $v_method_info->payment_methods_id ?>
                                    </span>
                                </div>
                                <div class="mt-5 flex items-center justify-between gap-3">
                                    <div class="text-xs text-muted-foreground">Reusable across invoices, deposits and expenses</div>
                                    <div class="flex items-center gap-2">
                                        <?php if (!empty($edited)) { ?>
                                            <?= action_btn(
                                                'admin/settings/payment_method/edit_payment_method/' . $v_method_info->payment_methods_id,
                                                lang('edit'),
                                                'secondary',
                                                'square-pen',
                                                'xs',
                                                array('class' => 'whitespace-nowrap')
                                            ) ?>
                                        <?php } ?>
                                        <?php if (!empty($deleted)) { ?>
                                            <?= ajax_anchor(
                                                base_url("admin/settings/delete_payment_method/" . $v_method_info->payment_methods_id),
                                                lang('delete'),
                                                array(
                                                    "class" => "whitespace-nowrap",
                                                    "title" => lang('delete'),
                                                    "data-fade-out-on-success" => "#payment_methods_" . $v_method_info->payment_methods_id
                                                )
                                            ); ?>
                                        <?php } ?>
                                    </div>
                                </div>
                            </div>
                        <?php } ?>
                    </div>
                <?php } else { ?>
                    <div class="rounded-2xl border border-dashed border-border bg-background/60 px-6 py-12 text-center">
                        <div class="mx-auto flex h-14 w-14 items-center justify-center rounded-2xl bg-slate-100 text-slate-600 dark:bg-slate-800 dark:text-slate-300">
                            <i data-lucide="credit-card" class="h-6 w-6"></i>
                        </div>
                        <h3 class="mt-4 text-lg font-semibold text-foreground">No payment methods yet</h3>
                        <p class="mt-2 text-sm text-muted-foreground">Create your first payment method to start using it in ERP transaction forms.</p>
                    </div>
                <?php } ?>
            </div>
        </div>
    </div>
</div>
