<template>
  <z-paging
    class="page"
    ref="paging"
    v-model="dataList"
    :loading-more-enabled="true"
    @query="queryList">
    <!-- z-paging默认铺满全屏，此时页面所有view都应放在z-paging标签内，否则会被盖住 -->
    <!-- 需要固定在页面顶部的view请通过slot="top"插入，包括自定义的导航栏 -->
    <template #top>
      <!-- 工单基础信息 -->
      <view class="item top">
        <view class="item-content flex-row align-center">
          <view class="item-title full-cell flex-row align-center">
            <span class="item-title-width"
              >{{ $t("operationRecordFilling.workOrderName") }}:</span
            >
            <span>{{ params.orderName }}</span>
          </view>
        </view>
        <view class="item-content flex-row align-center">
          <view class="item-title full-cell flex-row align-center">
            <span class="item-title-width"
              >{{ $t("operationRecordFilling.responsiblePerson") }}:</span
            >
            <span>{{ params.userName }}</span>
          </view>
        </view>
        <view class="item-content flex-row align-center">
          <view class="item-title full-cell flex-row align-center">
            <span class="item-title-width"
              >{{ $t("operation.createTime") }}:</span
            >
            <span>{{ params.createTime }}</span>
          </view>
        </view>
        <view
          v-if="deptName === 'ry' || deptName === 'rh'"
          class="item-content flex-row align-center">
          <view class="item-title full-cell flex-row align-center">
            <span class="item-title-width">井号:</span>
            <uni-data-select
              :localdata="taskOptions"
              style="text-align: right"
              :styles="{ disableColor: '#fff' }"
              :clear="false"
              :disabled="!isView"
              :placeholder="$t('operation.PleaseSelect')"
              v-model="taskId"></uni-data-select>
          </view>
        </view>
        <view v-else class="item-content flex-row align-center">
          <view class="item-title full-cell flex-row align-center">
            <span class="item-title-width">井号:</span>
            <span>{{ params.wellName || "" }}</span>
          </view>
        </view>
      </view>
    </template>
    <!-- 填报列表 -->
    <view class="list">
      <view class="item" v-for="(item, index) in dataList" :key="index">
        <view class="item-module flex-row align-center justify-between">
          <view class="module-name">
            {{ item.deviceCode }}({{ item.deviceName }})
          </view>
          <view class="module-border"> </view>
        </view>
        <view class="item-content flex-row align-center justify-between bold">
          <view class="item-title flex-row align-center">
            <span>{{ $t("operationRecordFilling.belongToTeam") }}:</span>
            <span>{{ item.orgName }}</span>
          </view>
        </view>
        <view
          class="item-content flex-row align-center justify-between bold"
          v-for="sum in item.sumList">
          <view class="item-title flex-row align-center word-break-all">
            <span>{{ sum.name }}:</span>
          </view>
          <view class="item-value flex-row align-center justify-end total">
            <uni-easyinput
              style="text-align: right"
              :inputBorder="false"
              :clearable="true"
              :styles="{ disableColor: '#fff' }"
              :value="`${sum.totalRunTime} ${sum.modelAttr ? (sum.modelAttr.includes('Time') ? 'h' : '') : ''}`"
              :disabled="true"></uni-easyinput>
          </view>
        </view>
        <view
          class="item-content flex-col align-center justify-between"
          :class="{ 'bottom-bold': item.nonSumList.length > 0 }"
          v-for="nosum in item.nonSumList.filter(
            (nosum) =>
              !keys.includes(nosum.description) &&
              (deptName === 'ry'
                ? nosum.description !== 'productionStatus'
                : true)
          )">
          <!-- isCollection为1，提示：以下数值取自PLC，如有不符请修改 -->
          <uni-notice-bar
            :text="$t('operationRecordFilling.plcNotice')"
            v-if="nosum.isCollection == 1" />
          <view class="flex-row align-center justify-between item-content">
            <view class="item-title flex-row align-center">
              <span>{{ nosum.name }}:</span>
            </view>
            <!-- 判断填写项的属性 -->
            <!-- type为double时，输入框为数字类型 -->
            <view
              class="item-value flex-row align-center justify-end"
              v-if="nosum.type == 'double'">
              <uni-easyinput
                style="text-align: right"
                :styles="{ disableColor: '#fff' }"
                :inputBorder="false"
                :clearable="true"
                :placeholder="$t('operation.PleaseFillIn')"
                :disabled="!isView"
                v-model="nosum.fillContent"
                :type="'digit'"
                @blur="
                  nosum.threshold > 0
                    ? checkThreshold(nosum)
                    : checkLessThreshold(nosum)
                "
                @input="handleRealTimeUpdate(nosum, item)"></uni-easyinput>
            </view>
            <!-- type为textarea时，输入框为文本类型 -->
            <view
              class="item-value flex-row align-center justify-end"
              v-else-if="nosum.type == 'textarea'">
              <uni-easyinput
                style="text-align: right"
                :styles="{ disableColor: '#fff' }"
                :inputBorder="false"
                :clearable="true"
                :placeholder="$t('operation.PleaseFillIn')"
                :disabled="!isView"
                v-model="nosum.fillContent"
                :type="'textarea'"
                :autoHeight="true"
                :maxlength="-1"></uni-easyinput>
            </view>
            <!-- type为enum时，使用下拉菜单 -->
            <view
              class="item-value select flex-row align-center justify-end"
              v-else-if="nosum.type == 'enum' && nosum.description !== null">
              <uni-data-select
                :localdata="nosum.enumList"
                style="text-align: right"
                :styles="{ disableColor: '#fff' }"
                :clear="false"
                :disabled="!isView"
                :placeholder="$t('operation.PleaseSelect')"
                v-model="nosum.fillContent"></uni-data-select>
            </view>
            <!-- 其他类型时，输入框为文本类型 -->
            <view class="item-value flex-row align-center justify-end" v-else>
              <uni-easyinput
                style="text-align: right"
                :styles="{ disableColor: '#fff' }"
                :inputBorder="false"
                :clearable="true"
                :placeholder="$t('operation.PleaseFillIn')"
                :disabled="!isView"
                v-model="nosum.fillContent"
                :type="'text'"></uni-easyinput>
            </view>
          </view>
        </view>
        <!-- {{ nosum.name }} {{ nosum.description }} -->

        <uni-forms
          v-if="item.deviceName === '生产日报'"
          ref="formRef"
          labelWidth="auto"
          validateTrigger="submit"
          err-show-type="toast"
          :model="{ nonSumList: item.nonSumList, reportDetails }">
          <template
            v-for="(nosum, nosumIndex) in item.nonSumList"
            :key="nosumIndex">
            <uni-forms-item
              v-if="keys.includes(nosum.description)"
              :label="nosum.name"
              :rules="rules[nosum.description]?.(index)"
              :name="['nonSumList', nosumIndex, 'fillContent']">
              <uni-easyinput
                v-if="nosum.description !== 'otherNptReason'"
                type="number"
                :placeholder="$t('operation.PleaseFillIn')"
                style="text-align: right"
                :styles="{ disableColor: '#fff' }"
                :inputBorder="false"
                :clearable="false"
                :disabled="!isView"
                v-model.number="nosum.fillContent"
                @input="(val) => onInputChange(val, nosum)" />
              <uni-easyinput
                v-else
                type="textarea"
                autoHeight
                :placeholder="$t('operation.PleaseFillIn')"
                style="text-align: right"
                :styles="{ disableColor: '#fff' }"
                :inputBorder="false"
                :clearable="false"
                :disabled="!isView"
                v-model="nosum.fillContent"
                :maxlength="1000" />
            </uni-forms-item>
          </template>
          <template v-if="deptName === 'ry'">
            <uv-divider text="生产动态" textPosition="left"> </uv-divider>
            <uni-forms-item v-if="isView">
              <button
                type="primary"
                size="mini"
                class="detail-btn"
                @click="addProductionStatusRow()">
                添加一行
              </button>
            </uni-forms-item>
            <template v-for="(detail, index) in reportDetails" :key="index">
              <uv-divider v-if="index !== 0" class="divider"></uv-divider>
              <uni-forms-item
                label="日期"
                required
                :name="['reportDetails', index, 'reportDate']"
                :rules="[{ required: true, errorMessage: '请选择日期' }]">
                <uni-datetime-picker
                  class="datetime-picker"
                  type="date"
                  returnType="timestamp"
                  v-model="detail.reportDate"
                  :border="false"
                  :disabled="!isView"
                  @change="inputCurrentDepth(item.nonSumList)" />
              </uni-forms-item>
              <uni-forms-item :label="`${$t('ruiDu.timeNode')}:`" required>
                <view
                  @click="
                    !isView
                      ? undefined
                      : handleClickTimeRangeItem(index, item.nonSumList)
                  ">
                  <view
                    class="time-range-item"
                    v-if="detail.startTime && detail.endTime">
                    {{ detail.startTime }} 至 {{ detail.endTime }}
                  </view>
                  <view class="time-range-item" v-else> 请选择 </view>
                </view>
              </uni-forms-item>
              <uni-forms-item label="时长(H)">
                <span>{{ detail.duration }}</span>
              </uni-forms-item>
              <uni-forms-item
                label="工况"
                required
                :name="['reportDetails', index, 'currentOperation']"
                :rules="[{ required: true, errorMessage: '请输入工况' }]">
                <uni-easyinput
                  type="textarea"
                  autoHeight
                  v-bind="defaultProps"
                  v-model="detail.currentOperation"
                  :disabled="!isView"
                  :maxlength="2000" />
              </uni-forms-item>
              <uni-forms-item
                label="结束井深(m)"
                required
                v-if="showDepth(item.nonSumList)"
                :name="['reportDetails', index, 'currentDepth']"
                :rules="[{ required: true, errorMessage: '请输入结束深度' }]">
                <uni-easyinput
                  type="number"
                  v-bind="defaultProps"
                  :disabled="!isView"
                  v-model.number="detail.currentDepth"
                  @input="(val) => inputCurrentDepth(item.nonSumList)" />
              </uni-forms-item>
              <uni-forms-item
                label="详情"
                required
                :name="['reportDetails', index, 'constructionDetail']"
                :rules="[{ required: true, errorMessage: '请输入详情' }]">
                <uni-easyinput
                  type="textarea"
                  autoHeight
                  :disabled="!isView"
                  v-bind="defaultProps"
                  v-model="detail.constructionDetail"
                  :maxlength="2000" />
              </uni-forms-item>
              <uni-forms-item v-if="isView" label="操作">
                <button
                  type="warn"
                  size="mini"
                  class="detail-btn"
                  @click="removeReportDetailRow(index)">
                  删除
                </button>
              </uni-forms-item>
            </template>
          </template>
        </uni-forms>
      </view>
    </view>
    <!-- 如果需要使用页脚，请使用slot="bottom"slot节点不支持通过v-if或v-show动态显示/隐藏，若需要动态控制，可将v-if添加在其子节点上 -->
    <template #bottom>
      <button
        style="border-radius: 0"
        type="primary"
        @click="onSubmit()"
        :disabled="dataList.length < totalNum || isSubmitting"
        v-if="isView">
        {{ isSubmitting ? "提交中" : $t("operation.save") }}
      </button>
    </template>
  </z-paging>

  <tpf-time-range
    ref="reportDetailsTimeRangeRef"
    :startTime="startTime"
    :startDefaultTime="startDefaultTime"
    :endTime="endTime"
    :endDefaultTime="endDefaultTime"
    @timeRange="reportDetailsTimeRange"></tpf-time-range>
