<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>
<div class="page-header">
  <div><h1><i class="bi bi-calculator me-2"></i><?= esc($est['est_number']) ?></h1><span class="fm-badge badge-status-<?= esc($est['status']) ?>"><?= ucfirst(str_replace('_',' ',$est['status'])) ?></span></div>
  <div class="d-flex gap-2">
    <?php if($est['status']==='pending_approval' && in_array(session()->get('user_role'),['super_admin','facility_manager','finance_manager'])): ?>
    <?= form_open(base_url('estimations/approve/'.$est['id'])) ?>
    <button type="submit" class="btn btn-fm-primary btn-sm" onclick="return confirm('Approve this estimation?')"><i class="bi bi-check-circle me-1"></i>Approve</button>
    <?= form_close() ?>
    <?php endif; ?>
    <?php if($est['status']==='approved' && in_array(session()->get('user_role'),['super_admin','facility_manager'])): ?>
    <?= form_open(base_url('estimations/convert/'.$est['id'])) ?>
    <button type="submit" class="btn btn-sm btn-success" onclick="return confirm('Convert to Work Order?')"><i class="bi bi-arrow-right-circle me-1"></i>Convert to WO</button>
    <?= form_close() ?>
    <?php endif; ?>
    <a href="<?= base_url('estimations/print/'.$est['id']) ?>" class="btn btn-fm-outline btn-sm" target="_blank"><i class="bi bi-printer me-1"></i>Print</a>
    <?php if(in_array($est['status'],['draft','pending_approval'])): ?>
    <a href="<?= base_url('estimations/edit/'.$est['id']) ?>" class="btn btn-fm-outline btn-sm"><i class="bi bi-pencil me-1"></i>Edit</a>
    <?php endif; ?>
  </div>
</div>
<div class="row g-3">
  <div class="col-lg-4">
    <div class="fm-form-section mb-3">
      <h6><i class="bi bi-info-circle"></i>Details</h6>
      <div class="small mb-2"><span class="text-muted">EST #:</span> <strong><?= esc($est['est_number']) ?></strong></div>
      <div class="small mb-2"><span class="text-muted">Title:</span> <?= esc($est['title']) ?></div>
      <div class="small mb-2"><span class="text-muted">Facility:</span> <strong><?= esc($est['facility_name']??'—') ?></strong></div>
      <?php if($est['wo_number']): ?><div class="small mb-2"><span class="text-muted">Work Order:</span> <a href="<?= base_url('workorders/view/'.$est['wo_id']) ?>"><?= esc($est['wo_number']) ?></a></div><?php endif; ?>
      <div class="small mb-2"><span class="text-muted">Revision:</span> v<?= $est['revision'] ?></div>
      <div class="small mb-2"><span class="text-muted">Created by:</span> <?= esc($est['created_by_name']??'—') ?></div>
      <div class="small"><span class="text-muted">Created:</span> <?= date('d M Y',strtotime($est['created_at'])) ?></div>
      <?php if($est['approved_by_name']): ?><div class="small mt-1"><span class="text-muted">Approved by:</span> <?= esc($est['approved_by_name']) ?></div><?php endif; ?>
    </div>
    <div class="fm-form-section">
      <h6><i class="bi bi-receipt"></i>Cost Breakdown</h6>
      <?php foreach(['labor_cost'=>'Labor','material_cost'=>'Materials','other_cost'=>'Other'] as $k=>$l): ?>
      <div class="d-flex justify-content-between small mb-2"><span><?= $l ?></span><strong><?= $currency ?> <?= number_format($est[$k]??0,2) ?></strong></div>
      <?php endforeach; ?>
      <hr>
      <div class="d-flex justify-content-between small mb-1"><span>Subtotal</span><strong><?= $currency ?> <?= number_format($est['subtotal'],2) ?></strong></div>
      <?php if($est['vat_amount']>0): ?><div class="d-flex justify-content-between small mb-1"><span>VAT (<?= $est['vat_rate'] ?>%)</span><strong><?= $currency ?> <?= number_format($est['vat_amount'],2) ?></strong></div><?php endif; ?>
      <div class="d-flex justify-content-between fw-bold mt-2" style="font-size:1.05rem"><span>TOTAL</span><span style="color:var(--fm-primary)"><?= $currency ?> <?= number_format($est['total'],2) ?></span></div>
    </div>
    <?php if(in_array($est['status'],['draft'])): ?>
    <div class="mt-3">
      <?= form_open(base_url('estimations/approve/'.$est['id'])) ?>
      <?php // First submit for approval ?>
      <button type="button" class="btn btn-fm-outline w-100" onclick="if(confirm('Submit for approval?')){this.form.action='<?= base_url('estimations/update/'.$est['id']) ?>';this.form.submit();}">Submit for Approval</button>
      <?= form_close() ?>
    </div>
    <?php endif; ?>
  </div>
  <div class="col-lg-8">
    <?php if($est['description']): ?>
    <div class="fm-form-section mb-3">
      <h6><i class="bi bi-card-text"></i>Scope of Work</h6>
      <div style="white-space:pre-wrap;font-size:.85rem;line-height:1.7"><?= esc($est['description']) ?></div>
    </div>
    <?php endif; ?>
    <?php if(!empty($items)): ?>
    <div class="fm-card mb-3">
      <div class="card-header-fm"><h5><i class="bi bi-list-ol me-2"></i>Line Items</h5></div>
      <div class="fm-card-body p-0">
        <table class="fm-table">
          <thead><tr><th>Type</th><th>Description</th><th>Qty</th><th>Unit Cost</th><th>Total</th></tr></thead>
          <tbody>
          <?php foreach($items as $item): ?>
          <tr>
            <td><span class="fm-badge badge-status-<?= $item['type']==='labor'?'assigned':($item['type']==='material'?'new':'in_progress') ?>"><?= ucfirst($item['type']) ?></span></td>
            <td class="small"><?= esc($item['description']) ?></td>
            <td class="small"><?= $item['quantity'] ?></td>
            <td class="small"><?= $currency ?> <?= number_format($item['unit_cost'],2) ?></td>
            <td class="small fw-bold"><?= $currency ?> <?= number_format($item['total_cost'],2) ?></td>
          </tr>
          <?php endforeach; ?>
          </tbody>
        </table>
      </div>
    </div>
    <?php endif; ?>
    <?php if($est['notes']): ?>
    <div class="fm-form-section"><h6><i class="bi bi-sticky"></i>Notes</h6><p class="small mb-0"><?= nl2br(esc($est['notes'])) ?></p></div>
    <?php endif; ?>
  </div>
</div>
<?= $this->endSection() ?>
