<template>
  <el-dialog title="结单" v-model="visible" width="800px" append-to-body @close="handleClose">
    <el-alert type="info" :closable="false" style="border-color: #14b8a6; background-color: #f0fdfa; color: #0d9488; height: 35px;">
      <template #default>
        <i class="fa fa-file-text-o mr-2" style="color: #0d9488;"> 请选择信息录入方式，并上传相关附件完成结单。</i>
      </template>
    </el-alert>
    <h4 class="text-sm font-medium text-gray-800 mb-3"></h4>
    <el-form ref="finalizeFormRef" :model="formData" :rules="finalizeRules" label-width="120px" label-position="top">
      <el-row>
        <el-col :span="12">
          <el-form-item label="工单编码"><el-input v-model="formData.workOrderProjectNo" disabled /></el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="风机编号"><el-input v-model="formData.pcsDeviceName" disabled /></el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工单状态">
            <el-select v-model="formData.workOrderStatus" style="width: 100%" disabled>
              <el-option
                  v-for="dict in workOrderStatusOptions"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维保中心"><el-input v-model="formData.gxtCenter" disabled /> </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="场站"><el-input v-model="formData.pcsStationName" disabled /> </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="品牌"><el-input v-model="formData.brand" disabled /> </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="故障代码"><el-input v-model="formData.faultCode" disabled /> </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="故障条文"><el-input v-model="formData.faultBarcode" disabled /> </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="故障描述">
            <el-input
              v-model="formData.faultDesc"
              type="textarea"
              :rows="3"
              disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="下发人"><el-input v-model="formData.assignUserName" disabled /> </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="下发时间"><el-input v-model="formData.assignTime" disabled /> </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="接单人"><el-input v-model="formData.acceptUserName" disabled /> </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="接单时间"><el-input v-model="formData.acceptTime" disabled /> </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="信息录入" prop="infoEntry">
            <el-radio-group v-model="formData.infoEntry" @change="handleInfoEntryChange">
              <el-radio
                v-for="dict in infoEntryOptions"
                :key="dict.value"
                :label="dict.value"
              >
                {{ dict.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="formData.infoEntry == 1">
          <el-form-item label="MIS工单编码" prop="misOrderNo" >
            <el-input
              v-model="formData.misOrderNo"
              placeholder="请输入MIS工单编码或点击搜索选择"
              clearable
              @focus="handleMisNoInputFocus"
              @blur="handleMisNoInputBlur"
              @input="handleMisNoInput"
              @clear="handleMisNoClear"
            >
              <template #append>
                <el-button @click="handleSelectMisInfo" icon="Search"></el-button>
              </template>
            </el-input>
            <!-- 快速检索下拉框 -->
            <div class="quick-select-dropdown" v-show="showMisNoQuickSelect && quickMisNoList.length > 0">
              <div
                  v-for="item in quickMisNoList"
                  :key="item.misNo"
                  class="quick-select-item"
                  @click="handleMisNoQuickSelect(item)">
                <span class="mis-no">{{ item.misNo }}</span>
              </div>
            </div>
            <div class="quick-select-dropdown no-data" v-show="showMisNoQuickSelect && quickMisNoList.length === 0 && formData.misOrderNo">
              <div>未找到匹配的MIS工单</div>
            </div>
          </el-form-item>
        </el-col>
        <!-- MIS选择组件 -->
        <slot name="mis-info-select"></slot>
        <el-col :span="12">
          <el-form-item label="工作票编号" prop="workPermitNum">
            <el-input v-model="workPermitNumProxy" placeholder="请输入工作票编号" maxlength="20" show-word-limit :readonly="formData.infoEntry == '1'" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工作部位" prop="workArea">
            <el-select
                v-model="formData.workArea"
                multiple
                placeholder="请选择工作部位"
                style="width: 100%"
            >
              <el-option
                  v-for="dict in workAreaOptions"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开始时间" prop="realStartTime">
            <el-date-picker
                v-model="formData.realStartTime"
                type="datetime"
                format="YYYY-MM-DD HH:mm"
                value-format="YYYY-MM-DD HH:mm"
                placeholder="请选择开始时间"
                style="width: 100%"
                :disabled-date="disabledStartDate"
                @change="handleStartTimeChange"
                :readonly="formData.infoEntry == '1'"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="结束时间" prop="realEndTime">
            <el-date-picker
                v-model="formData.realEndTime"
                type="datetime"
                format="YYYY-MM-DD HH:mm"
                value-format="YYYY-MM-DD HH:mm"
                placeholder="请选择结束时间"
                style="width: 100%"
                :disabled-date="disabledEndDate"
                @change="handleEndTimeChange"
                :readonly="formData.infoEntry == '1'"
            />
          </el-form-item>
        </el-col>

        <el-col :span="12" v-if="resumeInfo && resumeShow">
          <el-form-item label="挂起结束时间" prop="resumeTime">
            <el-date-picker
                v-model="formData.resumeTime"
                type="datetime"
                format="YYYY-MM-DD HH:mm"
                value-format="YYYY-MM-DD HH:mm"
                placeholder="请选择挂起结束时间"
                style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工作负责人" prop="teamLeaderName">
            <el-input
                v-model="formData.teamLeaderName"
                placeholder="请输入工作负责人姓名或点击选择"
                clearable
                @focus="handleTeamLeaderInputFocus"
                @blur="handleTeamLeaderInputBlur"
                @input="handleTeamLeaderInput"
                @clear="handleTeamLeaderClear"
            >
            </el-input>
            <!-- 快速检索下拉框 -->
            <div class="quick-select-dropdown" v-show="showTeamLeaderQuickSelect && quickTeamLeaderList.length > 0">
              <div
                  v-for="item in quickTeamLeaderList"
                  :key="item.userId"
                  class="quick-select-item"
                  @click="handleTeamLeaderQuickSelect(item)">
                <span class="user-name">{{ item.nickName }}</span>
                <span class="user-name">{{ item.dept.deptName }}</span>
              </div>
            </div>
            <div class="quick-select-dropdown no-data" v-show="showTeamLeaderQuickSelect && quickTeamLeaderList.length === 0 && formData.teamLeaderName && !teamLeaderLoading">
              <div>未找到匹配的人员</div>
            </div>
            <div class="quick-select-dropdown no-data" v-show="showTeamLeaderQuickSelect && teamLeaderLoading">
              <div>
                <i class="el-icon-loading"></i>
                搜索中...
              </div>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工作班成员" prop="workGroupMemberName">
            <el-input
                v-model="formData.workGroupMemberName"
                placeholder="请选择工作班成员"
                :readonly="formData.infoEntry == '1'"
            >
              <template #append v-if="formData.infoEntry == '2'">
                <el-button @click="userSelectVisible = true" icon="User"></el-button>
              </template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="外委人员数（人）" prop="wwryNum">
            <el-input-number
                v-model="formData.wwryNum"
                placeholder="请输入外委人员数"
                controls-position="right"
                style="width: 100%"
                class="input-number-left"
                :min="0"
                :step="1"
                :precision="0"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="外来人员数（人）" prop="wlryNum">
            <el-input-number
                v-model="formData.wlryNum"
                placeholder="请输入外来人员数"
                controls-position="right"
                style="width: 100%"
                class="input-number-left"
                :min="0"
                :step="1"
                :precision="0"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="真实故障原因" prop="realContent">
            <el-input v-model="formData.realFailureReason"
                      type="textarea"
                      placeholder="请输入真实故障原因，最多500字"
                      maxlength="500"
                      :rows="5"
                      show-word-limit />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="附件（可选）">
            <preview :limit="8" v-model="formData.attachmentUrls" :filesize="5"></preview>
          </el-form-item>
        </el-col>
      </el-row>
      <MisInfoSelectSingle :key="commonKey" v-model="misInfoSelectVisible" @onSelected="onMisInfoSelected"
                           :pcsStationName="formData.pcsStationName" :pcsDeviceName="formData.pcsDeviceName"
                           :workOrderStatus="'结束'" >
      </MisInfoSelectSingle>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确认结单</el-button>
      </div>
    </template>
  </el-dialog>

  <!-- 人员选择组件 -->
  <UserSelectMulti
    v-model="userSelectVisible"
    :pre-selected-users="selectedUsers"
    :sort-dept-id="formData.pcsStationPid"
    @onSelected="onUserSelected"
  />
</template>

<script setup>
import { ref, defineProps, defineEmits, getCurrentInstance, computed, watch } from 'vue'
import preview from '@/components/FileUpload/preview.vue'
import UserSelectMulti from '@/components/userSelect/multi.vue'
import { ElMessageBox } from 'element-plus'
import {listUser, listUserData, listUserNoPermi, listLeader} from "@/api/system/user";
import MisInfoSelectSingle from "@/components/misInfoSelect/single.vue";
import {listRepairOrder} from "@/api/gxt/repairOrder.js";
import {listWorkPerson} from "@/api/gxt/misInfo.js";
import {getOrderList} from "@/api/gxt/gxtOrder.js";

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

// 定义属性
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  data: {
    type: Object,
    default: () => ({})
  },
  workOrderStatusOptions: {
    type: Array,
    default: () => ([])
  },
  infoEntryOptions: {
    type: Array,
    default: () => ([])
  },
  workAreaOptions: {
    type: Array,
    default: () => ([])
  },
  onSubmit: {
    type: Function,
    default: null
  },
  listRepairOrder: {
    type: Function,
    default: null
  },
  listWorkPerson: {
    type: Function,
    default: null
  },
  listUserData: {
    type: Function,
    default: null
  },
  listMisInfo: {
    type: Function,
    default: null
  },
  infoEntryDisabled: {
    type: Boolean,
    default: false
  },
  commonKey: {
    type: Number,
    default: 0
  }
})