</template>

<script setup>
import { ref, reactive, getCurrentInstance, watch, computed } from "vue";
import { onReady, onLoad } from "@dcloudio/uni-app";
import dayjs from "dayjs";
import {
  getRecordFillingDetail,
  recordFillingUpOperationOrder,
  recordFillingDetailGetPageAndAttrs,
  recordFillingDetailInsertDataList,
  getDeptName,
  reportDetailsGet,
} from "@/api/recordFilling";
import { reloginByUserId } from "@/utils/auth.js";
import { useDataDictStore } from "@/store/modules/dataDict";
import { useDebounceFn } from "@/utils/useDebounceFn.js";
import tpfTimeRange from "@/components/tpf-time-range/tpf-time-range.vue";

// 引用全局变量$t
const { appContext } = getCurrentInstance();
const t = appContext.config.globalProperties.$t;

// 获取字典项
const { getStrDictOptions, getIntDictOptions } = useDataDictStore();

const defaultProps = computed(() => ({
  inputBorder: false,
  clearable: false,
  placeholder: "请输入",
  style: {
    "text-align": "right",
  },
  styles: {
    disableColor: "#fff",
  },
}));

const reportDetailsTimeRangeRef = ref(null);

const startTime = ref("00:00");
const startDefaultTime = ref("08:00");
const endTime = ref("24:00");
const endDefaultTime = ref("08:00");

const reportDetailIndex = ref(0);

const aaaList = ref();

const handleClickTimeRangeItem = (index, list) => {
  reportDetailIndex.value = index;
  aaaList.value = list;
  reportDetailsTimeRangeRef.value.open();
};

const calculateDuration = (row) => {
  if (!row.startTime || !row.endTime) {
    row.duration = 0;
    return;
  }

  const todayStr = dayjs().format("YYYY-MM-DD");
  const start = dayjs(`${todayStr} ${row.startTime}`);
  const end = dayjs(`${todayStr} ${row.endTime}`);

  let diffMinutes = end.diff(start, "minute");

  if (diffMinutes < 0) {
    diffMinutes += 1440;
  }

  row.duration = Number((diffMinutes / 60).toFixed(2));
};

const reportDetailsTimeRange = (data) => {
  reportDetails.value[reportDetailIndex.value].startTime = data[0];
  reportDetails.value[reportDetailIndex.value].endTime = data[1];

  calculateDuration(reportDetails.value[reportDetailIndex.value]);
  inputCurrentDepth(aaaList.value ?? []);
};

const reportDetails = ref([]);

const addProductionStatusRow = () => {
  if (!reportDetails.value) {
    reportDetails.value = [];
  }
  reportDetails.value.push({
    reportDate: params.createTime
      ? dayjs(params.createTime).valueOf()
      : dayjs().valueOf(),
    startTime: "08:00",
    endTime: "08:00",
    duration: 0,
    currentDepth: 0,
    currentOperation: "",
    constructionDetail: "",
  });
};

const removeReportDetailRow = (index) => {
  if (index === 0) {
    uni.showToast({ title: "至少填写一条生产动态", icon: "none" });
    return;
  }

  reportDetails.value.splice(index, 1);
};

const taskId = ref(undefined);
const taskOptions = ref([]);
const noGasTasks = ref([]);

const inputCurrentDepth = useDebounceFn(function inputCurrentDepth(list) {
  const details = reportDetails.value;

  if (Array.isArray(details) && details.length > 0) {
    const latestDetail = details.reduce((prev, current) => {
      const currentFullTime = dayjs(current.reportDate)
        .hour(parseInt(current.endTime.split(":")[0]))
        .minute(parseInt(current.endTime.split(":")[1]))
        .valueOf();

      const prevFullTime = dayjs(prev.reportDate)
        .hour(parseInt(prev.endTime.split(":")[0]))
        .minute(parseInt(prev.endTime.split(":")[1]))
        .valueOf();

      return currentFullTime >= prevFullTime ? current : prev;
    });

    const currentDepth = list.find(
      (item) => item?.description === "currentDepth"
    );
    if (currentDepth) currentDepth.fillContent = latestDetail.currentDepth;
  }
}, 300);

const showDepth = computed(() => (list) => {
  return list.some((item) => item.description === "currentDepth");
});

