<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="areaCode">
        <el-input
          v-model="queryParams.areaCode"
          placeholder="请输入库区编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库区名称" prop="areaName">
        <el-input
          v-model="queryParams.areaName"
          placeholder="请输入库区名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属仓库" prop="warehouseId">
        <el-input
          v-model="queryParams.warehouseId"
          placeholder="请输入所属仓库ID"
          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="areaList"
      default-expand-all
      @selection-change="handleSelectionChange"
      row-key="areaId"
      :tree-props="{ children: 'children' }"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="库区编码" align="center" prop="areaCode" />
      <el-table-column label="库区名称" align="center" prop="areaName" />
      <el-table-column label="所属仓库ID" align="center" prop="warehouseId" />
      <el-table-column label="库区类型" align="center" prop="areaType" />
      <el-table-column label="温度范围" align="center" prop="temperatureRange" />
      <el-table-column label="湿度范围" align="center" prop="humidityRange" />
      <el-table-column label="库区容量" align="center" prop="capacity" />
      <el-table-column label="状态" align="center" prop="status" />
    </el-table>

    <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="AreaSelectTreeDialog">
import { ref, reactive, watch } from 'vue'
import { listWmsArea } from "@/api/ware/wmsArea"
import { handleTree } from "@/utils/ruoyi"

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 areaList = ref([])
// 加载状态
const loading = ref(false)
// 选中的库区
const selectedAreas = ref([])

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 100,
  areaCode: null,
  areaName: null,
  warehouseId: null
})

// 加载库区列表
const handleQuery = () => {
  loading.value = true
  listWmsArea(queryParams).then(response => {
    // 处理树形结构
    areaList.value = handleTree(response.rows, 'areaId', 'parentId')
    loading.value = false
  })
}

// 重置查询参数
const resetQuery = () => {
  queryParams.areaCode = null
  queryParams.areaName = null
  queryParams.warehouseId = null
  queryParams.pageNum = 1
  handleQuery()
}

// 库区选择变化
const handleSelectionChange = (selection) => {
  selectedAreas.value = selection
}

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

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

// 暴露方法
const exposeMethods = {
  handleQuery
}

defineExpose(exposeMethods)
</script>

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