const ROOT_WIDTH = '560rpx'
const CARD_BG = '#F8FAFF'

function cardWrapper(views) {
  return {
    css: { width: ROOT_WIDTH },
    views: [{
      type: 'view',
      css: {
        width: ROOT_WIDTH,
        borderRadius: '16rpx',
        paddingBottom: '68rpx',
        paddingTop: '30rpx',
        backgroundColor: CARD_BG,
      },
      views,
    }],
  }
}

/**
 * 强化居中组件 (仅用于诊所/推广员的标题，不影响医生)
 */
function centerText(text, css = {}) {
  return {
    type: 'view',
    css: { width: ROOT_WIDTH, textAlign: 'center', marginTop: '8rpx', ...css },
    views: [{ type: 'text', text: text || '', css: { color: '#333', fontSize: '28rpx', textAlign: 'center', ...css } }],
  }
}

// ----------------------------------------------------------------
// 医生海报 - 100% 还原为你最初始的代码，未作任何更改
// ----------------------------------------------------------------
function buildDoctorPoster(info) {
  const doctor = info.DoctorInfo || {}
  const store = (info.store && info.store.store) ? info.store.store : {}
  const depart = (doctor.depart && doctor.depart.name) ? doctor.depart.name : ''
  const titleName = (doctor.title && doctor.title.name) ? doctor.title.name : ''
  let sex = 1
  if (doctor.idcard) {
    sex = doctor.idcard.charAt(16)
  }
  const defaultAvatar = `/static/image/${sex % 2 === 0 ? 'nv' : 'nan'}.png`
  const avatar = doctor.avatar || defaultAvatar
  const qrUrl = doctor.qr_code || ''

  return cardWrapper([
    {
      type: 'image',
      src: avatar,
      css: {
        width: '180rpx',
        height: '180rpx',
        borderRadius: '50%',
        marginLeft: '190rpx',
      },
    },
    {
      type: 'view',
      css: { textAlign: 'center', marginTop: '8rpx' },
      views: [
        { type: 'text', text: doctor.name || '', css: { color: '#000', fontSize: '36rpx' } },
        {
          type: 'text',
          text: titleName,
          css: { color: '#1E293B', fontSize: '28rpx', paddingTop: '10rpx', marginLeft: '8rpx' },
        },
      ],
    },
    {
      type: 'view',
      css: { textAlign: 'center', marginTop: '8rpx' },
      views: [
        { type: 'text', text: store.name || '', css: { color: '#475569', fontSize: '24rpx' } },
        { type: 'text', text: depart, css: { color: '#475569', fontSize: '24rpx', marginLeft: '10rpx' } },
      ],
    },
    {
      type: 'view',
      css: { textAlign: 'center', marginTop: '20rpx', marginBottom: '20rpx' },
      views: [{
        type: 'image',
        src: qrUrl,
        css: { width: '388rpx', height: '388rpx' },
      }],
    },
    {
      type: 'view',
      css: { paddingLeft: '20rpx', paddingRight: '20rpx' },
      views: [
        {
          type: 'text',
          text: '专业擅长',
          css: { color: '#6ACDBB', fontSize: '28rpx', textAlign: 'center', marginLeft: '56rpx' },
        },
        {
          type: 'text',
          text: doctor.good_at || '',
          css: { color: '#000', fontSize: '24rpx', marginLeft: '8rpx', marginTop: '4rpx' },
        },
      ],
    },
  ])
}