const NON_KEYS = [
  "repairTime",
  "selfStopTime",
  "accidentTime",
  "complexityTime",
  "rectificationTime",
  "waitingStopTime",
  "partyaDesign",
  "partyaPrepare",
  "partyaResource",
  "relocationTime",
  "winterBreakTime",
  "otherNptTime",
];

const keys = [
  "repairTime",
  "selfStopTime",
  "accidentTime",
  "complexityTime",
  "rectificationTime",
  "waitingStopTime",
  "partyaDesign",
  "partyaPrepare",
  "partyaResource",
  "relocationTime",
  "winterBreakTime",
  "otherNptTime",
  "drillingWorkingTime",
  "otherProductionTime",
  "ratedProductionTime",
  "productionTime",
  "dailyInjectGasTime",
  "otherNptReason",
];

const sumNonProdTimes = (index) => {
  let sum = 0;
  const { nonSumList } = dataList.value[index];
  NON_KEYS.forEach((field) => {
    sum += Number(
      (nonSumList || []).find((item) => item.description === field)
        ?.fillContent || 0
    );
  });
  return sum;
};

const handleInputRaw = (val, item) => {
  let num = Number(val);
  if (val === "" || isNaN(num) || num < 0) {
    num = 0;
  } else if (num > 24) {
    num = 24;
  }
  item.fillContent = num;
};
const onInputChange = debounce(handleInputRaw, 500);

// 校验函数：总时间必须为 24
const ryValidateTotalTime = (index) => (rule, value, data, callback) => {
  const { nonSumList } = dataList.value[index];

  const drillingTime = Number(
    (nonSumList || []).find(
      (item) => item.description === "drillingWorkingTime"
    )?.fillContent || 0
  );

  const otherTime = Number(
    (nonSumList || []).find(
      (item) => item.description === "otherProductionTime"
    )?.fillContent || 0
  );

  const nonProdSum = sumNonProdTimes(index);

  let total = 0;
  let msg = "";

  total = parseFloat((drillingTime + otherTime + nonProdSum).toFixed(2));
  msg = `进尺(${drillingTime})+其他(${otherTime})+非生产(${nonProdSum})=${total}H，必须为24H`;

  if (Math.abs(total - 24) > 0.01) {
    callback(msg);
  }
  return true;
};

const rhValidateTotalTime = (index) => (rule, value, data, callback) => {
  const { nonSumList } = dataList.value[index];

  const gasTime = Number(
    (nonSumList || []).find((item) => item.description === "dailyInjectGasTime")
      ?.fillContent || 0
  );

  const nonProdSum = sumNonProdTimes(index);

  let total = 0;
  let msg = "";

  total = parseFloat((gasTime + nonProdSum).toFixed(2));
  msg = `运转(${gasTime})+非生产(${nonProdSum})=${total}H，必须为24H`;

  console.log("total :>> ", total);

  if (Math.abs(total - 24) > 0.01) {
    callback(msg);
  }
  return true;
};

const rules = reactive({
  drillingWorkingTime: (index) => [
    { required: true, errorMessage: "请输入进尺工作时间" },
    { validateFunction: ryValidateTotalTime(index) },
  ],
  dailyInjectGasTime: (index) => [
    { required: true, errorMessage: "请输入当日运转时间" },
    { validateFunction: rhValidateTotalTime(index) },
  ],
});

// -------------------------------------
const isFromMsg = ref(false);
const params = ref({});
const isView = ref(false); // 是否编辑 -- view == 1为编辑状态
let deptName = ref("");

// 累加状态对象，用于在生产日报加载前存储累加值
const accumulatedValues = reactive({
  "当日注水量-方": 0,
  "当日用电量kWh": 0,
  "当日运转时间H": 0,
  "当日注气量-方": 0,
});

onReady(() => {
  console.log("onReady");
});

onLoad(async (option) => {
  console.log("onLoad", option);
  await reloginByUserId(option.reloginUserId);
  isFromMsg.value = !!option.reloginUserId;
  // 初始化params
  params.value = JSON.parse(option.param);
  // 处理createTime
  params.value.createTime = params.value.createTime
    ? dayjs(Number.parseInt(params.value.createTime)).format("YYYY-MM-DD")
    : "";
  // 请求工单详情
  if (params.value?.orderId) {
    const detail = (await getRecordFillingDetail(params.value.orderId)).data;

    const daily = detail ?? {
      wellNamePair: {},
      noGasTasks: [],
    };

    taskOptions.value = Object.keys(daily.wellNamePair ?? {}).map((key) => ({
      text: daily.wellNamePair[key],
      value: Number(key),
    }));

    noGasTasks.value = (daily.noGasTasks ?? []).map(Number);

    taskId.value = taskOptions.value[0]?.value || undefined;

    const data = await getDeptName(detail.deptId);
    console.log("data>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", data);
    deptName.value = data.data;

    console.log("deptName.value>>>>>>>>>>>>>>>>>>>", deptName.value);

    params.value = {
      ...params.value,
      ...detail,
      // 处理createTime
      createTime: detail.createTime
        ? dayjs(Number.parseInt(detail.createTime)).format("YYYY-MM-DD")
        : "",
      orderId: detail.id,
    };
  }

  console.log("🚀 ~ params.value:", params.value);
  // 处理是否可编辑 {0: '待填写', 1: '已完成', 2: '填写中', 3: '忽略'}
  isView.value = params.value?.orderStatus % 2 == 0;

  console.log("🚀 ~ isView.value:", isView.value);
});
const paging = ref(null);
// v-model绑定的这个变量不要在分页请求结束中自己赋值，直接使用即可
const dataList = ref([]);
// 列表总数
const totalNum = ref(0);

// 监听dataList变化，初始化时计算一次总和
watch(
  dataList,
  (newVal) => {
    // calculateTotalRunTime();
  },
  { deep: true }
);

// 处理fillContent变化的方法
const handleFillContentChange = (nosum, deviceItem) => {
  console.log("🚀 ~ nosum, deviceItem:", nosum, deviceItem);
  // 处理增压机
  if (
    deviceItem.deviceName.includes("增压机") &&
    nosum.name === "当日运转时间"
  ) {
    calculateTotalRunTime("增压机", "当日运转时间"); // 计算当日运转时间总和
  }
  // 处理提纯撬
  if (deviceItem.deviceName.includes("提纯撬") && nosum.name === "当日注气量") {
    calculateTotalRunTime("提纯撬", "当日注气量"); // 计算当日注气量总和
  }
  // 处理注水泵
  if (deviceItem.deviceName.includes("注水泵") && nosum.name === "当日注水量") {
    calculateTotalRunTime("注水泵", "当日注水量"); // 计算当日注水量总和
  }
  // 处理箱式变电站
  if (
    deviceItem.deviceName.includes("箱式变电站") &&
    nosum.name === "当日用电量"
  ) {
    calculateTotalRunTime("箱式变电站", "当日用电量"); // 计算当日用电量总和
  }
};

// 防抖函数
function debounce(func, delay) {
  let timer;
  return function (...args) {
    if (timer) clearTimeout(timer);
    timer = setTimeout(() => func.apply(this, args), delay);
  };
}

//防抖
const debouncedCalculateTotalRunTime = debounce(calculateTotalRunTime, 100);
const debouncedApplyAccumulatedToReport = debounce(
  applyAccumulatedToReport,
  100
);

/**
 * 更新累加值
 * @param fieldName 字段名
 * @param deviceType 设备类型
 */
const updateAccumulatedValue = (fieldName, deviceType) => {
  let total = 0;
  dataList.value.forEach((item) => {
    if (item.deviceName.includes(deviceType) && item.nonSumList) {
      item.nonSumList.forEach((nonSum) => {
        if (
          nonSum.type === "double" &&
          nonSum.name === fieldName &&
          nonSum.fillContent !== null &&
          nonSum.fillContent !== ""
        ) {
          const value = parseFloat(nonSum.fillContent) || 0;
          total += value;
        }
      });
    }
  });
  accumulatedValues[fieldName] = total;
};

