<template>
  <div class="work-order-management">
    <el-card class="work-order-list">
      <h3>待审批工单列表</h3>
      <el-table :data="workOrders" style="width: 100%" @row-click="handleRowClick">
        <el-table-column prop="orderNumber" label="工单编号"></el-table-column>
        <el-table-column prop="caseNumber" label="案件编号"></el-table-column>
        <el-table-column prop="alarmLevel" label="报警级别"></el-table-column>
        <el-table-column prop="alarmType" label="报警类型"></el-table-column>
        <el-table-column prop="alarmDescription" label="报警描述"></el-table-column>
        <el-table-column prop="purpose" label="用途"></el-table-column>
        <el-table-column prop="assignedUnit" label="权属单位"></el-table-column>
        <el-table-column prop="address" label="详细地址"></el-table-column>
        <el-table-column fixed="right" label="操作">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleApprove(row)">审批</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-card v-if="selectedWorkOrder" class="work-order-details">
      <h3>工单详情</h3>
      <el-descriptions :column="1" border>
        <el-descriptions-item label="案件编号">{{ selectedWorkOrder.caseNumber }}</el-descriptions-item>
        <el-descriptions-item label="报警名称">{{ selectedWorkOrder.alarmName }}</el-descriptions-item>
        <el-descriptions-item label="报警时间">{{ selectedWorkOrder.alarmTime }}</el-descriptions-item>
        <el-descriptions-item label="报警描述">{{ selectedWorkOrder.alarmDescription }}</el-descriptions-item>
        <el-descriptions-item label="井盖设备编号">{{ selectedWorkOrder.deviceSerial }}</el-descriptions-item>
        <el-descriptions-item label="井盖设备名称">{{ selectedWorkOrder.deviceName }}</el-descriptions-item>
        <el-descriptions-item label="井盖设备用途">{{ selectedWorkOrder.purpose }}</el-descriptions-item>
        <el-descriptions-item label="权属单位">{{ selectedWorkOrder.assignedUnit }}</el-descriptions-item>
        <el-descriptions-item label="权属单位电话">{{ selectedWorkOrder.unitPhone }}</el-descriptions-item>
        <el-descriptions-item label="安装时间">{{ selectedWorkOrder.installationTime }}</el-descriptions-item>
        <el-descriptions-item label="区">{{ selectedWorkOrder.district }}</el-descriptions-item>
        <el-descriptions-item label="街道">{{ selectedWorkOrder.street }}</el-descriptions-item>
        <el-descriptions-item label="道路">{{ selectedWorkOrder.road }}</el-descriptions-item>
        <el-descriptions-item label="辅助定位地址">{{ selectedWorkOrder.auxiliaryAddress }}</el-descriptions-item>
        <el-descriptions-item label="井盖设备材质">{{ selectedWorkOrder.material }}</el-descriptions-item>
        <el-descriptions-item label="工单办理经过">
          <p>{{ selectedWorkOrder.process }}</p>
        </el-descriptions-item>
        <el-descriptions-item label="附件多媒体信息">
          <el-image v-for="(media, index) in selectedWorkOrder.media" :key="index" :src="media.url" fit="cover" />
        </el-descriptions-item>
      </el-descriptions>
    </el-card>

    <!-- 审批弹窗 -->
    <el-dialog title="审批工单" v-model="dialogApproveVisible" width="30%">
      <el-form :model="approveForm">
        <el-form-item label="审批结果" :label-width="formLabelWidth">
          <el-select v-model="approveForm.result" placeholder="请选择审批结果">
            <el-option label="通过" value="approved" />
            <el-option label="退回" value="rejected" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="approveForm.result === 'rejected'" label="退回理由" :label-width="formLabelWidth">
          <el-input v-model="approveForm.rejectionReason" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogApproveVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleConfirmApprove">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus';

// 模拟数据
const workOrders = ref([
  {
    orderNumber: 'W20230915001',
    caseNumber: 'C20230915001',
    alarmLevel: '紧急',
    alarmType: '损坏',
    alarmDescription: '井盖破损严重，存在安全隐患。',
    purpose: '排水',
    assignedUnit: '水务局',
    address: '上海市浦东新区陆家嘴环路1000号',
    location: [31.2304, 121.4737],
    media: [
      { url: 'https://example.com/image1.jpg' },
      { url: 'https://example.com/image2.jpg' }
    ]
  },
  // 更多模拟数据...
]);

const selectedWorkOrder = ref(null);
const dialogApproveVisible = ref(false);
const approveForm = ref({ result: '', rejectionReason: '' });
const formLabelWidth = ref('120px');

// 处理表格点击事件
function handleRowClick(row) {
  selectedWorkOrder.value = row;
}

// 审批处理
function handleApprove(row) {
  selectedWorkOrder.value = row;
  dialogApproveVisible.value = true;
}

// 确认审批
function handleConfirmApprove() {
  if (approveForm.result === 'approved') {
    // 审批通过的逻辑
    ElMessage.success('工单已审批通过');
  } else if (approveForm.result === 'rejected') {
    // 审批退回的逻辑
    ElMessage.warning('工单已退回');
  }
  dialogApproveVisible.value = false;
}
</script>

<style scoped>
.work-order-management {
  display: flex;
  flex-direction: column;
  gap: 20px;
}
</style>
