// pages/guanshizhongxin/guanshizhongxin.js
import request from '../../utils/request.js';
import popupService from '../../services/popupService.js';
import { buildInviterPeerId } from '../../utils/im-user.js';
import { resolveAvatarUrl } from '../../utils/avatar.js';

Page({
  data: {
    // 图片路径对象（逻辑层拼接完整URL）
    imgUrls: {},

    isGuanshi: false,
    isLoading: false,
    inviteCode: '',
    uid: '',
    nicheng: '',
    avatarUrl: '',

    // 管事信息
    gszhstatus: '',
    yaoqingzongshu: 0,
    fenyongzonge: '0.00',
    fenyongtixian: '0.00',
    yichongzhiDashou: 0,
    zhuanhualv: 0,

    // 称号列表
    chenghaoList: [],

    // 邀请人（组长）缓存
    inviterCache: null,
  },

  onLoad(options) {
    let url = '/pages/dashouduan/dashouduan?registerType=guanshi';
    if (options.scene) {
      try {
        url += '&inviteCode=' + encodeURIComponent(decodeURIComponent(options.scene));
      } catch (e) {
        console.error('scene解码失败', e);
      }
    } else if (options.inviteCode) {
      url += '&inviteCode=' + encodeURIComponent(options.inviteCode);
    }
    wx.redirectTo({ url });
    return;
    // --- 以下保留供恢复参考，不再执行 ---
    // 拼接所有图片完整路径
    const app = getApp();
    const ossImageUrl = app.globalData.ossImageUrl || '';
    const imgBase = ossImageUrl + 'beijing/guanshiduan/';
    this.setData({
      imgUrls: {
        pageBg: imgBase + 'page-bg.jpg',
        topDecor: imgBase + 'top-decor.png',
        cardBg1: imgBase + 'card-bg1.png',
        cardBg2: imgBase + 'card-bg2.png',
        iconRefresh: imgBase + 'icon-refresh.png',
        iconCopy: imgBase + 'icon-copy.png',
        avatarDefault: ossImageUrl + 'avatar/default.jpg',
        iconInvite: imgBase + 'icon-invite.png',
        iconSub: imgBase + 'icon-sub.png',
        iconRecord: imgBase + 'icon-record.png',
        iconRank: imgBase + 'icon-rank.png',
        iconPoster: imgBase + 'icon-poster.png',
        iconWithdraw: imgBase + 'icon-withdraw.png',
        iconContact: imgBase + 'icon-contact.png',
        iconArrow: imgBase + 'icon-arrow.png',
      }
    });

    // 处理扫码 scene
    if (options.scene) {
      try {
        const scene = decodeURIComponent(options.scene);
        options.inviteCode = scene;
      } catch (e) {
        console.error('scene解码失败', e);
      }
    }

    this.checkGuanshiStatus();

    const { inviteCode } = options || {};
    if (inviteCode) {
      this.setData({ inviteCode });
      setTimeout(() => {
        this.handleInviteCodeWithLoginCheck(inviteCode);
      }, 500);
    }

    if (!app.globalData.hasShownPopupOnColdStart) {
      app.globalData.hasShownPopupOnColdStart = true;
      setTimeout(() => {
        popupService.checkAndShow(this, 'guanshiduan');
      }, 300);
    }
  },

  onReady() {
    if (this._isGuanshiPageActive()) {
      this.ensureGuanshiRoleAndTabBar();
    }
  },

  onHide() {
    const popupComp = this.selectComponent('#popupNotice');
    if (popupComp && popupComp.cleanup) {
      popupComp.cleanup();
    }
  },

  onShow() {
    const inviterCache = wx.getStorageSync('inviterCache') || null;
    this.setData({ inviterCache });

    this.registerNotificationComponent();

    if (this._isGuanshiPageActive()) {
      if (!this.data.isGuanshi) {
        this.setData({ isGuanshi: true });
      }
      this.ensureGuanshiRoleAndTabBar();
      setTimeout(() => {
        this.getGuanshiInfo();
        this.fetchChenghaoList();
      }, 300);
    }

    const pages = getCurrentPages();
    const currentPage = pages[pages.length - 1];
    const options = currentPage.options || {};
    if (options.inviteCode && !this._isGuanshiPageActive()) {
      setTimeout(() => {
        this.handleInviteCodeWithLoginCheck(options.inviteCode);
      }, 300);
    }
  },

  // ========== 全局通知注册 ==========
  registerNotificationComponent() {
    const app = getApp();
    const comp = this.selectComponent('#global-notification');
    if (comp && comp.showNotification) {
      app.globalData.globalNotification = {
        show: (data) => comp.showNotification(data),
        hide: () => comp.hideNotification()
      };
    }
  },

  // ========== 称号列表（新接口，区分管事） ==========
  async fetchChenghaoList() {
    try {
      const res = await request({
        url: '/dengji/chenghao',
        method: 'POST',
        data: { role: 'guanshi' }
      });
      if (res && res.data.code === 0 && res.data.data) {
        this.setData({ chenghaoList: res.data.data.chenghao_list || [] });
      }
    } catch (e) {
      console.error('获取称号失败', e);
    }
  },

  // ========== 联系组长（新接口） ==========
  // 管事联系组长 —— 获取组长信息并跳转聊天
  // 管事联系组长
async contactInviter() {
    wx.showLoading({ title: '获取组长信息...', mask: true });
    try {
        const res = await request({
            url: '/yonghu/guanshi_contact',
            method: 'POST'
        });

        if (res && res.data.code === 200 && res.data.data) {
            const inviter = res.data.data;
            const ossImageUrl = getApp().globalData.ossImageUrl || '';
            const fullAvatar = inviter.touxiang
                ? (inviter.touxiang.startsWith('http') ? inviter.touxiang : ossImageUrl + inviter.touxiang)
                : '';

            // 缓存邀请人信息
            const cacheData = {
                uid: inviter.uid,
                nicheng: inviter.nicheng || '',
                avatar: fullAvatar
            };
            wx.setStorageSync('inviterCache', cacheData);
            this.setData({ inviterCache: cacheData });

            // 联系邀请人：统一 Ds 前缀（管事/组长均走打手身份）
            const param = {
                toUserId: buildInviterPeerId(inviter.uid),
                toName: inviter.nicheng || '邀请人',
                toAvatar: fullAvatar
            };
            wx.navigateTo({
                url: '/pages/liaotian/liaotian?data=' + encodeURIComponent(JSON.stringify(param))
            });
        } else {
            wx.showToast({ title: '未绑定邀请人', icon: 'none' });
        }
    } catch (e) {
        wx.showToast({ title: '网络错误', icon: 'none' });
    } finally {
        wx.hideLoading();
    }
},
  // ========== 管事状态辅助 ==========
  _isApiSuccess(body) {
    if (!body) return false;
    const code = Number(body.code);
    return code === 200 || code === 0;
  },

  _getApiMsg(body) {
    if (!body) return '';
    return body.msg || body.message || '';
  },

  _extractUserData(body) {
    if (!body) return {};
    if (body.data && typeof body.data === 'object' && !Array.isArray(body.data)) {
      return body.data;
    }
    const userData = { ...body };
    delete userData.code;
    delete userData.msg;
    delete userData.message;
    return userData;
  },

  _isGuanshiStatusActive(status) {
    return Number(status) === 1;
  },

  _isAlreadyGuanshiMsg(msg) {
    if (!msg || typeof msg !== 'string') return false;
    return msg.includes('已是管事') || msg.includes('已经是管事');
  },

  _syncGuanshiCache() {
    const app = getApp();
    wx.setStorageSync('guanshistatus', 1);
    app.globalData.guanshistatus = 1;
  },

  // 仅同步后端响应里「明确返回」的身份 status 字段（含 0），不臆测、不补打手
  _syncRoleStatuses(data) {
    if (!data || typeof data !== 'object') return;
    const app = getApp();
    const roleFields = ['dashoustatus', 'guanshistatus', 'shangjiastatus', 'zuzhangstatus', 'kaoheguanstatus'];
    roleFields.forEach(field => {
      if (Object.prototype.hasOwnProperty.call(data, field)) {
        const val = Number(data[field]);
        const normalized = Number.isNaN(val) ? 0 : val;
        wx.setStorageSync(field, normalized);
        app.globalData[field] = normalized;
      }
    });
  },

  // 后端若返回打手详情字段则写入 globalData（与 dashoustatus 是否=1 无关）
  _syncDashouGlobalData(data) {
    const app = getApp();
    const map = {
      dashouNicheng: data.dashounicheng,
      zhanghaoStatus: data.zhanghaostatus,
      yongjin: data.yongjin,
      zonge: data.zonge,
      yajin: data.yajin,
      chenghao: data.chenghao,
      jinfen: data.jinfen,
      clumber: data.clumber,
      chengjiaoliang: data.chengjiaoliang,
      zaixianZhuangtai: data.zaixianzhuangtai,
      dashouzhuangtai: data.dashouzhuangtai,
    };
    Object.keys(map).forEach(key => {
      if (map[key] !== undefined) {
        app.globalData[key] = map[key];
      }
    });
  },

  _isGuanshiPageActive() {
    return this.data.isGuanshi || this._isGuanshiStatusActive(wx.getStorageSync('guanshistatus'));
  },

  // 管事页必须切到 guanshi 角色，并强制刷新 TabBar（扫码落地页不在老板 Tab 列表里）
  ensureGuanshiRoleAndTabBar() {
    const app = getApp();
    if (app.setCurrentRole) {
      app.setCurrentRole('guanshi');
    } else {
      app.globalData.currentRole = 'guanshi';
      wx.setStorageSync('currentRole', 'guanshi');
      app.emitEvent('currentRoleChanged', { role: 'guanshi' });
    }
    this._refreshTabBar();
  },

  _refreshTabBar() {
    const tryRefresh = () => {
      const tabBar = this.getTabBar && this.getTabBar();
      if (tabBar && tabBar.refresh) {
        tabBar.refresh();
      }
    };
    tryRefresh();
    [50, 150, 300, 500, 800].forEach((delay) => setTimeout(tryRefresh, delay));
  },

  _applyRegisterSuccess(userData, toastMsg) {
    const app = getApp();
    const data = userData || {};
    if (data.token) wx.setStorageSync('token', data.token);
    if (data.token) app.globalData.token = data.token;

    const profileFields = ['nicheng', 'uid', 'touxiang'];
    profileFields.forEach(field => {
      if (data[field] !== undefined && data[field] !== null) {
        wx.setStorageSync(field, data[field]);
        if (field === 'nicheng') {
          app.globalData.nicheng = data[field];
        }
      }
    });

    // 身份列表完全以后端返回的 *status 字段为准（见 _syncRoleStatuses）
    this._syncRoleStatuses(data);
    // 仅在本页管事注册成功、但后端未带 guanshistatus 时兜底
    if (!Object.prototype.hasOwnProperty.call(data, 'guanshistatus') &&
        !this._isGuanshiStatusActive(wx.getStorageSync('guanshistatus'))) {
      this._syncGuanshiCache();
    }
    this._syncDashouGlobalData(data);

    const groupFields = ['dashouqun', 'dashouqunid', 'guanshiqun', 'guanshiqunid'];
    groupFields.forEach(field => {
      if (data[field] !== undefined) {
        wx.setStorageSync(field, data[field]);
        app.globalData[field] = data[field];
      }
    });

    const guanshiData = {
      gszhstatus: data.gszhstatus ?? '',
      yaoqingzongshu: data.yaoqingzongshu ?? 0,
      fenyongzonge: data.fenyongzonge ?? '0.00',
      fenyongtixian: data.fenyongtixian ?? '0.00',
      yichongzhiDashou: data.yichongzhiDashou ?? 0
    };
    if (!app.globalData.guanshi) app.globalData.guanshi = {};
    Object.assign(app.globalData.guanshi, guanshiData);

    const zhuanhualv = guanshiData.yaoqingzongshu > 0
      ? (guanshiData.yichongzhiDashou / guanshiData.yaoqingzongshu * 100).toFixed(1)
      : 0;

    this.setData({
      isGuanshi: true,
      uid: data.uid || wx.getStorageSync('uid') || '',
      nicheng: data.nicheng || app.globalData.nicheng || wx.getStorageSync('nicheng') || '',
      ...guanshiData,
      zhuanhualv,
      isLoading: false
    });
    this.loadUserInfo();
    this.switchToGuanshiRole();
    wx.hideLoading();
    if (toastMsg) {
      wx.showToast({ title: toastMsg, icon: 'success', duration: 2000 });
    }
  },

  _enterRegisteredGuanshiState(toastMsg, userData) {
    if (userData && typeof userData === 'object') {
      this._applyRegisterSuccess(userData, toastMsg);
      return;
    }
    this._syncGuanshiCache();
    this.setData({ isGuanshi: true, isLoading: false });
    this.loadUserInfo();
    this.switchToGuanshiRole();
    this.getGuanshiInfo(false);
    this.fetchChenghaoList();
    wx.hideLoading();
    if (toastMsg) {
      wx.showToast({ title: toastMsg, icon: 'success', duration: 2000 });
    }
  },

  // ========== 邀请码处理 ==========
  handleInviteCodeWithLoginCheck(inviteCode) {
    const token = wx.getStorageSync('token');
    const uid = wx.getStorageSync('uid');
    if (!token || !uid) {
      this.loginAndRegisterWithInviteCode(inviteCode);
    } else {
      if (!this.data.isGuanshi) {
        wx.showLoading({ title: '自动注册中...', mask: true });
        this.callRegisterApi(inviteCode);
      } else {
        this._enterRegisteredGuanshiState('您已是管事');
      }
    }
  },

  loginAndRegisterWithInviteCode(inviteCode) {
    const that = this;
    wx.showLoading({ title: '登录注册中...', mask: true });
    wx.login({
      success: (loginRes) => {
        if (loginRes.code) {
          that.callLoginRegisterApi(loginRes.code, inviteCode);
        } else {
          wx.hideLoading();
          wx.showToast({ title: '微信登录失败', icon: 'none' });
        }
      },
      fail: () => {
        wx.hideLoading();
        wx.reLaunch({ url: '/pages/gerenzhongxin/gerenzhongxin' });
      }
    });
  },

  callLoginRegisterApi(code, inviteCode) {
    const that = this;
    const app = getApp();
    const apiBaseUrl = app.globalData.apiBaseUrl;
    if (!apiBaseUrl) {
      wx.hideLoading();
      wx.showToast({ title: '服务器配置错误', icon: 'none' });
      return;
    }
    wx.request({
      url: apiBaseUrl + '/yonghu/zuzhangyqmzc',
      method: 'POST',
      data: { code: code, inviteCode: inviteCode },
      header: { 'content-type': 'application/json' },
      success: (res) => {
        const body = res.data || {};
        if (res.statusCode === 200 && that._isApiSuccess(body)) {
          const userData = that._extractUserData(body);
          const toastMsg = that._isAlreadyGuanshiMsg(that._getApiMsg(body))
            ? that._getApiMsg(body)
            : '注册成功！';
          that._applyRegisterSuccess(userData, toastMsg);
        } else if (res.statusCode === 200 && that._isAlreadyGuanshiMsg(that._getApiMsg(body))) {
          that._enterRegisteredGuanshiState(that._getApiMsg(body) || '您已是管事', that._extractUserData(body));
        } else {
          wx.hideLoading();
          that.setData({ isLoading: false });
          wx.showToast({ title: that._getApiMsg(body) || '注册失败', icon: 'none' });
        }
      },
      fail: () => {
        wx.hideLoading();
        wx.showToast({ title: '网络请求失败', icon: 'none' });
      }
    });
  },

  async callRegisterApi(inviteCode) {
    try {
      const res = await request({
        url: '/yonghu/guanshizhuce',
        method: 'POST',
        data: { inviteCode }
      });
      const body = res?.data || {};
      if (res && this._isApiSuccess(body)) {
        const userData = this._extractUserData(body);
        const toastMsg = this._isAlreadyGuanshiMsg(this._getApiMsg(body))
          ? this._getApiMsg(body)
          : '注册成功！';
        this._applyRegisterSuccess(userData, toastMsg);
      } else if (this._isAlreadyGuanshiMsg(this._getApiMsg(body))) {
        this._enterRegisteredGuanshiState(this._getApiMsg(body) || '您已是管事', this._extractUserData(body));
      } else {
        wx.hideLoading();
        this.setData({ isLoading: false });
        wx.showToast({ title: this._getApiMsg(body) || '注册失败', icon: 'none' });
      }
    } catch (e) {
      console.error('注册失败:', e);
      wx.hideLoading();
      this.setData({ isLoading: false });
      wx.showToast({ title: e.message || '注册失败', icon: 'error' });
    }
  },

  switchToGuanshiRole() {
    const app = getApp();
    this.ensureGuanshiRoleAndTabBar();
    if (app.switchRoleAndReconnect) {
      app.switchRoleAndReconnect('dashou').catch(() => {});
    } else if (app.connectForCurrentRole) {
      app.connectForCurrentRole();
    } else if (app.ensureConnection) {
      app.ensureConnection();
    }
  },

  checkGuanshiStatus() {
    try {
      const status = wx.getStorageSync('guanshistatus');
      if (this._isGuanshiStatusActive(status)) {
        this.setData({ isGuanshi: true });
        this.loadUserInfo();
        this.checkGlobalData();
        this.ensureGuanshiRoleAndTabBar();
      } else {
        this.setData({ isGuanshi: false });
      }
    } catch (e) {
      this.setData({ isGuanshi: false });
    }
  },

  loadUserInfo() {
    const app = getApp();
    const globalData = app.globalData || {};
    const touxiang = wx.getStorageSync('touxiang');
    const ossImageUrl = globalData.ossImageUrl || '';
    const morentouxiang = 'avatar/default.jpg';
    let avatarUrl = '';
    if (touxiang) {
      avatarUrl = ossImageUrl + (touxiang.startsWith('/') ? touxiang : '/' + touxiang);
    } else {
      avatarUrl = ossImageUrl + (morentouxiang.startsWith('/') ? morentouxiang : '/' + morentouxiang);
    }
    this.setData({
      avatarUrl,
      uid: wx.getStorageSync('uid') || '',
      nicheng: globalData.nicheng || wx.getStorageSync('nicheng') || ''
    });
  },

  checkGlobalData() {
    const app = getApp();
    const gd = app.globalData.guanshi || {};
    const needRefresh = !gd.yaoqingzongshu && !gd.fenyongzonge && !gd.fenyongtixian;
    if (needRefresh) {
      this.getGuanshiInfo();
    } else {
      this.setDataFromGlobalData();
    }
  },

  setDataFromGlobalData() {
    const gd = getApp().globalData.guanshi || {};
    const zhuanhualv = gd.yaoqingzongshu > 0
      ? (gd.yichongzhiDashou / gd.yaoqingzongshu * 100).toFixed(1)
      : 0;
    this.setData({
      gszhstatus: gd.gszhstatus || '',
      yaoqingzongshu: gd.yaoqingzongshu || 0,
      fenyongzonge: gd.fenyongzonge || '0.00',
      fenyongtixian: gd.fenyongtixian || '0.00',
      yichongzhiDashou: gd.yichongzhiDashou || 0,
      zhuanhualv,
      isLoading: false
    });
  },

  async getGuanshiInfo(showToast = true) {
    this.setData({ isLoading: true });
    try {
      const res = await request({
        url: '/yonghu/guanshixinxi',
        method: 'POST'
      });
      if (res && this._isApiSuccess(res.data)) {
        const data = this._extractUserData(res.data);
        const app = getApp();
        this._syncGuanshiCache();
        if (!app.globalData.guanshi) app.globalData.guanshi = {};
        const updateData = {
          gszhstatus: data.gszhstatus || '',
          yaoqingzongshu: data.yaoqingzongshu || 0,
          fenyongzonge: data.fenyongzonge || '0.00',
          fenyongtixian: data.fenyongtixian || '0.00',
          yichongzhiDashou: data.yichongzhiDashou || 0
        };
        Object.assign(app.globalData.guanshi, updateData);
        const zhuanhualv = updateData.yaoqingzongshu > 0
          ? (updateData.yichongzhiDashou / updateData.yaoqingzongshu * 100).toFixed(1)
          : 0;
        this.setData({
          isGuanshi: true,
          ...updateData,
          zhuanhualv,
          isLoading: false
        });
        if (showToast) {
          wx.showToast({ title: '信息已更新', icon: 'success', duration: 1500 });
        }
      } else {
        this.setData({ isLoading: false });
        if (showToast) {
          wx.showToast({ title: this._getApiMsg(res?.data) || '获取失败', icon: 'none' });
        }
      }
    } catch (e) {
      this.setData({ isLoading: false });
      if (showToast) {
        wx.showToast({ title: e.message || '获取失败', icon: 'error' });
      }
    }
  },

  refreshGuanshiInfo() {
    this.getGuanshiInfo();
    this.fetchChenghaoList();
  },

  onInviteCodeInput(e) {
    this.setData({ inviteCode: e.detail.value.trim() });
  },

  async onRegister() {
    const { inviteCode } = this.data;
    if (!inviteCode) {
      wx.showToast({ title: '请输入邀请码', icon: 'none' });
      return;
    }
    if (inviteCode.length > 100) {
      wx.showToast({ title: '邀请码过长', icon: 'none' });
      return;
    }
    this.setData({ isLoading: true });
    try {
      const res = await request({
        url: '/yonghu/guanshizhuce',
        method: 'POST',
        data: { inviteCode }
      });
      const body = res?.data || {};
      if (res && this._isApiSuccess(body)) {
        const userData = this._extractUserData(body);
        const toastMsg = this._isAlreadyGuanshiMsg(this._getApiMsg(body))
          ? this._getApiMsg(body)
          : '注册成功！';
        this._applyRegisterSuccess(userData, toastMsg);
      } else if (this._isAlreadyGuanshiMsg(this._getApiMsg(body))) {
        this._enterRegisteredGuanshiState(this._getApiMsg(body) || '您已是管事', this._extractUserData(body));
      } else {
        this.setData({ isLoading: false });
        wx.showToast({ title: this._getApiMsg(body) || '注册失败', icon: 'none' });
      }
    } catch (e) {
      this.setData({ isLoading: false });
      wx.showToast({ title: e.message || '注册失败', icon: 'error' });
    }
  },

  copyUid() {
    const { uid } = this.data;
    if (!uid) return;
    wx.setClipboardData({
      data: uid,
      success: () => wx.showToast({ title: '已复制', icon: 'success' })
    });
  },

  // ========== 页面跳转 ==========
  goToWithdraw() { wx.navigateTo({ url: '/pages/tixian/tixian' }); },
  goToInviteUser() { wx.navigateTo({ url: '/pages/yaoqingdashou/yaoqingdashou' }); },
  goToMySubordinate() { wx.navigateTo({ url: '/pages/wodedashou/wodedashou' }); },
  goToRechargeRecord() { wx.navigateTo({ url: '/pages/czjilu/czjilu' }); },
  goToGuanshiRanking() { wx.navigateTo({ url: '/pages/guanshipaihang/guanshipaihang' }); },
  goToPromoPoster() { wx.navigateTo({ url: '/pages/haibao/haibao' }); }
});