<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>
    <el-form ref="finishRef" :model="formData" :rules="finishRules" label-width="120px" label-position="top">
      <!-- 工单信息 -->
      <h4 class="text-sm font-medium text-gray-800 mb-3"></h4>
      <el-row :gutter="20">
        <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="工单状态" prop="workOrderStatus">
            <el-select v-model="formData.workOrderStatus" 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-row>
      <el-row :gutter="20">
        <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-input v-model="formData.gxtCenter" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <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-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="机型">
            <el-input v-model="formData.model" disabled />
          </el-form-item>
        </el-col>
<!--        <el-col :span="12" v-if="formData.infoEntry == '1'">-->
<!--          <el-form-item label="MIS工单编码">-->
<!--            <el-input v-model="formData.misNo" disabled />-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--        <el-col :span="12" v-if="formData.infoEntry == '2'">-->
<!--          <el-form-item label="工作票编号">-->
<!--            <el-input v-model="formData.workPermitNum" disabled />-->
<!--          </el-form-item>-->
<!--        </el-col>-->
      </el-row>
      <el-row>
        <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 :gutter="20">
        <el-col :span="24">
          <el-form-item label="维保内容">
            <el-input v-model="formData.content" type="textarea" :rows="3" maxlength="500" disabled show-word-limit />
          </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-row>
      <el-row>
        <el-col :span="12" v-if="formData.infoEntry == 1">
          <el-form-item label="MIS工单编码" prop="misNo" >
            <el-input
                v-model="formData.misNo"
                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.misNo">
              <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" maxlength="20" show-word-limit :readonly="formData.infoEntry == '1'" />
          </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-row>
      <el-row>
        <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"
                :readonly="formData.infoEntry == '1'"
            >
            </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-row>-->
<!--      <el-row>-->
        <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-row>
      <el-row>
        <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-row>
      <el-row>
        <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" :loading="submitLoading">确认结单</el-button>
      </div>
    </template>

  </el-dialog>

  <!-- 人员选择组件 -->
  <UserSelectMulti
      v-model="userSelectVisible"
      :pre-selected-users="selectedUsers"
      @onSelected="onUserSelected"
  />


</template>

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

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

// 定义属性
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  data: {
    type: Object,
    default: () => ({})
  },
  workOrderStatusOptions: {
    type: Array,
    default: () => ([])
  },
  listUserData: {
    type: Function,
    default: null
  },
  onSubmit: {
    type: Function,
    default: null
  },
  infoEntryOptions: {
    type: Array,
    default: () => ([])
  },
  listMisInfo: {
    type: Function,
    default: null
  },
  listWorkPerson: {
    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 finishRef = ref()
const submitLoading = ref(false)
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 showMisNoQuickSelect = ref(false)
const quickMisNoList = ref([])
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 finishRules = ref({
  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) && formData.value.infoEntry == '2') {
        //   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) {
                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: 'change'
    }
  ],
  content: [
    { required: true, message: "请输入维保内容", 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'
    }
  ],
  misNo: [
    { required: true, message: "MIS工单编码不能为空", 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) {
              listGxtOrder({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'
    }
  ],
  teamLeaderName: [
    { required: true, message: "工作负责人不能为空", trigger: "change" }
  ],
})
// 时间禁用函数
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;
};

// 监听modelValue变化
watch(() => props.modelValue, (val) => {
  visible.value = val
  if (val) {
    // 初始化表单数据
    formData.value = { ...props.data }
    flowList.value = formData.value.workOrderFlowList || []
    debugger
    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
      }
      handleStartTimeChange(formData.value.realStartTime)
    }
  }
})

// 监听props.data变化
watch(() => props.data, (newData) => {
  if (visible.value) {
    // 只有在对话框打开时才更新数据
    formData.value = { ...newData }

    flowList.value = formData.value.workOrderFlowList || []
    debugger
    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
      }
      handleStartTimeChange(formData.value.realStartTime)
    }
  }
}, { deep: true })