// ----------------------------------------------------------------
// 推广员海报 - 修复文本居中、图片居中、Label同行不换行
// ----------------------------------------------------------------
function buildSalespersonPoster(payload) {
  const store = (payload.qr_code && payload.qr_code.store) ? payload.qr_code.store : {}
  const qrUrl = (payload.qr_code && payload.qr_code.qr_code) ? payload.qr_code.qr_code : ''
  const storeName = store.name || ''
  const position = store.position || ''
  const nickName = payload.nick_name || ''

  return cardWrapper([
    // 店铺名居中
    centerText(storeName, { fontSize: '36rpx', fontWeight: 'bold', marginTop: '20rpx' }),

    // 诊所地址 Label 和 具体地址拆分为两行居中（支持长文本折行）
    centerText('诊所地址：', { color: '#3a8ee6', fontSize: '24rpx', marginTop: '20rpx' }),
    {
      type: 'view',
      css: { width: ROOT_WIDTH, marginTop: '8rpx' },
      views: [
        {
          type: 'text',
          text: position,
          css: {
            color: '#333',
            fontSize: '24rpx',
            textAlign: 'center',
            width: '512rpx',
            marginLeft: '24rpx',
            maxLines: 2,
            lineHeight: '36rpx'
          }
        }
      ],
    },

    // 还原回初始的图片容器写法：依靠 textAlign 居中图片，不干预 width
    {
      type: 'view',
      css: { textAlign: 'center', marginTop: '24rpx', marginBottom: '24rpx' },
      views: [{
        type: 'image',
        src: qrUrl,
        css: { width: '388rpx', height: '388rpx' },
      }],
    },

    // 推广员信息：保持在同一行（不换行），整体水平居中
    {
      type: 'view',
      css: { width: ROOT_WIDTH, textAlign: 'center' },
      views: [
        { type: 'text', text: '推广员：', css: { color: '#3a8ee6', fontSize: '24rpx', display: 'inline-block' } },
        { type: 'text', text: nickName, css: { color: '#333', fontSize: '28rpx', display: 'inline-block' } },
      ]
    }
  ])
}

// ----------------------------------------------------------------
// 诊所管理员海报 - 修复文本居中、图片居中、地址换行
// ----------------------------------------------------------------
function buildClinicPoster(payload) {
  const title = payload.title || ''
  const address = payload.address || ''
  const qrUrl = payload.qr_code || ''

  return cardWrapper([
    // 诊所名称居中
    centerText(title, { fontSize: '36rpx', fontWeight: 'bold', marginTop: '20rpx' }),

    // 还原回初始的图片容器写法：依靠 textAlign 居中图片
    {
      type: 'view',
      css: { textAlign: 'center', marginTop: '24rpx', marginBottom: '24rpx' },
      views: [{
        type: 'image',
        src: qrUrl,
        css: { width: '388rpx', height: '388rpx' },
      }],
    },

    // 拆分诊所地址，使其换行居中，并支持过长折行
    centerText('诊所地址：', { color: '#3a8ee6', fontSize: '24rpx' }),
    {
      type: 'view',
      css: { width: ROOT_WIDTH, marginTop: '8rpx' },
      views: [
        {
          type: 'text',
          text: address,
          css: {
            color: '#333',
            fontSize: '24rpx',
            textAlign: 'center',
            width: '512rpx',
            marginLeft: '24rpx',
            maxLines: 2,
            lineHeight: '36rpx'
          }
        }
      ],
    },
  ])
}

export function buildQrOnlyPoster(qrUrl) {
  return {
    css: { width: '420rpx' },
    views: [{
      type: 'view',
      css: {
        width: '420rpx',
        padding: '24rpx',
        backgroundColor: '#fff',
        borderRadius: '16rpx',
        textAlign: 'center',
      },
      views: [{
        type: 'image',
        src: qrUrl || '',
        css: { width: '388rpx', height: '388rpx' },
      }],
    }],
  }
}

export function buildPosterBoard(type, payload) {
  if (type === 'salesperson') {
    return buildSalespersonPoster(payload || {})
  }
  if (type === 'clinic') {
    return buildClinicPoster(payload || {})
  }
  const info = payload || uni.getStorageSync('doctorInfo') || {}
  return buildDoctorPoster(info)
}

export function resolveQrImageUrl(type, payload) {
  if (type === 'salesperson') {
    return (payload && payload.qr_code && payload.qr_code.qr_code) ? payload.qr_code.qr_code : ''
  }
  if (type === 'clinic') {
    return (payload && payload.qr_code) ? payload.qr_code : ''
  }
  const info = payload || uni.getStorageSync('doctorInfo') || {}
  return (info.DoctorInfo && info.DoctorInfo.qr_code) ? info.DoctorInfo.qr_code : ''
}

export function navTitleForType(type) {
  if (type === 'salesperson') return '推广二维码'
  if (type === 'clinic') return '诊所二维码'
  return '医生码'
}

export function buttonLabelsForType(type) {
  if (type === 'clinic') {
    return { card: '保存诊所卡片图片', qr: '保存诊所二维码图片' }
  }
  return { card: '二维码卡片下载', qr: '二维码下载' }
}