<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, user-scalable=no"
    />
    <title>基本信息</title>
    <script src="/js/mp_base/base.js"></script>

    <style>
      [v-cloak] {
        display: none !important;
      }

      #app {
        background: #f5f5f5;
        min-height: 100vh;
        padding-top: 8px;
        box-sizing: border-box;
      }

      /* 调整班级点名页加载态为纯转圈 by xu 2026-03-06 */
      .loading {
        min-height: 40vh;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 32px 16px;
      }

      .loading-spinner {
        width: 30px;
        height: 30px;
        border: 3px solid #e8f3ed;
        border-top-color: #40ac6d;
        border-radius: 50%;
        animation: page-spin 0.9s linear infinite;
      }
      @keyframes page-spin {
        from {
          transform: rotate(0deg);
        }
        to {
          transform: rotate(360deg);
        }
      }

      .error {
        text-align: center;
        padding: 50px;
        color: #ff4d4f;
      }

      .section-card {
        margin: 0 8px 10px;
        background: #ffffff;
        border-radius: 8px;
        overflow: hidden;
      }

      .form {
        width: 100%;
        border-collapse: collapse;
      }

      .form th {
        width: 140px;
        max-width: 170px;
      }
    </style>
  </head>
  <body>
    <div id="app" v-cloak>
      <!-- 调整班级点名页加载态为纯转圈 by xu 2026-03-06 -->
      <div v-if="loading" class="loading">
        <div class="loading-spinner"></div>
      </div>
      <div v-else-if="error" class="error">{{ error }}</div>
      <!-- 新增班级点名基本信息回显页 by xu 2026-03-06 -->
      <div v-else class="content-div">
        <div class="section-card">
          <table class="form">
            <tr>
              <th>班级</th>
              <td>{{ displayData.bjmc || formData.bjid || '-' }}</td>
            </tr>
            <tr>
              <th>节时间</th>
              <td>{{ displayData.jkssj || '-' }}</td>
            </tr>
            <tr>
              <th>点名时间</th>
              <td>{{ displayData.dmsj || '-' }}</td>
            </tr>
            <tr>
              <th>点名人</th>
              <td>{{ displayData.dmrymc || formData.dmryid || '-' }}</td>
            </tr>
            <tr>
              <th>旷课人数</th>
              <td>{{ displayData.kkrs ?? '-' }}</td>
            </tr>
            <tr>
              <th>请假人数</th>
              <td>{{ displayData.qjrs ?? '-' }}</td>
            </tr>
          </table>
        </div>
      </div>
    </div>

    <script>
      window.SS.ready(function () {
        window.SS.dom.initializeFormApp({
          el: "#app",
          data() {
            return {
              pageParams: {},
              loading: false,
              error: "",
              formData: {},
              displayData: {
                bjmc: "",
                jkssj: "",
                dmsj: "",
                dmrymc: "",
                kkrs: "",
                qjrs: "",
              },
            };
          },
          async mounted() {
            this.pageParams = this.getUrlParams();
            await this.loadData();
          },
          methods: {
            getUrlParams() {
              const params = {};
              const urlSearchParams = new URLSearchParams(
                window.location.search
              );
              for (const [key, value] of urlSearchParams) {
                params[key] = decodeURIComponent(value);
              }
              return params;
            },

            parseParamObject(paramStr) {
              if (!paramStr) return {};
              try {
                return JSON.parse(paramStr);
              } catch (err) {
                try {
                  const fixed = paramStr.replace(
                    /([,{]\s*)([a-zA-Z_][a-zA-Z0-9_]*)\s*:/g,
                    '$1"$2":'
                  );
                  return JSON.parse(fixed);
                } catch (err2) {
                  console.error("解析param失败:", err2);
                  return {};
                }
              }
            },

            async loadData() {
              // 加载班级点名详情数据并回显 by xu 2026-03-06
              const service = this.pageParams.service;
              if (!service) {
                this.error = "缺少service参数";
                return;
              }

              this.loading = true;
              this.error = "";

              try {
                const paramObj = this.parseParamObject(this.pageParams.param);
                const requestData = {
                  ...paramObj,
                };
                const appendIfMissing = (targetKey, sourceKeys) => {
                  if (
                    requestData[targetKey] !== undefined &&
                    requestData[targetKey] !== null &&
                    requestData[targetKey] !== ""
                  ) {
                    return;
                  }
                  for (let i = 0; i < sourceKeys.length; i += 1) {
                    const sourceKey = sourceKeys[i];
                    const value = this.pageParams[sourceKey];
                    if (value !== undefined && value !== null && value !== "") {
                      requestData[targetKey] = value;
                      return;
                    }
                  }
                };

                appendIfMissing("sqid", ["sqid"]);
                appendIfMissing("shid", ["shid"]);
                appendIfMissing("ssObjName", ["ssObjName", "ssobjname"]);
                appendIfMissing("ssObjId", ["ssObjId", "ssobjid"]);
                appendIfMissing("bdlbm", ["bdlbm"]);
                appendIfMissing("dataType", ["dataType", "datatype"]);
                appendIfMissing("encode_shid", ["encode_shid"]);
                appendIfMissing("jdmc", ["jdmc"]);
                appendIfMissing("ssToken", ["ssToken"]);

                const response = await request.post(
                  `/service?ssServ=${service}&ssDest=data`,
                  requestData,
                  { loading: false, formData: true }
                );

                const raw = this.pickBjdmData(response?.data);
                if (!raw) {
                  this.error = "未获取到班级点名数据";
                  return;
                }

                this.formData = raw;
                await this.buildDisplayData(raw);
              } catch (error) {
                console.error("加载班级点名基本信息失败:", error);
                this.error = "加载失败：" + (error.message || "未知错误");
              } finally {
                this.loading = false;
              }
            },

            pickBjdmData(data) {
              if (!data) return null;
              if (data.bjdm) return data.bjdm;
              if (Array.isArray(data.objectList) && data.objectList.length > 0) {
                return data.objectList[0]?.bjdm || data.objectList[0];
              }
              if (Array.isArray(data.data) && data.data.length > 0) {
                return data.data[0]?.bjdm || data.data[0];
              }
              if (typeof data === "object") return data;
              return null;
            },

            async buildDisplayData(raw) {
              // 格式化班级点名显示字段 by xu 2026-03-06
              const bjmc = await this.translateDict(
                "bj",
                raw.bjid,
                raw.bjmc || raw.bjname || raw.bjdm_mc
              );
              const dmrymc = await this.translateDict(
                "ry",
                raw.dmryid,
                raw.dmrymc || raw.dmryname || raw.xm
              );

              this.displayData = {
                bjmc,
                jkssj: this.formatDate(raw.jkssj),
                dmsj: this.formatDate(raw.dmsj),
                dmrymc,
                kkrs: raw.kkrs,
                qjrs: raw.qjrs,
              };
            },

            formatDate(value) {
              if (!value) return "";
              if (
                window.H5FieldFormatter &&
                typeof window.H5FieldFormatter.formatDate === "function"
              ) {
                return window.H5FieldFormatter.formatDate(
                  value,
                  "YYYY-MM-DD HH:mm:ss"
                );
              }
              return value;
            },

            async translateDict(dictName, value, fallbackName) {
              if (fallbackName) return fallbackName;
              if (value === undefined || value === null || value === "") {
                return "";
              }

              try {
                if (typeof window.getDictOptions === "function") {
                  const options = await window.getDictOptions(dictName);
                  const target = (options || []).find(
                    (item) => String(item.v) === String(value)
                  );
                  if (target && target.n) return target.n;
                }
              } catch (error) {
                console.error(`${dictName} 字典翻译失败:`, error);
              }

              return String(value);
            },
          },
        });
      });
    </script>
  </body>
</html>
