<template>
  <el-dialog :title="title" v-model="dialogVisible" width="1000px" append-to-body>
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="80px">
      <el-form-item label="出库单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入出库单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户ID" prop="customerId">
        <el-input
          v-model="queryParams.customerId"
          placeholder="请输入客户ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据状态" prop="orderStatus">
        <el-input
          v-model="queryParams.orderStatus"
          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="outboundOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="出库单号" align="center" prop="orderNo" />
      <el-table-column label="原入库单号" align="center" prop="originalOrderNo" />
      <el-table-column label="仓库ID" align="center" prop="warehouseId" />
      <el-table-column label="客户ID" align="center" prop="customerId" />
      <el-table-column label="出库类型" align="center" prop="outboundType" />
      <el-table-column label="总数量" align="center" prop="totalQuantity" />
      <el-table-column label="总金额" align="center" prop="totalAmount" />
      <el-table-column label="单据状态" align="center" prop="orderStatus" />
    </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>
</template>

<script setup name="OutboundOrderSelectDialog">
import { ref, reactive, watch } from 'vue'
import { listWmsOutboundOrder } from "@/api/ware/wmsOutboundOrder"

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

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

// 本地对话框可见性状态
const dialogVisible = ref(props.visible)

// 监听props变化，同步本地状态
watch(() => props.visible, (newVal) => {
  dialogVisible.value = newVal
})

// 监听本地状态变化，通知父组件
watch(dialogVisible, (newVal) => {
  emit('update:visible', newVal)
})

// 出库单列表
const outboundOrderList = ref([])
// 加载状态
const loading = ref(false)
// 总条数
const total = ref(0)
// 选中的出库单
const selectedOutboundOrders = ref([])

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  orderNo: null,
  originalOrderNo: null,
  warehouseId: null,
  customerId: null,
  outboundType: null,
  orderStatus: null,
})

// 加载出库单列表
const handleQuery = () => {
  loading.value = true
  listWmsOutboundOrder(queryParams).then(response => {
    outboundOrderList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 重置查询参数
const resetQuery = () => {
  queryParams.orderNo = null
  queryParams.customerId = null
  queryParams.orderStatus = null
  queryParams.pageNum = 1
  handleQuery()
}

// 出库单选择变化
const handleSelectionChange = (selection) => {
  selectedOutboundOrders.value = selection
}

// 确认选择
const handleConfirm = () => {
  if (selectedOutboundOrders.value.length === 0) {
    return
  }
  emit('confirm', selectedOutboundOrders.value)
  dialogVisible.value = false
}

// 取消选择
const handleCancel = () => {
  emit('cancel')
  dialogVisible.value = false
}

// 暴露方法
defineExpose({
  handleQuery
})
</script>

<style scoped>
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
</style>