<template>
  <div class="work-order-platform">
    <!-- 搜索栏 -->
    <el-card class="filter-card">
      <el-form :model="queryParams" ref="queryRef" :inline="true" class="filter-form">
        <el-form-item label="工单号" prop="orderId">
          <el-input
              v-model="queryParams.orderId"
              placeholder="请输入工单号（如：WO2024001）"
              clearable
              @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="工单类型" prop="type">
          <el-select
              v-model="queryParams.type"
              placeholder="请选择工单类型"
              style="width: 180px;"
          >
            <el-option label="全部" value="" />
            <el-option label="维修" value="维修" />
            <el-option label="清洁" value="清洁" />
            <el-option label="投诉" value="投诉" />
          </el-select>
        </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-card>

    <!-- 数据表格 -->
    <el-card class="device-table">
      <el-table v-loading="loading" :data="workOrders" border>
        <el-table-column label="序号" type="index" align="center" />
        <el-table-column label="工单号" align="center" prop="orderId" />
        <el-table-column label="提交人" align="center" prop="submitter" />
        <el-table-column label="工单类型" align="center" prop="type" />
        <el-table-column label="紧急程度" align="center" prop="urgency" />
        <el-table-column label="处理人" align="center" prop="handler" />
        <el-table-column label="进度" align="center" prop="status" >
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="耗时" align="center" prop="duration" />
      </el-table>

      <!-- 分页器 -->
      <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { getEstateManagementWorkOrderDataList } from "@/api/estateManagement/estateManagement"; // 假设API路径

// 查询参数（两个条件：orderId、type）
const queryParams = reactive({
  orderId: "", // 工单号（精确查询）
  type: "", // 工单类型（维修/清洁/投诉）
  pageNum: 1,
  pageSize: 10,
  interfaceName: "工单调度平台"
});

// 表格数据
const workOrders = ref([]);
const total = ref(0);
const loading = ref(false);

// 获取工单列表
const getList = async () => {
  loading.value = true;
  try {
    const response = await getEstateManagementWorkOrderDataList(queryParams);
    workOrders.value =  [];
    total.value =  0;
  } catch (error) {
    console.error("获取工单数据失败", error);
  } finally {
    loading.value = false;
  }
};

// 搜索方法
const handleQuery = () => {
  queryParams.pageNum = 1; // 重置页码
  getList();
};

// 重置方法
const resetQuery = () => {
  queryParams.orderId = "";
  queryParams.type = "";
  handleQuery();
};

// 分页事件
const handleSizeChange = (newSize) => {
  queryParams.pageSize = newSize;
  getList();
};

const handleCurrentChange = (newPage) => {
  queryParams.pageNum = newPage;
  getList();
};

// 状态标签类型映射
const getStatusType = (status) => {
  if (status === "待接单") return "warning";
  if (status === "处理中") return "info";
  if (status === "完成") return "success";
  return "info";
};

// 初始化加载
getList();
</script>

<style scoped>
.filter-card {
  margin-bottom: 20px;
}

.el-table .cell {
  white-space: nowrap;
}

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