<template>
  <div class="app-container">
    <!-- 查询条件区域 -->
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px" label-position="top">
      <el-form-item label="工单编码" prop="workOrderProjectNo" label-position="top">
        <el-input
            v-model="queryParams.workOrderProjectNo"
            placeholder="请输入工单编码"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="风机编号" prop="pcsDeviceName" label-position="top">
        <el-input
            v-model="queryParams.pcsDeviceName"
            placeholder="请输入风机编号"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维保中心" prop="gxtCenter" label-position="top">
        <el-select
            v-model="queryParams.gxtCenter"
            placeholder="请选择维保中心"
            clearable
            @change="handleMaintenanceCenterChange"
        >
          <el-option
              v-for="item in maintenanceCenterOptions"
              :key="item.deptId"
              :label="item.deptName"
              :value="item.deptName">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="场站" prop="pcsStationName" label-position="top">
        <el-select
            v-model="queryParams.pcsStationName"
            placeholder="请选择场站"
            clearable
            :disabled="!queryParams.gxtCenter"
        >
          <el-option
              v-for="item in stationOptions"
              :key="item.deptId"
              :label="item.deptName"
              :value="item.deptName">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工单状态" prop="workOrderStatus" label-position="top">
        <el-select v-model="queryParams.workOrderStatus" placeholder="请选择工单状态" clearable>
          <el-option
              v-for="dict in gxt_work_order_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
            <el-form-item label-position="top">
          <div class="item-search">&nbsp;</div>
          <div class="item-search">
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          </div>
      </el-form-item>
    </el-form>

    <!-- 操作按钮区域 -->
    <el-row :gutter="10" class="mb8">
      <!-- 空的按钮区域，保持与其他页面一致的间距 -->
    </el-row>

    <!-- 工单列表 -->
    <el-table
        v-loading="loading"
        :data="orderList"
        style="width: 100%"
        :max-height="tableHeight"
    >
      <el-table-column label="工单类型" align="center" prop="orderType" width="100">
        <template #default="scope">
          <el-tag v-if="scope.row.orderType === 1" type="danger">维修工单</el-tag>
          <el-tag v-else type="success">维保工单</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="工单编码" align="center" prop="workOrderProjectNo" width="160" fixed>
        <template #default="scope">
          <el-button link type="primary" @click="handleView(scope.row)">
            {{ scope.row.workOrderProjectNo }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="风机编号" align="center" prop="pcsDeviceName" width="120" :show-overflow-tooltip="true">
        <template #default="scope">
          {{ scope.row.pcsDeviceName || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="工单状态" align="center" prop="workOrderStatus" min-width="100">
        <template #default="scope">
          <dict-tag :options="gxt_work_order_status" :value="scope.row.workOrderStatus" />
        </template>
      </el-table-column>
      <el-table-column label="维保中心" align="center" prop="gxtCenter"  width="150" :show-overflow-tooltip="true">
        <template #default="scope">
          {{ scope.row.gxtCenter || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="场站" align="center" prop="pcsStationName" width="150" :show-overflow-tooltip="true">
        <template #default="scope">
          {{ scope.row.pcsStationName || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="品牌" align="center" prop="brand" width="120" :show-overflow-tooltip="true">
        <template #default="scope">
          {{ scope.row.brand || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="机型" align="center" prop="model" width="120" :show-overflow-tooltip="true">
        <template #default="scope">
          {{ scope.row.model || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="接单人" align="center" prop="acceptUserName" width="100" >
        <template #default="scope">
          {{ scope.row.acceptUserName || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="接单时间" align="center" prop="acceptTime" width="150">
        <template #default="scope">
          <span>{{ scope.row.acceptTime ? parseTime(scope.row.acceptTime, '{y}-{m}-{d} {h}:{i}') : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工作负责人" align="center" prop="teamLeaderName" width="100" >
        <template #default="scope">
          {{ scope.row.teamLeaderName || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="realStartTime" width="150">
        <template #default="scope">
          <span>{{ scope.row.realStartTime ? parseTime(scope.row.realStartTime, '{y}-{m}-{d} {h}:{i}') : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="realEndTime" width="150">
        <template #default="scope">
          <span>{{ scope.row.realEndTime ? parseTime(scope.row.realEndTime, '{y}-{m}-{d} {h}:{i}') : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="150" fixed="right">
        <template #default="scope">
          <el-button
              type="info"
              link
              @click="handleView(scope.row)"
          ><i class="fa fa-eye"></i>查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 维修工单详情对话框 -->
    <el-dialog title="维修工单详情" v-model="repairDetailDialogVisible" width="1000px" append-to-body>
      <el-row :gutter="20">
        <!-- 工单信息 -->
        <el-col :span="8">
          <div class="info-section">
            <h3 class="section-title">工单信息</h3>
            <div class="info-content">
              <el-form label-width="100px" label-position="top">
                <el-row :gutter="20">
                  <el-col :span="24">
                    <el-form-item label="工单编码">{{ detailData.workOrderProjectNo }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="工单状态">
                      <dict-tag :options="gxt_work_order_status" :value="detailData.workOrderStatus" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="风机编号">{{ detailData.pcsDeviceName || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="维保中心">{{ detailData.gxtCenter || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="场站">{{ detailData.pcsStationName || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="品牌">{{ detailData.brand || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="机型">{{ detailData.model || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="发生时间">{{ parseTime(detailData.occurTime, '{y}-{m}-{d} {h}:{i}') || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="故障代码">{{ detailData.faultCode || '-' }}</el-form-item>
                  </el-col>
                </el-row>
              </el-form>
            </div>
          </div>
        </el-col>

        <!-- 处理信息 -->
        <el-col :span="8">
          <div class="info-section">
            <h3 class="section-title">处理信息</h3>
            <div class="info-content">
              <el-form label-width="100px" label-position="top">
                <el-row :gutter="20">
                  <el-col :span="24">
                    <el-form-item label="下发人">{{ detailData.assignUserName || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="下发时间">{{ parseTime(detailData.assignTime, '{y}-{m}-{d} {h}:{i}') || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="接单人">{{ detailData.acceptUserName || '-'}}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="接单时间">{{ parseTime(detailData.acceptTime, '{y}-{m}-{d} {h}:{i}') || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="工作负责人">{{ detailData.teamLeaderName || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="工作班成员">{{ detailData.workGroupMemberName || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="MIS工单编码">{{ detailData.misOrderNo || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="开始时间">{{ parseTime(detailData.realStartTime, '{y}-{m}-{d} {h}:{i}') || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="结束时间">{{ parseTime(detailData.realEndTime, '{y}-{m}-{d} {h}:{i}') || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="恢复运行时间">{{ parseTime(detailData.restartTime, '{y}-{m}-{d} {h}:{i}') || '-' }}</el-form-item>
                  </el-col>
                </el-row>
              </el-form>
            </div>
          </div>
        </el-col>

        <!-- 工单流转记录 -->
        <el-col :span="8">
          <div class="info-section">
            <h3 class="section-title">工单流转</h3>
            <div class="flow-history">
              <el-timeline>
                <el-timeline-item type="primary"
                                  v-for="(flow, index) in flowList"
                                  :key="index"
                                  :timestamp="parseTime(flow.actionTime, '{y}-{m}-{d} {h}:{i}')"
                >
                  <div class="flow-item">
                    <h4><dict-tag :options="gxt_repair_order_flow_action_type" :value="flow.actionType" /></h4>
                    <p>
                      {{ flow.operatorName }}
                    </p>
                  </div>
                </el-timeline-item>
              </el-timeline>
            </div>
          </div>
        </el-col>
      </el-row>
      <el-form label-position="top">
        <el-row>
          <el-col :span="24">
            <el-form-item label="故障信息">
              <div class="content-text">{{ detailData.faultBarcode || '-' }}</div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- 附件信息 -->
      <div class="info-section" v-if="detailData.attachmentUrls">
        <h3 class="section-title">结单附件</h3>
        <el-row>
          <el-col :span="24">
            <el-form-item label="附件">
              <preview :limit="8" v-model="detailData.attachmentUrls" :filesize="5" disabled></preview>
            </el-form-item>
          </el-col>
        </el-row>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="repairDetailDialogVisible = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 维保工单详情对话框 -->
    <el-dialog title="维保工单详情" v-model="workDetailDialogVisible" width="1000px" append-to-body>
      <el-row :gutter="20">
        <!-- 工单信息 -->
        <el-col :span="8">
          <div class="info-section">
            <h3 class="section-title">工单信息</h3>
            <div class="info-content">
              <el-form label-width="100px" label-position="top">
                <el-row :gutter="20">
                  <el-col :span="24">
                    <el-form-item label="工单编码">{{ detailData.workOrderProjectNo }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="工单状态">
                      <dict-tag :options="gxt_work_order_status" :value="detailData.workOrderStatus" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="风机编号">{{ detailData.pcsDeviceName || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="维保中心">{{ detailData.gxtCenter || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="场站">{{ detailData.pcsStationName || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="品牌">{{ detailData.brand || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="机型">{{ detailData.model || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="维保类型">
                      <dict-tag :options="gxt_inspection_type" :value="detailData.inspectionType" />
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
            </div>
          </div>
        </el-col>

        <!-- 处理信息 -->
        <el-col :span="8">
          <div class="info-section">
            <h3 class="section-title">处理信息</h3>
            <div class="info-content">
              <el-form label-width="100px" label-position="top">
                <el-row :gutter="20">
                  <el-col :span="24">
                    <el-form-item label="下发人">{{ detailData.assignUserName || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="下发时间">{{ parseTime(detailData.assignTime, '{y}-{m}-{d} {h}:{i}') || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="接单人">{{ detailData.acceptUserName || '-'}}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="接单时间">{{ parseTime(detailData.acceptTime, '{y}-{m}-{d} {h}:{i}') || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="工作负责人">{{ detailData.teamLeaderName || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="工作班成员">{{ detailData.workGroupMemberName || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="MIS工单编码">{{ detailData.misNo || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="开始时间">{{ parseTime(detailData.realStartTime, '{y}-{m}-{d} {h}:{i}') || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="结束时间">{{ parseTime(detailData.realEndTime, '{y}-{m}-{d} {h}:{i}') || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="恢复运行时间">{{ parseTime(detailData.restartTime, '{y}-{m}-{d} {h}:{i}') || '-' }}</el-form-item>
                  </el-col>
                </el-row>
              </el-form>
            </div>
          </div>
        </el-col>

        <!-- 工单流转记录 -->
        <el-col :span="8">
          <div class="info-section">
            <h3 class="section-title">工单流转</h3>
            <div class="flow-history">
              <el-timeline>
                <el-timeline-item type="primary"
                                  v-for="(flow, index) in flowList"
                                  :key="index"
                                  :timestamp="parseTime(flow.actionTime, '{y}-{m}-{d} {h}:{i}')"
                >
                  <div class="flow-item">
                    <h4><dict-tag :options="gxt_repair_order_flow_action_type" :value="flow.actionType" /></h4>
                    <p>
                      {{ flow.operatorName }}
                    </p>
                  </div>
                </el-timeline-item>
              </el-timeline>
            </div>
          </div>
        </el-col>
      </el-row>
      <el-form label-position="top">
        <el-row>
          <el-col :span="24">
            <el-form-item label="维保内容">
              <div class="content-text">{{ detailData.content || '-' }}</div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- 附件信息 -->
      <div class="info-section" v-if="detailData.attachmentUrls">
        <h3 class="section-title">结单附件</h3>
        <el-row>
          <el-col :span="24">
            <el-form-item label="附件">
              <preview :limit="8" v-model="detailData.attachmentUrls" :filesize="5" disabled></preview>
            </el-form-item>
          </el-col>
        </el-row>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="workDetailDialogVisible = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { getCurrentInstance, reactive, ref, toRefs, onMounted } from "vue";
import { listMyDone, getOrderInfo } from "@/api/gxt/orderMine.js";
import { listMaintenanceCenters, listStationsByMaintenanceCenter } from "@/api/gxt/equipment.js";
import preview from '@/components/FileUpload/preview.vue'

const { proxy } = getCurrentInstance();

// 显示搜索栏
const showSearch = ref(true);

// 加载状态
const loading = ref(false);

// 表格高度
const tableHeight = ref(window.innerHeight - 300);

// 数据总数
const total = ref(0);

// 维修工单详情对话框可见性
const repairDetailDialogVisible = ref(false);

// 维保工单详情对话框可见性
const workDetailDialogVisible = ref(false);

// 维保中心选项
const maintenanceCenterOptions = ref([]);

// 场站选项
const stationOptions = ref([]);

// 工单列表
const orderList = ref([]);

// 详情数据
const detailData = ref({});

// 工单流转记录
const flowList = ref([]);

// 查询参数
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    workOrderProjectNo: undefined,
    workOrderStatus: undefined,
    gxtCenter: undefined,
    pcsStationName: undefined,
    pcsDeviceName: undefined
  }
});

const { queryParams } = toRefs(data);

// 获取字典数据
const { gxt_maintenance_type, gxt_work_order_status, gxt_order_priority_type,gxt_repair_order_flow_action_type,gxt_order_suspend_reason,gxt_inspection_type,gxt_pause_reasons } = proxy.useDict("gxt_maintenance_type", "gxt_work_order_status", "gxt_order_priority_type","gxt_repair_order_flow_action_type","gxt_order_suspend_reason","gxt_inspection_type","gxt_pause_reasons")

// 页面加载时执行
onMounted(() => {
  getList();
  getMaintenanceCenterAndStationList();
  window.addEventListener('resize', handleResize);
});

// 获取列表数据
function getList() {
  loading.value = true;
  listMyDone(queryParams.value).then(response => {
    orderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 获取维保中心和场站列表
function getMaintenanceCenterAndStationList() {
  listMaintenanceCenters().then(response => {
    maintenanceCenterOptions.value = response.data.map(item => {
      return { deptName: item, deptId: item };
    });

    // 如果已选择维保中心，则筛选出该维保中心下的场站
    if (queryParams.value.gxtCenter) {
      listStationsByMaintenanceCenter(queryParams.value.gxtCenter).then(res => {
        stationOptions.value = res.data.map(item => {
          return { deptName: item, deptId: item };
        });
      });
    } else {
      // 如果未选择维保中心，则清空场站选项
      stationOptions.value = [];
    }
  });
}

// 处理维保中心变更
function handleMaintenanceCenterChange(selectedCenter) {
  // 清空场站选择
  queryParams.value.pcsStationName = null;

  // 如果选择了维保中心，则加载对应的场站
  if (selectedCenter) {
    listStationsByMaintenanceCenter(selectedCenter).then(response => {
      stationOptions.value = response.data.map(item => {
        return { deptName: item, deptId: item };
      });
    });
  } else {
    // 如果未选择维保中心，则清空场站选项
    stationOptions.value = [];
  }
}

// 搜索按钮操作
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

// 重置按钮操作
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 查看详情
function handleView(row) {
  // 调用后端接口获取详细信息
  getOrderInfo(row.orderType, row.id).then(response => {
    detailData.value = response.data;
    // 从工单详情中获取流转记录
    flowList.value = response.data.workOrderFlowList || response.data.repairOrderFlowList || [];

    if (row.orderType === 1) {
      // 维修工单
      repairDetailDialogVisible.value = true;
    } else {
      // 维保工单
      workDetailDialogVisible.value = true;
    }
  }).catch(error => {
    proxy.$modal.msgError("获取工单详情失败：" + error.message);
  });
}

// 处理窗口大小变化
function handleResize() {
  tableHeight.value = window.innerHeight - 300;
}
</script>

<style scoped>
/* 表单字段间距调整 */
.el-form-item {
  margin-bottom: 18px;
}

.text-gray-500 {
  color: #6b7280;
}

.card-header {
  font-weight: bold;
  font-size: 16px;
}

.info-section {
  padding: 16px 0;
  height: 100%;
  box-sizing: border-box;
}

.section-title {
  font-weight: bold;
  font-size: 16px;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid #ebeef5;
}

.info-content {
  height: calc(100% - 30px);
  overflow-y: auto;
  overflow-x: hidden; /* 防止横向滚动 */
  padding: 0 10px; /* 添加内边距防止内容贴边 */
}

.info-item {
  margin-bottom: 12px;
}

.info-item label {
  display: block;
  font-size: 12px;
  color: #999;
  margin-bottom: 4px;
}

.info-item p {
  font-size: 14px;
  color: #333;
  margin: 0;
  line-height: 1.4;
}

.flow-history {
  height: 100%;
  padding: 0 10px; /* 添加内边距防止内容贴边 */
}

.flow-history .el-timeline {
  margin-left: 0;
  padding-left: 0;
}

.flow-item {
  /*padding: 10px;
  border-radius: 4px;*/
}

.flow-item h4 {
  margin: 0 0 5px 0;
  font-size: 14px;
  font-weight: bold;
}

.flow-item p {
  margin: 0;
  font-size: 12px;
  line-height: 1.4;
  color: #606266;
}

/* 修复对话框底部按钮点击区域问题 */
:deep(.el-dialog__footer) {
  padding: 20px !important;
  position: relative;
  z-index: 10;
}

/* 确保关闭按钮有完整的点击区域 */
.dialog-footer :deep(.el-button) {
  padding: 12px 20px;
  min-width: 80px;
  z-index: 20;
  position: relative;
}

/* 表单字段间距调整 */
:deep(.el-form-item) {
  margin-bottom: 18px;
}

.content-text {
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.5;
}
</style>
