import { req } from '@/common/js/index.js';
import { unwrapClinicRes } from '@/api/clinicAdmin.js';

function getPrefix() {
  const mode = uni.getStorageSync('loginMode');
  if (mode === 'platform_admin') {
    return { prefix: '/newApi/platform-admin-store-bank-card-report', headerKey: '_platformAdmin' };
  }
  return { prefix: '/newApi/salesperson-store-bank-card-report', headerKey: '_salesperson' };
}

function bizRequest(options) {
  const { prefix, headerKey } = getPrefix();
  return req.request({
    ...options,
    url: `${prefix}${options.path}`,
    header: {
      ...(options.header || {}),
      [headerKey]: '1',
    },
  }).then((res) => {
    const data = unwrapClinicRes(res);
    const ok = res && (res.code === 0 || (res.errcode != null && Number(res.errcode) !== -1));
    return { res, data, ok };
  });
}

export function getStoreBankCardReportDetail(storeId) {
  return bizRequest({ path: '/detail', method: 'GET', data: { store_id: storeId } });
}

export function reportStoreBankCard(data) {
  return bizRequest({ path: '/report', method: 'POST', data });
}

export function updateStoreBankCard(data) {
  return bizRequest({ path: '/update', method: 'POST', data });
}

export function syncStoreBankCardStatusByStore(storeId) {
  return bizRequest({ path: '/sync-status-by-store', method: 'POST', data: { store_id: storeId } });
}
