<template>
  <el-dialog
    :title="title"
    v-model="dialogVisible"
    width="1500px"
    append-to-body
  >
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
      <el-form-item label="流水类型" prop="flowType">
        <el-input
          v-model="queryParams.flowType"
          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" height="600px">
      <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="flowType" />
      <el-table-column label="流水单号" align="center" prop="flowCode" width="150" />
      <el-table-column label="批次号" align="center" prop="batchNo" width="150" />
      <el-table-column label="变动前数量" align="center" prop="quantityBefore" />
      <el-table-column label="变动数量" align="center" prop="quantityChange" />
      <el-table-column label="变动后数量" align="center" prop="quantityAfter" />
      <el-table-column label="单价" align="center" prop="unitPrice" />
      <el-table-column label="总价" align="center" prop="totalPrice" />
      <el-table-column label="操作人" align="center" prop="operatorId" />
      <el-table-column label="操作时间" align="center" prop="operationTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.operationTime, '{y}-{m}-{d}') }}</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,
    flowType: 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.flowType = null
  queryParams.value.operationTime = null
  queryParams.value.pageNum = 1
  getList()
}

</script>

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