<template>
  <ContentWrap>
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="auto">
      <el-form-item label="样品码" prop="barCode">
        <el-input v-model="queryParams.barCode" placeholder="请输入样品码" clearable class="!w-220px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="样品名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入样品名称" clearable class="!w-220px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="样品类型" prop="categoryType">
        <el-select v-model="queryParams.categoryType" placeholder="请选择样品类型" clearable class="!w-180px">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.MATERIAL_CLASSIFICATION_TYPE)" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="样品分类" prop="sampleCategory">
        <el-select v-model="queryParams.sampleCategory" placeholder="请选择样品分类" clearable class="!w-180px">
          <el-option v-for="dict in getStrDictOptions(DICT_TYPE.BIT_SAMPLE_CATEGORY)" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input v-model="queryParams.customerName" placeholder="请输入客户名称" clearable class="!w-220px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" />查询</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" />重置</el-button>
        <el-button type="primary" plain v-hasPermi="['erp:bit-wms-sample:create']" @click="openProductCreate">
          <Icon icon="ep:plus" class="mr-5px" />新增样品
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" row-key="id">
      <el-table-column label="样品码" align="center" prop="barCode" min-width="160" />
      <el-table-column label="样品名称" align="left" prop="name" min-width="180" />
      <el-table-column label="样品类型" align="center" prop="categoryType" min-width="110">
        <template #default="{ row }">
          <dict-tag :type="DICT_TYPE.MATERIAL_CLASSIFICATION_TYPE" :value="row.categoryType" />
        </template>
      </el-table-column>
      <el-table-column label="样品分类" align="center" prop="sampleCategory" min-width="120">
        <template #default="{ row }">
          <dict-tag :type="DICT_TYPE.BIT_SAMPLE_CATEGORY" :value="row.sampleCategory" />
        </template>
      </el-table-column>
      <el-table-column label="规格" align="center" prop="standard" min-width="140" />
      <el-table-column label="客户名称" align="center" prop="customerName" min-width="140" />
      <el-table-column label="默认仓库" align="center" min-width="160">
        <template #default="{ row }">
          {{ row.defaultWarehouseName || row.warehouseName || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template #default="{ row }">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="210" fixed="right">
        <template #default="{ row }">
          <el-button link type="primary" @click="openProductDetail(row.id)">详情</el-button>
          <el-button link type="primary" v-hasPermi="['erp:bit-wms-sample:update']" @click="openProductEdit(row.id)">编辑</el-button>
          <BitWmsPrintButton
            v-hasPermi="['erp:bit-wms-sample:print']"
            title="样品标签"
            :load-label="() => loadSampleLabel(row.id)"
          />
        </template>
      </el-table-column>
    </el-table>
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize" @pagination="getList" />
  </ContentWrap>
</template>

<script setup lang="ts">
import { DICT_TYPE, getIntDictOptions, getStrDictOptions } from '@/utils/dict'
import { BitWmsApi } from '@/api/erp/bitwms'
import BitWmsPrintButton from '../components/BitWmsPrintButton.vue'

defineOptions({ name: 'ErpBitWmsSample' })

const router = useRouter()
const loading = ref(true)
const list = ref<any[]>([])
const total = ref(0)
const queryFormRef = ref()
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  barCode: undefined,
  name: undefined,
  categoryType: undefined,
  sampleCategory: undefined,
  customerName: undefined
})

const buildQueryParams = () =>
  Object.fromEntries(Object.entries(queryParams).filter(([, value]) => value !== undefined && value !== null && value !== ''))

const getList = async () => {
  loading.value = true
  try {
    const data = await BitWmsApi.getSamplePage(buildQueryParams())
    list.value = data.list || []
    total.value = data.total || 0
  } finally {
    loading.value = false
  }
}

const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const loadSampleLabel = async (id: number) => await BitWmsApi.getSampleLabel(id)
const openProductCreate = () => router.push({ path: '/erp/product/product' })
const openProductEdit = (id: number) => router.push({ path: '/erp/product/product', query: { id, type: 'update' } })
const openProductDetail = (id: number) => router.push({ path: '/erp/product/product', query: { id, type: 'detail' } })

onMounted(getList)
</script>
