<script setup lang="ts">
import { adminInventoryCredentialTypeOptions } from '@/utils/admin-options'

const entrySkuCode = defineModel<string>('entrySkuCode', { required: true })
const entryCredentialType = defineModel<string>('entryCredentialType', { required: true })
const entryBatchNo = defineModel<string>('entryBatchNo', { required: true })
const entryInventoryGroupCode = defineModel<string>('entryInventoryGroupCode', { required: true })
const singleDisplayValue = defineModel<string>('singleDisplayValue', { required: true })
const bulkText = defineModel<string>('bulkText', { required: true })

defineProps<{
  creating: boolean
  importing: boolean
  skuSuggestionsLoading: boolean
  entrySkuSuggestions: Array<{
    credentialType: string
    inventoryGroupCode: string
    skuCode: string
    availableCount: number
    totalCount: number
  }>
  submitCreate: () => Promise<void>
  submitImport: () => Promise<void>
  applySkuSuggestion: (skuCode: string) => void
  getCredentialTypeLabel: (value: string) => string
  getCredentialTypeHint: (value: string) => string
  getSkuNamingHint: () => string
}>()
</script>

<template>
  <el-card shadow="never" class="section-card">
    <template #header>
      <div class="card-header">
        <span class="card-title">入库与导入</span>
        <span class="card-desc"
          >填写内部履约 SKU，不是平台侧 sku_code。建议同类商品长期复用同一个内部名称。</span
        >
      </div>
    </template>

    <div class="form-grid">
      <div class="sku-input-wrap">
        <input
          v-model="entrySkuCode"
          list="inventory-sku-suggestions"
          class="native-input"
          placeholder="内部履约 SKU，可选已有同类型，也可自定义新的中文名"
        />
        <datalist id="inventory-sku-suggestions">
          <option
            v-for="suggestion in entrySkuSuggestions"
            :key="`${suggestion.credentialType}-${suggestion.inventoryGroupCode}-${suggestion.skuCode}`"
            :value="suggestion.skuCode"
            :label="`${suggestion.skuCode} · ${suggestion.inventoryGroupCode || '未分组'} · 可用 ${suggestion.availableCount} / 共 ${suggestion.totalCount}`"
          />
        </datalist>
      </div>
      <el-select v-model="entryCredentialType" placeholder="凭据类型">
        <el-option
          v-for="opt in adminInventoryCredentialTypeOptions.filter((item) => item.value)"
          :key="opt.value"
          :label="opt.label"
          :value="opt.value"
        />
      </el-select>
      <el-input v-model="entryBatchNo" placeholder="批次号，可选" clearable />
      <el-input
        v-model="entryInventoryGroupCode"
        placeholder="库存组，例如 A组 / B组"
        clearable
      />
    </div>

    <div class="sku-helper-bar">
      <span class="helper-text">{{ getSkuNamingHint() }}</span>
      <span v-if="skuSuggestionsLoading" class="helper-text">正在加载同类型内部 SKU...</span>
    </div>

    <div v-if="entrySkuSuggestions.length > 0" class="sku-chip-row">
      <button
        v-for="suggestion in entrySkuSuggestions"
        :key="`${suggestion.credentialType}-${suggestion.inventoryGroupCode}-${suggestion.skuCode}-chip`"
        type="button"
        class="sku-chip"
        @click="applySkuSuggestion(suggestion.skuCode)"
      >
        <span class="sku-chip-title">{{ suggestion.skuCode }}</span>
        <span class="sku-chip-meta"
          >{{ suggestion.inventoryGroupCode || '未分组' }} · 可用
          {{ suggestion.availableCount }} / 共 {{ suggestion.totalCount }}</span
        >
      </button>
    </div>

    <div class="helper-grid">
      <div class="helper-item">
        <span class="helper-title">当前默认匹配</span>
        <span class="helper-copy">{{ getCredentialTypeLabel(entryCredentialType) }}</span>
      </div>
      <div class="helper-item">
        <span class="helper-title">库存组</span>
        <span class="helper-copy">{{ entryInventoryGroupCode || '未分组，默认全局可用' }}</span>
      </div>
      <div class="helper-item helper-item-wide">
        <span class="helper-title">说明</span>
        <span class="helper-copy">{{ getCredentialTypeHint(entryCredentialType) }}</span>
      </div>
    </div>

    <div class="single-row">
      <el-input
        v-model="singleDisplayValue"
        placeholder="单条新增的凭据内容"
        @keyup.enter="submitCreate"
      />
      <el-button :loading="creating" round type="success" @click="submitCreate"
        >单条新增</el-button
      >
    </div>

    <el-input
      v-model="bulkText"
      type="textarea"
      :rows="8"
      placeholder="批量导入支持两种格式：&#10;1. 每行一个凭据内容（使用上方默认内部 SKU / 凭据类型 / 批次 / 库存组）&#10;2. 每行一条：内部SKU|凭据类型|批次号|库存组|凭据内容"
      style="margin-top: 14px"
    />
    <p class="helper-text" style="margin-top: 8px">
      示例：sjz_hdl_test01|tencent_code|sjz-20260412|A组|DJQFf7HgXXXXXXXXXX
    </p>

    <div class="action-row">
      <el-button :loading="importing" round type="primary" @click="submitImport"
        >批量导入</el-button
      >
    </div>
  </el-card>
