<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-select v-model="queryParams.flowType" placeholder="请选择流水类型">
          <el-option v-for="item in flow_type" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="流水单号" prop="flowCode">
        <el-input
          v-model="queryParams.flowCode"
          placeholder="请输入流水单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </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 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="物资" align="center" width="150" fixed="left">
        <template #default="scope">
          <div>{{ scope.row.material?.materialName }}</div>
          <div class="text-ellipsis text-[12px] text-[#909399]">{{ scope.row.material?.materialCode }} | {{ scope.row.material?.specification }}</div>
        </template>
      </el-table-column>
      <el-table-column label="仓库/库区/货架" align="center">
        <template #default="scope">
          {{ scope.row.warehouse?.warehouseName || '-' }}/{{ scope.row.area?.areaName || '-' }}/{{ scope.row.location?.locationName || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="流水类型" align="center" prop="flowType">
        <template #default="scope">
          <dict-tag :options="flow_type" :value="scope.row.flowType" />
        </template>
      </el-table-column>
      <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="operator.nickName" />
      <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 { proxy } = getCurrentInstance()

const { flow_type} = proxy.useDict('flow_type')

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,
    flowCode: null,
    batchNo: 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.flowCode = null
  queryParams.value.batchNo = null
  queryParams.value.operationTime = null
  queryParams.value.pageNum = 1
  getList()
}

</script>

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