// 定义事件
const emit = defineEmits(['update:modelValue', 'success', 'select-mis-info'])

// 响应式数据
const visible = ref(false)
const formData = ref({})
const finalizeFormRef = ref()
const showMisNoQuickSelect = ref(false)
const quickMisNoList = ref([])
const misNoSearchTimer = ref(null)
const userSelectVisible = ref(false) // 添加人员选择组件可见性
const selectedUsers = ref([]) // 存储选中的用户
const inputUsers = ref([]) // 存储选中的用户
const flowList = ref([])
const suspendInfo = ref(null)
const resumeInfo = ref(null)
const resumeShow = ref(false)
const misInfoSelectVisible = ref(false)

// 工作负责人快速检索相关响应式数据
const showTeamLeaderQuickSelect = ref(false)
const quickTeamLeaderList = ref([])
const teamLeaderLoading = ref(false)
const teamLeaderSearchTimer = ref(null)
const allUserList = ref([]) // 存储所有设备数据用于快速检索
const lastLoadedCenterId = ref(null)

// 计算属性
const workPermitNumProxy = computed({
  get() {
    return formData.value.workPermitNum
  },
  set(val) {
    formData.value.workPermitNum = val
  }
})

// 表单验证规则
const finalizeRules = ref({
  // repairMethod: [
  //   { required: true, message: "请选择维修方式", trigger: "change" }
  // ],
  misOrderNo: [
    { required: true, message: "MIS工单编码不能为空", trigger: "change" }
  ],
  realStartTime: [
    { required: true, message: "请选择开始时间", trigger: "change" },
    {
      validator: (rule, value, callback) => {
        if (value && new Date(value) > new Date()) {
          callback(new Error('开始时间不能大于当前时间'));
        // } else if(value && new Date(value) < new Date(formData.value.acceptTime)) {
        //
        //   callback(new Error('开始时间不能小于接单时间'));
        } else {
          callback();
        }
      },
      trigger: 'change'
    }
  ],
  realEndTime: [
    { required: true, message: "请选择结束时间", trigger: "change" },
    {
      validator: (rule, value, callback) => {
        if (value && new Date(value) > new Date() && formData.value.infoEntry == '2') {
          callback(new Error('结束时间不能大于当前时间'));
        } else if(value && new Date(value) < new Date(formData.value.realStartTime) && formData.value.infoEntry == '2') {
          callback(new Error('结束时间不能小于开始时间'));
        } else {
          callback();
        }
      },
      trigger: 'change'
    }
  ],
  workGroupMemberName: [
    { required: true, message: "请选择工作班成员", trigger: "change" },
    {
      validator: async (rule, value, callback) => {
        // 如果值为空、关联MIS，直接通过验证
        if (!value || formData.value.infoEntry == '1') {
          selectedUsers.value = [];
          return callback();
        }
        try {
          inputUsers.value = []
          // 将输入的工作班成员姓名按逗号分割
          const names = value.split(',').map(name => name.trim());
          // 验证每个工作班成员是否存在于组织架构中
          for (const name of names) {
            if (name.length > 0) {
              // 检查输入中重复
              if (inputUsers.value.some(u => u.nickName === name)) {
                return callback(new Error(`工作班成员"${name}"重复，请重新输入`));
              }
              // 使用从属性传入的listUserData方法
              if (!props.listUserData) {
                return callback(new Error('系统配置错误：缺少用户数据查询方法'));
              }
              const response = await props.listUserData({nickName: name});
              if (!response.rows || response.rows.length === 0) {
                return callback(new Error(`工作班成员"${name}"非系统内人员，请重新输入`));
              }else{
                inputUsers.value.push(response.rows[0]);
              }
            } else {
              return callback(new Error(`请正确输入工作班成员名单`));
            }
          }
          selectedUsers.value = inputUsers.value;
          callback();
        } catch (error) {
          callback(new Error('验证工作班成员时发生错误'));
        }
      },
      trigger: 'blur'
    }
  ],
  workArea: [
    { required: true, message: "请选择工作部位", trigger: "change" }
  ],
  workPermitNum: [
    { required: true, message: "工作票编号不能为空", trigger: "change" },
    {
      validator: (rule, value, callback) => {
        if (formData.value.infoEntry == '2') {
          if (!value) {
            callback(new Error('工作票编号不能为空'))
          } else if (!/^[a-zA-Z0-9_\-\.]*$/.test(value)) {
            callback(new Error('仅支持英文、数字、下划线'))
          } else if (value.length > 20) {
            callback(new Error('不能超过20个字符'))
          } else {
            // 验证唯一性
            if (value && props.listRepairOrder) {
              props.listRepairOrder({pageNum: 1, pageSize: 10, workPermitNum: value}).then(response => {
                const gxtOrders = response.rows
                if (gxtOrders.length > 0) {
                  if (gxtOrders[0].id != formData.value.id) {
                    callback(new Error('工作票编号已存在！'))
                  } else {
                    callback()
                  }
                } else {
                  callback()
                }
              }).catch(() => {
                callback()
              })
            } else {
              callback()
            }
          }
        } else {
          callback()
        }
      },
      trigger: 'change'
    }
  ],
  resumeTime: [
    { required: true, message: "挂起结束时间不能为空", trigger: "change" },
    {
      validator: (rule, value, callback) => {
        const realStartTime = formData.value.realStartTime
        const realEndTime = formData.value.realEndTime
        const suspendTime = suspendInfo.value.actionTime
        debugger
        if (suspendTime && realStartTime && new Date(suspendTime) < new Date(realStartTime)) { // 开工前挂起
          if (value && new Date(value) > new Date(realStartTime)) {
            callback(new Error('开工前挂起结束时间晚于实际开始时间，请调整'));
          } else if(realEndTime && value && new Date(value) > new Date(realEndTime)) {
            callback(new Error('开工前挂起结束时间晚于实际结束时间，请调整'));
          } else {
            callback();
          }

        } else if(suspendTime && realStartTime && new Date(suspendTime) >= new Date(realStartTime)) { // 作业中挂起
          if (value && new Date(value) < new Date(realStartTime)) {
            callback(new Error('作业中挂起结束时间早于实际开始时间，请调整'));
          } else if(realEndTime && value && new Date(value) > new Date(realEndTime)) {
            callback(new Error('作业中挂起结束时间晚于实际结束时间，请调整'));
          } else {
            callback();
          }
        } else {
          callback();
        }
      },
      trigger: 'change'
    }
  ],
  teamLeaderName: [
    { required: true, message: "工作负责人不能为空", trigger: "change" }
  ],
})

