<template>
  <ContentWrap v-loading="formLoading">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="合同名称" prop="contractName">
            <el-select
              v-model="formData.contractId"
              placeholder="请选择"
              @change="getProjectInfo"
              disabled
            >
              <el-option
                v-for="item in projectList"
                :key="item.id"
                :label="item.contractName"
                :value="item.id"
                clearable
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同编号" prop="contractCode">
            <el-input v-model="formData.contractCode" placeholder="请输入合同编号" disabled />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="客户名称" prop="manufacturerId">
            <el-select
              clearable
              @clear="zzClear"
              v-model="formData.manufacturerId"
              :placeholder="t('deviceForm.mfgHolder')"
              disabled
            >
              <el-option :label="formData.manufactureName" :value="formData.manufacturerId" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="t('project.payment')" prop="payment">
            <el-select v-model="formData.payment" placeholder="请选择" disabled>
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.PMS_PROJECT_SETTLEMENT)"
                :key="dict.id"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="开始时间" prop="startTime">
            <el-date-picker
              style="width: 150%"
              v-model="formData.startTime"
              type="date"
              value-format="x"
              placeholder="选择开始时间"
              disabled
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="完成时间" prop="endTime">
            <el-date-picker
              style="width: 150%"
              v-model="formData.endTime"
              type="date"
              value-format="x"
              placeholder="选择完成时间"
              disabled
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注" type="textarea" disabled />
      </el-form-item>
    </el-form>
  </ContentWrap>

  <ContentWrap v-if="false">
    <div class="content">
      <div class="toolbar">
        <div class="actions">
          <!-- 操作按钮已移动到表单区域 -->
        </div>
      </div>

      <div class="table-container">
        <el-table
          :data="tableData"
          empty-text="暂无数据"
          highlight-current-row
          @current-change="handleRowClick"
        >
          <el-table-column prop="wellName" label="井号" />
          <el-table-column prop="wellType" label="井型">
            <template #default="{ row }">
              {{ getWellTypeLabel(row.wellType) }}
            </template>
          </el-table-column>
          <el-table-column prop="wellCategory" label="井别">
            <template #default="{ row }">
              {{ getWellCategoryLabel(row.wellCategory) }}
            </template>
          </el-table-column>
          <el-table-column prop="location" label="施工地点" />
          <el-table-column :label="t('project.technology')">
            <template #default="{ row }">
              {{ getTechniqueLabel(row.technique) }}
            </template>
          </el-table-column>
          <el-table-column prop="workloadDesign" label="设计工作量" />
          <el-table-column :label="t('project.unit')">
            <template #default="{ row }">
              {{ getWorkloadUnitLabel(row.workloadUnit) }}
            </template>
          </el-table-column>
          <el-table-column prop="deptIds" label="施工队伍">
            <template #default="{ row }">
              <el-tooltip
                effect="dark"
                :content="getAllDeptNames(row.deptIds)"
                placement="top"
                :disabled="!row.deptIds || row.deptIds.length <= 1"
              >
                <span class="dept-names">
                  {{ getDeptNames(row.deptIds) }}
                </span>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column prop="deviceIds" label="施工设备">
            <template #default="{ row }">
              <el-tooltip
                :content="getAllDeviceNames(row.deviceIds)"
                placement="top"
                :disabled="row.deviceIds && row.deviceIds.length <= 1"
              >
                <span class="device-names">
                  {{ getDeviceNames(row.deviceIds) }}
                </span>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column prop="responsiblePerson" label="责任人">
            <template #default="{ row }">
              <el-tooltip
                :content="getAllResponsiblePersonNames(row.responsiblePerson)"
                placement="top"
                :disabled="!row.responsiblePerson || row.responsiblePerson.length <= 1"
              >
                <span class="responsible-names">
                  {{ getResponsiblePersonNames(row.responsiblePerson) }}
                </span>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column prop="remark" label="备注" />
        </el-table>
      </div>
    </div>
  </ContentWrap>

  <!-- 新增任务详情编辑表单 -->
  <ContentWrap>
    <h3 style="margin-bottom: 20px">任务详情</h3>
    <el-form
      ref="taskFormRef"
      :model="currentTask"
      :rules="taskFormRules"
      label-width="120px"
      class="task-edit-form"
    >
      <el-row>
        <el-col :span="8">
          <el-form-item label="井号" prop="wellName">
            <el-input
              v-model="currentTask.wellName"
              placeholder="请输入井号"
              :disabled="currentTask.platformWell === '1'"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="t('project.workArea')" prop="location">
            <el-autocomplete
              ref="workAreaAutocomplete"
              v-model="currentTask.location"
              :fetch-suggestions="querySearch"
              :trigger-on-focus="true"
              placeholder="请输入施工区域"
              @focus="handleWorkAreaFocus"
              @select="handleSelect"
              :disabled="!workAreaOptions.length"
              popper-class="work-area-autocomplete"
            >
              <template #prefix>
                <el-icon v-if="loadingWorkAreaOptions" class="is-loading">
                  <Loading />
                </el-icon>
              </template>
            </el-autocomplete>
            <div v-if="!workAreaOptions.length" class="el-form-item__error">
              暂无可用施工区域选项，请手动输入
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="t('project.technology')" prop="technique">
            <el-select
              v-model="currentTask.technique"
              placeholder="请选择施工工艺"
              clearable
              :loading="loadingTechnologyOptions"
            >
              <el-option
                v-for="dict in technologyOptions"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
            <div
              v-if="!technologyOptions.length && !loadingTechnologyOptions"
              class="el-form-item__error"
            >
              暂无可用施工工艺选项
            </div>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="8">
          <el-form-item label="设计工作量" prop="workloadDesign">
            <el-tooltip
              :disabled="!workloadDesignError"
              :content="workloadDesignError"
              placement="top"
              effect="light"
              popper-class="workload-design-tooltip"
              :show-after="0"
            >
              <el-input
                v-model="currentTask.workloadDesign"
                placeholder="请输入设计工作量"
                :disabled="currentTask.platformWell === '1'"
                class="workload-input-with-button"
                :class="{ 'error-input': workloadDesignError }"
                @blur="validateWorkloadDesign"
              >
                <template #append>
                  <el-tooltip content="添加多个设计工作量" placement="top">
                    <el-button
                      class="workload-add-btn"
                      @click="openWorkloadDialog"
                      :disabled="getWorkloadAddBtnDisabled"
                    >
                      <span class="btn-text">+</span>
                    </el-button>
                  </el-tooltip>
                </template>
              </el-input>
            </el-tooltip>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="t('project.unit')" prop="workloadUnit">
            <el-select
              v-model="currentTask.workloadUnit"
              placeholder="请选择工作量单位"
              clearable
              :disabled="currentTask.platformWell === '1'"
            >
              <el-option
                v-for="dict in getStrDictOptions(DICT_TYPE.PMS_PROJECT_WORKLOAD_UNIT)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
                :disabled="
                  getFormSelectedWorkloadUnits().includes(dict.value) &&
                  dict.value !== currentTask.workloadUnit
                "
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="施工队伍" prop="deptIds">
            <el-tree-select
              multiple
              :multiple-limit="formData.deptId === 157 ? 1 : 0"
              v-model="currentTask.deptIds"
              :data="deptList"
              :props="treeSelectProps"
              :default-expanded-keys="defaultExpandedKeys"
              check-strictly
              node-key="id"
              filterable
              placeholder="请选择施工队伍"
              clearable
              @visible-change="handleTreeVisibleChange"
              collapse-tags
              collapse-tags-tooltip
              :max-collapse-tags="1"
              class="department-tree-select single-line-tree-select"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="8">
          <el-form-item label="施工设备" prop="deviceIds">
            <el-button @click="openDeviceDialogForForm" type="primary" size="small">
              选择设备
            </el-button>
            <!--
            <span v-if="currentTask.deviceIds && currentTask.deviceIds.length > 0" style="margin-left: 10px;">
              已选 {{ currentTask.deviceIds.length }} 台设备
            </span> -->
            <el-tooltip
              v-if="currentTask.deviceIds && currentTask.deviceIds.length > 0"
              :content="getAllDeviceNamesForForm(currentTask.deviceIds)"
              placement="top"
            >
              <span style="margin-left: 10px">
                {{ formatDevicesForForm(currentTask.deviceIds) }}
              </span>
            </el-tooltip>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="isSpecialDept ? '带班干部' : '责任人'" prop="responsiblePerson">
            <el-button @click="openResponsiblePersonDialogForForm" type="primary" size="small">
              选择{{ isSpecialDept ? '带班干部' : '责任人' }}
            </el-button>
            <el-tooltip
              v-if="currentTask.responsiblePerson && currentTask.responsiblePerson.length > 0"
              :content="getAllResponsiblePersonNamesForForm(currentTask.responsiblePerson)"
              placement="top"
            >
              <span style="margin-left: 10px">
                {{ formatResponsiblePersonsForForm(currentTask.responsiblePerson) }}
              </span>
            </el-tooltip>
          </el-form-item>
        </el-col>
        <el-col :span="8" v-if="isSpecialDept">
          <el-form-item label="填报人" prop="submitter">
            <el-button @click="openSubmitterDialogForForm" type="primary" size="small">
              选择填报人
            </el-button>
            <el-tooltip
              v-if="currentTask.submitter && currentTask.submitter.length > 0"
              :content="getAllSubmitterNamesForForm(currentTask.submitter)"
              placement="top"
            >
              <span style="margin-left: 10px">
                {{ formatSubmittersForForm(currentTask.submitter) }}
              </span>
            </el-tooltip>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="8" v-if="isSpecialDept">
          <el-form-item label="平台井" prop="platformWell">
            <el-switch
              v-model="currentTask.platformWell"
              active-value="1"
              inactive-value="0"
              @change="handlePlatformWellChange"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <!-- 动态属性部分 -->
      <el-row v-if="dynamicAttrs.length > 0">
        <el-col
          v-for="attr in dynamicAttrs"
          :key="attr.id"
          :span="attr.dataType === 'textarea' ? 24 : 8"
        >
          <el-form-item
            :label="attr.name + (attr.unit ? `(${attr.unit})` : '')"
            :prop="attr.identifier"
            :rules="getDynamicAttrRules(attr)"
          >
            <!-- 文本类型 -->
            <el-input
              v-if="attr.dataType === 'text'"
              v-model="currentTask[attr.identifier]"
              :placeholder="`请输入${attr.name}`"
            />

            <!-- 文本域类型 -->
            <el-input
              v-else-if="attr.dataType === 'textarea'"
              v-model="currentTask[attr.identifier]"
              :placeholder="`请输入${attr.name}`"
              type="textarea"
              :rows="3"
            />

            <!-- 数字类型 -->
            <el-input
              v-else-if="attr.dataType === 'double'"
              v-model="currentTask[attr.identifier]"
              :placeholder="`请输入${attr.name}`"
              type="number"
              :min="attr.minValue || undefined"
              :max="attr.maxValue || undefined"
            />

            <!-- 日期类型 -->
            <el-date-picker
              v-else-if="attr.dataType === 'date'"
              v-model="currentTask[attr.identifier]"
              type="date"
              value-format="x"
              :placeholder="`选择${attr.name}`"
              style="width: 100%"
            />

            <!-- 默认文本输入 -->
            <el-input
              v-else
              v-model="currentTask[attr.identifier]"
              :placeholder="`请输入${attr.name}`"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="currentTask.remark" placeholder="请输入备注" type="textarea" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item>
        <el-button @click="resetTaskForm" v-if="isNewTask">重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <div
    v-if="companyName === 'ry'"
    class="my-6 bg-white p-4 border-1 border-solid border-[var(--el-border-color-light)]"
  >
    <h3 style="margin-bottom: 20px">附件</h3>
    <el-form-item size="default" label="工程设计">
      <el-upload
        v-model:file-list="constructionFiles"
        :action="uploadUrl"
        multiple
        :headers="{ 'tenant-id': 1, 'device-id': 'undefined' }"
        name="files"
        class="w-50%"
        @preview="handlePreview"
      >
        <el-button type="primary">上传工程设计</el-button>
        <template #tip>
          <div class="el-upload__tip">文件大小不能超过50MB </div>
        </template>
      </el-upload>
    </el-form-item>
    <el-form-item size="default" label="地质设计">
      <el-upload
        v-model:file-list="geologicalFiles"
        :action="uploadUrl"
        multiple
        :headers="{ 'tenant-id': 1, 'device-id': 'undefined' }"
        name="files"
        class="w-50%"
        @preview="handlePreview"
      >
        <el-button type="primary">上传地质设计</el-button>
        <template #tip>
          <div class="el-upload__tip">文件大小不能超过50MB </div>
        </template>
      </el-upload>
    </el-form-item>
    <el-form-item size="default" label="完井报告">
      <el-upload
        v-model:file-list="completionFiles"
        :action="uploadUrl"
        multiple
        :headers="{ 'tenant-id': 1, 'device-id': 'undefined' }"
        name="files"
        class="w-50%"
        @preview="handlePreview"
      >
        <el-button :disabled="tableData[0].status !== 'wg'" type="primary">上传完井报告</el-button>
        <template #tip>
          <div class="el-upload__tip">文件大小不能超过50MB </div>
        </template>
      </el-upload>
    </el-form-item>
  </div>

  <ContentWrap v-if="currentTask.platformWell === '1'">
    <h3 style="margin-bottom: 20px">平台井</h3>
    <el-table :data="currentTask.platformWellDetails" style="width: 100%">
      <el-table-column prop="wellName" label="井号">
        <template #default="{ $index }">
          <el-input
            v-model="currentTask.platformWellDetails[$index].wellName"
            placeholder="请输入井号"
          />
        </template>
      </el-table-column>

      <el-table-column label="工作量单位" width="150">
        <template #default="{ $index }">
          <el-select
            v-model="currentTask.platformWellDetails[$index].workloadUnit"
            placeholder="请选择工作量单位"
            clearable
            :disabled="
              getPlatformWellWorkloadUnits($index).includes(
                currentTask.platformWellDetails[$index].workloadUnit
              ) && currentTask.platformWellDetails[$index].workloadUnit !== currentTask.workloadUnit
            "
          >
            <el-option
              v-for="dict in getStrDictOptions(DICT_TYPE.PMS_PROJECT_WORKLOAD_UNIT)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
              :disabled="
                getPlatformWellWorkloadUnits($index).includes(dict.value) &&
                dict.value !== currentTask.platformWellDetails[$index].workloadUnit
              "
            />
          </el-select>
        </template>
      </el-table-column>

      <el-table-column prop="workloadDesign" label="设计工作量">
        <template #default="{ $index }">
          <el-tooltip
            :disabled="!currentTask.platformWellDetails[$index].workloadDesignError"
            :content="currentTask.platformWellDetails[$index].workloadDesignError"
            placement="top"
            effect="light"
            popper-class="workload-design-tooltip"
            :show-after="0"
          >
            <el-input
              v-model="currentTask.platformWellDetails[$index].workloadDesign"
              placeholder="请输入设计工作量"
              class="workload-input-with-button"
              :class="{
                'error-input': currentTask.platformWellDetails[$index].workloadDesignError
              }"
              @blur="validatePlatformWellWorkloadDesign($index)"
            >
              <template #append>
                <el-tooltip content="添加多个设计工作量" placement="top">
                  <el-button
                    class="workload-add-btn"
                    @click="openPlatformWellWorkloadDialog($index)"
                    :disabled="getPlatformWellWorkloadAddBtnDisabled($index)"
                  >
                    <span class="btn-text">+</span>
                  </el-button>
                </el-tooltip>
              </template>
            </el-input>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="100">
        <template #default="{ $index }">
          <el-button
            type="danger"
            size="small"
            @click="removePlatformWellDetail($index)"
            :disabled="isMainPlatformWellDetail($index)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-button type="primary" size="small" @click="addPlatformWellDetail" style="margin-top: 10px">
      添加一行
    </el-button>
  </ContentWrap>

  <ContentWrap>
    <el-form style="float: right">
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </el-form>
  </ContentWrap>

  <!-- 设备选择对话框 -->
  <el-dialog
    v-model="deviceDialogVisible"
    title="选择施工设备"
    width="1000px"
    :before-close="handleDeviceDialogClose"
    class="device-select-dialog"
  >
    <div class="transfer-container">
      <el-transfer
        v-model="selectedDeviceIds"
        :data="filteredDeviceList"
        :titles="['可选设备', '已选设备']"
        :props="{ key: 'id', label: 'deviceCode' }"
        filterable
        :filter-method="filterDeviceMethod"
        class="transfer-component"
        @change="handleTransferChange"
      >
        <template #default="{ option }">
          <el-tooltip
            effect="dark"
            placement="top"
            :content="`${option.deviceCode || ''} - ${option.deviceName || ''}`"
            :disabled="!option.deviceCode && !option.deviceName"
            transition="fade-in-linear"
          >
            <span class="transfer-option-text">
              {{ option.deviceCode }} - {{ option.deviceName }}
            </span>
          </el-tooltip>
        </template>
      </el-transfer>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleDeviceDialogClose">取消</el-button>
        <el-button type="primary" @click="confirmDeviceSelection">确定</el-button>
      </span>
    </template>
  </el-dialog>

  <!-- 责任人选择对话框 -->
  <el-dialog
    v-model="responsiblePersonDialogVisible"
    title="选择责任人"
    width="1000px"
    :before-close="handleResponsiblePersonDialogClose"
    class="responsible-person-select-dialog"
  >
    <div class="transfer-container">
      <el-transfer
        v-model="selectedResponsiblePersonIds"
        :data="responsiblePersonList"
        :titles="['可选人员', '已选人员']"
        :props="{ key: 'id', label: 'nickname' }"
        filterable
        class="transfer-component"
      >
        <template #default="{ option }">
          <el-tooltip
            effect="dark"
            placement="top"
            :content="`${option.nickname} - ${option.deptName || '未分配部门'}`"
          >
            <span class="transfer-option-text">
              {{ option.nickname }} - {{ option.deptName || '未分配部门' }}
            </span>
          </el-tooltip>
        </template>
      </el-transfer>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleResponsiblePersonDialogClose">取消</el-button>
        <el-button type="primary" @click="confirmResponsiblePersonSelection">确定</el-button>
      </span>
    </template>
  </el-dialog>

  <!-- 填报人选择对话框 -->
  <el-dialog
    v-model="submitterDialogVisible"
    title="选择填报人"
    width="1000px"
    :before-close="handleSubmitterDialogClose"
    class="responsible-person-select-dialog"
  >
    <div class="transfer-container">
      <el-transfer
        v-model="selectedSubmitterIds"
        :data="submitterList"
        :titles="['可选人员', '已选人员']"
        :props="{ key: 'id', label: 'nickname' }"
        filterable
        class="transfer-component"
      >
        <template #default="{ option }">
          <el-tooltip
            effect="dark"
            placement="top"
            :content="`${option.nickname} - ${option.deptName || '未分配部门'}`"
          >
            <span class="transfer-option-text">
              {{ option.nickname }} - {{ option.deptName || '未分配部门' }}
            </span>
          </el-tooltip>
        </template>
      </el-transfer>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleSubmitterDialogClose">取消</el-button>
        <el-button type="primary" @click="confirmSubmitterSelection">确定</el-button>
      </span>
    </template>
  </el-dialog>

  <!-- 工作量维护对话框 -->
  <el-dialog v-model="workloadDialogVisible" title="设计工作量" width="600px">
    <el-table :data="workloadList" style="width: 100%">
      <el-table-column prop="workloadUnit" label="工作量单位" width="200">
        <template #default="{ $index }">
          <el-select
            v-model="workloadList[$index].workloadUnit"
            placeholder="请选择工作量单位"
            :disabled="$index === 0 && hasInitialWorkload"
          >
            <el-option
              v-for="dict in getStrDictOptions(DICT_TYPE.PMS_PROJECT_WORKLOAD_UNIT)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
              :disabled="getSelectedWorkloadUnits($index).includes(dict.value)"
            />
          </el-select>
        </template>
      </el-table-column>

      <el-table-column prop="workloadDesign" label="工作量">
        <template #default="{ $index }">
          <el-tooltip
            :disabled="!workloadList[$index].workloadDesignError"
            :content="workloadList[$index].workloadDesignError"
            placement="top"
            effect="light"
            popper-class="workload-design-tooltip"
            :show-after="0"
          >
            <el-input
              v-model="workloadList[$index].workloadDesign"
              placeholder="请输入工作量"
              :disabled="$index === 0 && hasInitialWorkload"
              :class="{ 'error-input': workloadList[$index].workloadDesignError }"
              @blur="validateWorkloadDialogDesign($index)"
            />
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="80">
        <template #default="{ $index }">
          <el-button
            type="danger"
            size="small"
            @click="removeWorkloadItem($index)"
            :disabled="$index === 0 && hasInitialWorkload"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-button type="primary" size="small" @click="addWorkloadItem" style="margin-top: 10px">
      添加一行
    </el-button>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="workloadDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmWorkloadSelection">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
