/** 客服邀请码管理 — 对接 merchant-staff API */
import request from '../../utils/request.js';
import { STAFF_API } from '../../utils/staff-api.js';

Page({
  data: {
    statusBar: 20,
    gudingCode: 'XQKF2026',
    kefuTips: '一次性邀请码绑定子客服，固定码仅展示不可绑定。',
    sublist: [{ name: '全部', status: null }, { name: '未使用', status: 0 }, { name: '已使用', status: 1 }],
    subCurrent: 0,
    list: [],
    roles: [],
    showQrcodeTips: false,
    loading: false,
  },

  onLoad() {
    const sys = wx.getSystemInfoSync();
    this.setData({ statusBar: sys.statusBarHeight || 20 });
    this.loadRoles();
    this.loadList();
  },

  onShow() {
    this.loadList();
  },

  async loadRoles() {
    try {
      const res = await request({ url: STAFF_API.roleList, method: 'POST' });
      if (res.data && res.data.code === 0) {
        this.setData({ roles: res.data.data.roles || [] });
      }
    } catch (e) {}
  },

  async loadList() {
    const tab = this.data.sublist[this.data.subCurrent];
    try {
      const res = await request({
        url: STAFF_API.inviteList,
        method: 'POST',
        data: tab.status === null ? {} : { status: tab.status },
      });
      if (res.data && res.data.code === 0) {
        const d = res.data.data || {};
        this.setData({
          list: d.list || [],
          gudingCode: d.gudingCode || 'XQKF2026',
        });
      }
    } catch (e) {
      wx.showToast({ title: '加载失败', icon: 'none' });
    }
  },

  goBack() { wx.navigateBack(); },

  copyFixed() {
    wx.setClipboardData({ data: this.data.gudingCode });
  },

  switchSub(e) {
    this.setData({ subCurrent: Number(e.currentTarget.dataset.idx) }, () => this.loadList());
  },

  async addKey() {
    const roles = this.data.roles;
    if (!roles.length) {
      wx.showToast({ title: '暂无角色，请稍后重试', icon: 'none' });
      return;
    }
    const names = roles.map(r => r.role_name);
    wx.showActionSheet({
      itemList: names,
      success: async (r) => {
        const role = roles[r.tapIndex];
        wx.showLoading({ title: '生成中' });
        try {
          const res = await request({
            url: STAFF_API.inviteCreate,
            method: 'POST',
            data: { role_id: role.id },
          });
          if (res.data && res.data.code === 0) {
            wx.showToast({ title: '已生成', icon: 'success' });
            this.loadList();
          } else {
            wx.showToast({ title: res.data.msg || '失败', icon: 'none' });
          }
        } finally {
          wx.hideLoading();
        }
      },
    });
  },

  copyCode(e) {
    wx.setClipboardData({ data: e.currentTarget.dataset.code || '' });
  },

  async delKey(e) {
    const id = e.currentTarget.dataset.id;
    if (!id) return;
    const res = await request({
      url: STAFF_API.inviteRevoke,
      method: 'POST',
      data: { id },
    });
    if (res.data && res.data.code === 0) {
      wx.showToast({ title: '已作废' });
      this.loadList();
    } else {
      wx.showToast({ title: res.data.msg || '失败', icon: 'none' });
    }
  },

  showTips() { this.setData({ showQrcodeTips: true }); },
  closeTips() { this.setData({ showQrcodeTips: false }); },
  goAudit() {
    wx.navigateTo({ url: '/pages/merchant-staff-audit/merchant-staff-audit' });
  },
  goToRoleManage() {
    wx.navigateTo({ url: '/pages/merchant-staff-role/merchant-staff-role' });
  },
  noop() {},
});
