<template>
  <q-dialog ref="dialogRef" @hide="onDialogHide">
    <q-card style="width: 800px; max-width: 95vw; border-radius: 12px">
      <q-bar class="bg-transparent q-px-md" style="height: 55px">
        <span class="text-h6 text-dark" style="font-weight: 600"
          >Estoque de Produtos</span
        >
        <q-space />
        <q-btn dense flat icon="mdi-close" @click="onDialogCancel" />
      </q-bar>

      <q-card-section class="q-pt-none q-pb-lg q-px-md">
        <q-card flat bordered style="border-radius: 8px; padding: 20px">
          <div class="text-subtitle1 text-dark q-mb-md" style="font-weight: 600">
            Meu Estoque
          </div>

          <q-card flat bordered style="border-radius: 8px">
            <div class="list-header q-px-md q-py-xs">
              <span class="text-caption text-grey-7">Produto</span>
              <span class="text-caption text-grey-7">Estoque</span>
            </div>

            <q-separator />

            <div v-if="props.products.length === 0" class="text-grey-6 text-center q-pa-lg">
              Nenhum produto cadastrado.
            </div>
            <template v-for="(item, index) in props.products" :key="item.id">
              <div class="list-row q-px-md q-py-sm">
                <span class="text-body2 text-dark">{{ item.name }}</span>
                <div class="row items-center" style="gap: 12px">
                  <span class="text-body2 text-dark">{{ item.quantity }}</span>
                  <q-badge
                    :label="item.status"
                    :outline="item.status === 'Limitado'"
                    :color="statusColor(item.status)"
                    style="border-radius: 8px; font-size: 11px; padding: 4px 10px"
                  />
                </div>
              </div>
              <q-separator v-if="index < props.products.length - 1" />
            </template>
          </q-card>
        </q-card>
      </q-card-section>
    </q-card>
  </q-dialog>
</template>

<script setup>
import { useDialogPluginComponent } from "quasar";

defineEmits([...useDialogPluginComponent.emits]);

const { dialogRef, onDialogHide, onDialogCancel } = useDialogPluginComponent();

const props = defineProps({
  products: {
    type: Array,
    default: () => [],
  },
});

const statusColor = (status) => {
  if (status === "Alto") return "primary";
  if (status === "Baixo") return "orange";
  return "primary";
};
</script>

<style scoped>
.list-header {
  display: grid;
  grid-template-columns: 1fr 1fr;
  align-items: center;
}

.list-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  align-items: center;
}
</style>