// 导入部分保持不变，与原始代码相同
import { IotProjectInfoApi, IotProjectInfoVO } from '@/api/pms/iotprojectinfo'
import { handleTree } from '@/utils/tree'
import * as DeptApi from '@/api/system/dept'
import { ref, reactive, computed, onMounted, watch } from 'vue'
import { useUserStore } from '@/store/modules/user'
import { IotProjectTaskApi, IotProjectTaskVO } from '@/api/pms/iotprojecttask'
import { ElMessage } from 'element-plus'
import { useTagsViewStore } from '@/store/modules/tagsView'
import { IotDeviceApi, IotDeviceVO } from '@/api/pms/device'
import * as UserApi from '@/api/system/user'
import { IotProjectTaskAttrsApi } from '@/api/pms/iotprojecttaskattrs'
import { DICT_TYPE, getStrDictOptions, getDictLabel } from '@/utils/dict'
import { IotOpeationFillApi } from '@/api/pms/iotopeationfill'
import { Base64 } from 'js-base64'
// 在导入部分添加Plus图标
// import { Plus } from '@element-plus/icons-vue'

const { query, params, name } = useRoute() // 查询参数
const id = params.id

const uploadUrl = import.meta.env.VITE_BASE_URL + '/admin-api/rq/file/upload'
const constructionFiles = ref<any>([])
const geologicalFiles = ref<any>([])
const completionFiles = ref<any>([])

// 修改projectId获取逻辑：优先使用params.projectId，其次使用query.projectId
const projectId = ref<string>('')
// 获取projectId的逻辑
if (params.projectId) {
  projectId.value = Array.isArray(params.projectId) ? params.projectId[0] : params.projectId
} else if (query.projectId) {
  projectId.value = Array.isArray(query.projectId as any)
    ? query.projectId[0]
    : (query.projectId as string)
}

function handlePreview(file: any) {
  if (!file.response) {
    message.error('附件路径不存在')
    return
  }

  try {
    const filePath = file.response.data.filePath ?? file.response.data.files[0].filePath
    const encodedPath = encodeURIComponent(Base64.encode(filePath))

    window.open(`http://doc.deepoil.cc:8012/onlinePreview?url=${encodedPath}`)
  } catch (error) {
    console.error('预览附件失败:', error)
    message.error('预览附件失败')
  }
}

// 施工队伍 选择树 响应式变量
const defaultExpandedKeys = ref<number[]>([]) // 默认展开的部门节点keys
const treeSelectRef = ref() // 树选择组件的引用

const { delView } = useTagsViewStore() // 视图操作
const { currentRoute, push } = useRouter()
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const deptList = ref<Tree[]>([]) // 树形结构
const deviceList = ref<IotDeviceVO[]>([]) // 设备列表
// 设备映射表（ID->设备对象）
const deviceMap = ref<Record<number, IotDeviceVO>>({})

const dialogVisible = ref(true) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const taskList = ref<IotProjectTaskVO[]>([]) // 列表的数据
const projectList = ref<IotProjectInfoVO[]>([])
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const loading = ref(true) // 列表的加载中

// 设备选择相关变量
const deviceDialogVisible = ref(false)
const filteredDeviceList = ref<IotDeviceVO[]>([]) // 过滤后的设备列表
const selectedDeviceIds = ref<number[]>([]) // 选中的设备ID

// 责任人相关变量
const responsiblePersonDialogVisible = ref(false)
const responsiblePersonList = ref([]) // 所有责任人列表
const selectedResponsiblePersonIds = ref([]) // 选中的责任人ID
const currentEditingRowForResponsible = ref(null) // 当前正在编辑责任人的行

const submitterDialogVisible = ref(false)
const submitterList = ref([]) // 所有填报人列表
const selectedSubmitterIds = ref([]) // 选中的日报填报人ID

// 动态属性相关变量
const dynamicAttrs = ref<any[]>([]) // 存储动态属性列表

// 跟踪是否已从任务数据中获取动态属性
const hasDynamicAttrsFromTask = ref(false)