/**
 * 重新计算所有累加值
 */
const recalculateAllAccumulatedValues = () => {
  const fields = [
    { field: "当日运转时间H", device: "增压机" },
    { field: "当日注气量-方", device: "提纯撬" },
    { field: "当日注水量-方", device: "注水泵" },
    { field: "当日用电量kWh", device: "箱式变电站" },
  ];

  fields.forEach(({ field, device }) => {
    updateAccumulatedValue(field, device);
  });
};

/**
 * 将累加值应用到生产日报
 */
function applyAccumulatedToReport() {
  // 先重新计算所有累加值
  recalculateAllAccumulatedValues();

  const reportItem = dataList.value.find(
    (item) => item.deviceName === "生产日报"
  );

  if (!reportItem) {
    console.warn("⚠️ 未找到生产日报，累加值已存储，等待加载");
    return;
  }

  Object.keys(accumulatedValues).forEach((fieldName) => {
    const targetItem = reportItem.nonSumList.find(
      (item) => item.name === fieldName
    );
    if (targetItem) {
      console.log(
        `📊 应用累加值 (${fieldName}):`,
        accumulatedValues[fieldName]
      );
      targetItem.fillContent = toFixed(accumulatedValues[fieldName]);
    }
  });
}

const handleRealTimeUpdate = (nosum, deviceItem) => {
  console.log("🚀 实时更新 ~ nosum, deviceItem:", nosum, deviceItem);

  let fieldName = "";
  let deviceType = "";

  // 当日运转时间累加
  if (
    deviceItem.deviceName.includes("增压机") &&
    nosum.name === "当日运转时间H"
  ) {
    fieldName = "当日运转时间H";
    deviceType = "增压机";
  }

  // 当日注气量累加
  if (
    deviceItem.deviceName.includes("提纯撬") &&
    nosum.name === "当日注气量-方"
  ) {
    fieldName = "当日注气量-方";
    deviceType = "提纯撬";
  }

  // 当日注水量累加
  if (
    deviceItem.deviceName.includes("注水泵") &&
    nosum.name === "当日注水量-方"
  ) {
    fieldName = "当日注水量-方";
    deviceType = "注水泵";
  }

  // 当日用电量累加
  if (
    deviceItem.deviceName.includes("箱式变电站") &&
    nosum.name === "当日用电量kWh"
  ) {
    fieldName = "当日用电量kWh";
    deviceType = "箱式变电站";
  }

  if (fieldName) {
    // 更新累加值
    updateAccumulatedValue(fieldName, deviceType);
    // 应用到生产日报（如果已加载）
    debouncedApplyAccumulatedToReport();
  }
};

/**
 * 计算所有deviceName中包含deviceNameToMatch的对象中对应的reportName的fillContent总和并更新到reportName的fillContent中
 * @param deviceNameToMatch {string} 设备名称包含的字符串
 * @param reportName {string} 填写项名称
 */
function calculateTotalRunTime(
  deviceNameToMatch,
  sourceFieldName,
  targetFieldName
) {
  const reportItem = dataList.value.find(
    (item) => item.deviceName === "生产日报"
  );

  if (!reportItem) {
    console.warn("⚠️ 未找到生产日报");
    return;
  }

  const targetItem = reportItem.nonSumList.find(
    (item) => item.name === targetFieldName
  );

  if (!targetItem) {
    console.warn(`⚠️ 未找到目标字段：${targetFieldName}`);
    return;
  }

  let total = 0;

  dataList.value.forEach((item) => {
    if (item.deviceName.includes(deviceNameToMatch) && item.nonSumList) {
      item.nonSumList.forEach((nonSum) => {
        if (
          nonSum.type === "double" &&
          nonSum.name === sourceFieldName &&
          nonSum.fillContent !== null &&
          nonSum.fillContent !== ""
        ) {
          const value = parseFloat(nonSum.fillContent) || 0;
          total += value;
        }
      });
    }
  });

  console.log(`📊 累计值 (${sourceFieldName} -> ${targetFieldName}):`, total);
  targetItem.fillContent = toFixed(total);
}

const formatT = (arr) =>
  `${arr[0].toString().padStart(2, "0")}:${arr[1].toString().padStart(2, "0")}`;

// @query所绑定的方法不要自己调用！！需要刷新列表数据时，只需要调用paging.value.reload()即可
const queryList = (pageNo, pageSize) => {
  const userId = uni.getStorageSync("userId");
  if (!userId) {
    paging.value.complete([]);
    return;
  }

  // 请求填报设备及属性
  recordFillingDetailGetPageAndAttrs({
    pageNo,
    pageSize,
    orderId: params.value.orderId,
    // deviceCategoryId: 1,
  })
    .then(async (res) => {
      console.log("🚀 ~ res:", res);
      const { data } = res;
      const resList = [].concat(data.list);
      // 列表总数
      totalNum.value = data.total;
      // 遍历列表，处理attrsDetail
      resList.map(async (item) => {
        const attrParams = {
          deviceCode: item.deviceCode,
          deviceName: item.deviceName,
          deptId: item.deptId,
          createTime: params.value.createTime,
          deviceCategoryId: item.deviceCategoryId,
          deviceId: item.deviceId,
          userId: params.value.userId,
          orderId: params.value.orderId,
        };
        // console.log(
        //   "getRecordFillingDetailGetAttrs- attrParams",
        //   attrParams
        // );
        const resAttrs = item?.attrsDetail;
        // console.log("resAttrs", resAttrs);
        if (resAttrs) {
          attrParams.createTime = attrParams.createTime
            ? dayjs(attrParams.createTime).format("YYYY-MM-DD")
            : "";
          attrParams.id = attrParams.orderId;
          delete attrParams.orderId;
          delete attrParams.deviceName;
          resAttrs.map((rtem) => {
            // 将rtem中sumList和nonSumList两个数组中的
            // fillContent字段判断是否为null, 如果为null,则赋值为0 不为null则保留两位小数
            // 将attrParams合并到两个数组的每个对象中
            // 然后将sumList和nonSumList分别赋值给item的sumList和nonSumList

            if (rtem.sumList) {
              rtem.sumList.map((sumItem) => {
                if (sumItem.fillContent == null || sumItem.fillContent == "") {
                  // console.log("🚀 ~ rtem.sumList.map ~ sumItem:", sumItem);
                  // sumItem.fillContent = 0;
                } else {
                  // 如果是double类型，保留两位小数
                  if (sumItem.type == "double") {
                    sumItem.fillContent = toFixed(sumItem.fillContent);
                  }
                }
                // 将sumItem的id赋值给modelId
                sumItem.modelId = sumItem.id;

                sumItem.pointName = sumItem.name;
                // 合并attrParams到sumItem中
                sumItem = Object.assign(sumItem, attrParams);
              });
            }
            if (rtem.nonSumList) {
              //
              rtem.nonSumList.map((nonSumItem) => {
                if (
                  nonSumItem.fillContent == null ||
                  nonSumItem.fillContent == ""
                ) {
                  // console.log(
                  //   "🚀 ~ rtem.nonSumList.map ~ nonSumItem:",
                  //   nonSumItem
                  // );
                  // nonSumItem.fillContent = 0;
                } else {
                  // 如果是double类型，保留两位小数
                  if (nonSumItem.type == "double") {
                    nonSumItem.fillContent = toFixed(nonSumItem.fillContent);
                  }
                }
                nonSumItem.pointName = nonSumItem.name;
                // 将nonSumItem的id赋值给modelId
                nonSumItem.modelId = nonSumItem.id;
                // 合并attrParams到nonSumItem中
                nonSumItem = Object.assign(nonSumItem, attrParams);
                // 如果是enum类型，且description不为null,则根据description获取对应字典项数组，赋值给enumList
                if (nonSumItem.type == "enum" && nonSumItem.description) {
                  console.log("🚀 ~ onSumItem.description:");
                  const dictOptions =
                    nonSumItem.name === "非生产原因"
                      ? getIntDictOptions(nonSumItem.description)
                      : getStrDictOptions(nonSumItem.description);

                  nonSumItem.enumList = dictOptions.map((dict) => {
                    return {
                      ...dict,
                      text: dict.label,
                    };
                  });

                  // 确保 fillContent 的类型与 enumList 中的 value 类型匹配
                  if (nonSumItem.name === "非生产原因") {
                    // 如果是"非生产原因"，将 fillContent 转换为数字类型以匹配 getIntDictOptions
                    if (
                      nonSumItem.fillContent !== null &&
                      nonSumItem.fillContent !== ""
                    ) {
                      nonSumItem.fillContent = parseInt(nonSumItem.fillContent);
                    }
                  }

                  console.log("🚀 ~ nonSumItem.enumList:", nonSumItem.enumList);
                }
              });
            }
            item.sumList = rtem.sumList;

            const timeKeys = keys.filter((k) => k !== "otherNptReason");

            item.nonSumList = rtem.nonSumList
              .sort((a, b) => a.modelId - b.modelId)
              .map((item) => {
                if (timeKeys.includes(item.description)) {
                  item.fillContent = Number(item.fillContent || 0);
                }
                return item;
              });
          });
          console.log("resAttrs-modelId", resAttrs);
        }
        return item;
      });

      console.log("resList--", resList);
      // 将请求结果通过complete传给z-paging处理，同时也代表请求结束，这一行必须调用

      paging.value.completeByNoMore(
        resList,
        pageNo * pageSize >= totalNum.value
      );

      // 如果加载的数据中包含生产日报，应用累加值
      const hasReport = resList.some((item) => item.deviceName === "生产日报");

      if (hasReport) {
        applyAccumulatedToReport();

        reportDetailsGet(params.value.orderId).then((res) => {
          reportDetails.value = (res.data ?? []).map((item) => ({
            reportDate: item.reportDate ?? dayjs(params.createTime).valueOf(),
            startTime: formatT(item.startTime),
            endTime: formatT(item.endTime),
            duration: item.duration,
            currentDepth: item.currentDepth,
            currentOperation: item.currentOperation,
            constructionDetail: item.constructionDetail,
          }));

          console.log("🚀 ~ reportDetails.value:", reportDetails.value);

          if (!reportDetails.value.length) {
            addProductionStatusRow();
          }
        });
      }
    })
    .catch((res) => {
      // 如果请求失败写paging.value.complete(false);
      // 注意，每次都需要在catch中写这句话很麻烦，z-paging提供了方案可以全局统一处理
      // 在底层的网络请求抛出异常时，写uni.$emit('z-paging-error-emit');即可
      paging.value.complete(false);
    });
};

