<?php $this->start('body');
    $cats  = $this->data['categories'] ?? [];
    $asset = $this->data['asset'] ?? null;
    $projectScope = !empty($this->data['project_scope']);
    $project = $this->data['project'] ?? null;
    $existing = $this->data['existing_for_project'] ?? [];
    $addedId  = (int)($this->data['added_id'] ?? 0);
    $formAction = $projectScope
        ? (PROOT . 'Assets/addForProject/' . (int)$project['c0d'])
        : (PROOT . 'Assets/add');
    $backHref   = $projectScope
        ? (PROOT . 'ProjectStarter/index/' . (int)$project['c0d'])
        : (PROOT . 'Assets');
?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="mb-0" style="font-weight:600;color:#334155;">
            <i class="fas fa-plus-circle me-2" style="color:#7A8BA1;"></i>Add asset
            <?php if ($projectScope && $project): ?>
                <span class="text-muted small ms-2">to <?= htmlspecialchars($project['project_no']); ?> · <?= htmlspecialchars($project['project_name']); ?></span>
            <?php endif; ?>
        </h5>
    </div>
    <a href="<?= $backHref; ?>" class="btn btn-sm" style="background:#F8FAFC;border:1px solid #E2E8F0;color:#334155;"><i class="fas fa-arrow-left me-1"></i><?= $projectScope ? 'Back to checklist' : 'Back to register'; ?></a>
</div>

<?php if ($addedId > 0): ?>
<div class="alert py-2 mb-3" style="background:#DCFCE7;border:1px solid #22c55e;color:#166534;font-size:.86rem;">
    <i class="fas fa-check-circle me-1"></i> Asset saved. Add another below, or head back to the onboarding checklist.
</div>
<?php endif; ?>

<form method="post" action="<?= $formAction; ?>" class="card border-0 shadow-sm" style="background:#F8FAFC;">
    <div class="card-body p-3">
        <div class="row g-2">
            <div class="col-md-6">
                <label class="form-label" style="color:#334155;">Name *</label>
                <input type="text" name="name" required class="form-control form-control-sm">
            </div>
            <div class="col-md-3">
                <label class="form-label" style="color:#334155;">Category *</label>
                <select name="category_id" id="cat" required class="form-select form-select-sm">
                    <option value="">Choose…</option>
                    <?php foreach ($cats as $c): ?>
                        <option value="<?= (int)$c['c0d']; ?>" data-life="<?= (int)$c['default_useful_life_months']; ?>" data-method="<?= htmlspecialchars($c['default_depreciation_method']); ?>"><?= htmlspecialchars($c['name']); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3">
                <label class="form-label" style="color:#334155;">Serial no</label>
                <input type="text" name="serial_no" class="form-control form-control-sm">
            </div>

            <div class="col-md-3">
                <label class="form-label" style="color:#334155;">Purchase date *</label>
                <input type="date" name="purchase_date" required value="<?= date('Y-m-d'); ?>" class="form-control form-control-sm">
            </div>
            <div class="col-md-3">
                <label class="form-label" style="color:#334155;">Purchase cost (TZS) *</label>
                <input type="number" step="0.01" name="purchase_cost" required value="0" class="form-control form-control-sm">
            </div>
            <div class="col-md-3">
                <label class="form-label" style="color:#334155;">Salvage value</label>
                <input type="number" step="0.01" name="salvage_value" value="0" class="form-control form-control-sm">
            </div>
            <div class="col-md-3">
                <label class="form-label" style="color:#334155;">Depreciation start date</label>
                <input type="date" name="depreciation_start_date" value="<?= date('Y-m-d'); ?>" class="form-control form-control-sm">
            </div>

            <div class="col-md-3">
                <label class="form-label" style="color:#334155;">Useful life (months)</label>
                <input type="number" name="useful_life_months" id="life" value="60" class="form-control form-control-sm">
            </div>
            <div class="col-md-3">
                <label class="form-label" style="color:#334155;">Method</label>
                <select name="depreciation_method" id="method" class="form-select form-select-sm">
                    <option value="SLM">Straight-line (SLM)</option>
                    <option value="RB">Reducing balance (RB)</option>
                </select>
            </div>
            <div class="col-md-3">
                <label class="form-label" style="color:#334155;">Custodian</label>
                <input type="text" name="custodian_name" class="form-control form-control-sm">
            </div>
            <div class="col-md-3">
                <label class="form-label" style="color:#334155;">Location</label>
                <input type="text" name="location" class="form-control form-control-sm">
            </div>

            <div class="col-12">
                <label class="form-label" style="color:#334155;">Description</label>
                <textarea name="description" rows="2" class="form-control form-control-sm"></textarea>
            </div>
            <div class="col-12">
                <label class="form-label" style="color:#334155;">Notes</label>
                <textarea name="notes" rows="2" class="form-control form-control-sm"></textarea>
            </div>
        </div>
    </div>
    <div class="card-footer bg-white d-flex justify-content-end gap-2" style="border-top:1px solid #E2E8F0;">
        <a href="<?= $backHref; ?>" class="btn btn-sm" style="background:#F8FAFC;border:1px solid #E2E8F0;color:#334155;">Cancel</a>
        <button type="submit" class="btn btn-sm" style="background:#7A8BA1;border-color:#7A8BA1;color:#fff;font-weight:600;"><i class="fas fa-save me-1"></i>Save asset</button>
    </div>
</form>

<?php if ($projectScope && !empty($existing)): ?>
<div class="card border-0 shadow-sm mt-3">
    <div class="card-header bg-white"><strong>Already on this project · <?= count($existing); ?></strong></div>
    <div class="card-body p-0">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr><th>Asset no</th><th>Name</th><th>Status</th><th class="text-end">Open</th></tr>
            </thead>
            <tbody>
                <?php foreach ($existing as $a): ?>
                    <tr>
                        <td><code><?= htmlspecialchars($a['asset_no']); ?></code></td>
                        <td><?= htmlspecialchars($a['name']); ?></td>
                        <td><span class="badge" style="background:#7A9E7E;color:#fff;text-transform:capitalize;"><?= htmlspecialchars(strtolower(str_replace('_',' ',$a['status']))); ?></span></td>
                        <td class="text-end"><a href="<?= PROOT; ?>Assets/view/<?= (int)$a['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-eye"></i></a></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<?php $this->end(); ?>

<?php $this->start('section_js'); ?>
<script>
$(function () {
    $('#cat').on('change', function () {
        var $o = $(this).find('option:selected');
        var life = parseInt($o.data('life'), 10);
        var method = $o.data('method');
        if (life > 0) $('#life').val(life);
        if (method) $('#method').val(method);
    });
});
</script>
<?php $this->end(); ?>
