<?php
if (!function_exists('pj_fmt_stok')) {
  function pj_fmt_stok($val, $maxDec = 2)
  {
    if ($val === null || $val === '') return '0';
    $n = (float)$val;

    $isInt = abs($n - round($n)) < 0.000000001;
    if ($isInt) return number_format($n, 0, ',', '.');

    $n = round($n, $maxDec);
    $s = number_format($n, $maxDec, ',', '.');
    return rtrim(rtrim($s, '0'), ',');
  }
}

if (!function_exists('pj_unit_norm')) {
  function pj_unit_norm($u)
  {
    $u = trim((string)$u);
    if ($u === '') return '';

    $k = strtoupper($u);
    $map = [
      'MTR' => 'm', 'METER' => 'm', 'M' => 'm',
      'CM'  => 'cm',
      'PCS' => 'pcs',
    ];
    return $map[$k] ?? strtolower($u);
  }
}

if (!function_exists('pj_stock_cell')) {
  function pj_stock_cell($val, $unitRaw)
  {
    $num  = pj_fmt_stok($val);
    $unit = pj_unit_norm($unitRaw);
    return $unit ? ($num . '&nbsp;<small class="text-muted">'.$unit.'</small>') : $num;
  }
}

if (!function_exists('pj_low_badge')) {
  function pj_low_badge($total, $minimum)
  {
    $t = (float)$total;
    $m = (float)$minimum;
    $badgeStyle = 'font-size:11px;letter-spacing:0;display:inline-flex;align-items:center;gap:4px;border-radius:6px;padding:3px 7px;background:#ff4d4f;border:1px solid #ff4d4f;color:#fff;';
    $icon = '<i class="fa fa-warning" style="color:#ffd43b;font-size:15px;line-height:1;text-shadow:-1px 0 #111,0 1px #111,1px 0 #111,0 -1px #111;"></i>';

    if ($t <= 0) {
      return ' <span class="badge badge-danger ml-1" style="'.$badgeStyle.'">'.$icon.'<span>STOK KOSONG</span></span>';
    }

    if ($m > 0 && $t <= $m) {
      return ' <span class="badge badge-danger ml-1" style="'.$badgeStyle.'">'.$icon.'<span>LOW STOCK</span></span>';
    }

    return '';
  }
}


?>