/**
 * 检查累计公里数和运转时长限制（仅针对deptName为'rd'的公司）
 * @param item 需要检查的填报项
 * @param totalValue 累计值
 * @param maxIncrement 最大增量
 * @param itemName 项目名称
 */
const rdThresholdExceededItems = ref([]);

const checkRdThreshold = (item, totalValue, maxIncrement, itemName) => {
  if (deptName.value !== "rd") {
    return true; // 不是rd公司，跳过检查
  }

  if (!item.fillContent) {
    return true; // 没有填写内容，跳过检查
  }

  const fillValue = parseFloat(item.fillContent);
  const maxValue = totalValue + maxIncrement;

  if (fillValue > maxValue) {
    // 收集超限信息
    rdThresholdExceededItems.value.push({
      deviceCode: item.deviceCode,
      // deviceName: item.deviceName,
      itemName: itemName,
      maxValue: maxValue,
      currentValue: fillValue,
    });
    return false;
  }

  return true;
};

// 判断是否小于阈值 (<0)
const checkLessThreshold = (item) => {
  if (item.fillContent < 0) {
    uni.showToast({
      title:
        item.name +
        t("operationRecordFilling.fillContentCannotLessThanThreshold") +
        "0",
      icon: "none",
    });
    item.fillContent = ""; // 清空输入
    return false; // 返回false表示校验失败
  }
};
// 判断是否大于阈值
const checkThreshold = (item) => {
  checkLessThreshold(item);
  // 如果threshold > 0，则判断fillContent是否大于threshold，如果大于则提示用户填写小于等于threshold的值
  if (item.fillContent > item.threshold) {
    uni.showToast({
      title:
        item.name +
        t("operationRecordFilling.fillContentCannotGreaterThanThreshold") +
        item.threshold,
      icon: "none",
    });
    item.fillContent = ""; // 清空输入
    return false; // 返回false表示校验失败
  }
};

// 保留两位小数
const toFixed = (num) => {
  if (num) {
    num = Number(num);
    num = num.toFixed(2);
  } else {
    num = 0.0;
  }
  return num;
};

const isSubmitting = ref(false); // 添加提交状态
// const onSubmit = async () => {
//   // 清空之前的超限记录
//   rdThresholdExceededItems.value = [];
//   // console.log("onSubmit", dataList.value);
//   // 校验是否所有待填写项都已加载
//   if (dataList.value.length < totalNum) {
//     uni.showToast({
//       title: t("operationRecordFilling.PleaseLoadAllItems"),
//       icon: "none",
//     });
//     return; // 校验失败直接返回
//   }
//   // 1. 校验所有必填项
//   // 遍历dataList.value中nonSumList每个item（非生产日报 isReport!=1）的fillContent字段,
//   // 如果为null或者为空，则提示用户填写，
//   // 如果threshold > 0，则判断fillContent是否大于threshold，如果大于则提示用户填写小于等于threshold的值
//   // 如果所有项全部填写，则调用填写记录接口

//   for (const item of dataList.value) {
//     const nonSumList = Array.isArray(item.nonSumList) ? item.nonSumList : [];

//     // 查找当日运转时间H项目
//     const runtimeItem = nonSumList.find((i) => i.name === "当日运转时间H");
//     const isRuntime24 =
//       runtimeItem &&
//       (runtimeItem.fillContent == 24 || runtimeItem.fillContent == "24");
//     for (const nonSumItem of nonSumList) {
//       // 增加判断条件：如果当日运转时间H等于24，则非生产原因和非生产时间H为非必填，否则为必填
//       const isExemptField =
//         isRuntime24 &&
//         (nonSumItem.name === "非生产原因" || nonSumItem.name === "非生产时间H");
//       if (
//         (!item.isReport || item.isReport != 1) &&
//         !isExemptField &&
//         (nonSumItem.fillContent == null || nonSumItem.fillContent === "")
//       ) {
//         uni.showToast({
//           title:
//             t("operation.PleaseFillIn") +
//             item.deviceCode +
//             "(" +
//             item.deviceName +
//             ")" +
//             t("operation.allItem"),
//           icon: "none",
//         });
//         return; // 校验失败直接返回
//       }
//       if (nonSumItem.fillContent != "" && nonSumItem.fillContent != null) {
//         console.log("🚀 ~ nonSumItem:", nonSumItem);
//         console.log("🚀 ~ nonSumItem.fillContent:", nonSumItem.fillContent);
//         // 先将值转换为字符串进行操作
//         const fillContentStr = String(nonSumItem.fillContent);
//         //  将字符串转换为数字
//         const num = Number(fillContentStr);

//         // 检查转换后的数字是否有效
//         if (!isNaN(num)) {
//           // 检查是否包含小数（使用字符串检查）
//           if (fillContentStr.includes(".")) {
//             // 保留两位小数（假设toFixed是你定义的保留两位小数的函数）
//             nonSumItem.fillContent = toFixed(num);
//           } else {
//             // 转换为整数
//             nonSumItem.fillContent = Math.floor(num);
//           }
//         }

