<template>
  <div class="app-container">
    <el-card shadow="always">
      <template #header>
        <div class="card-header">
          <div class="header-left">
            <span class="title">出库拣货选单</span>
            <span class="subtitle">(打印目标: 192.168.9.205)</span>
          </div>
          <div>
            <el-button type="success" :icon="Printer" :disabled="selectedItems.length === 0" @click="handlePreview">
              生成并预览出库单
            </el-button>
          </div>
        </div>
      </template>

      <div class="filter-container">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-input
                v-model="searchKeyword"
                placeholder="请输入物料名称 或 规格型号 进行搜索"
                class="search-input"
                clearable
                :prefix-icon="Search"
            />
          </el-col>

          <el-col :span="12" style="text-align: right;">
            <el-button type="primary" plain :icon="Plus" @click="handleCreateBom">
              创建 BOM 表
            </el-button>
          </el-col>
        </el-row>

        <el-row :gutter="20" style="margin-top:15px">
          <el-col :span="12">
            <el-select v-model="selectedParentId" placeholder="选择 BOM 表" filterable style="width:100%" @change="onBomParentChange">
              <el-option v-for="item in bomParents" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-col>
          <el-col :span="12" style="text-align:right">
            <el-button type="primary" plain @click="addChildrenToSelection">添加子件到出库选单</el-button>
          </el-col>
        </el-row>
      </div>

      <el-table v-if="bomChildren.length > 0" :data="bomChildren" border style="width:100%; margin-bottom:15px">
        <el-table-column prop="child_name" label="子件名称" />
        <el-table-column prop="dosage" label="所需个数" />
        <el-table-column prop="current_stock" label="当前库存" />
        <el-table-column prop="max_producible" label="最大可生产" />
      </el-table>

      <el-alert
          v-if="selectedItems.length > 0"
          :title="`当前已选中 ${selectedItems.length} 项物品`"
          type="success"
          show-icon
          style="margin-bottom: 15px"
          :closable="false"
      />

      <el-table
          v-loading="loading"
          :data="filteredTableData"
          style="width: 100%"
          @selection-change="handleSelectionChange"
          row-key="uuid"
          border
          height="600"
      >
        <el-table-column type="selection" width="55" align="center" />

        <el-table-column label="类型" width="100" align="center">
          <template #default="{ row }">
            <el-tag :type="getTypeTag(row.type)">{{ row.typeLabel }}</el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="name" label="名称" min-width="150" show-overflow-tooltip>
          <template #default="{ row }">
            <span v-html="highlightKeyword(row.name)"></span>
          </template>
        </el-table-column>

        <el-table-column prop="standard" label="规格型号" min-width="150" show-overflow-tooltip>
          <template #default="{ row }">
            <span v-html="highlightKeyword(row.standard)"></span>
          </template>
        </el-table-column>

        <el-table-column prop="batch_no" label="批号" width="120" />
        <el-table-column prop="uuid" label="条码UUID" width="280" show-overflow-tooltip />
        <el-table-column prop="create_time" label="入库时间" width="170" />
      </el-table>
    </el-card>

    <el-dialog
        v-model="previewVisible"
        title="出库单打印预览"
        width="800px"
        destroy-on-close
    >
      <div class="print-preview-content">
        <el-alert title="请核对以下清单，确认无误后点击下方【确认打印】按钮" type="warning" :closable="false" style="margin-bottom: 10px;" />

        <el-table :data="selectedItems" border size="small" style="width: 100%">
          <el-table-column prop="typeLabel" label="类型" width="80" />
          <el-table-column prop="name" label="名称" />
          <el-table-column prop="standard" label="规格" />
          <el-table-column prop="batch_no" label="批号" width="100" />
          <el-table-column prop="uuid" label="条码UUID" show-overflow-tooltip />
        </el-table>

        <div class="summary-info" style="margin-top: 20px; text-align: right; font-weight: bold;">
          总计出库数量: <span style="color: red; font-size: 18px;">{{ selectedItems.length }}</span> 件
        </div>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="previewVisible = false">取消</el-button>
          <el-button type="primary" :loading="printLoading" @click="confirmPrint">
            确认打印
          </el-button>
        </span>
      </template>
    </el-dialog>

    <!-- BOM 编辑弹窗 -->
    <el-dialog v-model="bomDialogVisible" title="创建/编辑 BOM" width="800px">
      <el-form :model="bomForm" label-width="120px">
        <el-form-item label="父件 (成品)">
          <el-select v-model="bomForm.parent_id" placeholder="请选择" filterable style="width:100%">
            <el-option
              v-for="item in materialBaseOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <div style="font-weight: bold; margin-bottom: 10px;">子件列表</div>
        <el-table :data="bomForm.children" border style="width:100%; margin-top:10px">
          <el-table-column label="子件物料" width="300">
            <template #default="{ row, $index }">
              <el-select v-model="row.child_id" placeholder="请选择" filterable style="width:100%">
                <el-option
                  v-for="item in materialBaseOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="个数" width="150">
            <template #default="{ row }">
              <el-input-number v-model="row.dosage" :min="0" :precision="4" style="width:100%" />
            </template>
          </el-table-column>
          <el-table-column label="操作" width="80">
            <template #default="{ $index }">
              <el-button type="danger" size="small" @click="removeChildRow($index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div style="margin-top:10px">
          <el-button type="primary" @click="addChildRow">添加子件</el-button>
        </div>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="bomDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="saveBom">保存 BOM</el-button>
        </span>
      </template>
    </el-dialog>

  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { Printer, Search, Plus } from '@element-plus/icons-vue'