// 监听visible变化
watch(visible, (val) => {
  emit('update:modelValue', val)
  if (val) {
    // 打开对话框后重置表单验证错误
    proxy.$nextTick(() => {
      if (finishRef.value) {
        finishRef.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 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 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.workOrderPersonList = selectedUsers.value.map(user => ({
    userId: user.userId,
    nickName: user.nickName,
    orderId: formData.value.id,
    orderCode: formData.value.workOrderProjectNo,
    status: 1
  }));
};

// 提交操作
const handleSubmit = async () => {
  if (!finishRef.value) return

  await finishRef.value.validate(async (valid) => {
    if (valid) {
      const { realStartTime, acceptTime } = formData.value;
      debugger
      // if (realStartTime && acceptTime && (new Date(realStartTime) < new Date(acceptTime))) { //开始时间早于接单时间，为补录工单
        // formData.value.orderEntryType = '2'
        // try {
        //   debugger
        //   await ElMessageBox.confirm(
        //       '检测到开始时间早于接单时间，系统将按“补录工单”处理，准备工时为0。是否继续？',
        //       '提示',
        //       {
        //         confirmButtonText: '是',
        //         cancelButtonText: '否',
        //         type: 'warning',
        //         distinguishCancelAndClose: true
        //       }
        //   );
        //   formData.value.orderEntryType = '2'
        //   // 用户点击“是”，继续提交
        // } catch (error) {
        //   // 用户点击“否”或关闭弹窗
        //   finalizeFormRef.value?.validateField('realStartTime');
        //   return;
        // }
      // }


      try {
        if (!formData.value.realEndTime) {
          proxy.$modal.msgError("该工单未结束，无法结单！")
          return
        }
        submitLoading.value = true

        // 调用父组件传入的提交函数
        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.workOrderFlowList = flowList.value
          formData.value.finalizeMethod = '2'
          formData.value.createTime = null
          formData.value.updateTime = null
          await props.onSubmit(formData.value)
        } else {
          throw new Error("未提供提交方法")
        }

        proxy.$modal.msgSuccess("结单成功")
        visible.value = false
        emit('success')
      } catch (error) {
        proxy.$modal.msgError("操作失败: " + (error.message || "未知错误"))
      } finally {
        submitLoading.value = false
      }
    }
  })
}


// 开始时间变化处理
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.misNo && formData.value.misNo.trim()) {
    handleMisNoInput(formData.value.misNo)
  }
}

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.misNo = undefined
}

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

const handleMisNoQuickSelect = (item) => {
  formData.value.misNo = 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 handleInfoEntryChange = (val) => {
  // 选中2工作票编号时修改其他值
  if (val === '2') {
    formData.value.misNo = undefined;
    formData.value.realStartTime = undefined;
    formData.value.realEndTime = undefined;
    formData.value.workGroupMemberName = undefined;
    formData.value.workPermitNum = 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 = [];
  }
  // if (finishRef.value) {
  //   finishRef.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
}

const onMisInfoSelected = (row) => {
  if (row) {
    listGxtOrder({pageNum: 1, pageSize: 10, misNo: row.misNo }).then(response => {
      const  gxtOrders= response.rows
      debugger
      if (gxtOrders.length > 0) {
        if (formData.value.id == null) {
          proxy.$modal.msgWarning('选择工单已存在！请重新选择！')
          return
        } else {
          if (gxtOrders[0].id != formData.value.id) {
            proxy.$modal.msgWarning('选择工单已存在！请重新选择！')
            return
          }
        }
      }

      // 使用展开运算符创建新对象以确保响应式更新
      formData.value = {
        ...formData.value,
        misNo: row.misNo,
        realStartTime: row.realStartTime,
        realEndTime: row.realEndTime,
        content: row.content,
        workPermitNum: row.workPermitNum
      };
      // 使用从属性传入的listWorkPerson方法
      if (typeof listWorkPerson === 'function') {
        listWorkPerson({ misNo: row.misNo }).then(response => {
          debugger
          const updatedData = {
            ...formData.value,
            workOrderPersonList: 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;
}

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

/* MIS工单快速检索样式 */
.quick-select-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: white;
  border: 1px solid #e4e7ed;
  border-top: none;
  border-radius: 0 0 4px 4px;
  max-height: 200px;
  overflow-y: auto;
  z-index: 2000;
  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;
}

.quick-select-item:last-child {
  border-bottom: none;
}
</style>
