<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="维保类型">{{ formattedInspectionType }}</el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="停机时间">{{ formData.pauseTime || '-' }}</el-form-item>
                </el-col>

                <!--                  <el-col :span="24">-->
                <!--                    <el-form-item label="维保内容">{{ formData.content || '-' }}</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.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>
                <el-col :span="24" v-if="formData.infoEntry == '1'">
                  <el-form-item label="MIS工单编码">{{ formData.misNo || '-' }}</el-form-item>
                </el-col>
                <el-col :span="24">
                  <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>
                <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-col :span="24">
                  <el-form-item label="结单方式">
                    <dict-tag :options="finalizeMethodOptions" :value="formData.finalizeMethod" />
                  </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.actionRemark }}<br>-->
                    {{ 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.content || '-' }}</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: () => ([])
  },
  repairOrderFlowActionTypeOptions: {
    type: Array,
    default: () => ([])
  },
  inspectionTypeOptions: {
    type: Array,
    default: () => ([])
  },
  finalizeMethodOptions: {
    type: Array,
    default: () => ([])
  }
})

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

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

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

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

// 维保类型
const formattedInspectionType = computed(() => {
  debugger
  const { inspectionType } = formData.value;
  if (!inspectionType) return '-';

  let values = [];
  if (typeof inspectionType === 'string') {
    values = inspectionType.split(',').filter(v => v.trim()); // 去空
  } else if (Array.isArray(inspectionType)) {
    values = inspectionType;
  } else {
    values = [inspectionType]; // 单个值
  }
  const options = props.inspectionTypeOptions || [];
  return values
      .map(v => {
        // 注意：v 可能是字符串，确保类型一致（建议都转字符串比较）
        const opt = options.find(o => String(o.value) === String(v));
        return opt ? opt.label : v;
      })
      .join(',');
})
</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: 800px;
  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>