<div class="card-block">
	<?php
		$stokTriggerStatus = isset($stok_keluar_trigger_status) ? (int)$stok_keluar_trigger_status : stok_keluar_trigger_status();
		$stokTriggerLabel = stok_keluar_trigger_status_label($stokTriggerStatus);
	?>
	<div class="d-flex align-items-center justify-content-between flex-wrap mb-3 stok-info-banner">
	  <div class="d-flex align-items-center" style="gap:8px;">
	    <i class="fa fa-info-circle" style="color:#2b6cb0;"></i>
	    <div class="stok-info-banner__text">
	      <b>Info:</b> Data Stok akan otomatis berkurang (setelah status di List Pekerjaan berubah menjadi <b class="stok-trigger-status-label"><?=$stokTriggerLabel;?></b>).
	    </div>
	  </div>
	  <button type="button" class="btn btn-primary btn-sm open_stok_trigger_setting stok-info-banner__button" data-status="<?=$stokTriggerStatus;?>">
	    <i class="fa fa-cog"></i> Atur Stok Berkurang
	  </button>
	</div>


  <table class="table table-striped table-mailcard">
    <thead>
      <tr>
        <th class="w-1 text-center">No</th>
        <th class="w-10 pl-1">Merk</th>
        <th class="w-8 text-right">Stok Awal</th>
        <th class="w-8 text-right">Stok Keluar</th>
        <th class="w-10 text-right">Total Stok</th>
      </tr>
    </thead>
    <tbody>
      <?php if(!empty($result)){
        $no = $this->uri->segment(3)+1;
        foreach ($result as $row){

          $unit = !empty($row->satuan) ? $row->satuan : '';

          $jml_masuk  = isset($row->stok_masuk)  ? (float)$row->stok_masuk  : (float)stok_masuk($row->id);
          $jml_keluar = isset($row->stok_keluar) ? (float)$row->stok_keluar : (float)stok_keluar($row->id);
          $total      = isset($row->total_stok)  ? (float)$row->total_stok  : ($jml_masuk - $jml_keluar);
          $minimum    = isset($row->batas_minimum_stok) ? (float)$row->batas_minimum_stok : 0;
          $title      = htmlspecialchars($row->title, ENT_QUOTES, 'UTF-8');
      ?>
        <tr>
          <td class="text-center"><?=$no?></td>
          <td class="pl-1"><?=$title?></td>

          <td class="text-right" data-value="<?=$jml_masuk?>"><?=pj_stock_cell($jml_masuk, $unit)?></td>
          <td class="text-right" data-value="<?=$jml_keluar?>"><?=pj_stock_cell($jml_keluar, $unit)?></td>
          <?php
            $unitNorm = pj_unit_norm($unit);
            $badgeLow = pj_low_badge($total, $minimum);
          ?>
          <td class="text-right" data-value="<?=$total?>">
            <?=pj_stock_cell($total, $unit).$badgeLow?>
          </td>

        </tr>
      <?php
          $no++;
        }
      } else { ?>
        <tr><td colspan="5">Data belum ada</td></tr>
      <?php } ?>
    </tbody>
  </table>

  <div class="stok-mobile-list">
    <?php if(!empty($result)){
      $no = $this->uri->segment(3)+1;
      foreach ($result as $row){
        $unit = !empty($row->satuan) ? $row->satuan : '';
        $jml_masuk  = isset($row->stok_masuk)  ? (float)$row->stok_masuk  : (float)stok_masuk($row->id);
        $jml_keluar = isset($row->stok_keluar) ? (float)$row->stok_keluar : (float)stok_keluar($row->id);
        $total      = isset($row->total_stok)  ? (float)$row->total_stok  : ($jml_masuk - $jml_keluar);
        $minimum    = isset($row->batas_minimum_stok) ? (float)$row->batas_minimum_stok : 0;
        $title      = htmlspecialchars($row->title, ENT_QUOTES, 'UTF-8');
        $badgeLow   = pj_low_badge($total, $minimum);
        $isDanger   = ($total <= 0 || ($minimum > 0 && $total <= $minimum));
        ?>
        <div class="stok-mobile-card">
          <div class="stok-mobile-head">
            <div class="stok-mobile-head-left">
              <div class="stok-mobile-badge"><?php echo $no; ?></div>
              <div class="min-w-0">
                <div class="stok-mobile-title"><?=$title?></div>
              </div>
            </div>
            <?php if (!empty($badgeLow)) { ?>
              <div class="stok-mobile-status"><?=$badgeLow?></div>
            <?php } ?>
          </div>

          <div class="stok-mobile-stats">
            <div class="stok-mobile-stat">
              <div class="stok-mobile-stat-label">Stok Awal</div>
              <div class="stok-mobile-stat-value"><?=pj_stock_cell($jml_masuk, $unit)?></div>
            </div>
            <div class="stok-mobile-stat">
              <div class="stok-mobile-stat-label">Stok Keluar</div>
              <div class="stok-mobile-stat-value"><?=pj_stock_cell($jml_keluar, $unit)?></div>
            </div>
            <div class="stok-mobile-stat">
              <div class="stok-mobile-stat-label">Total Stok</div>
              <div class="stok-mobile-stat-value <?= $isDanger ? 'is-danger' : ''; ?>"><?=pj_stock_cell($total, $unit)?></div>
            </div>
          </div>
        </div>
    <?php
        $no++;
      }
    }else{ ?>
      <div class="stok-mobile-footer">Data belum ada</div>
    <?php } ?>
  </div>

  <?php if(!empty($result)){ ?>
    <div class="stok-mobile-footer">Menampilkan : <?=count($result)?></div>
  <?php } ?>

  <nav aria-label="Page navigation" class="mt-2">
    <?php echo $this->ajax_pagination->create_links(); ?>
  </nav>
</div>
