<div class="d-flex justify-content-between align-items-center mb-4">
    <h4 class="mb-0"><i class="fas fa-snowflake me-2"></i><?= e($freeze['freeze_number']) ?> <?= status_badge($freeze['status']) ?></h4>
    <div class="d-flex gap-2">
        <?php if (in_array($freeze['status'], ['active', 'approved']) && auth_can('freezes.approve')): ?>
        <button class="btn btn-warning" id="btn-end-freeze"><i class="fas fa-stop me-1"></i>End Freeze</button>
        <button class="btn btn-info" data-bs-toggle="modal" data-bs-target="#extend-modal"><i class="fas fa-plus me-1"></i>Extend</button>
        <?php endif; ?>
        <?php if ($freeze['status'] === 'pending' && auth_can('freezes.approve')): ?>
        <button class="btn btn-success btn-approve-show" data-id="<?= $freeze['id'] ?>"><i class="fas fa-check me-1"></i>Approve</button>
        <button class="btn btn-danger btn-reject-show" data-id="<?= $freeze['id'] ?>"><i class="fas fa-times me-1"></i>Reject</button>
        <?php endif; ?>
        <a href="/freezes" class="btn btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
    </div>
</div>

<div class="row g-4">
    <div class="col-md-8">
        <div class="card mb-3">
            <div class="card-header"><h6 class="mb-0">Freeze Details</h6></div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-md-6"><strong>Member:</strong><br><a href="/members/<?= $freeze['member_id'] ?>"><?= e($freeze['membership_number'] . ' - ' . $freeze['member_name']) ?></a></div>
                    <div class="col-md-6"><strong>Policy:</strong><br><?= e($freeze['policy_name'] ?? 'N/A') ?></div>
                    <div class="col-md-4"><strong>Start Date:</strong><br><?= format_date($freeze['start_date']) ?></div>
                    <div class="col-md-4"><strong>Original End:</strong><br><?= format_date($freeze['original_end_date']) ?></div>
                    <div class="col-md-4"><strong>Actual End:</strong><br><?= $freeze['actual_end_date'] ? format_date($freeze['actual_end_date']) : '<em>—</em>' ?></div>
                    <div class="col-md-4"><strong>Duration:</strong><br><?= $freeze['duration_days'] ?> days</div>
                    <div class="col-md-4"><strong>Fee:</strong><br><?= format_money($freeze['fee_amount']) ?> <?= $freeze['fee_paid'] ? '<span class="badge bg-success">Paid</span>' : '<span class="badge bg-warning">Unpaid</span>' ?></div>
                    <div class="col-md-4"><strong>Category:</strong><br><?= e($freeze['reason_category'] ?? '-') ?></div>
                    <div class="col-12"><strong>Reason:</strong><br><p class="mb-0"><?= e($freeze['freeze_reason']) ?></p></div>
                </div>
            </div>
        </div>

        <?php if ($freeze['rejection_reason']): ?>
        <div class="alert alert-danger"><strong>Rejection Reason:</strong> <?= e($freeze['rejection_reason']) ?></div>
        <?php endif; ?>
        <?php if ($freeze['early_end_reason']): ?>
        <div class="alert alert-warning"><strong>Early End Reason:</strong> <?= e($freeze['early_end_reason']) ?></div>
        <?php endif; ?>

        <!-- Extensions -->
        <?php if (!empty($extensions)): ?>
        <div class="card">
            <div class="card-header"><h6 class="mb-0">Extensions</h6></div>
            <div class="card-body p-0">
                <table class="table table-sm mb-0">
                    <thead><tr><th>Days</th><th>New End</th><th>Reason</th><th>Status</th><th>Approved By</th></tr></thead>
                    <tbody>
                    <?php foreach ($extensions as $ext): ?>
                    <tr>
                        <td>+<?= $ext['extension_days'] ?></td>
                        <td><?= format_date($ext['new_end_date']) ?></td>
                        <td><?= e($ext['reason']) ?></td>
                        <td><?= status_badge($ext['status']) ?></td>
                        <td><?= e($ext['approved_by_name'] ?? '-') ?></td>
                    </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
        <?php endif; ?>
    </div>

    <div class="col-md-4">
        <div class="card mb-3">
            <div class="card-header"><h6 class="mb-0">Timeline</h6></div>
            <div class="card-body">
                <ul class="list-unstyled mb-0">
                    <li class="mb-2"><small class="text-muted"><?= format_datetime($freeze['created_at']) ?></small><br>Created by <?= e($freeze['created_by_name'] ?? 'System') ?></li>
                    <?php if ($freeze['approved_at']): ?><li class="mb-2"><small class="text-muted"><?= format_datetime($freeze['approved_at']) ?></small><br>Approved by <?= e($freeze['approved_by_name'] ?? '-') ?></li><?php endif; ?>
                    <?php if ($freeze['activated_at']): ?><li class="mb-2"><small class="text-muted"><?= format_datetime($freeze['activated_at']) ?></small><br>Activated</li><?php endif; ?>
                    <?php if ($freeze['ended_at']): ?><li class="mb-2"><small class="text-muted"><?= format_datetime($freeze['ended_at']) ?></small><br>Ended by <?= e($freeze['ended_by_name'] ?? '-') ?></li><?php endif; ?>
                </ul>
            </div>
        </div>
    </div>