// 监听modelValue变化
watch(() => props.modelValue, (val) => {
  visible.value = val
  if (val) {
    // 初始化表单数据
    formData.value = { ...props.data, selectedMembers: [] }
  }
})

// 监听props.data变化
watch(() => props.data, (newData) => {
  if (visible.value) {
    // 只有在对话框打开时才更新数据
    formData.value = { ...newData, selectedMembers: [] }
    flowList.value = formData.value.repairOrderFlowList || []
    if (formData.value.suspendReason && flowList.value.length > 0) {
      // 获取最后一个 actionType 等于 'resume' 的项
      const lastResumeItem = flowList.value.findLast(item => item.actionType === 'resume')
      if (lastResumeItem) {
        // 做你想做的事，比如记录时间、设置状态等
        console.log('最后一个 resume 项:', lastResumeItem)
        resumeInfo.value = lastResumeItem
        formData.value.resumeTime = lastResumeItem.actionTime
      }

      const lastSuspendItem = flowList.value.findLast(item => item.actionType === 'to_approve')
      if (lastSuspendItem) {
        // 做你想做的事，比如记录时间、设置状态等
        console.log('最后一个 to_approve 项:', lastSuspendItem)
        suspendInfo.value = lastSuspendItem
      }

    }
  }
}, { deep: true })

