<template>
  <div>
    <div class="page-header">
      <h2>Printers</h2>
      <router-link to="/printers/new" class="btn btn-primary">Add Printer</router-link>
    </div>

    <!-- Filters -->
    <div class="filters">
      <input
        v-model="search"
        type="text"
        class="form-control"
        placeholder="Search printers..."
        @input="debouncedSearch"
      />
    </div>

    <div class="card">
      <div v-if="loading" class="loading">Loading...</div>

      <template v-else>
        <div class="table-container">
          <table>
            <thead>
              <tr>
                <th>Asset #</th>
                <th>Name</th>
                <th>Business Unit</th>
                <th>Model</th>
                <th>Status</th>
                <th>Actions</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="printer in printers" :key="printer.printer?.printerid || printer.assetid">
                <td>{{ printer.assetnumber }}</td>
                <td>{{ printer.name || '-' }}</td>
                <td>{{ printer.businessunit_name || '-' }}</td>
                <td>{{ printer.printer?.model_name || '-' }}</td>
                <td>
                  <span class="badge" :class="getStatusClass(printer.status_name)">
                    {{ printer.status_name || 'Active' }}
                  </span>
                </td>
                <td class="actions">
                  <router-link
                    :to="`/printers/${printer.printer?.printerid || printer.assetid}`"
                    class="btn btn-secondary btn-sm"
                  >
                    View
                  </router-link>
                </td>
              </tr>
              <tr v-if="printers.length === 0">
                <td colspan="6" style="text-align: center; color: var(--text-light);">
                  No printers found
                </td>
              </tr>
            </tbody>
          </table>
        </div>

        <!-- Pagination -->
        <div class="pagination" v-if="totalPages > 1">
          <button
            v-for="p in totalPages"
            :key="p"
            :class="{ active: p === page }"
            @click="goToPage(p)"
          >
            {{ p }}
          </button>
        </div>
      </template>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { printersApi } from '../../api'

const printers = ref([])
const loading = ref(true)
const search = ref('')
const page = ref(1)
const totalPages = ref(1)

let searchTimeout = null

onMounted(() => {
  loadPrinters()
})

async function loadPrinters() {
  loading.value = true
  try {
    const params = {
      page: page.value,
      perpage: 20
    }
    if (search.value) params.search = search.value

    const response = await printersApi.list(params)
    printers.value = response.data.data || []
    totalPages.value = response.data.meta?.pagination?.total_pages || 1
  } catch (error) {
    console.error('Error loading printers:', error)
  } finally {
    loading.value = false
  }
}

function debouncedSearch() {
  clearTimeout(searchTimeout)
  searchTimeout = setTimeout(() => {
    page.value = 1
    loadPrinters()
  }, 300)
}

function goToPage(p) {
  page.value = p
  loadPrinters()
}

function getStatusClass(status) {
  if (!status) return 'badge-info'
  const s = status.toLowerCase()
  if (s === 'in use' || s === 'active') return 'badge-success'
  if (s === 'in repair') return 'badge-warning'
  if (s === 'retired') return 'badge-danger'
  return 'badge-info'
}
</script>