</template>

<style scoped>
@import '../../../../styles/admin-list-pages.css';

.form-grid {
  display: grid;
  grid-template-columns: 1.3fr 0.95fr 0.85fr 1fr 1fr;
  gap: var(--space-3);
}

.sku-input-wrap {
  display: grid;
  min-width: 0;
}

.native-input {
  width: 100%;
  min-width: 0;
  min-height: 40px;
  padding: 0 14px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-default);
  background: rgba(255, 255, 255, 0.74);
  color: var(--text-primary);
  font-size: var(--text-sm);
  outline: none;
  transition:
    border-color 0.16s ease,
    box-shadow 0.16s ease;
}

.native-input:focus {
  border-color: rgba(37, 89, 167, 0.38);
  background: rgba(255, 255, 255, 0.9);
  box-shadow: 0 0 0 3px rgba(37, 89, 167, 0.08);
}

.single-row,
.action-row {
  display: flex;
  gap: var(--space-3);
  align-items: center;
  margin-top: 14px;
}
.single-row :deep(.el-input) {
  min-width: 0;
}

.helper-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: var(--space-3);
  margin-top: 14px;
}

.helper-item {
  display: grid;
  gap: 6px;
  padding: 12px 14px;
  border: 1px solid rgba(148, 163, 184, 0.18);
  border-radius: var(--radius-md);
  background: rgba(248, 250, 252, 0.72);
}

.helper-title {
  color: #2559a7;
  font-size: var(--text-xs);
  font-weight: 700;
}
.helper-copy {
  min-width: 0;
  color: #334155;
  line-height: 1.6;
  word-break: break-word;
}

.sku-helper-bar {
  display: flex;
  justify-content: space-between;
  gap: var(--space-3);
  align-items: center;
  margin-top: var(--space-3);
}
.helper-text {
  color: var(--text-secondary);
  font-size: var(--text-xs);
  margin: 0;
}

.sku-chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: var(--space-3);
}

.sku-chip {
  display: inline-grid;
  gap: 4px;
  max-width: 100%;
  padding: 10px 14px;
  border-radius: var(--radius-md);
  border: 1px solid rgba(86, 108, 138, 0.16);
  background: rgba(248, 250, 252, 0.78);
  color: var(--text-primary);
  cursor: pointer;
  transition:
    transform 0.16s ease,
    border-color 0.16s ease,
    box-shadow 0.16s ease;
}

.sku-chip:hover {
  transform: translateY(-1px);
  border-color: rgba(37, 89, 167, 0.26);
  box-shadow: 0 12px 24px rgba(37, 89, 167, 0.08);
}
.sku-chip-title {
  min-width: 0;
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sku-chip-meta {
  min-width: 0;
  color: var(--text-secondary);
  font-size: var(--text-xs);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

@media (max-width: 1200px) {
  .form-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (max-width: 900px) {
  .form-grid,
  .helper-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .card-header {
    flex-direction: column;
    align-items: flex-start;
  }
}

@media (max-width: 640px) {
  .form-grid,
  .helper-grid {
    grid-template-columns: 1fr;
  }
  .single-row,
  .action-row,
  .sku-helper-bar {
    flex-direction: column;
    align-items: stretch;
  }
}
</style>