// 监听visible变化
watch(visible, (val) => {
  emit('update:modelValue', val)
  if (val) {
    // 打开对话框后重置表单验证错误
    proxy.$nextTick(() => {
      if (finalizeFormRef.value) {
        finalizeFormRef.value.clearValidate()
      }
    })
  }
})

watch(
    () => props.data?.realStartTime, // 只监听 realStartTime
    (newStartTime, oldStartTime) => {
      // 判断是否真的变化了（避免 undefined === undefined 误判）
      debugger
      if (resumeInfo.value && newStartTime && newStartTime !== oldStartTime) {
        // 注意：此时对话框可能未打开，根据你的需求决定是否加 visible 判断
        if (visible.value) {
          handleStartTimeChange(newStartTime);
        }
      }
    },
    { immediate: true } // 如果需要初始化时也触发一次，可加；否则去掉
);

watch(
    () => props.data?.realEndTime, // 只监听 realEndTime
    (newEndTime, oldEndTime) => {
      // 判断是否真的变化了（避免 undefined === undefined 误判）
      debugger
      if (resumeInfo.value && newEndTime && oldEndTime !== oldEndTime) {
        // 注意：此时对话框可能未打开，根据你的需求决定是否加 visible 判断
        if (visible.value) {
          handleEndTimeChange(newEndTime);
        }
      }
    },
    { immediate: true } // 如果需要初始化时也触发一次，可加；否则去掉
);