//         //  **********************
//         // 新增：针对rd公司的特殊阈值检查
//         // 检查包含"累计公里数填报"的字段不能超过对应"累计公里数"字段 + 3000
//         if (nonSumItem.name.includes("累计公里数填报")) {
//           const correspondingSumItem = item.sumList.find((sumItem) =>
//             sumItem.name.includes("累计公里数")
//           );
//           if (correspondingSumItem) {
//             const totalKm = parseFloat(correspondingSumItem.totalRunTime) || 0;
//             checkRdThreshold(nonSumItem, totalKm, 3000, nonSumItem.name);
//           }
//         }
//         // 检查包含"累计运转时长填报"的字段不能超过对应"累计运转时长"字段 + 100
//         else if (nonSumItem.name.includes("累计运转时长填报")) {
//           const correspondingSumItem = item.sumList.find((sumItem) =>
//             sumItem.name.includes("累计运转时长")
//           );
//           if (correspondingSumItem) {
//             const totalRunTime =
//               parseFloat(correspondingSumItem.totalRunTime) || 0;
//             checkRdThreshold(nonSumItem, totalRunTime, 100, nonSumItem.name);
//           }
//         }
//       }

//       // 如果threshold > 0，则判断fillContent是否大于threshold
//       if (nonSumItem.threshold > 0) {
//         if (nonSumItem.fillContent > nonSumItem.threshold) {
//           uni.showToast({
//             title:
//               item.deviceCode +
//               "(" +
//               item.deviceName +
//               ")" +
//               nonSumItem.name +
//               t(
//                 "operationRecordFilling.fillContentCannotGreaterThanThreshold"
//               ) +
//               nonSumItem.threshold,
//             icon: "none",
//             duration: 3000,
//           });
//           nonSumItem.fillContent = ""; // 清空输入
//           return; // 校验失败直接返回
//         }
//       }
//     }
//   }

//   // 检查是否有超限的项目，如果有则统一显示
//   if (rdThresholdExceededItems.value.length > 0) {
//     const exceededInfo = rdThresholdExceededItems.value
//       .map(
//         (item, index) =>
//           `${index + 1}. ${item.deviceCode} ${item.itemName}不能超过${
//             item.maxValue
//           }，当前值为${item.currentValue}`
//       )
//       .join("\n");

//     uni.showModal({
//       title: "阈值超限提示",
//       content: `以下项目超过阈值限制：\n${exceededInfo}\n\n是否继续保存？`,
//       showCancel: true,
//       cancelText: "取消",
//       confirmText: "继续",
//       success: (res) => {
//         if (res.confirm) {
//           // 用户选择继续，执行保存操作
//           submitData();
//         } else if (res.cancel) {
//           // 用户选择取消，不执行保存
//           return;
//         }
//       },
//     });
//     return;
//   }

//   // 如果没有超限项目，直接执行保存
//   submitData();
// };

// 修改 onSubmit 方法

// const onSubmit = async () => {
//   // 校验是否所有待填写项都已加载
//   if (dataList.value.length < totalNum) {
//     uni.showToast({
//       title: t("operationRecordFilling.PleaseLoadAllItems"),
//       icon: "none",
//     });
//     return; // 校验失败直接返回，不设置按钮禁用
//   }

//   // 1. 校验所有必填项 - 在这里进行所有基础校验
//   for (const item of dataList.value) {
//     const nonSumList = Array.isArray(item.nonSumList) ? item.nonSumList : [];

//     // 查找当日运转时间H项目
//     const runtimeItem = nonSumList.find((i) => i.name === "当日运转时间H");
//     const isRuntime24 =
//       runtimeItem &&
//       (runtimeItem.fillContent == 24 || runtimeItem.fillContent == "24");
//     for (const nonSumItem of nonSumList) {
//       // 增加判断条件：如果当日运转时间H等于24，则非生产原因和非生产时间H为非必填，否则为必填
//       const isExemptField =
//         isRuntime24 &&
//         (nonSumItem.name === "非生产原因" || nonSumItem.name === "非生产时间H");
//       if (
//         (!item.isReport || item.isReport != 1) &&
//         !isExemptField &&
//         (nonSumItem.fillContent == null || nonSumItem.fillContent === "")
//       ) {
//         uni.showToast({
//           title:
//             t("operation.PleaseFillIn") +
//             item.deviceCode +
//             "(" +
//             item.deviceName +
//             ")" +
//             t("operation.allItem"),
//           icon: "none",
//         });
//         return; // 校验失败直接返回，不设置按钮禁用
//       }

//       // 如果threshold > 0，则判断fillContent是否大于threshold
//       if (nonSumItem.threshold > 0) {
//         if (nonSumItem.fillContent > nonSumItem.threshold) {
//           uni.showToast({
//             title:
//               item.deviceCode +
//               "(" +
//               item.deviceName +
//               ")" +
//               nonSumItem.name +
//               t(
//                 "operationRecordFilling.fillContentCannotGreaterThanThreshold"
//               ) +
//               nonSumItem.threshold,
//             icon: "none",
//             duration: 3000,
//           });
//           nonSumItem.fillContent = ""; // 清空输入
//           return; // 校验失败直接返回，不设置按钮禁用
//         }
//       }
//     }
//   }

//   // 检查是否有超限的项目，如果有则统一显示
//   if (rdThresholdExceededItems.value.length > 0) {
//     const exceededInfo = rdThresholdExceededItems.value
//       .map(
//         (item, index) =>
//           `${index + 1}. ${item.deviceCode} ${item.itemName}不能超过${
//             item.maxValue
//           }，当前值为${item.currentValue}`
//       )
//       .join("\n");

//     uni.showModal({
//       title: "阈值超限提示",
//       content: `以下项目超过阈值限制：\n${exceededInfo}\n\n是否继续保存？`,
//       showCancel: true,
//       cancelText: "取消",
//       confirmText: "继续",
//       success: (res) => {
//         if (res.confirm) {
//           // 用户选择继续，执行保存操作，此时才设置按钮禁用
//           submitDataWithDisable();
//         } else if (res.cancel) {
//           // 用户选择取消，不执行保存
//           return;
//         }
//       },
//     });
//     return;
//   }

//   // 所有基础校验通过后，才设置提交状态并执行提交
//   submitDataWithDisable();
// };

const formRef = ref(null);