import { getAllStock, printSelectionList, getMaterialBaseList, saveBom as saveBomApi, getBomParents, getBom } from '@/api/inbound/stock'
import { ElMessage, ElMessageBox } from 'element-plus'

// --- 类型定义 ---
interface BaseStockItem {
  id: number | string;
  standard: string;
  batch_no: string;
  uuid: string;
  create_time: string;
  // 原始数据中可能存在的字段
  material_name?: string;
  product_name?: string;
}

// 统一后的显示对象
interface DisplayItem extends BaseStockItem {
  name: string;      // 统一后的名称
  type: 'material' | 'semi' | 'product'; // 类型标识
  typeLabel: string; // 类型中文名
}

// --- 状态变量 ---
const loading = ref(false)
const printLoading = ref(false)
const searchKeyword = ref('') // 搜索关键词
const previewVisible = ref(false) // 预览弹窗控制

// 原始扁平化数据
const allStockData = ref<DisplayItem[]>([])

// 当前选中的行
const selectedItems = ref<DisplayItem[]>([])

// BOM 相关
const bomDialogVisible = ref(false)
const materialBaseOptions = ref<any[]>([])

// BOM 选择功能
const bomParents = ref<any[]>([])
const selectedParentId = ref<number|null>(null)
const bomChildren = ref<any[]>([])

const bomForm = ref({
  parent_id: null as number | null,
  children: [] as any[]
})

// --- 计算属性：前端模糊搜索过滤 ---
const filteredTableData = computed(() => {
  const keyword = searchKeyword.value.trim().toLowerCase()
  if (!keyword) {
    return allStockData.value
  }
  return allStockData.value.filter(item => {
    const nameMatch = item.name && item.name.toLowerCase().includes(keyword)
    const stdMatch = item.standard && item.standard.toLowerCase().includes(keyword)
    // 也可以加上UUID搜索
    const uuidMatch = item.uuid && item.uuid.toLowerCase().includes(keyword)
    return nameMatch || stdMatch || uuidMatch
  })
})

// --- 方法 ---

// 1. 获取并处理数据
const fetchData = async () => {
  loading.value = true
  try {
    const res: any = await getAllStock()
    // 假设 res 结构为 { materials: [], semis: [], products: [] }
    const materials = (res.materials || []).map((item: any) => ({
      ...item,
      name: item.material_name,
      type: 'material',
      typeLabel: '采购件',
      base_id: item.base_id
    }))
    const semis = (res.semis || []).map((item: any) => ({
      ...item,
      name: item.material_name || item.product_name, // 半成品字段名不确定，做个兼容
      type: 'semi',
      typeLabel: '半成品',
      base_id: item.base_id
    }))
    const products = (res.products || []).map((item: any) => ({
      ...item,
      name: item.product_name,
      type: 'product',
      typeLabel: '成品',
      base_id: item.base_id
    }))

    // 合并所有数据
    allStockData.value = [...materials, ...semis, ...products]
  } catch (error) {
    console.error(error)
    ElMessage.error('无法获取库存数据')
  } finally {
    loading.value = false
  }
}

// 2. 表格选择
const handleSelectionChange = (val: DisplayItem[]) => {
  selectedItems.value = val
}

// 3. 点击“生成并预览”
const handlePreview = () => {
  if (selectedItems.value.length === 0) {
    ElMessage.warning('请先勾选需要出库的物品')
    return
  }
  previewVisible.value = true
}

// 4. 确认打印 (在弹窗中触发)
const confirmPrint = async () => {
  printLoading.value = true
  try {
    // 这里调用真实的打印接口
    await printSelectionList(selectedItems.value)
    ElMessage.success('指令已发送，请前往打印机(192.168.9.205)取单')
    previewVisible.value = false // 关闭弹窗
    // 可选：打印后是否清空选中？
    // selectedItems.value = []
    // 注意：el-table 需要调用 clearSelection 方法来清空UI选中状态
  } catch (err) {
    ElMessage.error('打印请求失败')
  } finally {
    printLoading.value = false
  }
}

