<template>
  <el-dialog
    v-model="dialogVisible"
    title="批量垛位"
    width="500px"
    :close-on-click-modal="false"
    destroy-on-close
    @close="handleClose"
  >
    <el-form :model="form" label-width="100px" label-position="right">
      <el-form-item label="库房名称">
        <el-select
          v-model="form.wareId"
          clearable
          filterable
          placeholder="请选择"
          style="width: 100%;"
        >
          <el-option
            v-for="item in wareOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="新垛位">
        <el-input v-model="form.stackName" placeholder="请输入新垛位" />
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="success" @click="handleSave">保存</el-button>
        <el-button type="danger" @click="handleCancel">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, getCurrentInstance } from 'vue'
import { ElMessage } from 'element-plus'

const { proxy } = getCurrentInstance()

const dialogVisible = ref(false)
const wareOptions = ref([])
const selectedRows = ref([])

const form = ref({
  wareId: '',
  stackName: ''
})

// 获取库房选项
const fetchWareOptions = async () => {
  try {
    const response = await proxy.$axios.get('sto/Ware/options', {})
    if (response.data.code === 0 || response.data.code === '0') {
      const options = response.data.data || []
      wareOptions.value = options.map(item => ({
        label: item.label || item.name || item.value || '',
        value: item.value || item.id || item.code || item.label || ''
      })).filter(item => item.label && item.value)
    }
  } catch (error) {
    console.error('获取库房选项失败:', error)
  }
}

// 打开弹窗
const open = (rows) => {
  if (!rows || rows.length === 0) {
    ElMessage.warning('请先选择要批量倒垛的数据')
    return
  }
  
  selectedRows.value = rows
  
  // 重置表单
  form.value = {
    wareId: '',
    stackName: ''
  }
  
  // 获取库房选项
  fetchWareOptions()
  
  dialogVisible.value = true
}

// 关闭弹窗
const handleClose = () => {
  dialogVisible.value = false
  selectedRows.value = []
  form.value = {
    wareId: '',
    stackName: ''
  }
}

// 保存
const handleSave = async () => {
  if (!form.value.wareId) {
    ElMessage.warning('请选择库房名称')
    return
  }
  
  if (!form.value.stackName || form.value.stackName.trim() === '') {
    ElMessage.warning('请输入新垛位')
    return
  }
  
  try {
    // 构建批量倒垛的数据
    const moveData = {
      wareId: form.value.wareId,
      stackName: form.value.stackName.trim(),
      ids: selectedRows.value.map(row => row.id || row.inProdNo).filter(id => id)
    }
    
    // TODO: 根据实际API调整接口地址和参数格式
    const url = 'sto/OutRecord/batchMove' // 需要根据实际API调整
    const param = {
      json: JSON.stringify(moveData),
      jsonList: JSON.stringify(selectedRows.value)
    }
    
    const response = await proxy.$axios.post(url, param)
    
    if (response.data.code === 0 || response.data.code === '0') {
      ElMessage.success('批量倒垛成功')
      handleClose()
      emit('success')
    } else {
      ElMessage.error(`批量倒垛失败：${response.data.msg || ''}`)
    }
  } catch (error) {
    ElMessage.error(`批量倒垛失败：${error.message || ''}`)
  }
}

// 取消
const handleCancel = () => {
  handleClose()
}

const emit = defineEmits(['success'])

defineExpose({
  open
})
</script>

<style scoped>
.dialog-footer {
  text-align: right;
}
</style>