const onSubmit = async () => {
  try {
    if (formRef.value) {
      for (const item of formRef.value) {
        await item.validate();
      }
    }
  } catch (error) {
    return;
  }
  // 清空之前的超限记录
  rdThresholdExceededItems.value = [];

  // 校验是否所有待填写项都已加载
  if (dataList.value.length < totalNum) {
    uni.showToast({
      title: t("operationRecordFilling.PleaseLoadAllItems"),
      icon: "none",
    });
    return; // 校验失败直接返回，不设置按钮禁用
  }

  // 1. 校验所有必填项 - 在这里进行所有基础校验
  for (const item of dataList.value) {
    const nonSumList = Array.isArray(item.nonSumList) ? item.nonSumList : [];

    // 查找当日运转时间H项目

    const otherNptTime = nonSumList.find((i) => i.name === "其他非生产时间H");
    const runtimeItem = nonSumList.find((i) => i.name === "当日运转时间H");

    const dailyInjectGasTime = nonSumList.find(
      (i) => i.description === "dailyInjectGasTime"
    );

    const isRuntime24 =
      runtimeItem &&
      (runtimeItem.fillContent == 24 || runtimeItem.fillContent == "24");
    for (const nonSumItem of nonSumList) {
      // 增加判断条件：如果当日运转时间H等于24，则非生产原因和非生产时间H为非必填，否则为必填
      if (
        nonSumItem.description === "dailyGasInjection" &&
        deptName.value === "rh" &&
        taskId.value &&
        !noGasTasks.value.includes(taskId.value)
      ) {
        // if (dailyInjectGasTime.fillContent > 0) {
        //   if (nonSumItem.fillContent <= 0) {
        //     uni.showToast({
        //       title: "当日运转时间大于0，注气量也需要大于0",
        //       icon: "none",
        //     });
        //     return;
        //   }
        // }
        if (dailyInjectGasTime.fillContent > 0) {
          const hasDailyGasInjection =
            nonSumItem.fillContent !== undefined &&
            nonSumItem.fillContent !== null &&
            nonSumItem.fillContent !== "";

          if (!hasDailyGasInjection) {
            uni.showToast({
              title: "当日运转时间大于0，需要填写当日注气量",
              icon: "none",
            });
            return;
          }
        }
      }

      if (
        nonSumItem.name === "其他非生产时间原因" &&
        !nonSumItem.fillContent &&
        otherNptTime.fillContent > 0
      ) {
        uni.showToast({
          title: "其他非生产时间大于 0 时，必须填写其他非生产时间原因",
          icon: "none",
        });
        return;
      }

      const isExemptField =
        isRuntime24 &&
        (nonSumItem.name === "非生产原因" || nonSumItem.name === "非生产时间H");
      if (
        (!item.isReport || item.isReport != 1) &&
        !isExemptField &&
        (nonSumItem.fillContent == null || nonSumItem.fillContent === "") &&
        nonSumItem.name !== "其他非生产时间原因"
      ) {
        uni.showToast({
          title:
            t("operation.PleaseFillIn") +
            item.deviceCode +
            "(" +
            item.deviceName +
            ")" +
            t("operation.allItem"),
          icon: "none",
        });
        return; // 校验失败直接返回，不设置按钮禁用
      }

      // 如果threshold > 0，则判断fillContent是否大于threshold
      if (nonSumItem.threshold > 0) {
        if (nonSumItem.fillContent > nonSumItem.threshold) {
          uni.showToast({
            title:
              item.deviceCode +
              "(" +
              item.deviceName +
              ")" +
              nonSumItem.name +
              t(
                "operationRecordFilling.fillContentCannotGreaterThanThreshold"
              ) +
              nonSumItem.threshold,
            icon: "none",
            duration: 3000,
          });
          nonSumItem.fillContent = ""; // 清空输入
          return; // 校验失败直接返回，不设置按钮禁用
        }
      }
    }
  }

  // 检查是否有超限的项目，如果有则统一显示
  // 需要在基础校验通过后，执行rd特殊阈值检查
  // 重新检查rd特殊阈值
  for (const item of dataList.value) {
    const nonSumList = Array.isArray(item.nonSumList) ? item.nonSumList : [];

    for (const nonSumItem of nonSumList) {
      if (nonSumItem.fillContent != "" && nonSumItem.fillContent != null) {
        // **********************
        // 新增：针对rd公司的特殊阈值检查
        // 检查包含"累计公里数填报"的字段不能超过对应"累计公里数"字段 + 3000
        if (nonSumItem.name.includes("累计公里数填报")) {
          const correspondingSumItem = item.sumList.find((sumItem) =>
            sumItem.name.includes("累计公里数")
          );
          if (correspondingSumItem) {
            const totalKm = parseFloat(correspondingSumItem.totalRunTime) || 0;
            checkRdThreshold(nonSumItem, totalKm, 3000, nonSumItem.name);
          }
        }
        // 检查包含"累计运转时长填报"的字段不能超过对应"累计运转时长"字段 + 100
        else if (nonSumItem.name.includes("累计运转时长填报")) {
          const correspondingSumItem = item.sumList.find((sumItem) =>
            sumItem.name.includes("累计运转时长")
          );
          if (correspondingSumItem) {
            const totalRunTime =
              parseFloat(correspondingSumItem.totalRunTime) || 0;
            checkRdThreshold(nonSumItem, totalRunTime, 100, nonSumItem.name);
          }
        }
      }
    }
  }

  // 检查是否有超限的项目，如果有则统一显示
  if (rdThresholdExceededItems.value.length > 0) {
    const exceededInfo = rdThresholdExceededItems.value
      .map(
        (item, index) =>
          `${index + 1}. ${item.deviceCode} ${item.itemName}不能超过${item.maxValue}，当前值为${item.currentValue}`
      )
      .join("\n");

    uni.showModal({
      title: "阈值超限提示",
      content: `以下项目超过阈值限制：\n${exceededInfo}\n\n是否继续保存？`,
      showCancel: true,
      cancelText: "取消",
      confirmText: "继续",
      success: (res) => {
        if (res.confirm) {
          // 用户选择继续，执行保存操作，此时才设置按钮禁用
          submitDataWithDisable();
        } else if (res.cancel) {
          // 用户选择取消，不执行保存
          return;
        }
      },
    });
    return;
  }

  // 所有基础校验通过后，才设置提交状态并执行提交
  submitDataWithDisable();
};

// 新增一个带禁用状态的提交方法
const submitDataWithDisable = async () => {
  // 如果正在提交，直接返回
  if (isSubmitting.value) {
    return;
  }

  // 设置提交状态为 true，禁用按钮
  isSubmitting.value = true;

  try {
    // 处理数据内容的校验和格式化
    for (const item of dataList.value) {
      const nonSumList = Array.isArray(item.nonSumList) ? item.nonSumList : [];

      for (const nonSumItem of nonSumList) {
        if (nonSumItem.fillContent != "" && nonSumItem.fillContent != null) {
          console.log("🚀 ~ nonSumItem:", nonSumItem);
          console.log("🚀 ~ nonSumItem.fillContent:", nonSumItem.fillContent);
          // 先将值转换为字符串进行操作
          const fillContentStr = String(nonSumItem.fillContent);
          //  将字符串转换为数字
          const num = Number(fillContentStr);

          // 检查转换后的数字是否有效
          if (!isNaN(num)) {
            // 检查是否包含小数（使用字符串检查）
            if (fillContentStr.includes(".")) {
              // 保留两位小数（假设toFixed是你定义的保留两位小数的函数）
              nonSumItem.fillContent = toFixed(num);
            } else {
              // 转换为整数
              nonSumItem.fillContent = Math.floor(num);
            }
          }

          // **********************
          // 新增：针对rd公司的特殊阈值检查
          // 检查包含"累计公里数填报"的字段不能超过对应"累计公里数"字段 + 3000
          if (nonSumItem.name.includes("累计公里数填报")) {
            const correspondingSumItem = item.sumList.find((sumItem) =>
              sumItem.name.includes("累计公里数")
            );
            if (correspondingSumItem) {
              const totalKm =
                parseFloat(correspondingSumItem.totalRunTime) || 0;
              checkRdThreshold(nonSumItem, totalKm, 3000, nonSumItem.name);
            }
          }
          // 检查包含"累计运转时长填报"的字段不能超过对应"累计运转时长"字段 + 100
          else if (nonSumItem.name.includes("累计运转时长填报")) {
            const correspondingSumItem = item.sumList.find((sumItem) =>
              sumItem.name.includes("累计运转时长")
            );
            if (correspondingSumItem) {
              const totalRunTime =
                parseFloat(correspondingSumItem.totalRunTime) || 0;
              checkRdThreshold(nonSumItem, totalRunTime, 100, nonSumItem.name);
            }
          }
        }
      }
    }

    // 执行实际的数据提交
    await submitData();
  } catch (error) {
    console.error("保存失败", error);
    // 启用按钮
    isSubmitting.value = false;
    uni.showToast({
      title: t("operation.fail"),
      icon: "none",
    });
  }
};
// 修改 submitData 方法，使其返回 Promise
const submitData = async () => {
  try {
    // 定义新的dataList副本 用于提交数据，避免修改原数据
    const subDataList = JSON.parse(JSON.stringify(dataList.value));

    // 3. 处理副本：删除 enumList（仅修改副本，不影响原数据）
    for (const item of subDataList) {
      // 先判断 item.nonSumList 存在，避免空指针
      if (item.nonSumList && item.nonSumList.length) {
        for (const nonSumItem of item.nonSumList) {
          if (nonSumItem.enumList) {
            delete nonSumItem.enumList;
          }
        }
      }
    }

    console.log("处理提交用的副本数据：subDataList", subDataList);
    // 2. 处理提交数据：将nonSumList和sumList合并为新数组并赋值给deviceInfoList对象,将所有的deviceInfoList合并为submitList
    const submitList = subDataList.map((item) => ({
      deviceInfoList: [].concat(item.sumList).concat(item.nonSumList),
    }));
    console.log("提交用的数据：submitList", submitList);
    // 3. 提交所有填写记录
    const reqData = {
      createReqVO: submitList,
      reportDetails: reportDetails.value.map((item) => ({
        ...item,
        taskId: taskId.value,
      })),
    };

    const res = await recordFillingDetailInsertDataList(reqData);
    console.log("🚀 ~ 提交工单填报内容结果 ~ res:", res);

    if (res?.code === 0) {
      // 3. 调用更新工单状态接口
      const upRes = await recordFillingUpOperationOrder({
        id: params.value.orderId,
      });
      console.log("🚀 ~ upRes:", upRes);
      if (upRes?.code === 0) {
        console.log("工单状态更新成功");
        uni.showToast({
          title: t("operation.success"),
          duration: 1500,
          icon: "none",
        });

        // 成功后延迟跳转，不启用按钮
        setTimeout(() => {
          uni.navigateBack();
        }, 1500);

        // 注意：这里不设置 isSubmitting.value = false，因为成功后会跳转页面
        return; // 成功完成，直接返回
      } else {
        console.error("工单状态更新失败", upRes);
        throw new Error("工单状态更新失败"); // 抛出错误，让 catch 处理
      }
    } else {
      throw new Error("提交失败"); // 抛出错误，让 catch 处理
    }
  } catch (error) {
    console.error("保存失败", error);
    // 启用按钮，让用户可以重试
    isSubmitting.value = false;
    uni.showToast({
      title: t("operation.fail"),
      icon: "none",
    });
    throw error; // 重新抛出错误
  }
};

