<template>
  <el-dialog :title="title" v-model="dialogVisible" width="1200px" append-to-body>
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="80px">
      <el-form-item label="物资编码" prop="materialCode">
        <el-input v-model="queryParams.materialCode" placeholder="请输入物资编码" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="物资名称" prop="materialName">
        <el-input v-model="queryParams.materialName" placeholder="请输入物资名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="货架" prop="locationId">
        <el-input v-model="queryParams.locationName" placeholder="请选择货架" readonly clearable>
          <template #append>
            <el-button type="primary" @click="openLocationSelect">选择货架</el-button>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="批次号" prop="batchNo">
        <el-input v-model="queryParams.batchNo" placeholder="请输入批次号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="inventoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="物资编码" align="center" prop="material.materialCode" />
      <el-table-column label="物资名称" align="center" prop="material.materialName" />
      <el-table-column label="规格型号" align="center" prop="material.specification" />
      <el-table-column label="计量单位" align="center" prop="material.unit" />
      <el-table-column label="仓库" align="center" prop="warehouse.warehouseName" />
      <el-table-column label="库区" align="center" prop="area.areaName" />
      <el-table-column label="货架" align="center" prop="location.locationName" /> 
      <el-table-column label="批次号" align="center" prop="batchNo" width="180" />
      <el-table-column label="生产日期" align="center" prop="productionDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.productionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="失效日期" align="center" prop="expirationDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.expirationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="库存数量" align="center" prop="quantity" />
      <el-table-column label="可用数量" align="center" prop="availableQuantity" />
      <el-table-column label="锁定数量" align="center" prop="lockedQuantity" />
      <el-table-column label="入库单价" align="center" prop="unitCost" />
      <el-table-column label="总成本" align="center" prop="totalCost" />
      <el-table-column label="库存状态" align="center" prop="inventoryStatus">
        <template #default="scope">
          <dict-tag :options="inventory_status" :value="scope.row.inventoryStatus" />
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="handleQuery" />

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
        <el-button @click="handleCancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>

  <!-- 货架选择对话框组件 -->
  <LocationSelectDialog ref="locationSelectDialogRef" v-model:visible="locationDialogVisible" title="选择货架"
    @confirm="handleLocationConfirm" />
</template>

<script setup name="InventorySelectDialog">
import { ref, computed, onMounted } from 'vue'
import { listWmsInventory } from '@/api/ware/wmsInventory'
import { parseTime } from '@/utils/ruoyi'
import { ElMessage } from 'element-plus'
import LocationSelectDialog from './LocationSelectDialog.vue'

const { proxy } = getCurrentInstance()

const {sys_yes_no, inventory_status} = proxy.useDict('sys_yes_no', 'inventory_status')


const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  title: {
    type: String,
    default: '选择库存'
  },
  warehouseId: {
    type: String,
    default: null
  }
})

const emit = defineEmits(['update:visible', 'confirm', 'cancel'])

const dialogVisible = computed({
  get() {
    return props.visible
  },
  set(value) {
    emit('update:visible', value)
  }
})

// 查询参数
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  materialCode: null,
  materialName: null,
  warehouseId: props.warehouseId,
  locationId: null,
  batchNo: null,
  specification: null,
  unit: null,
})

// 库存列表
const inventoryList = ref([])
// 加载状态
const loading = ref(false)
// 总条数
const total = ref(0)
// 选中的库存
const selectedInventory = ref([])

// 搜索按钮操作
function handleQuery() {
  loading.value = true
  listWmsInventory(queryParams.value).then(response => {
    inventoryList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 重置按钮操作
function handleReset() {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    materialCode: null,
    materialName: null,
    warehouseId: props.warehouseId,
    locationId: null,
    batchNo: null,
    specification: null,
    unit: null,
  }
  handleQuery()
}

// 重置按钮别名
const resetQuery = handleReset

// 处理选择变更
function handleSelectionChange(selection) {
  selectedInventory.value = selection
}

// 确认按钮操作
function handleConfirm() {
  if (selectedInventory.value.length === 0) {
    ElMessage.warning('请至少选择一个库存')
    return
  }
  emit('confirm', selectedInventory.value)
  handleCancel()
}

// 取消按钮操作
function handleCancel() {
  selectedInventory.value = []
  emit('cancel')
  dialogVisible.value = false
}

// 货架选择对话框状态
const locationDialogVisible = ref(false)
// 货架选择对话框实例引用
const locationSelectDialogRef = ref(null)

// 打开货架选择对话框
function openLocationSelect() {
  locationDialogVisible.value = true
  locationSelectDialogRef.value?.handleQuery()
}

// 处理货架选择确认
function handleLocationConfirm(selectedLocations) {
  if (selectedLocations.length === 0) {
    ElMessage.warning('请至少选择一个货架')
    return
  }
  // 只使用第一个选择的货架
  queryParams.value.locationId = selectedLocations[0].locationId
  queryParams.value.locationName = selectedLocations[0].locationName
  locationDialogVisible.value = false
}

defineExpose({
  handleQuery,
  handleReset,
})
</script>