</div>

<!-- Extend Modal -->
<div class="modal fade" id="extend-modal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header"><h5 class="modal-title">Extend Freeze</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div>
            <form id="extend-form">
                <div class="modal-body">
                    <div class="mb-3"><label class="form-label">Extension Days <span class="text-danger">*</span></label><input type="number" name="extension_days" class="form-control" min="1" max="365" required></div>
                    <div class="mb-3"><label class="form-label">Reason <span class="text-danger">*</span></label><textarea name="reason" class="form-control" rows="3" required></textarea></div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary">Extend</button>
                </div>
            </form>
        </div>
    </div>
</div>

<?php $__view->push('scripts') ?>
<script>
$(function(){
    var freezeId = <?= $freeze['id'] ?>;
    var token = $('meta[name="csrf-token"]').attr('content');

    $('#btn-end-freeze').on('click', function(){
        Swal.fire({title:'End Freeze Early?', input:'text', inputPlaceholder:'Reason...', showCancelButton:true, confirmButtonColor:'#ffc107'}).then(function(r){
            if(r.isConfirmed){
                $.post('/api/freezes/'+freezeId+'/end', {_token:token, reason:r.value||'Early termination'}, function(res){
                    if(res.success){ FormHelper.showToast('success',res.message); setTimeout(function(){location.reload();},800); }
                    else { FormHelper.showToast('error',res.message); }
                });
            }
        });
    });

    $('#extend-form').on('submit', function(e){
        e.preventDefault();
        $.post('/api/freezes/'+freezeId+'/extend', $(this).serialize()+'&_token='+token, function(res){
            if(res.success){ FormHelper.showToast('success',res.message); setTimeout(function(){location.reload();},800); }
            else { FormHelper.showToast('error',res.message||'Failed'); }
        });
    });

    $('.btn-approve-show').on('click', function(){
        $.post('/api/freezes/'+freezeId+'/approve', {_token:token}, function(res){
            if(res.success){ FormHelper.showToast('success',res.message); setTimeout(function(){location.reload();},800); }
        });
    });

    $('.btn-reject-show').on('click', function(){
        Swal.fire({title:'Reject?', input:'textarea', showCancelButton:true, confirmButtonColor:'#dc3545'}).then(function(r){
            if(r.isConfirmed){ $.post('/api/freezes/'+freezeId+'/reject', {_token:token, rejection_reason:r.value}, function(res){ if(res.success){location.reload();} }); }
        });
    });
});
</script>
<?php $__view->endPush() ?>