// 时间禁用函数
const disabledStartDate = (time) => {
  const getYYYYMMDD = (date) => {
    const y = date.getFullYear();
    const m = String(date.getMonth() + 1).padStart(2, '0');
    const d = String(date.getDate()).padStart(2, '0');
    return `${y}-${m}-${d}`;
  };

  const today = getYYYYMMDD(new Date());

  let acceptDateStr = null;
  const acceptStr = formData.value.acceptTime;
  if (acceptStr) {
    acceptDateStr = acceptStr.split(' ')[0];
  }

  const selectedDateStr = getYYYYMMDD(time);

  // 如果没有 acceptTime，只禁用未来日期
  if (!acceptDateStr) {
    return selectedDateStr > today;
  }

  // 要用：早于 acceptDate 或 晚于今天
  // return selectedDateStr < acceptDateStr || selectedDateStr > today;
  return selectedDateStr > today;
};

const disabledEndDate = (time) => {
  const getYYYYMMDD = (date) => {
    const y = date.getFullYear();
    const m = String(date.getMonth() + 1).padStart(2, '0');
    const d = String(date.getDate()).padStart(2, '0');
    return `${y}-${m}-${d}`;
  };

  const today = getYYYYMMDD(new Date());

  let acceptDateStr = null;
  const acceptStr = formData.value.acceptTime;
  const startStr = formData.value.realStartTime;
  if (startStr) {
    acceptDateStr = startStr.split(' ')[0];
  } else if(acceptStr) {
    acceptDateStr = acceptStr.split(' ')[0];
  }

  const selectedDateStr = getYYYYMMDD(time);

  // 如果没有 acceptTime，只禁用未来日期
  if (!acceptDateStr) {
    return selectedDateStr > today;
  }

  // 要用：早于 acceptDate 或 晚于今天
  return selectedDateStr < acceptDateStr || selectedDateStr > today;
};

// 关闭对话框
const handleClose = () => {
  visible.value = false
  selectedUsers.value = []
  resumeInfo.value = null
  suspendInfo.value = null
  resumeShow.value = false
}

// 取消操作
const handleCancel = () => {
  visible.value = false
  selectedUsers.value = []
  resumeInfo.value = null
  suspendInfo.value = null
  resumeShow.value = false
}

// 信息录入方式变化处理
const handleInfoEntryChange = (val) => {
  // 选中2工作票编号时修改其他值
  if (val === '2') {
    formData.value.misOrderNo = undefined;
    formData.value.realStartTime = undefined;
    formData.value.realEndTime = undefined;
    formData.value.workGroupMemberName = undefined;
    // formData.value.teamLeaderName = undefined
    // formData.value.teamLeaderId = undefined
    formData.value.repairOrderPersonList = [];
    selectedUsers.value = [];
  } else {
    formData.value.workPermitNum = undefined;
    formData.value.misOrderNo = undefined;
    formData.value.realStartTime = undefined;
    formData.value.realEndTime = undefined;
    formData.value.workGroupMemberName = undefined;
    formData.value.teamLeaderName = undefined
    formData.value.teamLeaderId = undefined
    formData.value.repairOrderPersonList = [];
  }
}


// 开始时间变化处理
const handleStartTimeChange = (value) => {
  debugger
  if (resumeInfo.value && value) {
    const realStartTime = formData.value.realStartTime
    const realEndTime = formData.value.realEndTime
    const suspendTime = suspendInfo.value.actionTime
    const resumeTime = formData.value.resumeTime
    if (suspendTime && value && new Date(suspendTime) < new Date(value)) { // 开工前挂起
      if (value && resumeTime > new Date(value)) {
        resumeShow.value = true
      } else if(realEndTime && resumeTime && new Date(resumeTime) > new Date(realEndTime)) {
        resumeShow.value = true
      } else {
        resumeShow.value = false
      }

    } else if(suspendTime && value && new Date(suspendTime) >= new Date(value)) { // 作业中挂起
      if (value && new Date(resumeTime) < new Date(value)) {
        resumeShow.value = true
      } else if(realEndTime && resumeTime && new Date(resumeTime) > new Date(realEndTime)) {
        resumeShow.value = true
      } else {
        resumeShow.value = false
      }
    } else {
      resumeShow.value = false
    }
  }
}