// 添加 workAreaOptions 引用和加载状态
const workAreaOptions = ref<any[]>([])
const loadingWorkAreaOptions = ref(false)
const workAreaAutocomplete = ref() // 添加对el-autocomplete的引用
// currentDictLabel 响应式变量
const currentDictLabel = ref('') // 存储当前项目对应的施工区域字典类型

// 当前正在操作的平台井索引
const currentPlatformWellIndex = ref(-1)

// 施工工艺相关变量
const technologyOptions = ref<any[]>([])
const loadingTechnologyOptions = ref(false)
const currentTechnologyDictLabel = ref('') // 存储当前项目对应的施工工艺字典类型

// 添加一个变量来保存平台井详情数据
const platformWellDetailsBackup = ref([])

// 存储井号到工作量列表的映射（key: wellName, value: Array<{workloadUnit, workloadDesign}>）
const workloadMap = ref(new Map())

// 工作量数据备份
const workloadBackup = ref<Map<string, any[]>>(new Map())

// 工作量维护相关变量
const workloadDialogVisible = ref(false)
const workloadList = ref<any[]>([])
const hasInitialWorkload = ref(false)

// 跟踪设备列表是否为空
const hasDevicesAvailable = ref(false) // 默认假设有设备，等接口返回后更新

// 设计工作量错误信息
const workloadDesignError = ref('')

/** 项目信息 表单 */
defineOptions({ name: 'IotProjectTaskInfo' })

const formData = ref({
  id: undefined,
  contractId: undefined,
  contractName: undefined,
  contractCode: undefined,
  workloadTotal: undefined,
  workloadFinish: undefined,
  workloadUnit: undefined,
  startTime: undefined,
  endTime: undefined,
  location: undefined,
  technique: undefined,
  payment: undefined,
  remark: undefined,
  manufactureName: undefined,
  manufacturerId: undefined,
  userName: undefined,
  userId: undefined,
  deptId: undefined // 新增项目部门ID字段
})

const close = () => {
  delView(unref(currentRoute))
  push({ name: 'IotProjectTask', params: {} })
}

// 添加计算属性来判断是否为特定部门
const isSpecialDept = computed(() => {
  return formData.value.deptId === 163
})

// 定义新的树选择器配置，添加禁用逻辑
const treeSelectProps = {
  children: 'children',
  label: 'name',
  disabled: (data: any) => {
    // 只有叶子节点（type = 3）可以选择，非叶子节点禁用
    return data.type !== '3'
  }
}

const getProjectInfo = async (contractId: number) => {
  const project = projectList.value.find((item) => item.id === contractId)
  if (project) {
    formData.value.contractName = project.contractName
    formData.value.contractCode = project.contractCode
    formData.value.payment = project.payment
    formData.value.workloadTotal = project.workloadTotal
    formData.value.startTime = project.startTime
    formData.value.endTime = project.endTime
    formData.value.remark = project.remark
    formData.value.manufactureName = project.manufactureName
    formData.value.manufacturerId = project.manufacturerId
    formData.value.id = project.id
    formData.value.deptId = project.deptId // 保存项目部门ID
    // 获取施工区域数据字典集合
    await loadWorkAreaOptions(formData.value.deptId)
    // 获取施工工艺数据字典集合
    await loadTechnologyOptions(formData.value.deptId)
    // 获取动态属性
    await fetchDynamicAttrs()
  }
}