// 将原来的保存逻辑提取到单独函数中
// const submitData = async () => {
//   // 定义新的dataList副本 用于提交数据，避免修改原数据
//   const subDataList = JSON.parse(JSON.stringify(dataList.value));

//   // 3. 处理副本：删除 enumList（仅修改副本，不影响原数据）
//   for (const item of subDataList) {
//     // 先判断 item.nonSumList 存在，避免空指针
//     if (item.nonSumList && item.nonSumList.length) {
//       for (const nonSumItem of item.nonSumList) {
//         if (nonSumItem.enumList) {
//           delete nonSumItem.enumList;
//         }
//       }
//     }
//   }

//   console.log("处理提交用的副本数据：subDataList", subDataList);
//   // 2. 处理提交数据：将nonSumList和sumList合并为新数组并赋值给deviceInfoList对象,将所有的deviceInfoList合并为submitList
//   const submitList = subDataList.map((item) => ({
//     deviceInfoList: [].concat(item.sumList).concat(item.nonSumList),
//   }));
//   console.log("提交用的数据：submitList", submitList);
//   // 3. 提交所有填写记录

//   await recordFillingDetailInsertDataList(submitList)
//     .then(async (res) => {
//       console.log("🚀 ~ 提交工单填报内容结果 ~ res:", res);
//       if (res?.code === 0) {
//         // 3. 调用更新工单状态接口
//         const upRes = await recordFillingUpOperationOrder({
//           id: params.value.orderId,
//         });
//         console.log("🚀 ~ upRes:", upRes);
//         if (upRes?.code === 0) {
//           console.log("工单状态更新成功");
//           uni.showToast({
//             title: t("operation.success"),
//             duration: 1500,
//             icon: "none",
//           });
//           setTimeout(() => {
//             uni.navigateBack();
//           }, 1500);
//         } else {
//           console.error("工单状态更新失败", upRes);
//           uni.showToast({
//             title: t("operation.fail"),
//             icon: "none",
//           });
//         }
//       } else {
//         uni.showToast({
//           title: t("operation.fail"),
//           icon: "none",
//         });
//       }
//     })
//     .catch((error) => {
//       console.error("保存失败", error);
//       uni.showToast({
//         title: t("operation.fail"),
//         icon: "error",
//       });
//     });
// };
</script>

<style lang="scss" scoped>
.page {
  padding: 0;
  box-sizing: border-box;
}

.top {
  padding: 10px;
}

.list {
  // margin-top: calc(10px);
  padding: 10px;
  // height: calc(100%);
}

.item {
  width: 100%;
  // height: 204px;
  background: #ffffff;
  border-radius: 6px;
  margin-bottom: 10px;
  box-sizing: border-box;
  padding: 20px 15px;
}

.item-module {
  width: 100%;
  height: 16px;
  position: relative;
  font-weight: 600;
  font-size: 14px;
  color: #333333;
  margin-bottom: 10px;

  .module-border {
    position: absolute;
    left: -15px;
    width: 0px;
    height: 12px;
    border: 1px solid #004098;
  }
}

.item-content {
  position: relative;
  width: 100%;
  // height: calc(38px);
  box-sizing: border-box;
  font-weight: 500;
  font-size: 14px;
  color: #333333;
  line-height: 20px;
  border-bottom: 1px dashed #cacccf;

  &:last-child {
    border-bottom: none;
  }

  &.bold {
    font-weight: 600;
    // :deep(.uni-easyinput__content-input){
    // 	padding-right: 0 !important;
    // }
  }
  &.bottom-bold {
    border-bottom: 1px dashed #cacccf;
  }
}

.item-title {
  position: relative;
  min-height: 38px;
  width: 55%;

  &.total {
    :deep(.is-disabled) {
      color: #333333 !important;
    }
  }
  &.full-cell {
    width: 100%;
    min-width: max-content;
  }
}

.item-value {
  width: 45%;
  position: relative;
  &.textarea {
    width: 65%;
  }
}
.word-break-all {
  min-width: unset;
}
:deep(.uni-select) {
  border: none;
  text-align: right;
  padding-right: 0;
  .uniui-bottom:before {
    content: "\e6b5" !important;
    font-size: 16px !important;
  }
}
:deep(.uni-select__input-text) {
  text-align: right;
  .align-left {
    text-align: right;
  }
}
:deep(.uni-select--disabled) {
  color: #d5d5d5 !important;
  background-color: transparent;
  .uni-select__input-text {
    color: #d5d5d5 !important;
  }
}
:deep(.uni-select__selector) {
  text-align: left;
}
:deep(.uni-select__selector-item) {
  border-bottom: 1px dashed #cacccf;
  text-align: left;
}
:deep(.uni-easyinput__content-textarea) {
  min-height: inherit;
  margin: 10px;
}

.uni-forms {
  .uni-forms-item {
    display: flex;
    align-items: center;
    flex: 1;
    margin-bottom: 0px;
    border-bottom: 1px dashed #cacccf;
  }

  :deep(.uni-forms-item__content) {
    text-align: right;
    .readOnly {
      padding-right: 10px;
    }
  }

  :deep(.uni-forms-item__label) {
    height: 44px;
    font-weight: 500;
    font-size: 14px;
    color: #333333 !important;
    width: max-content !important;
  }

  :deep(.uni-easyinput__content-input) {
    padding: 0 !important;
  }
}

:deep(.uni-easyinput__content-textarea) {
  margin: 0 !important;
}

.divider {
  margin: 0;
  transform: translateY(-2px);
  :deep(.uv-line) {
    border-width: 2px !important;
    border-color: rgb(41, 121, 255) !important;
  }
}

:deep(.datetime-picker) {
  .uniui-calendar {
    &::before {
      display: none;
    }
  }

  .uni-date-editor--x__disabled {
    opacity: 1 !important;
  }

  .uni-date__x-input {
    color: #333 !important;
  }
}
</style>
