<?php $__env->startSection('title', __('Cost Estimation')); ?>
<?php $__env->startSection('active', 'costs'); ?>
<?php $__env->startSection('page_eyebrow', 'Cost Estimation'); ?>
<?php $__env->startSection('page_heading', 'Repair cost workspace'); ?>

<?php $__env->startSection('content'); ?>
  <?php
    $severityClasses = [
      'high' => 'bg-red-50 text-red-600',
      'medium' => 'bg-amber-50 text-amber-600',
      'low' => 'bg-emerald-50 text-emerald-600',
    ];
    $activeCollection = $collections->firstWhere('id', (int) $filters['collection_id']);
  ?>

  <div class="rounded-2xl border border-[#c7d3fc] bg-[#eff3fe] p-6 lg:p-8">
    <div class="flex flex-col gap-5">
      <div>
        <p class="text-xs font-medium text-[#4f6ef7]"><?php echo e(__('Dynamic repair estimates')); ?> · <?php echo e(__('Workspace')); ?>: <?php echo e($activeWorkspace->name); ?></p>
        <h2 class="mt-2 text-2xl font-bold tracking-tight lg:text-3xl"><span data-live="repairCost">Rp <?php echo e(number_format($stats['repairCost'], 0, ',', '.')); ?></span> <?php echo e(__('in finding-level repairs.')); ?></h2>
        <p class="mt-2 max-w-2xl text-sm leading-6 text-gray-500">
          <?php echo e($activeCollection ? __('Filtered to collection: :name', ['name' => $activeCollection->displayName()]) : __('Compare total inspection estimates with AI damage finding estimates and prioritize high-severity work.')); ?>

        </p>
      </div>

      <div class="grid gap-3 md:grid-cols-2 xl:grid-cols-4">
        <?php $__currentLoopData = $workspaces; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $workspace): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
          <a href="<?php echo e(route('costs.index', ['workspace_id' => $workspace->id])); ?>" data-spa-link class="rounded-xl border p-4 transition-colors <?php echo e((int) $filters['workspace_id'] === $workspace->id ? 'border-[#4f6ef7] bg-white shadow-sm' : 'border-[#c7d3fc]/70 bg-white/60 hover:bg-white'); ?>">
            <div class="flex items-center justify-between gap-3">
              <p class="truncate text-sm font-semibold text-gray-900"><?php echo e($workspace->name); ?></p>
              <span class="rounded-full bg-[#eff3fe] px-2 py-0.5 text-[11px] font-medium text-[#4f6ef7]"><?php echo e($workspace->collections_count); ?></span>
            </div>
            <p class="mt-2 text-xs text-gray-500"><?php echo e(__('Estimated cost')); ?></p>
            <p class="mt-1 text-lg font-bold">Rp <?php echo e(number_format($workspace->estimated_cost_sum ?? 0, 0, ',', '.')); ?></p>
          </a>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
      </div>

      <form id="costFilterForm" method="GET" action="<?php echo e(route('costs.index')); ?>" class="grid w-full gap-3 md:grid-cols-2 xl:grid-cols-[1fr_220px_220px_160px_auto]">
        <input name="search" value="<?php echo e($filters['search']); ?>" type="search" placeholder="<?php echo e(__('Search finding...')); ?>" class="h-11 rounded-xl border border-[#c7d3fc] bg-white px-4 text-sm outline-none focus:border-[#4f6ef7] focus:ring-2 focus:ring-[#4f6ef7]/20">
        <select id="costWorkspaceSelect" name="workspace_id" class="h-11 rounded-xl border border-[#c7d3fc] bg-white px-4 text-sm outline-none focus:border-[#4f6ef7] focus:ring-2 focus:ring-[#4f6ef7]/20">
          <?php $__currentLoopData = $workspaces; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $workspace): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <option value="<?php echo e($workspace->id); ?>" <?php if((int) $filters['workspace_id'] === $workspace->id): echo 'selected'; endif; ?>><?php echo e($workspace->name); ?></option>
          <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </select>
        <select id="costCollectionSelect" name="collection_id" class="h-11 rounded-xl border border-[#c7d3fc] bg-white px-4 text-sm outline-none focus:border-[#4f6ef7] focus:ring-2 focus:ring-[#4f6ef7]/20">
          <option value=""><?php echo e(__('All collections')); ?></option>
          <?php $__currentLoopData = $collections; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $collection): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <option value="<?php echo e($collection->id); ?>" <?php if((int) $filters['collection_id'] === $collection->id): echo 'selected'; endif; ?>><?php echo e($collection->displayName()); ?></option>
          <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </select>
        <select name="severity" class="h-11 rounded-xl border border-[#c7d3fc] bg-white px-4 text-sm outline-none focus:border-[#4f6ef7] focus:ring-2 focus:ring-[#4f6ef7]/20">
          <option value=""><?php echo e(__('All severity')); ?></option>
          <?php $__currentLoopData = ['high', 'medium', 'low']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $severity): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
            <option value="<?php echo e($severity); ?>" <?php if($filters['severity'] === $severity): echo 'selected'; endif; ?>><?php echo e(Str::headline($severity)); ?></option>
          <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </select>
        <button type="submit" class="h-11 rounded-xl bg-[#4f6ef7] px-4 text-sm font-medium text-white shadow-sm hover:bg-[#3d5ce0]"><?php echo e(__('Filter')); ?></button>
      </form>
    </div>
  </div>

  <div class="grid gap-4 sm:grid-cols-2 xl:grid-cols-4">
    <div class="panel rounded-2xl border border-gray-100 bg-white p-5 shadow-sm">
      <p class="text-sm font-medium text-gray-500"><?php echo e(__('Inspection Cost')); ?></p>
      <p class="mt-2 text-2xl font-bold tracking-tight" data-live="estimatedCost">Rp <?php echo e(number_format($stats['inspectionCost'], 0, ',', '.')); ?></p>
      <p class="mt-2 text-xs font-medium text-[#4f6ef7]"><?php echo e(__('From inspections')); ?></p>
    </div>
    <div class="panel rounded-2xl border border-gray-100 bg-white p-5 shadow-sm">
      <p class="text-sm font-medium text-gray-500"><?php echo e(__('Repair Cost')); ?></p>
      <p class="mt-2 text-2xl font-bold tracking-tight" data-live="repairCost">Rp <?php echo e(number_format($stats['repairCost'], 0, ',', '.')); ?></p>
      <p class="mt-2 text-xs font-medium text-amber-500"><?php echo e(__('From findings')); ?></p>
    </div>
    <div class="panel rounded-2xl border border-gray-100 bg-white p-5 shadow-sm">
      <p class="text-sm font-medium text-gray-500"><?php echo e(__('High Risk Cost')); ?></p>
      <p class="mt-2 text-2xl font-bold tracking-tight">Rp <?php echo e(number_format($stats['highRiskCost'], 0, ',', '.')); ?></p>
      <p class="mt-2 text-xs font-medium text-red-500"><?php echo e(__('High severity only')); ?></p>
    </div>
    <div class="panel rounded-2xl border border-gray-100 bg-white p-5 shadow-sm">
      <p class="text-sm font-medium text-gray-500"><?php echo e(__('Findings')); ?></p>
      <p class="mt-2 text-2xl font-bold tracking-tight"><?php echo e($stats['findingCount']); ?></p>
      <p class="mt-2 text-xs font-medium text-gray-400"><?php echo e(__('Estimated items')); ?></p>
    </div>
  </div>

  <div class="grid gap-6 xl:grid-cols-[1.4fr_.8fr]">
    <div class="rounded-2xl border border-gray-100 bg-white p-5 shadow-sm">
      <div class="mb-4 flex flex-col gap-2 sm:flex-row sm:items-center sm:justify-between">
        <h3 class="text-base font-semibold tracking-tight"><?php echo e(__('Repair estimate list')); ?></h3>
        <p class="text-sm text-gray-400"><?php echo e(__(':count matched', ['count' => $findings->total()])); ?></p>
      </div>

      <div class="overflow-x-auto">
        <table class="w-full text-left text-sm">
          <thead class="text-xs uppercase text-gray-400">
            <tr>
              <th class="px-3 py-3"><?php echo e(__('Finding')); ?></th>
              <th class="px-3 py-3"><?php echo e(__('Collection')); ?></th>
              <th class="px-3 py-3"><?php echo e(__('Property')); ?></th>
              <th class="px-3 py-3"><?php echo e(__('Severity')); ?></th>
              <th class="px-3 py-3"><?php echo e(__('Confidence')); ?></th>
              <th class="px-3 py-3"><?php echo e(__('Cost')); ?></th>
            </tr>
          </thead>
          <tbody class="divide-y divide-gray-100">
            <?php $__empty_1 = true; $__currentLoopData = $findings; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $finding): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
              <tr>
                <td class="px-3 py-3">
                  <p class="font-medium"><?php echo e($finding->category); ?></p>
                  <p class="mt-0.5 text-xs text-gray-400"><?php echo e(Str::headline($finding->status)); ?></p>
                </td>
                <td class="px-3 py-3">
                  <p class="font-medium"><?php echo e($finding->inspection->collection?->displayName() ?? __('Unbatched')); ?></p>
                  <p class="mt-0.5 text-xs text-gray-400"><?php echo e(Str::headline($finding->inspection->collection?->collection_type ?? 'single scan')); ?></p>
                </td>
                <td class="px-3 py-3">
                  <p class="font-medium"><?php echo e($finding->inspection->property_name); ?></p>
                  <p class="mt-0.5 text-xs text-gray-400"><?php echo e($finding->inspection->property_address); ?></p>
                </td>
                <td class="px-3 py-3">
                  <span class="rounded-lg px-2 py-1 text-xs font-medium <?php echo e($severityClasses[$finding->severity] ?? 'bg-gray-100 text-gray-600'); ?>">
                    <?php echo e(Str::headline($finding->severity)); ?>

                  </span>
                </td>
                <td class="px-3 py-3"><?php echo e(number_format($finding->confidence, 1)); ?>%</td>
                <td class="px-3 py-3 font-medium">Rp <?php echo e(number_format($finding->estimated_repair_cost, 0, ',', '.')); ?></td>
              </tr>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
              <tr>
                <td colspan="6" class="px-3 py-10 text-center text-sm text-gray-400"><?php echo e(__('No cost estimates match the current filters.')); ?></td>
              </tr>
            <?php endif; ?>
          </tbody>
        </table>
      </div>

      <div class="mt-5">
        <?php echo e($findings->links()); ?>

      </div>
    </div>

    <div class="rounded-2xl border border-gray-100 bg-white p-5 shadow-sm">
      <h3 class="text-base font-semibold tracking-tight"><?php echo e(__('Severity breakdown')); ?></h3>
      <div class="mt-4 space-y-4">
        <?php $__currentLoopData = ['high' => __('High'), 'medium' => __('Medium'), 'low' => __('Low')]; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
          <?php
            $row = $severityBreakdown[$key] ?? null;
            $total = (int) ($row?->total ?? 0);
            $cost = (float) ($row?->cost ?? 0);
            $width = $stats['repairCost'] > 0 ? max(8, ($cost / $stats['repairCost']) * 100) : 0;
          ?>
          <div>
            <div class="flex items-center justify-between gap-3 text-sm">
              <span class="font-medium"><?php echo e($label); ?></span>
              <span class="text-gray-500"><?php echo e(__(':count items', ['count' => $total])); ?></span>
            </div>
            <div class="mt-2 h-2 overflow-hidden rounded-full bg-gray-100">
              <div class="h-full rounded-full bg-[#4f6ef7]" style="width: <?php echo e($width); ?>%"></div>
            </div>
            <p class="mt-2 text-xs text-gray-500">Rp <?php echo e(number_format($cost, 0, ',', '.')); ?></p>
          </div>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
      </div>
    </div>
  </div>

  <div class="rounded-2xl border border-gray-100 bg-white p-5 shadow-sm">
    <div class="flex flex-col gap-4 lg:flex-row lg:items-center lg:justify-between">
      <div>
        <h3 class="text-base font-semibold"><?php echo e(__('What-if Cost Adjustment')); ?></h3>
        <p class="text-sm text-gray-400"><?php echo e(__('Use this UI control to preview contingency planning without saving data.')); ?></p>
      </div>
      <div class="flex items-center gap-3">
        <input id="contingencySlider" type="range" min="0" max="30" value="10" class="w-40 accent-[#4f6ef7]">
        <span class="w-14 text-sm font-semibold text-[#4f6ef7]"><span id="contingencyValue">10</span>%</span>
        <span class="text-sm text-gray-500"><?php echo e(__('Preview')); ?>: <strong id="contingencyPreview">Rp <?php echo e(number_format($stats['repairCost'] * 1.1, 0, ',', '.')); ?></strong></span>
      </div>
    </div>
  </div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
<script data-page-script>
  (function () {
    const slider = document.getElementById('contingencySlider');
    const value = document.getElementById('contingencyValue');
    const preview = document.getElementById('contingencyPreview');
    const baseCost = <?php echo e((float) $stats['repairCost']); ?>;
    if (!slider || !value || !preview) return;

    function render() {
      const pct = Number(slider.value);
      value.textContent = pct;
      preview.textContent = 'Rp ' + Math.round(baseCost * (1 + pct / 100)).toLocaleString('id-ID');
    }

    slider.addEventListener('input', render);
    render();

    document.getElementById('costWorkspaceSelect')?.addEventListener('change', () => {
      const collection = document.getElementById('costCollectionSelect');
      if (collection) collection.value = '';
      document.getElementById('costFilterForm')?.submit();
    });
  })();
</script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\Users\ariqa\Documents\matkul\smster 4\data sains\prj\homeinspector\resources\views/costs/index.blade.php ENDPATH**/ ?>