// 5. BOM 操作 (只保留创建)

const handleCreateBom = async () => {
  bomDialogVisible.value = true
  if (materialBaseOptions.value.length === 0) {
    try {
      const res = await getMaterialBaseList({})
      if (res.code === 200) {
        materialBaseOptions.value = res.data
      } else {
        ElMessage.error('获取物料列表失败')
      }
    } catch (err) {
      ElMessage.error('网络错误，无法获取物料列表')
    }
  }
}

const addChildRow = () => {
  bomForm.value.children.push({
    child_id: null,
    dosage: 0,
    remark: ''
  })
}

const removeChildRow = (index: number) => {
  bomForm.value.children.splice(index, 1)
}

const saveBom = async () => {
  if (!bomForm.value.parent_id) {
    ElMessage.warning('请选择父件')
    return
  }
  if (bomForm.value.children.length === 0) {
    ElMessage.warning('请至少添加一个子件')
    return
  }
  for (const child of bomForm.value.children) {
    if (!child.child_id) {
      ElMessage.warning('请为每个子件选择物料')
      return
    }
  }
  const payload = {
    parent_id: bomForm.value.parent_id,
    children: bomForm.value.children.map(c => ({
      child_id: c.child_id,
      dosage: c.dosage,
      remark: c.remark || ''
    }))
  }
  try {
    const res = await saveBomApi(payload)
    if (res.code === 200) {
      ElMessage.success('BOM保存成功')
      bomDialogVisible.value = false
      // 清空表单
      bomForm.value = {
        parent_id: null,
        children: []
      }
    } else {
      ElMessage.error(res.msg || '保存失败')
    }
  } catch (err) {
    ElMessage.error('网络错误')
  }
}

// 辅助函数：高亮关键词 (可选)
const highlightKeyword = (text: string) => {
  if (!searchKeyword.value || !text) return text
  const reg = new RegExp(searchKeyword.value, 'gi')
  return text.replace(reg, (match) => `<span style="color: red; font-weight: bold;">${match}</span>`)
}

// 辅助函数：标签颜色
const getTypeTag = (type: string) => {
  switch (type) {
    case 'material': return 'info'
    case 'semi': return 'warning'
    case 'product': return 'success'
    default: return ''
  }
}

// 6. BOM 选择功能
const onBomParentChange = async (val: number) => {
  selectedParentId.value = val
  if (val) {
    try {
      const res = await getBom(val)
      if (res.code === 200) {
        bomChildren.value = res.data
      } else {
        ElMessage.error(res.msg || '获取BOM详情失败')
      }
    } catch (err) {
      ElMessage.error('网络错误，无法获取BOM详情')
    }
  } else {
    bomChildren.value = []
  }
}

const addChildrenToSelection = () => {
  if (bomChildren.value.length === 0) {
    ElMessage.warning('当前没有可添加的子件')
    return
  }
  let addedCount = 0
  for (const child of bomChildren.value) {
    // 寻找匹配的库存项 (根据 base_id)
    const matchingItems = allStockData.value.filter(item => item.base_id == child.child_id)
    if (matchingItems.length > 0) {
      const existingIds = selectedItems.value.map(s => s.id)
      // 最多添加 dosage 个 (简单起见每个匹配项添加一个)
      for (let i = 0; i < Math.min(child.dosage, matchingItems.length); i++) {
        const stock = matchingItems[i]
        if (!existingIds.includes(stock.id)) {
          selectedItems.value.push(stock)
          addedCount++
        }
      }
    } else {
      ElMessage.warning(`物料 ${child.child_name} 暂无库存`)
    }
  }
  if (addedCount > 0) {
    ElMessage.success(`已添加 ${addedCount} 个子件到选单`)
  }
}

onMounted(async () => {
  fetchData()
  // 加载BOM父件列表
  try {
    const res = await getBomParents()
    if (res.code === 200) {
      bomParents.value = res.data
    }
  } catch (err) {
    console.error('加载BOM父件列表失败', err)
  }
})
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left .title {
  font-size: 18px;
  font-weight: bold;
  margin-right: 10px;
}

.header-left .subtitle {
  font-size: 12px;
  color: #909399;
}

.filter-container {
  margin-bottom: 20px;
  background-color: #f5f7fa;
  padding: 15px;
  border-radius: 4px;
}

.search-input {
  width: 100%;
  max-width: 400px;
}
</style>