// 结束时间变化处理
const handleEndTimeChange = (value) => {
  debugger
  if (resumeInfo.value && value) {
    const realStartTime = formData.value.realStartTime
    const realEndTime = formData.value.realEndTime
    const suspendTime = suspendInfo.value.actionTime
    const resumeTime = formData.value.resumeTime
    if (suspendTime && realStartTime && new Date(suspendTime) < new Date(realStartTime)) { // 开工前挂起
      if (value && resumeTime > new Date(realStartTime)) {
        resumeShow.value = true
      } else if(value && resumeTime && new Date(resumeTime) > new Date(value)) {
        resumeShow.value = true
      } else {
        resumeShow.value = false
      }

    } else if(suspendTime && realStartTime && new Date(suspendTime) >= new Date(realStartTime)) { // 作业中挂起
      if (realStartTime && new Date(resumeTime) < new Date(realStartTime)) {
        resumeShow.value = true
      } else if(value && resumeTime && new Date(resumeTime) > new Date(value)) {
        resumeShow.value = true
      } else {
        resumeShow.value = false
      }
    } else {
      resumeShow.value = false
    }
  }
}

// MIS工单相关处理函数
const handleMisNoInputFocus = () => {
  // showMisNoQuickSelect.value = true
  showMisNoQuickSelect.value = true
  // 如果已有输入内容，立即搜索
  if (formData.value.misOrderNo && formData.value.misOrderNo.trim()) {
    handleMisNoInput(formData.value.misOrderNo)
  }
}

const handleMisNoInputBlur = () => {
  // 延迟隐藏下拉框，确保点击选项能触发
  setTimeout(() => {
    showMisNoQuickSelect.value = false
  }, 200)
}

const handleMisNoInput = (value) => {
  const searchText = value.trim()
  // 这里应该调用搜索函数
  if (!searchText) {
    quickMisNoList.value = []
    showMisNoQuickSelect.value = false
    return
  }
  showMisNoQuickSelect.value = true
  // 清除之前的定时器
  if (misNoSearchTimer.value) {
    clearTimeout(misNoSearchTimer.value)
  }
  // 设置新的定时器，防抖处理（500ms）
  misNoSearchTimer.value = setTimeout(() => {
    searchMisNoList(searchText)
  }, 500)
}

const handleMisNoClear = () => {
  formData.value.misOrderNo = undefined
}

const handleSelectMisInfo = () => {
  misInfoSelectVisible.value = true
  // emit('select-mis-info')
}

const handleMisNoQuickSelect = (item) => {
  formData.value.misOrderNo = item.misNo
  showMisNoQuickSelect.value = false
}

/** 搜索MIS工单列表 */
const searchMisNoList = async (keyword) => {
  if (!keyword) {
    quickMisNoList.value = []
    return
  }
  debugger
  // 检查是否提供了listMisInfo函数
  if (!props.listMisInfo || typeof props.listMisInfo !== 'function') {
    console.error('未提供listMisInfo函数')
    quickMisNoList.value = []
    return
  }

  try {
    const response = await props.listMisInfo({
      pageNum: 1,
      misNo: keyword
    })

    quickMisNoList.value = response.rows || []
  } catch (error) {
    console.error('搜索MIS工单失败:', error)
    proxy.$modal.msgError('搜索失败，请重试')
    quickMisNoList.value = []
  }
}

// 用户选择回调函数
const onUserSelected = (users) => {
  if (users && users.length > 0) {
    // 合并用户，避免重复
    const existingNames = selectedUsers.value.map(u => u.nickName);
    const newUsers = users.filter(u => !existingNames.includes(u.nickName));
    selectedUsers.value = [...selectedUsers.value, ...newUsers];
    // 构建用户姓名列表
    const userNames = selectedUsers.value.map(user => user.nickName).join(',');
    formData.value.workGroupMemberName = userNames;
  } else {
    selectedUsers.value = [];
    formData.value.workGroupMemberName = '';
  }
  formData.value.repairOrderPersonList = selectedUsers.value.map(user => ({
    userId: user.userId,
    nickName: user.nickName,
    orderId: formData.value.id,
    orderCode: formData.value.workOrderProjectNo,
    status: 1
  }));
};