const queryParams = reactive({
  projectId: undefined,
  id: undefined
})
const formRules = reactive({
  contractId: [{ required: true, message: '合同名称不能为空', trigger: 'blur' }],
  manufacturerId: [{ required: true, message: '客户不能为空', trigger: 'blur' }],
  payment: [{ required: true, message: '结算方式不能为空', trigger: 'blur' }],
  // workloadTotal: [{ required: true, message: '工作量总数不能为空', trigger: 'blur' }],
  startTime: [{ required: true, message: '开始时间不能为空', trigger: 'blur' }],
  endTime: [{ required: true, message: '结束时间不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

const zzClear = () => {
  formData.value.manufacturerId = undefined
  formData.value.manufactureName = undefined
}

// 根据部门ID获取部门名称
const getDeptNames = (deptIds) => {
  if (!deptIds || deptIds.length === 0) return ''

  const names = []
  const findDept = (list, id) => {
    for (const item of list) {
      if (item.id === id) {
        names.push(item.name)
        return true
      }
      if (item.children && findDept(item.children, id)) {
        return true
      }
    }
    return false
  }

  deptIds.forEach((id) => findDept(deptList.value, id))
  if (names.length > 1) {
    return `${names[0]}...`
  }
  return names.join(', ')
}

const getAllDeptNames = (deptIds) => {
  if (!deptIds || deptIds.length === 0) return '无施工队伍'

  const names = []
  const findDept = (list, id) => {
    for (const item of list) {
      if (item.id === id) {
        names.push(item.name)
        return true
      }
      if (item.children && findDept(item.children, id)) {
        return true
      }
    }
    return false
  }

  deptIds.forEach((id) => findDept(deptList.value, id))

  return names.join(', ') || '无有效施工队伍'
}

const getDeviceNames = (deviceIds: number[]) => {
  if (!deviceIds || deviceIds.length === 0) return ''

  // 获取所有有效设备名称
  const deviceNames = deviceIds
    .map((id) => deviceMap.value[id]?.deviceCode)
    .filter((name) => name !== undefined && name !== '')

  if (deviceNames.length === 0) return ''

  // 如果设备数量超过2个，显示前两个加省略号
  if (deviceNames.length > 2) {
    return `${deviceNames[0]}, ${deviceNames[1]}...`
  }

  // 设备数量不超过2个，正常显示所有
  return deviceNames.join(', ')
}

const initDynamicAttrsToCurrentTask = () => {
  dynamicAttrs.value.forEach((attr) => {
    if (!currentTask.value.hasOwnProperty(attr.identifier)) {
      // 如果是从任务数据中获取的属性，并且有实际值，使用实际值
      if (
        hasDynamicAttrsFromTask.value &&
        attr.actualValue !== undefined &&
        attr.actualValue !== null &&
        attr.actualValue !== ''
      ) {
        currentTask.value[attr.identifier] = attr.actualValue
      } else {
        // 否则使用默认值
        currentTask.value[attr.identifier] = attr.defaultValue || ''
      }
    }
  })
}

// 当前编辑的任务
const currentTask = ref({
  id: undefined,
  wellName: '',
  wellType: '',
  wellCategory: '',
  location: '',
  dictType: '',
  technique: '',
  workloadDesign: '',
  workloadUnit: '',
  deptIds: [],
  deviceIds: [],
  responsiblePerson: [],
  submitter: [],
  remark: '',
  projectId: '',
  platformWell: '0', // 新增平台井字段，默认非平台井
  platformWellDetails: [] // 新增平台井详情列表
})

const isNewTask = ref(false) // 是否是新任务
const taskFormRef = ref() // 任务表单ref
const currentEditingIndex = ref(-1) // 当前编辑的行索引

// 任务表单验证规则
const taskFormRules = computed(() => {
  const rules = {
    wellName: [
      { required: currentTask.value.platformWell !== '1', message: '井号不能为空', trigger: 'blur' }
    ],
    wellType: [{ required: true, message: '井型不能为空', trigger: 'change' }],
    location: [{ required: true, message: '施工地点不能为空', trigger: 'blur' }],
    technique: [{ required: true, message: '施工工艺不能为空', trigger: 'change' }],
    workloadDesign: [
      {
        required: currentTask.value.platformWell !== '1',
        message: '设计工作量不能为空',
        trigger: 'blur'
      },
      {
        validator: (rule, value, callback) => {
          if (currentTask.value.platformWell === '1') {
            callback()
            return
          }

          if (value === '' || value === null || value === undefined) {
            callback(new Error('设计工作量不能为空'))
            return
          }

          const numValue = Number(value)
          if (isNaN(numValue) || numValue < 0) {
            callback(new Error('请填写大于0的数字'))
          } else {
            callback()
          }
        },
        trigger: 'blur'
      }
    ],
    workloadUnit: [
      {
        required: currentTask.value.platformWell !== '1',
        message: '工作量单位不能为空',
        trigger: 'change'
      }
    ],
    deptIds: [{ required: true, message: '施工队伍不能为空', trigger: 'change' }],
    deviceIds: [
      { required: hasDevicesAvailable.value, message: '施工设备不能为空', trigger: 'change' }
    ],
    responsiblePerson: [
      {
        required: true,
        message: isSpecialDept.value ? '带班干部不能为空' : '责任人不能为空',
        trigger: 'change',
        validator: (rule, value, callback) => {
          if (!value || value.length === 0) {
            callback(new Error(isSpecialDept.value ? '带班干部不能为空' : '责任人不能为空'))
          } else {
            callback()
          }
        }
      }
    ],
    submitter: [
      {
        required: isSpecialDept.value,
        message: '填报人不能为空',
        trigger: 'change',
        validator: (rule, value, callback) => {
          if (isSpecialDept.value && (!value || value.length === 0)) {
            callback(new Error('填报人不能为空'))
          } else {
            callback()
          }
        }
      }
    ]
  }

  return rules
})

// 动态属性验证规则
const getDynamicAttrRules = (attr) => {
  const rules = []
  if (attr.required === 1) {
    rules.push({ required: true, message: `${attr.name}不能为空`, trigger: 'blur' })
  }

  // 数字类型验证
  if (attr.dataType === 'double') {
    rules.push({
      validator: (rule, value, callback) => {
        if (value === '' || value === null || value === undefined) {
          callback()
          return
        }

        const numValue = Number(value)
        if (isNaN(numValue)) {
          callback(new Error(`${attr.name}必须是数字`))
        } else if (attr.minValue !== null && numValue < Number(attr.minValue)) {
          // callback(new Error(`${attr.name}不能小于${attr.minValue}`));
        } else if (attr.maxValue !== null && numValue > Number(attr.maxValue)) {
          // callback(new Error(`${attr.name}不能大于${attr.maxValue}`));
        } else {
          callback()
        }
      },
      trigger: 'blur'
    })
  }

  return rules
}

const tableData = ref([])

// 打开责任人选择对话框（用于表单）
const openResponsiblePersonDialogForForm = async () => {
  if (!currentTask.value.deptIds || currentTask.value.deptIds.length === 0) {
    ElMessage.warning('请先选择施工队伍')
    return
  }

  selectedResponsiblePersonIds.value = [...(currentTask.value.responsiblePerson || [])]

  try {
    const params = {
      deptIds: currentTask.value.deptIds // 使用当前项目所属部门的deptId
    }
    const response = await UserApi.selectedDeptsEmployee(params)
    responsiblePersonList.value = response
    responsiblePersonDialogVisible.value = true
  } catch (error) {
    ElMessage.error('获取责任人列表失败')
    console.error('获取责任人列表失败:', error)
  }
}

// 打开工单人选择对话框（用于表单）
const openSubmitterDialogForForm = async () => {
  if (!currentTask.value.deptIds || currentTask.value.deptIds.length === 0) {
    ElMessage.warning('请先选择施工队伍')
    return
  }

  selectedSubmitterIds.value = [...(currentTask.value.submitter || [])]

  try {
    const params = {
      deptIds: currentTask.value.deptIds // 使用当前项目所属部门的deptId
    }
    const response = await UserApi.selectedDeptsEmployee(params)
    submitterList.value = response
    submitterDialogVisible.value = true
  } catch (error) {
    ElMessage.error('获取填报人列表失败')
    console.error('获取填报人列表失败:', error)
  }
}

// 获取动态属性
const fetchDynamicAttrs = async () => {
  // 如果已经通过任务数据获取了动态属性，则不再调用接口
  if (hasDynamicAttrsFromTask.value) {
    return
  }

  if (!formData.value.deptId) {
    console.warn('部门ID为空，无法获取动态属性')
    return
  }

  try {
    const queryParams = {
      deptId: formData.value.deptId
    }

    const response = await IotProjectTaskAttrsApi.getIotProjectTaskAttrsList(queryParams)
    dynamicAttrs.value = response || []

    // 初始化动态属性到当前任务
    initDynamicAttrsToCurrentTask()
  } catch (error) {
    console.error('获取动态属性失败:', error)
    ElMessage.error('获取动态属性失败')
  }
}

// 打开设备选择对话框（用于表单）
const openDeviceDialogForForm = async () => {
  if (!currentTask.value.deptIds || currentTask.value.deptIds.length === 0) {
    ElMessage.warning('请先选择施工队伍')
    return
  }

  selectedDeviceIds.value = [...(currentTask.value.deviceIds || [])]

  try {
    const params = {
      deptIds: currentTask.value.deptIds
    }
    const data = await IotDeviceApi.getDevicesByDepts(params)

    // 更新 hasDevicesAvailable 状态
    hasDevicesAvailable.value = data && data.length > 0

    // 更新设备映射表
    const newDeviceMap = { ...deviceMap.value }
    data.forEach((device) => {
      newDeviceMap[device.id] = device
    })
    deviceMap.value = newDeviceMap

    filteredDeviceList.value = data
    deviceDialogVisible.value = true

    // 如果没有设备可用，显示提示信息
    if (!hasDevicesAvailable.value) {
      ElMessage.info('当前施工队伍下没有可用设备，设备字段为非必填项')
    }
  } catch (error) {
    ElMessage.error('获取设备列表失败')
    console.error('获取设备列表失败:', error)
    // 如果获取失败，也设为非必填，避免用户无法提交
    hasDevicesAvailable.value = false
  }
}

// 打开工作量维护对话框
const openWorkloadDialog = () => {
  currentPlatformWellIndex.value = -1 // 设置为-1表示普通井模式

  // 前置校验：设计工作量不合法则直接返回
  if (!validateWorkloadDesign()) {
    ElMessage.error(workloadDesignError.value)
    return
  }

  // 重置列表
  workloadList.value = []
  hasInitialWorkload.value = false

  // 获取当前操作的井号（兼容普通井/平台井）
  let currentWellName = ''
  if (currentTask.value.platformWell === '1') {
    // 平台井：取主井号（优先当前任务wellName，其次取平台井详情第一个）
    currentWellName =
      currentTask.value.wellName ||
      (currentTask.value.platformWellDetails.length > 0
        ? currentTask.value.platformWellDetails[0].wellName
        : '')
  } else {
    // 普通井：直接取当前任务井号
    currentWellName = currentTask.value.wellName
  }

  if (!currentWellName) {
    ElMessage.warning('请先填写井号')
    return
  }

  // 从Map中回显当前井号的工作量数据
  if (workloadMap.value.has(currentWellName)) {
    // 深拷贝避免原数据被修改
    workloadList.value = JSON.parse(JSON.stringify(workloadMap.value.get(currentWellName)))
    hasInitialWorkload.value = workloadList.value.length > 0
  } else {
    // 无历史数据时，沿用原有逻辑（取表单已有值初始化首行）
    if (currentTask.value.workloadUnit && currentTask.value.workloadDesign) {
      workloadList.value.push({
        workloadUnit: currentTask.value.workloadUnit,
        workloadDesign: currentTask.value.workloadDesign
      })
      hasInitialWorkload.value = true
    }
  }

  workloadDialogVisible.value = true
}

// 添加工作量项
const addWorkloadItem = () => {
  workloadList.value.push({
    workloadUnit: '',
    workloadDesign: ''
  })
}

// 删除工作量项
const removeWorkloadItem = (index: number) => {
  workloadList.value.splice(index, 1)
}

// 确认工作量选择
const confirmWorkloadSelection = () => {
  // 验证工作量对话框中的所有数据
  let allValid = true

  workloadList.value.forEach((item, index) => {
    if (!validateWorkloadDialogDesign(index)) {
      allValid = false
    }
  })

  if (!allValid) {
    ElMessage.error('请完善所有工作量信息')
    return
  }

  // 验证工作量单位唯一性
  const unitList = workloadList.value.map((item) => item.workloadUnit).filter(Boolean)
  const uniqueUnits = new Set(unitList)
  if (unitList.length !== uniqueUnits.size) {
    ElMessage.error('工作量单位不能重复，请选择不同的单位')
    return
  }

  // 获取当前井号并存储到Map
  let currentWellName = ''

  if (currentPlatformWellIndex.value >= 0) {
    // 平台井模式
    const wellDetail = currentTask.value.platformWellDetails[currentPlatformWellIndex.value]
    currentWellName = wellDetail.wellName

    // 同步更新平台井表格中的工作量单位
    if (workloadList.value.length > 0) {
      const firstItem = workloadList.value[0]
      if (wellDetail.workloadUnit !== firstItem.workloadUnit) {
        wellDetail.workloadUnit = firstItem.workloadUnit
      }
      wellDetail.workloadDesign = firstItem.workloadDesign
    }
  } else {
    // 普通井模式
    currentWellName = currentTask.value.wellName

    // 同步更新表单中的工作量单位
    if (workloadList.value.length > 0) {
      const firstItem = workloadList.value[0]
      if (currentTask.value.workloadUnit !== firstItem.workloadUnit) {
        currentTask.value.workloadUnit = firstItem.workloadUnit
      }
      currentTask.value.workloadDesign = firstItem.workloadDesign
    }
  }

  // 存储到Map
  if (currentWellName) {
    workloadMap.value.set(currentWellName, JSON.parse(JSON.stringify(workloadList.value)))
  }

  workloadDialogVisible.value = false
  currentPlatformWellIndex.value = -1 // 重置索引
  ElMessage.success('工作量信息已更新')
}

// 处理穿梭框变化
const handleTransferChange = (value, direction, movedKeys) => {
  // 可以添加额外的处理逻辑
}

// 确认设备选择（用于表单）
const confirmDeviceSelection = () => {
  currentTask.value.deviceIds = [...selectedDeviceIds.value]
  // 强制更新视图
  currentTask.value = { ...currentTask.value }
  deviceDialogVisible.value = false
}

// 确认责任人选择（用于表单）
const confirmResponsiblePersonSelection = () => {
  currentTask.value.responsiblePerson = [...selectedResponsiblePersonIds.value]
  responsiblePersonDialogVisible.value = false

  // 立即触发责任人字段验证
  nextTick(() => {
    taskFormRef.value.validateField('responsiblePerson')
  })
}

// 确认填报人选择（用于表单）
const confirmSubmitterSelection = () => {
  currentTask.value.submitter = [...selectedSubmitterIds.value]
  submitterDialogVisible.value = false

  // 立即触发填报人字段验证
  nextTick(() => {
    taskFormRef.value.validateField('submitter')
  })
}

// 关闭设备选择对话框
const handleDeviceDialogClose = () => {
  deviceDialogVisible.value = false
}

// 关闭责任人选择对话框
const handleResponsiblePersonDialogClose = () => {
  responsiblePersonDialogVisible.value = false
}

// 关闭填报人选择对话框
const handleSubmitterDialogClose = () => {
  submitterDialogVisible.value = false
}

// 获取井型标签的方法
const getWellTypeLabel = (value) => {
  const options = getStrDictOptions(DICT_TYPE.PMS_PROJECT_WELL_TYPE)
  const option = options.find((opt) => opt.value === value)
  return option ? option.label : value
}

// 获取井别标签的方法
const getWellCategoryLabel = (value) => {
  const options = getStrDictOptions(DICT_TYPE.PMS_PROJECT_WELL_CATEGORY)
  const option = options.find((opt) => opt.value === value)
  return option ? option.label : value
}

// 获取施工工艺标签的方法
const getTechniqueLabel = (value) => {
  const options = getStrDictOptions(DICT_TYPE.PMS_PROJECT_TECHNOLOGY)
  const option = options.find((opt) => opt.value === value)
  return option ? option.label : value
}

// 获取 工作量单位 标签的方法
const getWorkloadUnitLabel = (value) => {
  const options = getStrDictOptions(DICT_TYPE.PMS_PROJECT_WORKLOAD_UNIT)
  const option = options.find((opt) => opt.value === value)
  return option ? option.label : value
}

// 打开平台井工作量对话框
const openPlatformWellWorkloadDialog = (index) => {
  currentPlatformWellIndex.value = index
  const wellDetail = currentTask.value.platformWellDetails[index]

  // 前置校验
  if (!validatePlatformWellWorkloadDesign(index)) {
    ElMessage.error(wellDetail.workloadDesignError)
    return
  }

  // 重置列表
  workloadList.value = []
  hasInitialWorkload.value = false

  if (!wellDetail.wellName) {
    ElMessage.warning('请先填写井号')
    return
  }

  // 从Map中回显工作量数据
  if (workloadMap.value.has(wellDetail.wellName)) {
    workloadList.value = JSON.parse(JSON.stringify(workloadMap.value.get(wellDetail.wellName)))
    hasInitialWorkload.value = workloadList.value.length > 0
  } else {
    // 无历史数据时，使用当前行的值初始化
    if (wellDetail.workloadUnit && wellDetail.workloadDesign) {
      workloadList.value.push({
        workloadUnit: wellDetail.workloadUnit,
        workloadDesign: wellDetail.workloadDesign
      })
      hasInitialWorkload.value = true
    }
  }

  workloadDialogVisible.value = true
}

// 校验平台井设计工作量
const validatePlatformWellWorkloadDesign = (index) => {
  const wellDetail = currentTask.value.platformWellDetails[index]
  wellDetail.workloadDesignError = ''

  const value = wellDetail.workloadDesign
  if (value === '' || value === null || value === undefined) {
    wellDetail.workloadDesignError = '设计工作量不能为空'
    return false
  }

  const numValue = Number(value)
  if (isNaN(numValue) || numValue < 0) {
    wellDetail.workloadDesignError = '请填写大于0的数字'
    return false
  }

  return true
}

/** 同步当前任务表单数据到表格数据 */
const syncCurrentTaskToTable = () => {
  // 平台井模式特殊处理
  if (currentTask.value.platformWell === '1') {
    if (currentTask.value.platformWellDetails.length === 0) {
      ElMessage.warning('请先添加平台井详情')
      return false
    }

    // 验证平台井详情数据
    const invalidDetails = currentTask.value.platformWellDetails.some(
      (detail) => !detail.wellName || !detail.wellName.trim() || !detail.workloadDesign
    )

    if (invalidDetails) {
      ElMessage.error('请完善平台井的井号和设计工作量信息')
      return false
    }

    // 平台井模式：为每个平台井详情生成任务对象
    const platformTasks = []

    // 查找当前编辑的主任务对应的平台井详情
    let mainTaskDetailIndex = -1

    // 如果有当前任务ID，查找对应的平台井详情
    if (currentTask.value.id) {
      mainTaskDetailIndex = currentTask.value.platformWellDetails.findIndex(
        (detail) => detail.taskId === currentTask.value.id
      )
    }

    // 如果没有找到对应的详情，使用第一个详情作为主任务
    if (mainTaskDetailIndex === -1 && currentTask.value.platformWellDetails.length > 0) {
      mainTaskDetailIndex = 0
    }

    // 处理每个平台井详情
    currentTask.value.platformWellDetails.forEach((detail, index) => {
      // 获取当前井号的工作量数据
      const workloadData = workloadMap.value.get(detail.wellName) || []

      if (index === mainTaskDetailIndex) {
        // 主任务：使用当前任务对象
        currentTask.value.wellName = detail.wellName
        currentTask.value.workloadDesign = detail.workloadDesign
        currentTask.value.workloadUnit = detail.workloadUnit
        // platformWell 保持为 '1' - 主平台井任务
        currentTask.value.platformWell = '1'

        platformTasks.push({ ...currentTask.value })
      } else {
        // 子任务：创建新的任务对象
        const newTask = {
          ...currentTask.value, // 拷贝所有属性
          id: detail.taskId || undefined, // 已有任务使用taskId，新增任务为undefined
          wellName: detail.wellName,
          workloadDesign: detail.workloadDesign,
          workloadUnit: detail.workloadUnit,
          platformWell: detail.taskId ? '2' : '2' // 子任务platformWell设为2
        }

        // 移除不需要的属性
        delete newTask.platformWellDetails

        platformTasks.push(newTask)
      }
    })

    // 设置项目ID
    platformTasks.forEach((task) => {
      task.projectId = formData.value.id
    })

    if (isNewTask.value) {
      // 新增任务 - 为所有任务生成ID
      let maxId =
        tableData.value.length > 0 ? Math.max(...tableData.value.map((item) => item.id)) : 0

      platformTasks.forEach((task) => {
        maxId += 1
        task.id = maxId
        tableData.value.unshift({ ...task })
      })
      isNewTask.value = false
    } else {
      // 更新现有任务 - 比较复杂，需要先删除原有任务再添加新任务
      // 这里简化处理：删除当前任务相关的所有平台井任务，然后重新添加
      const currentTaskId = currentTask.value.id
      tableData.value = tableData.value.filter((item) => item.id !== currentTaskId)

      platformTasks.forEach((task) => {
        tableData.value.unshift({ ...task })
      })
    }

    return true
  } else {
    // 非平台井模式，验证原有字段
    if (!currentTask.value.wellName) {
      ElMessage.warning('请先填写任务详情')
      return false
    }

    // 设置项目ID
    currentTask.value.projectId = formData.value.id

    if (isNewTask.value) {
      // 新增任务 - 生成唯一ID并添加到表格
      const newId =
        tableData.value.length > 0 ? Math.max(...tableData.value.map((item) => item.id)) + 1 : 1
      currentTask.value.id = newId
      tableData.value.unshift({ ...currentTask.value })
      isNewTask.value = false
    } else {
      // 更新现有任务
      const index = tableData.value.findIndex((item) => item.id === currentTask.value.id)
      if (index !== -1) {
        tableData.value.splice(index, 1, { ...currentTask.value })
      } else {
        // 如果没有找到，添加到表格（可能是意外情况）
        tableData.value.unshift({ ...currentTask.value })
      }
    }

    return true
  }
}

// 重置任务表单
const resetTaskForm = () => {
  // 记录旧井号，用于清理Map
  const oldWellName = currentTask.value.wellName

  currentTask.value = {
    id: undefined,
    wellName: '',
    wellType: '',
    wellCategory: '',
    location: '',
    technique: '',
    workloadDesign: '',
    workloadUnit: '',
    deptIds: [],
    deviceIds: [],
    responsiblePerson: [],
    remark: '',
    projectId: formData.value.id,
    platformWell: '0', // 重置平台井状态
    platformWellDetails: [], // 清空平台井详情
    // 如果是特殊部门，初始化填报人字段
    ...(isSpecialDept.value ? { submitter: [] } : {})
  }

  // 清理当前井号对应的Map数据
  if (oldWellName) {
    if (workloadMap.value.has(oldWellName)) {
      workloadMap.value.delete(oldWellName)
    }
    if (workloadBackup.value.has(oldWellName)) {
      workloadBackup.value.delete(oldWellName)
    }
  }

  // 重新初始化动态属性
  initDynamicAttrsToCurrentTask()

  isNewTask.value = false
  taskFormRef.value?.resetFields()

  // 清除表单验证状态
  nextTick(() => {
    taskFormRef.value?.clearValidate()
  })
}

// 获取已选的工作量单位（排除当前行） ==========
const getSelectedWorkloadUnits = (currentIndex) => {
  if (!workloadList.value || workloadList.value.length === 0) {
    return []
  }
  // 过滤当前行 + 空值，收集已选的单位
  return workloadList.value
    .filter((_, index) => index !== currentIndex)
    .map((item) => item.workloadUnit)
    .filter((unit) => unit && unit.trim() !== '')
}

// 处理行点击事件
const handleRowClick = (row, index) => {
  if (row) {
    currentTask.value = { ...row }
    currentEditingIndex.value = index // 保存当前编辑的行索引
    isNewTask.value = false
  }
}

// 设备过滤方法
const filterDeviceMethod = (query: string, item: IotDeviceVO) => {
  if (!query) return true

  const searchText = query.toLowerCase()
  const deviceCode = (item.deviceCode || '').toLowerCase()
  const deviceName = (item.deviceName || '').toLowerCase()

  // 同时匹配设备编码和设备名称
  return deviceCode.includes(searchText) || deviceName.includes(searchText)
}

// 根据责任人ID获取责任人名称
const getResponsiblePersonNames = (responsiblePersonIds: number[]) => {
  if (!responsiblePersonIds || responsiblePersonIds.length === 0) return ''

  // 获取所有有效责任人名称
  const personNames = responsiblePersonIds
    .map((id) => {
      const person = responsiblePersonList.value.find((p) => p.id === id)
      return person ? person.nickname : ''
    })
    .filter((name) => name !== undefined && name !== '')

  if (personNames.length === 0) return ''

  // 如果责任人数量超过2个，显示前两个加省略号
  if (personNames.length > 2) {
    return `${personNames[0]}, ${personNames[1]}...`
  }

  // 责任人数量不超过2个，正常显示所有
  return personNames.join(', ')
}

// 获取所有责任人名称（用于tooltip提示）
const getAllResponsiblePersonNames = (responsiblePersonIds: number[]) => {
  if (!responsiblePersonIds || responsiblePersonIds.length === 0) return '无责任人'

  const personNames = responsiblePersonIds
    .map((id) => {
      const person = responsiblePersonList.value.find((p) => p.id === id)
      return person ? person.nickname : '未知人员'
    })
    .filter((name) => name !== '未知人员')

  return personNames.join(', ') || '无有效责任人'
}

// 获取所有设备名称（用于tooltip提示）
const getAllDeviceNames = (deviceIds: number[]) => {
  if (!deviceIds || deviceIds.length === 0) return '无设备'

  const deviceNames = deviceIds
    .map((id) => deviceMap.value[id]?.deviceCode || '未知设备')
    .filter((name) => name !== '未知设备')

  return deviceNames.join(', ') || '无有效设备'
}

const companyName = ref('')

/** 打开弹窗 */
const open = async () => {
  resetForm()
  resetTaskForm()
  hasDynamicAttrsFromTask.value = false // 重置标志

  // 修改时，设置数据
  if (id) {
    formType.value = 'update'
    formLoading.value = true
    try {
      queryParams.id = id
      const data = await IotProjectTaskApi.getIotProjectTaskPage(queryParams)
      tableData.value = data.list

      const company = await IotOpeationFillApi.getOrgName(tableData.value[0].deptId)
      companyName.value = company

      // 收集所有设备ID
      const allDeviceIds = new Set<number>()
      // 收集所有责任人ID
      const allResponsiblePersonIds = new Set<number>()
      // 收集所有填报人ID
      const allSubmitterIds = new Set<number>()

      data.list.forEach((item) => {
        if (item.deviceIds?.length) {
          item.deviceIds.forEach((id) => allDeviceIds.add(id))
        }
        if (item.responsiblePerson?.length) {
          item.responsiblePerson.forEach((id) => allResponsiblePersonIds.add(id))
        }
        if (item.submitter?.length) {
          item.submitter.forEach((id) => allSubmitterIds.add(id))
        }
      })

      // 批量获取设备信息
      if (allDeviceIds.size > 0) {
        const deviceIdsArray = Array.from(allDeviceIds)
        const devices = await IotDeviceApi.getDevicesByDepts({
          deviceIds: deviceIdsArray
        })

        // 更新设备列表和映射表
        deviceList.value = devices
        deviceMap.value = {}
        devices.forEach((device) => {
          deviceMap.value[device.id] = device
        })
      }

      // 批量获取责任人信息
      if (allResponsiblePersonIds.size > 0) {
        const personIdsArray = Array.from(allResponsiblePersonIds)
        const params = {
          userIds: personIdsArray
        }
        const persons = await UserApi.companyDeptsEmployee(params)

        // 更新责任人列表
        responsiblePersonList.value = persons
      }

      // 批量获取填报人信息
      if (allSubmitterIds.size > 0) {
        const personIdsArray = Array.from(allSubmitterIds)
        const params = {
          userIds: personIdsArray
        }
        const persons = await UserApi.companyDeptsEmployee(params)

        // 更新责任人列表
        submitterList.value = persons
      }

      if (tableData.value && tableData.value.length > 0) {
        // 使用深拷贝，避免修改表单时直接影响表格数据
        currentTask.value = JSON.parse(JSON.stringify(tableData.value[0]))
        isNewTask.value = false

        // 如果当前任务有施工队伍，立即查询设备并更新 hasDevicesAvailable
        if (currentTask.value.deptIds && currentTask.value.deptIds.length > 0) {
          try {
            const params = {
              deptIds: currentTask.value.deptIds
            }
            const deviceData = await IotDeviceApi.getDevicesByDepts(params)

            // 更新 hasDevicesAvailable 状态
            hasDevicesAvailable.value = deviceData && deviceData.length > 0

            // 更新设备列表和映射表（如果有设备）
            if (hasDevicesAvailable.value) {
              deviceList.value = deviceData
              deviceMap.value = {}
              deviceData.forEach((device) => {
                deviceMap.value[device.id] = device
              })
            }
          } catch (error) {
            console.error('初始化查询设备失败:', error)
            hasDevicesAvailable.value = false
          }
        } else {
          // 如果没有施工队伍，默认没有设备可用
          hasDevicesAvailable.value = false
        }

        // 处理平台井数据
        // 将数字类型的 platformWell 转换为字符串类型
        currentTask.value.platformWell = currentTask.value.platformWell?.toString() || '0'

        // 如果 platformWell 为 1，处理 platformWells 数据
        if (currentTask.value.platformWell === '1' && currentTask.value.platformWells) {
          // 将 platformWells 映射到 platformWellDetails
          currentTask.value.platformWellDetails = currentTask.value.platformWells.map(
            (item: any) => ({
              taskId: item.id,
              wellName: item.wellName,
              workloadUnit: item.workloadUnit || '', // 确保有workloadUnit字段
              workloadDesign: item.workloadDesign || '',
              workloadDesignError: '' // 新增错误信息字段
            })
          )

          // 使用专门的排序方法
          sortPlatformWellDetails()

          // 初始化备份数据
          platformWellDetailsBackup.value = [...currentTask.value.platformWellDetails]
          // 处理每个平台井的工作量数据
          processPlatformWellWorkloadData(currentTask.value.platformWells)
        } else {
          // 非平台井模式，确保 platformWellDetails 是数组
          currentTask.value.platformWellDetails = currentTask.value.platformWellDetails || []
          platformWellDetailsBackup.value = []

          // 处理非平台井的工作量数据
          if (currentTask.value.extProperty?.[0]?.dropdownList) {
            processWorkloadDataForWell(
              currentTask.value.wellName,
              currentTask.value.extProperty[0].dropdownList
            )
          }
        }

        // 检查任务数据中是否有extProperty
        if (tableData.value[0].extProperty && tableData.value[0].extProperty.length > 0) {
          // 使用任务数据中的extProperty作为动态属性
          dynamicAttrs.value = tableData.value[0].extProperty
          hasDynamicAttrsFromTask.value = true

          // 将extProperty中的值设置到当前任务
          tableData.value[0].extProperty.forEach((attr) => {
            if (attr.identifier) {
              // 优先使用actualValue，如果没有则使用defaultValue
              currentTask.value[attr.identifier] =
                attr.actualValue !== undefined &&
                attr.actualValue !== null &&
                attr.actualValue !== ''
                  ? attr.actualValue
                  : attr.defaultValue || ''
            }
          })
        }

        const attachments = tableData.value[0].attachments || []

        constructionFiles.value = attachments
          .filter((item) => item.type === 'CONSTRUCTION_DESIGN')
          .map((item) => ({
            name: item.filename,
            percentage: 100,
            response: { data: item },
            status: 'success',
            uid: item.createTime
          }))
        geologicalFiles.value = attachments
          .filter((item) => item.type === 'GEOLOGICAL_DESIGN')
          .map((item) => ({
            name: item.filename,
            percentage: 100,
            response: { data: item },
            status: 'success',
            uid: item.createTime
          }))
        completionFiles.value = attachments
          .filter((item) => item.type === 'COMPLETION_DESIGN')
          .map((item) => ({
            name: item.filename,
            percentage: 100,
            response: { data: item },
            status: 'success',
            uid: item.createTime
          }))
      }
    } finally {
      formLoading.value = false
    }
  } else {
    formType.value = 'create'
    // 初始化备份数据
    platformWellDetailsBackup.value = []
  }
  // 如果有projectId参数，自动选中对应的合同
  if (projectId.value) {
    autoSelectContract(projectId.value)
  }
}

defineExpose({ open }) // 提供 open 方法，用于打开弹窗

// 获取平台井已选的工作量单位（排除当前行）
const getPlatformWellWorkloadUnits = (currentIndex) => {
  if (
    !currentTask.value.platformWellDetails ||
    currentTask.value.platformWellDetails.length === 0
  ) {
    return []
  }

  // 获取当前井号的井名
  const currentWellName = currentTask.value.platformWellDetails[currentIndex]?.wellName
  if (!currentWellName) {
    return []
  }

  // 从workloadMap中获取当前井号的工作量列表
  const currentWellWorkloadList = workloadMap.value.get(currentWellName) || []

  return currentWellWorkloadList
    .filter((_, index) => index !== 0)
    .map((item) => item.workloadUnit)
    .filter((unit) => unit && unit.trim() !== '')
}

/** 自动选择合同 */
const autoSelectContract = async (projectId: string) => {
  console.log('项目id：' + projectId)
  // 查找匹配的合同
  const project = projectList.value.find((item) => item.id === Number(projectId))
  if (project) {
    // 设置选中值
    formData.value.contractId = project.id
    // 触发合同信息加载
    getProjectInfo(project.id)
  } else {
    ElMessage.warning('未找到指定的合同信息')
  }
}

/** 校验所有行数据 */
const validateAllRows = (): boolean => {
  // 判断任务列表是否为空
  if (!tableData.value || tableData.value.length === 0) {
    ElMessage.error('没有任务数据，无法保存')
    return false
  }

  let allValid = true

  tableData.value.forEach((row) => {
    if (!row.wellName || row.wellName.trim() === '') {
      allValid = false
    }

    // 对于平台井模式下的后续任务，platformWell为0，但wellName和workloadDesign仍需验证
    if (!row.workloadDesign) {
      allValid = false
    }

    if (!row.location || row.location.trim() === '') {
      allValid = false
    }

    if (!row.technique || row.technique.trim() === '') {
      allValid = false
    }

    if (!row.deptIds || row.deptIds.length === 0) {
      allValid = false
    }

    // 修改：只有当有设备可用时才验证设备字段
    if (hasDevicesAvailable.value && (!row.deviceIds || row.deviceIds.length === 0)) {
      allValid = false
    }

    if (!row.responsiblePerson || row.responsiblePerson.length === 0) {
      allValid = false
    }

    // 如果是特殊部门，验证填报人
    if (isSpecialDept.value && (!row.submitter || row.submitter.length === 0)) {
      allValid = false
    }
  })

  if (!allValid) {
    ElMessage.error('请检查表格中的数据，部分必填项未填写或格式不正确')
  }

  return allValid
}

// 添加表单专用的格式化方法
// 格式化设备显示（用于表单）
const formatDevicesForForm = (deviceIds: number[]) => {
  if (!deviceIds || deviceIds.length === 0) return ''

  const deviceInfos = deviceIds
    .map((id) => {
      const device = deviceMap.value[id]
      return device ? `${device.deviceCode}-${device.deviceName}` : ''
    })
    .filter((info) => info !== '')

  if (deviceInfos.length === 0) return ''

  if (deviceInfos.length > 2) {
    return `${deviceInfos[0]}, ${deviceInfos[1]}...`
  }

  return deviceInfos.join(', ')
}

// 获取所有设备名称（用于表单的tooltip）
const getAllDeviceNamesForForm = (deviceIds: number[]) => {
  if (!deviceIds || deviceIds.length === 0) return '无设备'

  const deviceInfos = deviceIds
    .map((id) => {
      const device = deviceMap.value[id]
      return device ? `${device.deviceCode}-${device.deviceName}` : '未知设备'
    })
    .filter((info) => info !== '未知设备')

  return deviceInfos.join(', ') || '无有效设备'
}

// 格式化责任人显示（用于表单）
const formatResponsiblePersonsForForm = (responsiblePersonIds: number[]) => {
  if (!responsiblePersonIds || responsiblePersonIds.length === 0) return ''

  const personNames = responsiblePersonIds
    .map((id) => {
      const person = responsiblePersonList.value.find((p) => p.id === id)
      return person ? person.nickname : ''
    })
    .filter((name) => name !== undefined && name !== '')

  if (personNames.length === 0) return ''

  if (personNames.length > 2) {
    return `${personNames[0]}, ${personNames[1]}...`
  }

  return personNames.join(', ')
}

// 获取任务的工作量dropdownList数据
const getWorkloadDropdownListForTask = (task: any) => {
  const dropdownList = []

  // 从workloadMap中获取当前任务井号的工作量数据
  const workloadItems = workloadMap.value.get(task.wellName) || []

  if (workloadItems.length > 0) {
    // 如果workloadMap中有数据，使用workloadMap的数据
    workloadItems.forEach((item) => {
      if (item.workloadUnit && item.workloadDesign) {
        // 获取工作量单位的标签
        const unitOptions = getStrDictOptions(DICT_TYPE.PMS_PROJECT_WORKLOAD_UNIT)
        const unitLabel =
          unitOptions.find((opt) => opt.value === item.workloadUnit)?.label || item.workloadUnit

        dropdownList.push({
          name: unitLabel, // 工作量单位名称
          value: item.workloadDesign // 工作量值
        })
      }
    })
  } else {
    // 【新增】如果workloadMap中没有数据，使用任务本身的工作量数据（直接从表格行中填写的数据）
    if (task.workloadUnit && task.workloadDesign) {
      const unitOptions = getStrDictOptions(DICT_TYPE.PMS_PROJECT_WORKLOAD_UNIT)
      const unitLabel =
        unitOptions.find((opt) => opt.value === task.workloadUnit)?.label || task.workloadUnit

      dropdownList.push({
        name: unitLabel,
        value: task.workloadDesign
      })
    }
  }

  return dropdownList
}

// 平台井工作量加号按钮禁用状态
const getPlatformWellWorkloadAddBtnDisabled = (index) => {
  const wellDetail = currentTask.value.platformWellDetails[index]
  return !wellDetail.workloadUnit || !!wellDetail.workloadDesignError
}

// 格式化填报人显示（用于表单）
const formatSubmittersForForm = (submitterIds: number[]) => {
  if (!submitterIds || submitterIds.length === 0) return ''

  const personNames = submitterIds
    .map((id) => {
      const person = submitterList.value.find((p) => p.id === id)
      return person ? person.nickname : ''
    })
    .filter((name) => name !== undefined && name !== '')

  if (personNames.length === 0) return ''

  if (personNames.length > 2) {
    return `${personNames[0]}, ${personNames[1]}...`
  }

  return personNames.join(', ')
}

// 获取所有责任人名称（用于表单的tooltip）
const getAllResponsiblePersonNamesForForm = (responsiblePersonIds: number[]) => {
  if (!responsiblePersonIds || responsiblePersonIds.length === 0) return '无责任人'

  const personNames = responsiblePersonIds
    .map((id) => {
      const person = responsiblePersonList.value.find((p) => p.id === id)
      return person ? person.nickname : '未知人员'
    })
    .filter((name) => name !== '未知人员')

  return personNames.join(', ') || '无有效责任人'
}

// 获取所有填报人名称（用于表单的tooltip）
const getAllSubmitterNamesForForm = (submitterIds: number[]) => {
  if (!submitterIds || submitterIds.length === 0) return '无填报人'

  const personNames = submitterIds
    .map((id) => {
      const person = submitterList.value.find((p) => p.id === id)
      return person ? person.nickname : '未知人员'
    })
    .filter((name) => name !== '未知人员')

  return personNames.join(', ') || '无有效填报人'
}

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调

// 处理平台井工作量数据
const processPlatformWellWorkloadData = (platformWells: any[]) => {
  if (!platformWells || !platformWells.length) return

  platformWells.forEach((well: any) => {
    if (well.wellName && well.extProperty?.[0]?.dropdownList) {
      processWorkloadDataForWell(well.wellName, well.extProperty[0].dropdownList)
    }
  })
}

// 处理单个井的工作量数据
const processWorkloadDataForWell = (wellName: string, dropdownList: any[]) => {
  if (!wellName || !dropdownList || !dropdownList.length) return

  // 转换dropdownList格式为workloadMap需要的格式
  const workloadData = dropdownList.map((item) => ({
    workloadUnit: getWorkloadUnitValueByName(item.name), // 通过name获取对应的value
    workloadDesign: item.value
  }))

  // 存储到workloadMap
  workloadMap.value.set(wellName, workloadData)

  // 如果这个井在platformWellDetails中，更新其工作量单位和工作量设计
  if (currentTask.value.platformWellDetails) {
    const detailIndex = currentTask.value.platformWellDetails.findIndex(
      (detail: any) => detail.wellName === wellName
    )

    if (detailIndex !== -1 && workloadData.length > 0) {
      // 使用第一个工作量项作为平台井表格中显示的值
      currentTask.value.platformWellDetails[detailIndex].workloadUnit = workloadData[0].workloadUnit
      currentTask.value.platformWellDetails[detailIndex].workloadDesign =
        workloadData[0].workloadDesign
    }
  }

  // 如果是主任务井，也更新当前任务的工作量数据
  if (wellName === currentTask.value.wellName && workloadData.length > 0) {
    currentTask.value.workloadUnit = workloadData[0].workloadUnit
    currentTask.value.workloadDesign = workloadData[0].workloadDesign
  }
}

// 通过工作量单位名称获取对应的值
const getWorkloadUnitValueByName = (name: string): string => {
  const options = getStrDictOptions(DICT_TYPE.PMS_PROJECT_WORKLOAD_UNIT)
  const option = options.find((opt) => opt.label === name)
  return option ? option.value : ''
}

const submitForm = async () => {
  // 施工区域的数据字典类型
  currentTask.value.dictType = currentDictLabel.value

  // 先校验设计工作量
  if (currentTask.value.platformWell !== '1' && !validateWorkloadDesign()) {
    ElMessage.error('请完善设计工作量信息')
    return
  }

  // 在同步任务前，确保平台井关联井的工作量数据正确设置
  if (currentTask.value.platformWell === '1') {
    currentTask.value.platformWellDetails.forEach((detail) => {
      // 如果该关联井在workloadMap中没有数据，但表格行中有工作量数据，则设置到workloadMap中
      if (
        detail.wellName &&
        !workloadMap.value.has(detail.wellName) &&
        detail.workloadUnit &&
        detail.workloadDesign
      ) {
        workloadMap.value.set(detail.wellName, [
          {
            workloadUnit: detail.workloadUnit,
            workloadDesign: detail.workloadDesign
          }
        ])
      }
    })
  }

  // 先同步当前任务表单数据到表格
  if (!syncCurrentTaskToTable()) {
    return
  }

  // 校验表单
  await formRef.value.validate()

  // 校验所有行数据
  if (!validateAllRows()) {
    return
  }

  const getFileType = (filename: string) => {
    const ext = filename.split('.').pop()?.toLowerCase()
    if (['jpg', 'jpeg', 'png', 'gif', 'bmp'].includes(ext || '')) {
      return 'image'
    } else if (['pdf'].includes(ext || '')) {
      return 'pdf'
    } else if (['doc', 'docx'].includes(ext || '')) {
      return 'word'
    } else if (['xls', 'xlsx'].includes(ext || '')) {
      return 'excel'
    } else {
      return 'other'
    }
  }

  const formatFileSize = (bytes: number) => {
    if (bytes === 0) return '0 Bytes'
    const k = 1024
    const sizes = ['Bytes', 'KB', 'MB', 'GB']
    const i = Math.floor(Math.log(bytes) / Math.log(k))
    return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
  }

  function formatterAttachments(attachments: any[], type: string, taskId: number) {
    return attachments.flatMap((item) => {
      if (item.response.data.files) {
        return item.response.data.files.map((file) => ({
          category: 'DAILY_REPORT',
          bizId: taskId,
          filename: file.name || '未知文件',
          fileType: getFileType(file.name),
          filePath: file.filePath,
          fileSize: formatFileSize(file.size || 0),
          remark: '',
          type
        }))
      } else return item.response.data
    })
  }

  // 处理动态属性数据
  const processedTableData = tableData.value.map((task) => {
    // 获取当前任务的工作量数据并转换为dropdownList格式
    const workloadDropdownList = getWorkloadDropdownListForTask(task)

    // 为每个任务构建extProperty数组
    const extProperties = dynamicAttrs.value.map((attr) => {
      return {
        name: attr.name,
        sort: attr.sort,
        unit: attr.unit,
        actualValue: task[attr.identifier] || '', // 从任务中获取用户填写的值
        dataType: attr.dataType,
        maxValue: attr.maxValue,
        minValue: attr.minValue,
        required: attr.required,
        accessMode: attr.accessMode,
        identifier: attr.identifier,
        defaultValue: attr.defaultValue,
        // 添加dropdownList到任务对象中
        dropdownList: workloadDropdownList
      }
    })

    // 返回处理后的任务数据，包含extProperty字段
    return {
      ...task,
      dictType: task.dictType || currentDictLabel.value, // 确保每个任务都有dictType
      extProperty: extProperties,
      attachments: formatterAttachments(constructionFiles.value, 'CONSTRUCTION_DESIGN', task.id)
        .concat(formatterAttachments(geologicalFiles.value, 'GEOLOGICAL_DESIGN', task.id))
        .concat(formatterAttachments(completionFiles.value, 'COMPLETION_REPORT', task.id))
    }
  })

  // 提交请求
  formLoading.value = true
  try {
    tableData.value.forEach((item) => {
      item.projectId = formData.value.id
    })

    const data = {
      taskList: processedTableData
    }

    if (formType.value === 'create') {
      await IotProjectTaskApi.createIotProjectTask(data)
      message.success(t('common.createSuccess'))
    } else {
      await IotProjectTaskApi.updateIotProjectTask(data)
      message.success(t('common.updateSuccess'))
    }
    // 发送操作成功的事件
    emit('success')
    close()
  } finally {
    formLoading.value = false
  }
}

// 判断是否为主要的平台井详情（不能删除）
const isMainPlatformWellDetail = (index: number) => {
  if (
    !currentTask.value.platformWellDetails ||
    currentTask.value.platformWellDetails.length === 0
  ) {
    return false
  }

  const detail = currentTask.value.platformWellDetails[index]
  // 主记录的条件：是第一条记录且有关联的任务ID，或者是自动从非平台井转换过来的主记录
  return (
    index === 0 &&
    (detail.taskId === currentTask.value.id ||
      (detail.wellName === currentTask.value.wellName &&
        detail.workloadDesign === currentTask.value.workloadDesign))
  )
}

// 处理平台井开关变化
const handlePlatformWellChange = (value) => {
  if (value === '1') {
    // 切换到平台井模式前，备份当前井号的工作量数据
    if (currentTask.value.wellName) {
      const currentWorkload = workloadMap.value.get(currentTask.value.wellName)
      if (currentWorkload) {
        workloadBackup.value.set(currentTask.value.wellName, [...currentWorkload])
      }
    }

    // 检查是否是从非平台井切换过来，且当前任务有井号信息
    const hasWellName = currentTask.value.wellName && currentTask.value.wellName.trim() !== ''
    const hasWorkloadDesign = currentTask.value.workloadDesign

    if (hasWellName && hasWorkloadDesign) {
      // 从非平台井切换到平台井，自动添加一条记录
      const mainDetail = {
        taskId: currentTask.value.id, // 关联当前任务ID
        wellName: currentTask.value.wellName,
        workloadUnit: currentTask.value.workloadUnit, // 同步工作量单位
        workloadDesign: currentTask.value.workloadDesign,
        workloadDesignError: ''
      }

      // 如果workloadMap中有主井数据，同步到平台井表格
      if (workloadMap.value.has(currentTask.value.wellName)) {
        const workloadData = workloadMap.value.get(currentTask.value.wellName)
        if (workloadData && workloadData.length > 0) {
          mainDetail.workloadUnit = workloadData[0].workloadUnit
          mainDetail.workloadDesign = workloadData[0].workloadDesign
        }
      }

      // 清空现有详情，添加主记录
      currentTask.value.platformWellDetails = [mainDetail]
    } else {
      // 恢复之前保存的数据或初始化
      if (platformWellDetailsBackup.value.length > 0) {
        currentTask.value.platformWellDetails = [...platformWellDetailsBackup.value]
        sortPlatformWellDetails()
      } else {
        addPlatformWellDetail()
      }
    }
    // 更新备份数据
    platformWellDetailsBackup.value = [...currentTask.value.platformWellDetails]
  } else {
    // 如果有平台井详情，将第一个详情的数据设置回主任务
    if (currentTask.value.platformWellDetails.length > 0) {
      const mainDetail = currentTask.value.platformWellDetails[0]
      currentTask.value.wellName = mainDetail.wellName || ''
      currentTask.value.workloadDesign = mainDetail.workloadDesign || ''

      // 恢复工作量数据
      if (mainDetail.wellName && workloadBackup.value.has(mainDetail.wellName)) {
        const backupWorkload = workloadBackup.value.get(mainDetail.wellName)
        if (backupWorkload && backupWorkload.length > 0) {
          workloadMap.value.set(mainDetail.wellName, [...backupWorkload])

          // 恢复表单中的工作量单位（取第一项）
          currentTask.value.workloadUnit = backupWorkload[0].workloadUnit
          currentTask.value.workloadDesign = backupWorkload[0].workloadDesign
        }
      }
    }

    // 切换到非平台井模式，保存当前数据到备份
    platformWellDetailsBackup.value = [...currentTask.value.platformWellDetails]
    // 切换到非平台井模式，清空详情列表
    currentTask.value.platformWellDetails = []
  }
}

// 校验工作量对话框中的设计工作量
const validateWorkloadDialogDesign = (index) => {
  const workloadItem = workloadList.value[index]
  workloadItem.workloadDesignError = ''

  const value = workloadItem.workloadDesign
  if (value === '' || value === null || value === undefined) {
    workloadItem.workloadDesignError = '设计工作量不能为空'
    return false
  }

  const numValue = Number(value)
  if (isNaN(numValue) || numValue < 0) {
    workloadItem.workloadDesignError = '请填写大于0的数字'
    return false
  }

  return true
}

// 获取表单中已选的工作量单位（来自workloadMap）
const getFormSelectedWorkloadUnits = () => {
  // 兼容普通井/平台井的井号获取逻辑（与工作量弹窗逻辑一致）
  let currentWellName = ''
  if (currentTask.value.platformWell === '1') {
    // 平台井：取主井号（优先当前任务wellName，其次取平台井详情第一个）
    currentWellName =
      currentTask.value.wellName ||
      (currentTask.value.platformWellDetails.length > 0
        ? currentTask.value.platformWellDetails[0].wellName
        : '')
  } else {
    // 普通井：直接取当前任务井号
    currentWellName = currentTask.value.wellName
  }

  // 从Map中获取当前井号的工作量列表，提取已选单位
  const workloadList = workloadMap.value.get(currentWellName) || []
  return workloadList.map((item) => item.workloadUnit).filter((unit) => unit && unit.trim() !== '') // 过滤空值
}

// 添加平台井详情行
const addPlatformWellDetail = () => {
  const newDetail = {
    taskId: '',
    wellName: '',
    workloadUnit: '', // 新增工作量单位字段
    workloadDesign: '',
    workloadDesignError: '' // 新增错误信息字段
  }

  // 将新记录添加到数组末尾，而不是开头
  currentTask.value.platformWellDetails.push(newDetail)
}

// 删除平台井详情行
const removePlatformWellDetail = (index: number) => {
  // 获取被删除的井号，清理Map
  const removedDetail = currentTask.value.platformWellDetails[index]
  if (removedDetail.wellName && workloadMap.value.has(removedDetail.wellName)) {
    workloadMap.value.delete(removedDetail.wellName)
  }

  currentTask.value.platformWellDetails.splice(index, 1)
  // 删除后重新排序
  sortPlatformWellDetails()
}

// 平台井详情排序方法
const sortPlatformWellDetails = () => {
  if (
    !currentTask.value.platformWellDetails ||
    currentTask.value.platformWellDetails.length === 0
  ) {
    return
  }

  currentTask.value.platformWellDetails.sort((a, b) => {
    // 主记录（关联当前任务ID或从非平台井转换过来的记录）排在最前面
    const aIsMain = a.taskId === currentTask.value.id
    const bIsMain = b.taskId === currentTask.value.id

    if (aIsMain && !bIsMain) return -1
    if (!aIsMain && bIsMain) return 1

    // 其他记录保持原有顺序
    return 0
  })
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    contractId: undefined,
    contractName: undefined,
    contractCode: undefined,
    workloadTotal: undefined,
    workloadFinish: undefined,
    workloadUnit: undefined,
    startTime: undefined,
    endTime: undefined,
    location: undefined,
    technique: undefined,
    payment: undefined,
    remark: undefined,
    manufactureName: undefined,
    manufacturerId: undefined,
    userName: undefined,
    userId: undefined
  }
  formRef.value?.resetFields()
}

// 加载工作区域选项的方法
const loadWorkAreaOptions = async (deptId: number) => {
  // 如果正在加载，直接返回
  if (loadingWorkAreaOptions.value) return

  loadingWorkAreaOptions.value = true
  try {
    // 先尝试通过 getDictLabel 获取字典标签
    const dictLabel = await getDictLabel(DICT_TYPE.PMS_PROJECT_WORK_AREA, deptId)
    console.log('当前专业公司的施工区域数据字典类型：' + dictLabel)
    // 将 dictLabel 存储到 currentDictLabel
    currentDictLabel.value = dictLabel || '' // 如果没有获取到，设为空字符串

    if (dictLabel) {
      // 如果获取到标签，再获取完整的字典选项
      const dictOptions = getStrDictOptions(dictLabel)
      console.log('当前专业公司的施工区域数据字典集合长度：' + dictOptions.length)
      workAreaOptions.value = dictOptions.map((option) => ({
        value: option.label, // 使用标签作为显示值
        id: option.value // 保留原始值
      }))
    } else {
      // 如果获取不到标签，清空选项
      workAreaOptions.value = []
    }
  } catch (error) {
    console.error('获取工作区域选项失败:', error)
    workAreaOptions.value = []
  } finally {
    loadingWorkAreaOptions.value = false
  }
}

// 当没有查询字符串时返回所有选项
const querySearch = (queryString: string, cb: any) => {
  // 如果没有查询字符串，返回所有选项
  if (!queryString) {
    cb(workAreaOptions.value)
    return
  }

  // 有查询字符串时进行筛选
  const results = workAreaOptions.value.filter((option) =>
    option.value.toLowerCase().includes(queryString.toLowerCase())
  )

  // 调用 callback 返回建议列表的数据
  cb(results)
}

// 添加处理施工区域获取焦点的方法
const handleWorkAreaFocus = async () => {
  // 如果已经有选项数据，直接显示下拉框
  if (workAreaOptions.value.length > 0) {
    // 使用Element Plus提供的方法触发下拉框
    try {
      // 直接调用el-autocomplete的focus方法
      if (workAreaAutocomplete.value && typeof workAreaAutocomplete.value.focus === 'function') {
        workAreaAutocomplete.value.focus()
      }
    } catch (error) {
      console.error('触发下拉框显示失败:', error)
    }
    return
  }

  // 如果没有选项数据，尝试加载
  if (formData.value.deptId && !loadingWorkAreaOptions.value) {
    await loadWorkAreaOptions(formData.value.deptId)
    // 加载完成后尝试显示下拉框
    if (workAreaOptions.value.length > 0) {
      try {
        if (workAreaAutocomplete.value && typeof workAreaAutocomplete.value.focus === 'function') {
          workAreaAutocomplete.value.focus()
        }
      } catch (error) {
        console.error('触发下拉框显示失败:', error)
      }
    }
  }
}

// 添加选择处理方法
const handleSelect = (item: any) => {
  // 这里可以根据需要处理选择后的逻辑
  console.log('选择了:', item.value)
}

// 加载施工工艺选项的方法
const loadTechnologyOptions = async (deptId: number) => {
  if (loadingTechnologyOptions.value) return

  loadingTechnologyOptions.value = true
  try {
    // 先尝试通过 getDictLabel 获取字典标签
    const dictLabel = await getDictLabel(DICT_TYPE.PMS_PROJECT_TECHNOLOGY, deptId)
    console.log('当前专业公司的施工工艺数据字典类型：' + dictLabel)
    // 将 dictLabel 存储到 currentTechnologyDictLabel
    currentTechnologyDictLabel.value = dictLabel || '' // 如果没有获取到，设为空字符串

    if (dictLabel) {
      // 如果获取到标签，再获取完整的字典选项
      const dictOptions = getStrDictOptions(dictLabel)
      console.log('当前专业公司的施工工艺数据字典集合长度：' + dictOptions.length)
      technologyOptions.value = dictOptions.map((option) => ({
        value: option.value,
        label: option.label
      }))
    } else {
      // 如果获取不到标签，清空选项
      technologyOptions.value = []
    }
  } catch (error) {
    console.error('获取施工工艺选项失败:', error)
    technologyOptions.value = []
  } finally {
    loadingTechnologyOptions.value = false
  }
}

// 工作量加号按钮禁用状态（计算属性）
const getWorkloadAddBtnDisabled = computed(() => {
  // 禁用条件：无工作量单位 || 平台井模式 || 设计工作量校验失败
  return (
    !currentTask.value.workloadUnit ||
    currentTask.value.platformWell === '1' ||
    !!workloadDesignError.value
  )
})

// 监听平台井状态变化，清空错误信息
watch(
  () => currentTask.value.platformWell,
  () => {
    workloadDesignError.value = ''
  }
)

// 监听工作量单位变化，同步更新到 workloadMap
watch(
  () => currentTask.value.workloadUnit,
  (newWorkloadUnit, oldWorkloadUnit) => {
    if (!newWorkloadUnit || newWorkloadUnit === oldWorkloadUnit) return

    // 获取当前井号（兼容普通井/平台井）
    let currentWellName = ''
    if (currentTask.value.platformWell === '1') {
      currentWellName =
        currentTask.value.wellName ||
        (currentTask.value.platformWellDetails.length > 0
          ? currentTask.value.platformWellDetails[0].wellName
          : '')
    } else {
      currentWellName = currentTask.value.wellName
    }

    if (!currentWellName) return

    // 如果workloadMap中存在当前井号的工作量数据，更新第一项的workloadUnit
    if (workloadMap.value.has(currentWellName)) {
      const workloadList = workloadMap.value.get(currentWellName)
      if (workloadList && workloadList.length > 0) {
        // 更新第一项的workloadUnit
        workloadList[0].workloadUnit = newWorkloadUnit
        // 重新设置到Map中（虽然引用相同，但为了确保响应性）
        workloadMap.value.set(currentWellName, [...workloadList])
      } else {
        // 如果列表为空但有设计工作量，创建新项
        if (currentTask.value.workloadDesign) {
          workloadMap.value.set(currentWellName, [
            {
              workloadUnit: newWorkloadUnit,
              workloadDesign: currentTask.value.workloadDesign
            }
          ])
        }
      }
    } else if (currentTask.value.workloadDesign) {
      // 如果Map中不存在但有设计工作量，创建新条目
      workloadMap.value.set(currentWellName, [
        {
          workloadUnit: newWorkloadUnit,
          workloadDesign: currentTask.value.workloadDesign
        }
      ])
    }
  },
  { deep: true }
)

// 校验设计工作量
const validateWorkloadDesign = () => {
  workloadDesignError.value = ''

  if (currentTask.value.platformWell === '1') {
    return true
  }

  const value = currentTask.value.workloadDesign
  if (value === '' || value === null || value === undefined) {
    workloadDesignError.value = '设计工作量不能为空'
    return false
  }

  const numValue = Number(value)
  if (isNaN(numValue) || numValue < 0) {
    workloadDesignError.value = '请填写大于0的数字'
    return false
  }

  return true
}

// 监听设计工作量变化
watch(
  () => currentTask.value.workloadDesign,
  () => {
    if (workloadDesignError.value) {
      validateWorkloadDesign()
    }
  }
)

// 监听当前任务的变化，设置默认展开的keys
watch(
  () => currentTask.value.deptIds,
  (newVal, oldVal) => {
    if (newVal && newVal.length > 0) {
      defaultExpandedKeys.value = [...newVal]
    }

    // 【新增】施工队伍变化时，重新查询设备并更新状态
    const params = {
      deptIds: newVal
    }
    /* IotDeviceApi.getDevicesByDepts(params).then(data => {
    // 更新 hasDevicesAvailable 状态
    hasDevicesAvailable.value = data && data.length > 0;

    // 更新设备列表和映射表
    if (hasDevicesAvailable.value) {
      const newDeviceMap = {...deviceMap.value};
      data.forEach(device => {
        newDeviceMap[device.id] = device;
      });
      deviceMap.value = newDeviceMap;
    }
  }).catch(error => {
    console.error('施工队伍变化时查询设备失败:', error);
    hasDevicesAvailable.value = false;
  }); */

    if (!newVal || newVal.length === 0) {
      // 清空施工队伍时，同时清空相关人员和设备
      currentTask.value.responsiblePerson = []
      currentTask.value.submitter = []
      currentTask.value.deviceIds = []

      // 重置设备可用状态
      hasDevicesAvailable.value = true

      // 清除相关字段的验证状态
      nextTick(() => {
        taskFormRef.value?.validateField('responsiblePerson')
        taskFormRef.value?.validateField('submitter')
        taskFormRef.value?.validateField('deviceIds')
      })
    } else if (newVal !== oldVal) {
      // 施工队伍发生变化时，重置设备可用状态（因为不同队伍可能有不同的设备）
      // hasDevicesAvailable.value = true;
    }
  },
  { immediate: true, deep: true }
)

// 监听部门列表加载完成
watch(
  () => deptList.value,
  (newVal) => {
    if (
      newVal &&
      newVal.length > 0 &&
      currentTask.value.deptIds &&
      currentTask.value.deptIds.length > 0
    ) {
      // defaultExpandedKeys.value = [...currentTask.value.deptIds];
      // 过滤掉非叶子节点的ID，只保留叶子节点
      const leafNodeIds = currentTask.value.deptIds.filter((id) => {
        const node = findDeptNodeById(newVal, id)
        return node && node.type === '3'
      })

      if (leafNodeIds.length > 0) {
        defaultExpandedKeys.value = [...leafNodeIds]
      }
    }
  },
  { immediate: true, deep: true }
)

// 根据ID在部门树中查找节点
const findDeptNodeById = (tree: any[], id: number): any => {
  for (const node of tree) {
    if (node.id === id) {
      return node
    }
    if (node.children && node.children.length > 0) {
      const found = findDeptNodeById(node.children, id)
      if (found) return found
    }
  }
  return null
}

// 监听项目列表变化，确保列表加载完成后才执行自动选择
watch(projectList, (newVal) => {
  if (newVal && newVal.length > 0) {
    // 如果是编辑模式，使用任务数据中的合同ID
    if (id && tableData.value.length > 0 && tableData.value[0].projectId) {
      autoSelectContract(tableData.value[0].projectId.toString())
    }
    // 如果是新增模式且有传递的projectId，使用传递的projectId
    else if (!id && projectId.value) {
      console.log('watch-新增模式且有传递的projectId' + projectId.value + ' projectId not found')
      autoSelectContract(projectId.value)
    }
  }
})

// 处理树形下拉框可见性变化
const handleTreeVisibleChange = (visible: boolean) => {
  if (visible && currentTask.value.deptIds && currentTask.value.deptIds.length > 0) {
    // 当下拉框显示且当前任务有已选部门时，设置默认展开的keys
    defaultExpandedKeys.value = [...currentTask.value.deptIds]

    // 使用nextTick确保DOM更新后再执行展开操作
    nextTick(() => {
      if (treeSelectRef.value) {
        // 尝试访问内部树实例并展开节点
        const treeInstance = treeSelectRef.value.treeRef
        if (treeInstance && treeInstance.setExpandedKeys) {
          treeInstance.setExpandedKeys(defaultExpandedKeys.value)
        }
      }
    })
  }
}

onMounted(async () => {
  // 查询当前登录人所属公司下的所有部门
  deptList.value = handleTree(await DeptApi.companyLevelChildrenDepts())
  let deptId = useUserStore().getUser.deptId
  projectList.value = await IotProjectInfoApi.getIotProjectInfoUser(deptId)

  // 初始化时过滤当前任务中的部门ID，移除非叶子节点
  if (currentTask.value.deptIds && currentTask.value.deptIds.length > 0) {
    const validDeptIds = currentTask.value.deptIds.filter((id) => {
      const node = findDeptNodeById(deptList.value, id)
      return node && node.type === '3'
    })

    if (validDeptIds.length !== currentTask.value.deptIds.length) {
      currentTask.value.deptIds = validDeptIds
    }
  }

  // 查询当前任务已经选中的设备信息
  open()
})
</script>

<style scoped>
.edit-input {
  margin-right: 10px;
}

.error-message {
  margin-top: 5px;
  font-size: 12px;
  color: #f56c6c;
}

.action-cell {
  display: flex;
  gap: 8px;
}

/* 1. 穿梭框父容器：居中 + 内边距 */
.transfer-container {
  padding: 0; /* 上下内边距，避免紧贴对话框边缘 */
  text-align: center;
}

/* 2. 穿梭框组件：控制宽度，避免过窄或过宽 */
.transfer-component {
  width: 100%; /* 占对话框90%宽度，兼顾美观和内容显示 */
  min-width: 600px;
}

/* 3. 直接调整 el-transfer 左右窗口的宽度 */
:deep(.el-transfer-panel) {
  width: 40% !important; /* 强制设置左右面板宽度，确保两侧面板对等 */
}

/* 3. 深度选择器：修改el-transfer选项样式（解决内容省略问题） */
:deep(.el-transfer-panel__item) {
  /* white-space: nowrap;  文本不换行，保证一行显示 */

  /* overflow: hidden;  超出部分隐藏 */

  /* text-overflow: ellipsis;  超出显示省略号 */

  /* max-width: 100%;  确保文本不超出选项容器宽度 */

  /* padding: 6px 6px;  适当增加内边距，优化点击体验 */
}

:deep(.el-transfer-panel__item) {
  display: flex !important;
  height: 32px !important;
  padding: 0 8px !important;
  margin: 0 !important;
  overflow: hidden;
  line-height: 32px !important;
  text-overflow: ellipsis;
  white-space: nowrap;
  align-items: center !important;
}

/* 4. 选项文本：确保继承父容器宽度，省略号正常生效 */
.transfer-option-text {
  display: inline-block;
  max-width: 100%;
}

/* 设备名称显示区域样式 */
.device-names {
  max-width: 200px; /* 根据实际列宽调整 */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

:deep(.el-transfer-panel__list) {
  width: 100% !important; /* 使面板内部内容宽度占满 */
}

/* 责任人名称显示区域样式 */
.responsible-names {
  max-width: 200px; /* 根据实际列宽调整 */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* 添加部门名称的样式 */
.dept-names {
  display: inline-block;
  max-width: 200px; /* 根据实际列宽调整 */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  vertical-align: bottom;
}

/* 部门选择器样式优化 */
:deep(.department-tree-select .el-select__tags) {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex-wrap: nowrap;
}

:deep(.department-tree-select .el-select__tags .el-tag) {
  display: inline-block;
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
}

:deep(.department-tree-select .el-select__tags .el-tag + .el-tag) {
  margin-left: 6px;
}

:deep(.department-tree-select .el-select__tags-text) {
  display: inline-block;
  max-width: 100px;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 当有多个标签被折叠时显示的 "+N" 标签样式 */
:deep(.department-tree-select .el-select__collapse-tags) {
  display: inline-block;
  white-space: nowrap;
}

.task-edit-form {
  padding: 20px;
  margin-bottom: 20px;
  background-color: #fafafa;
  border: 1px solid #ebeef5;
  border-radius: 4px;
}

/* 按钮文字样式 - 红色字体 */
:deep(.workload-add-btn .btn-text) {
  font-size: 18px; /* 更大的字号 */
  font-weight: 900; /* 更粗的字体 */
  line-height: 1;
  color: #f00 !important;
}

/* 设计工作量加号按钮 - 启用状态（深蓝色） */
:deep(.workload-add-btn .el-icon) {
  color: #f00 !important; /* Element Plus 主色调深蓝色，可替换为 #003366 等自定义深蓝色 */
}

/* 按钮禁用状态 */
:deep(.workload-add-btn:disabled .btn-text) {
  color: #c0c4cc !important;
}

/* 调整按钮整体样式，确保icon居中 */
:deep(.workload-add-btn) {
  display: flex;
  width: 100%;
  height: 100%;
  min-width: auto;
  padding: 0 8px;
  align-items: center;
  justify-content: center;
}

/* 调整输入框append部分的宽度 */
:deep(.workload-input-with-button .el-input-group__append) {
  width: 45px; /* 稍微增加宽度以容纳文字 */
  padding: 0;
}

/* 确保按钮在禁用状态下有正确的样式 */
:deep(.workload-add-btn:disabled) {
  cursor: not-allowed;
  background-color: #f5f7fa;
  border-color: #e4e7ed;
}

/* 设计工作量错误输入框样式 */
:deep(.error-input .el-input__inner) {
  background-color: #fef0f0 !important;
  border-color: #f56c6c !important;
  box-shadow: 0 0 0 1px rgb(245 108 108 / 40%) !important;
}

:deep(.error-input .el-input__inner:hover) {
  background-color: #fef0f0 !important;
  border-color: #f56c6c !important;
}

:deep(.error-input .el-input__inner:focus) {
  background-color: #fef0f0 !important;
  border-color: #f56c6c !important;
  box-shadow: 0 0 0 1px rgb(245 108 108 / 20%) !important;
}

/* 设计工作量tooltip样式 */
:deep(.workload-design-tooltip) {
  max-width: 300px;
  padding: 8px 12px;
  font-size: 12px;
  color: #f56c6c !important;
  background: #fef0f0 !important;
  border: 1px solid #fbc4c4 !important;
}

/* 隐藏 Tooltip 箭头 */
:deep(.workload-design-tooltip .el-tooltip__arrow),
:deep(.workload-design-tooltip .el-tooltip__arrow::before) {
  display: none !important;
  width: 0 !important;
  height: 0 !important;
}
</style>
