<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
          v-model="listQuery.keyword"
          placeholder="SKU / 名称 / 规格 / 批次号 / 来源"
          style="width: 300px;"
          class="filter-item"
          clearable
          @keyup.enter="handleFilter"
      />

      <el-select v-model="listQuery.source_type" placeholder="全部来源" clearable class="filter-item" style="width: 140px; margin-left: 10px;">
        <el-option label="采购入库" value="buy" />
        <el-option label="半成品生产" value="semi" />
        <el-option label="成品完工" value="product" />
      </el-select>

      <el-date-picker
          v-model="listQuery.dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          class="filter-item"
          style="margin-left: 10px;"
          value-format="YYYY-MM-DD"
          @change="handleFilter"
      />
      <el-button type="primary" class="filter-item" style="margin-left: 10px;" @click="handleFilter">查询</el-button>
    </div>

    <el-table
        :data="list"
        v-loading="loading"
        border
        stripe
        style="width: 100%; margin-top: 20px;"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
    >
      <el-table-column prop="sku" label="SKU" min-width="140" fixed sortable show-overflow-tooltip />

      <el-table-column prop="name" label="物品名称" min-width="160" show-overflow-tooltip>
        <template #default="{ row }">
          <span style="font-weight: 500;">{{ row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="spec_model" label="规格型号" min-width="140" show-overflow-tooltip />

      <el-table-column label="分类" min-width="120" show-overflow-tooltip>
        <template #default="{ row }">
          <span>{{ row.category }}</span>
          <span v-if="row.category && row.material_type"> / </span>
          <span>{{ row.material_type }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="type_label" label="入库来源" width="110" align="center">
        <template #default="{ row }">
          <el-tag :type="getSourceTag(row.source_type)" effect="plain">
            {{ row.type_label }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="inbound_date" label="入库/生产日期" width="120" align="center" sortable />

      <el-table-column label="入库数量" width="110" align="center">
        <template #default="{ row }">
          <span style="font-weight: bold; color: #409EFF;">{{ row.quantity }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="batch_number" label="批次/序列号" min-width="140" show-overflow-tooltip />

      <el-table-column prop="source_info" label="供应商/负责人" min-width="140" show-overflow-tooltip />

      <el-table-column prop="status" label="当前状态" width="100" align="center" fixed="right">
        <template #default="{ row }">
          <el-tag size="small" :type="getStatusTag(row.status)">{{ row.status }}</el-tag>
        </template>
      </el-table-column>

    </el-table>

    <div style="margin-top: 20px; display: flex; justify-content: flex-end;">
      <el-pagination
          v-model:current-page="listQuery.page"
          v-model:page-size="listQuery.per_page"
          :total="total"
          :page-sizes="[10, 20, 50, 100, 200]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleFilter"
          @current-change="fetchData"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { getInboundSummaryList } from '@/api/inbound/inbound_summary'

const list = ref([])
const total = ref(0)
const loading = ref(false)

const listQuery = reactive({
  page: 1,
  per_page: 20, // 默认每页20
  keyword: '',
  source_type: '',
  dateRange: null as any
})

const fetchData = async () => {
  loading.value = true
  try {
    const params = {
      page: listQuery.page,
      per_page: listQuery.per_page,
      keyword: listQuery.keyword,
      source_type: listQuery.source_type,
      start_date: listQuery.dateRange ? listQuery.dateRange[0] : null,
      end_date: listQuery.dateRange ? listQuery.dateRange[1] : null
    }

    const res = await getInboundSummaryList(params)
    if (res.data) {
      list.value = res.data.items || []
      total.value = res.data.total || 0
    }
  } catch (error) {
    console.error('获取入库记录失败', error)
  } finally {
    loading.value = false
  }
}

// 查询操作重置页码
const handleFilter = () => {
  listQuery.page = 1
  fetchData()
}

// 来源类型的 Tag 颜色
const getSourceTag = (type: string) => {
  if (type === 'buy') return 'success'     // 绿色
  if (type === 'semi') return 'warning'    // 橙色
  if (type === 'product') return 'primary' // 蓝色
  return 'info'
}

// [新增] 状态的 Tag 颜色逻辑
const getStatusTag = (status: string) => {
  if (!status) return 'info'
  if (status.includes('已出库')) return 'info'    // 灰色
  if (status.includes('部分')) return 'warning'   // 橙色
  if (status.includes('不合格') || status.includes('异常')) return 'danger' // 红色
  return 'success' // 默认（如：合格、在库）为绿色
}

onMounted(() => {
  fetchData()
})
</script>