// 提交操作
// const handleSubmit = async () => {
//   if (!finalizeFormRef.value) return
//
//   await finalizeFormRef.value.validate(async (valid) => {
//     if (valid) {
//       const { realStartTime, acceptTime } = formData.value;
//       debugger
//       try {
//         debugger
//         // 根据维修方式清除不需要的字段
//         if (formData.value.repairMethod === '1') {
//           // 正常维修时清除复位方式
//           formData.value.resetMethod = undefined;
//           formData.value.workArea = Array.isArray(formData.value.workArea) ? formData.value.workArea.join(',')
//                                 : formData.value.workArea || ''
//         } else if (formData.value.repairMethod === '2') {
//           // 复位启机时清除MIS相关字段
//           formData.value.misOrderNo = undefined;
//           formData.value.realStartTime = undefined;
//           formData.value.realEndTime = undefined;
//           formData.value.workGroupMemberName = undefined;
//           formData.value.infoEntry = undefined;
//           formData.value.wwryNum = undefined;
//           formData.value.wlryNum = undefined;
//           formData.value.workArea = undefined;
//           formData.value.repairOrderPersonList = [];
//         }
//
//         // 调用父组件传入的提交函数
//         if (props.onSubmit && typeof props.onSubmit === 'function') {
//
//           flowList.value = []
//           if (formData.value.resumeTime && formData.value.resumeTime != resumeInfo.value.actionTime) { //存入新的挂起结束时间
//             resumeInfo.value.actionTime = formData.value.resumeTime
//             flowList.value.push(resumeInfo.value)
//           }
//           formData.value.repairOrderFlowList = flowList.value
//           formData.value.finalizeMethod = '2'
//           await props.onSubmit(formData.value)
//         } else {
//           throw new Error("未提供提交方法")
//         }
//
//         proxy.$modal.msgSuccess("结单成功")
//         visible.value = false
//         emit('success')
//       } catch (error) {
//         // proxy.$modal.msgError("操作失败: " + (error.message || "未知错误"))
//       }
//     }
//   })
// }

// 核心修改：提交函数 - 临时转换workArea格式，异常时恢复
const handleSubmit = async () => {
  if (!finalizeFormRef.value) return

  // 1. 提交前深拷贝当前表单数据（保留原始数组格式的workArea）
  const submitBackup = JSON.parse(JSON.stringify(formData.value))

  await finalizeFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        formData.value.repairMethod = '1';
        // 2. 构建提交用的临时数据（不修改原formData）
        const submitData = { ...formData.value }

        // 3. 仅对提交数据转换workArea格式（数组转逗号分隔字符串）
          submitData.resetMethod = undefined;
          // 数组转字符串：适配后端String类型
          submitData.workArea = Array.isArray(submitData.workArea)
              ? submitData.workArea.join(',')
              : submitData.workArea || '';

        // 4. 处理挂起结束时间逻辑
        flowList.value = []
        if (submitData.resumeTime && submitData.resumeTime != resumeInfo.value?.actionTime) {
          resumeInfo.value.actionTime = submitData.resumeTime
          flowList.value.push(resumeInfo.value)
        }
        submitData.repairOrderFlowList = flowList.value
        submitData.finalizeMethod = '2'
        // 防止提交时候时间格式转换错误
        submitData.createTime = null
        submitData.updateTime = null
        // 5. 调用父组件提交函数（传入转换后的临时数据）
        if (props.onSubmit && typeof props.onSubmit === 'function') {
          await props.onSubmit(submitData) // 提交转换后的数据
        } else {
          throw new Error("未提供提交方法")
        }

        // 提交成功逻辑
        proxy.$modal.msgSuccess("结单成功")
        visible.value = false
        emit('success')
      } catch (error) {
        // 6. 后端抛出异常时，恢复原始表单数据（保留workArea数组）
        // console.error('结单提交失败:', error)
        formData.value = submitBackup // 恢复原始数组格式的workArea
        // 重置表单验证状态
        proxy.$nextTick(() => {
          finalizeFormRef.value.clearValidate()
        })
      }
    }
  })
}
// 工作负责人快速检索方法
/** 过滤快速检索用户列表 */
const filterQuickUserList = (keyword) => {
  debugger
  if (!allUserList.value.length) {
    loadQuickTeamLeaderList();
    return;
  }

  const lowerKeyword = keyword.toLowerCase();
  quickTeamLeaderList.value = allUserList.value.filter(item =>
      (item.nickName && item.nickName.toLowerCase().includes(lowerKeyword))
  );
}
/** 工作负责人输入框获取焦点 */
const handleTeamLeaderInputFocus = () => {
  showTeamLeaderQuickSelect.value = true
  // 如果已有输入内容，立即搜索
  if (formData.value.teamLeaderName && formData.value.teamLeaderName.trim()) {
    handleTeamLeaderInput(formData.value.teamLeaderName)
  } else {
    // 如果没有输入内容，加载默认列表
    loadQuickTeamLeaderList()
  }
}

/** 工作负责人输入框失去焦点 */
const handleTeamLeaderInputBlur = () => {
  // 延迟隐藏下拉框，确保点击选项能触发
  setTimeout(() => {
    showTeamLeaderQuickSelect.value = false
  }, 200)
}

/** 工作负责人输入事件 - 实时搜索 */
const handleTeamLeaderInput = (value) => {
  const searchText = value.trim()

  if (!searchText) {
    quickTeamLeaderList.value = []
    showTeamLeaderQuickSelect.value = false
    return
  }

  showTeamLeaderQuickSelect.value = true

  // 清除之前的定时器
  if (teamLeaderSearchTimer.value) {
    clearTimeout(teamLeaderSearchTimer.value)
  }

  // 设置新的定时器，防抖处理（300ms）
  // teamLeaderSearchTimer.value = setTimeout(() => {
  // searchTeamLeaderList(searchText)
  filterQuickUserList(searchText)
  // }, 300)
}

