<template>
  <el-dialog title="查看工单详情" v-model="visible" width="1000px" append-to-body @close="handleClose">
    <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="工单编码">{{ formData.workOrderProjectNo }}</el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="工单状态">
                    <dict-tag :options="workOrderStatusOptions" :value="formData.workOrderStatus" />
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="风机编号">{{ formData.pcsDeviceName || '-' }}</el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="维保中心">{{ formData.gxtCenter || '-' }}</el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="场站">{{ formData.pcsStationName || '-' }}</el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="品牌">{{ formData.brand || '-' }}</el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="机型">{{ formData.model || '-' }}</el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="发生时间">{{ parseTime(formData.occurTime, '{y}-{m}-{d} {h}:{i}') || '-' }}</el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="故障代码">{{ formData.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="下发人">{{ formData.assignUserName || '-' }}</el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="下发时间">{{ parseTime(formData.assignTime, '{y}-{m}-{d} {h}:{i}') || '-' }}</el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="接单人">{{ formData.acceptUserName || '-'}}</el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="接单时间">{{ parseTime(formData.acceptTime, '{y}-{m}-{d} {h}:{i}') || '-' }}</el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="工作负责人">{{ formData.teamLeaderName || '-' }}</el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="检修人员">{{ formData.workGroupMemberName || '-' }}</el-form-item>
                </el-col>
                <template v-if="formData.repairMethod !== '2'">
                  <el-col :span="24">
                    <el-form-item label="工作部位">{{ formattedWorkArea }}</el-form-item>
                  </el-col>
                  <el-col :span="24" v-if="formData.infoEntry == 1">
                    <el-form-item label="MIS工单编码">{{ formData.misOrderNo || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24" v-if="formData.infoEntry == 2">
                    <el-form-item label="工票编号">{{ formData.workPermitNum || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="开始时间">{{ parseTime(formData.realStartTime, '{y}-{m}-{d} {h}:{i}') || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="结束时间">{{ parseTime(formData.realEndTime, '{y}-{m}-{d} {h}:{i}') || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="外委人员数（人）">{{ formData.wwryNum || '-' }}</el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="外来人员数（人）">{{ formData.wlryNum || '-' }}</el-form-item>
                  </el-col>
                </template>
                <template v-if="formData.repairMethod === '2'">
                  <el-col :span="24">
                    <el-form-item label="复位方式">
                      <dict-tag :options="resetMethodOptions" :value="formData.resetMethod" />
                    </el-form-item>
                  </el-col>
                </template>
                <el-col :span="24">
                  <el-form-item label="恢复运行时间">{{ parseTime(formData.restartTime, '{y}-{m}-{d} {h}:{i}') || '-' }}</el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="损失电量（kWh）">{{ formData.lostPower || '-' }}</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="repairOrderFlowActionTypeOptions" :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">{{ formData.faultBarcode || '-' }}</div>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="真实故障原因">
            <div class="content-text">{{ formData.realFailureReason || '-' }}</div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- 附件信息 -->
    <div class="info-section" v-if="formData.attachmentUrls">
      <h3 class="section-title">结单附件</h3>
      <el-row>
        <el-col :span="24">
          <el-form-item label="附件">
            <preview :limit="8" v-model="formData.attachmentUrls" :filesize="5" disabled></preview>
          </el-form-item>
        </el-col>
      </el-row>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCancel">关 闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, defineProps, defineEmits, getCurrentInstance, watch, computed } from 'vue'
import preview from '@/components/FileUpload/preview.vue'
import DictTag from '@/components/DictTag'

// 获取当前实例
const { proxy } = getCurrentInstance()

// 定义属性
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  data: {
    type: Object,
    default: () => ({})
  },
  workOrderStatusOptions: {
    type: Array,
    default: () => ([])
  },
  resetMethodOptions: {
    type: Array,
    default: () => ([])
  },
  repairOrderFlowActionTypeOptions: {
    type: Array,
    default: () => ([])
  }
})

// 定义事件
const emit = defineEmits(['update:modelValue'])

// 响应式数据
const visible = ref(false)
const formData = ref({})
const flowList = ref([])

// 计算属性
const formattedWorkArea = computed(() => {
  if (!formData.value.workArea) return '-'
  // 如果是数组，转换为字符串
  if (Array.isArray(formData.value.workArea)) {
    return formData.value.workArea.join(',')
  }
  return formData.value.workArea
})

// 监听modelValue变化
watch(() => props.modelValue, (val) => {
  visible.value = val
  if (val) {
    // 初始化表单数据
    formData.value = { ...props.data }
    // 从repairOrderFlowList属性获取流转记录
    flowList.value = props.data.repairOrderFlowList || []
  }
})

// 监听props.data变化
watch(() => props.data, (newData) => {
  if (visible.value) {
    // 只有在对话框打开时才更新数据
    formData.value = { ...newData }
    // 从repairOrderFlowList属性获取流转记录
    flowList.value = newData.repairOrderFlowList || []
  }
}, { deep: true })

// 监听visible变化
watch(visible, (val) => {
  emit('update:modelValue', val)
})
// 关闭对话框
const handleClose = () => {
  visible.value = false
}

// 取消操作
const handleCancel = () => {
  visible.value = false
}

// 解析时间函数
const parseTime = (time, pattern) => {
  if (!time) return ''
  return proxy.parseTime(time, pattern)
}
</script>

<style scoped>
.info-section {
  margin-bottom: 20px;
}

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

.info-content {
  padding: 15px;
  //background-color: #f9f9f9;
  border-radius: 4px;
}

.flow-history {
  padding: 15px;
  //background-color: #f9f9f9;
  border-radius: 4px;
  max-height: 400px;
  overflow-y: auto;
}

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

.flow-item p {
  margin: 0;
  font-size: 12px;
  color: #666;
}

.content-text {
  min-height: 60px;
  padding: 10px;
  //background-color: #f5f5f5;
  border-radius: 4px;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.5;
}
</style>
