<template>
  <el-dialog
    :title="title"
    v-model="dialogVisible"
    width="1200px"
    append-to-body
  >
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
      <el-form-item label="操作类型" prop="operationType">
        <el-input
          v-model="queryParams.operationType"
          placeholder="请输入操作类型"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作时间" prop="operationTime">
        <el-date-picker clearable
          v-model="queryParams.operationTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择操作时间">
        </el-date-picker>
      </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="wmsInventoryFlowList">
      <el-table-column label="流水ID" align="center" prop="flowId" />
      <el-table-column label="库存ID" align="center" prop="inventoryId" />
      <el-table-column label="物资ID" align="center" prop="materialId" />
      <el-table-column label="仓库ID" align="center" prop="warehouseId" />
      <el-table-column label="货架ID" align="center" prop="locationId" />
      <el-table-column label="操作类型" align="center" prop="operationType" />
      <el-table-column label="变动数量" align="center" prop="changeQuantity" />
      <el-table-column label="变动前数量" align="center" prop="beforeQuantity" />
      <el-table-column label="变动后数量" align="center" prop="afterQuantity" />
      <el-table-column label="操作人" align="center" prop="operator" />
      <el-table-column label="操作时间" align="center" prop="operationTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.operationTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
    </el-table>

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

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="InventoryFlowDialog">
import { ref, reactive, watch, toRefs } from 'vue'
import { listWmsInventoryFlow } from '@/api/ware/wmsInventoryFlow'
import { parseTime } from '@/utils/ruoyi'
import Pagination from '@/components/Pagination'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  inventoryId: {
    type: String,
    default: ''
  },
  title: {
    type: String,
    default: '库存履历'
  }
})

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

const dialogVisible = ref(false)
const loading = ref(true)
const total = ref(0)
const wmsInventoryFlowList = ref([])

// 监听可见性变化
watch(() => props.visible, (newVal) => {
  dialogVisible.value = newVal
})

watch(dialogVisible, (newVal) => {
  emit('update:visible', newVal)
})

// 查询参数
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    inventoryId: props.inventoryId,
    operationType: null,
    operationTime: null
  }
})

const { queryParams } = toRefs(data)

// 监听inventoryId变化，更新查询参数
watch(() => props.inventoryId, (newVal) => {
  queryParams.value.inventoryId = newVal
  getList()
})

// 监听查询参数中的inventoryId变化，通知父组件
watch(() => queryParams.value.inventoryId, (newVal) => {
  if (newVal !== props.inventoryId) {
    emit('update:inventoryId', newVal)
  }
})

/** 查询库存流水列表 */
function getList() {
  loading.value = true
  listWmsInventoryFlow(queryParams.value).then(response => {
    wmsInventoryFlowList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.value.operationType = null
  queryParams.value.operationTime = null
  queryParams.value.pageNum = 1
  getList()
}

// 暴露方法供外部调用
defineExpose({
  handleQuery
})
</script>

<style scoped>
.mb8 {
  margin-bottom: 8px;
}
</style>