/** 搜索工作负责人列表 */
const searchTeamLeaderList = async (keyword) => {
  if (!keyword) {
    quickTeamLeaderList.value = []
    return
  }

  teamLeaderLoading.value = true
  try {
    const response = await listLeader({
      nickName: keyword,
      // 可以根据需要添加其他搜索条件
      status: '0' // 只搜索启用状态的用户
    })

    quickTeamLeaderList.value = response.data || []

  } catch (error) {
    console.error('搜索工作负责人失败:', error)
    proxy.$modal.msgError('搜索失败，请重试')
    quickTeamLeaderList.value = []
  } finally {
    teamLeaderLoading.value = false
  }
}

/** 加载快速检索工作负责人列表 */
const loadQuickTeamLeaderList = async () => {
  // 如果已有所有用户数据，且是同一个维保中心，直接使用
  if (allUserList.value.length > 0 && lastLoadedCenterId.value === formData.value.gxtCenterId) {
    quickTeamLeaderList.value = allUserList.value;
    return;
  }

  // 记录当前维保中心ID
  lastLoadedCenterId.value = formData.value.gxtCenterId;

  teamLeaderLoading.value = true
  try {
    // 加载当前部门下的用户列表
    const response = await listLeader({
      deptId: -1, // 通过后台配置部门
      status: '0'
    })
    allUserList.value = response.data || []
    quickTeamLeaderList.value = allUserList.value;
  } catch (error) {
    console.error('加载工作负责人列表失败:', error)
    allUserList.value = []
  } finally {
    teamLeaderLoading.value = false
  }
}

/** 快速选择工作负责人 */
const handleTeamLeaderQuickSelect = (item) => {
  formData.value.teamLeaderId = item.userId
  formData.value.teamLeaderName = item.nickName
  showTeamLeaderQuickSelect.value = false
}

/** 清空工作负责人 */
const handleTeamLeaderClear = () => {
  formData.value.teamLeaderId = undefined
  formData.value.teamLeaderName = ''
  quickTeamLeaderList.value = []
  showTeamLeaderQuickSelect.value = false
}

/** 设备MIS信息回调 */
const onMisInfoSelected = (row) => {
  if (row) {
    // listRepairOrder({pageNum: 1, pageSize: 10, misOrderNo: row.misNo }).then(response => {
    getOrderList(row.misNo ).then(response => {
      const  gxtOrders= response.rows
      if (gxtOrders.length > 0) {
        proxy.$modal.msgWarning('该MIS工单已被其他工单关联！请重新选择！')
        return
      }
      // 使用展开运算符创建新对象以确保响应式更新
      formData.value = {
        ...formData.value,
        misOrderNo: row.misNo,
        realStartTime: row.realStartTime,
        realEndTime: row.realEndTime,
        workPermitNum: row.workPermitNum
      };
      // 使用从属性传入的listWorkPerson方法
      if (typeof listWorkPerson === 'function') {
        listWorkPerson({ misNo: row.misNo }).then(response => {
          debugger
          const updatedData = {
            ...formData.value,
            repairOrderPersonList: response.rows
          };
          if (response.rows) {
            // 查找 isLeader 等于 1 的负责人（优先获取第一个符合条件的，贴合常规单负责人场景）
            const leaderPerson = response.rows.find(person => person.isLeader === 1);
            // 给 teamLeaderName 赋值
            updatedData.teamLeaderName = leaderPerson?.nickName || '';
            const nickNames = response.rows
                .filter(person => person.isLeader != 1)
                .map(person => person.nickName)
                .join(',');
            updatedData.workGroupMemberName = nickNames
          }
          formData.value = updatedData;
        })
      }
      misInfoSelectVisible.value = false
    })
  }
}

</script>

<style scoped>
/* 表单中的列间距调整 */
:deep(.el-col) {
  padding-left: 5px;
  padding-right: 5px;
}
.quick-select-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  z-index: 1000;
  max-height: 200px;
  overflow-y: auto;
  background: white;
  border: 1px solid #dcdfe6;
  border-top: none;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.quick-select-item {
  padding: 8px 12px;
  cursor: pointer;
  border-bottom: 1px solid #f0f0f0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.quick-select-item:hover {
  background-color: #f5f7fa;
}

.no-data {
  text-align: center;
  padding: 10px;
  color: #909399;
}

/* 穿透修改组件内部输入框的对齐方式（关键） */
:deep(.input-number-left .el-input__inner) {
  text-align: left !important; /* !important 覆盖组件默认的居中 */
}
</style>
