export function inputStatusText(status) {
  const map = { 0: '待审核', 1: '审核通过', 2: '审核拒绝' }
  return map[Number(status)] ?? '-'
}

function statusText(status) {
  return inputStatusText(status)
}

function formatTime(ts) {
  if (!ts) return '-'
  const d = new Date(Number(ts) * 1000)
  const pad = (n) => String(n).padStart(2, '0')
  return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}`
}

function storeTypeText(type) {
  return Number(type) === 0 ? '诊所' : '药店'
}

function clinicTypeText(type) {
  if (Number(type) === 1) return '西医诊所'
  if (Number(type) === 2) return '中医诊所'
  return '未设置'
}

function doctorTypeText(type) {
  if (Number(type) === 1) return '中医医生'
  if (Number(type) === 2) return '西医医生'
  return '-'
}

function signTypeText(signType) {
  if (Number(signType) === 1) return '电子签名'
  if (Number(signType) === 2) return '手写签名'
  return '-'
}

function yesNoText(v) {
  return Number(v) === 1 ? '是' : '否'
}

function regionText(info) {
  const p = info.province?.name || ''
  const c = info.city?.name || ''
  return [p, c].filter(Boolean).join(' ') || '-'
}

function storeNamesText(info) {
  const stores = info.stores
  if (Array.isArray(stores) && stores.length) {
    const names = stores.map((s) => s.store?.name || s.name).filter(Boolean)
    if (names.length) return names.join('、')
  }
  if (info.store?.name) return info.store.name
  return '-'
}

function promoterText(info) {
  const u = info.promoter_admin || info.promoter || info.inputUser
  if (!u) return info.code || '-'
  return [u.nick_name, u.code].filter(Boolean).join(' · ') || '-'
}

export function getPromoterAdmin(info) {
  if (info?.promoter_admin) return info.promoter_admin
  if (info?.code) {
    return {
      id: info.promoter?.id,
      code: info.code,
      nick_name: info.promoter?.nick_name,
      avatar: info.promoter?.avatar,
      phone: info.promoter?.phone,
      role_name: info.promoter?.role_name,
    }
  }
  return null
}

function bankTypeText(type) {
  return Number(type) === 1 ? '对公' : '对私'
}

export function getStoreDetailSections() {
  return [
    {
      title: '基础信息',
      fields: [
        { key: 'id', label: 'ID' },
        { key: 'name', label: '门店名称' },
        { key: 'type', label: '门店类型', format: (_, info) => storeTypeText(info.type) },
        { key: 'clinic_type', label: '诊所类型', format: (_, info) => clinicTypeText(info.clinic_type) },
        { key: 'contact', label: '联系人' },
        { key: 'mobile', label: '联系电话' },
        { key: 'region', label: '省市区', format: (_, info) => regionText(info) },
        { key: 'position', label: '详细地址' },
        { key: 'code', label: '业务员', type: 'promoter' },
        { key: 'is_shipping_free', label: '是否包邮', format: (_, info) => yesNoText(info.is_shipping_free) },
        { key: 'subscribe_price_change', label: '订阅价格波动', format: (_, info) => yesNoText(info.subscribe_price_change) },
        {
          key: 'business_hours',
          label: '营业时间',
          format: (_, info) => {
            const s = info.start_time || ''
            const e = info.end_time || ''
            return s && e ? `${s} - ${e}` : '-'
          },
        },
      ],
    },
    {
      title: '结算信息',
      fields: [
        { key: 'bank_type', label: '账户类型', format: (_, info) => bankTypeText(info.bank_type) },
        { key: 'bank_user_name', label: '开户人' },
        { key: 'bank_card', label: '银行卡号' },
        { key: 'bank_name', label: '开户行' },
        { key: 'bank_no', label: '银联行号' },
        { key: 'z_buy_percent', label: '中药采购比例(%)' },
        { key: 'z_sale_percent', label: '中药销售比例(%)' },
      ],
    },
    {
      title: '审核信息',
      fields: [
        { key: 'inputUser', label: '录入人', format: (_, info) => info.inputUser?.nick_name || '-' },
        { key: 'status', label: '审核状态', format: (_, info) => statusText(info.status) },
        { key: 'auditAdmin', label: '审核人', format: (_, info) => info.auditAdmin?.nick_name || '-', show: (info) => !!info.auditAdmin },
        { key: 'audit_time', label: '审核时间', format: (_, info) => formatTime(info.audit_time), show: (info) => !!info.audit_time },
        { key: 'audit_remark', label: '审核备注', show: (info) => !!info.audit_remark },
      ],
    },
    {
      title: '图片资料',
      fields: [
        { key: 'see_rate', label: '公章', type: 'image' },
        { key: 'url', label: '轮播图', type: 'images' },
        { key: 'contract_files', label: '合同文件', type: 'files' },
      ],
    },
  ]
}

export function getDoctorDetailSections() {
  return [
    {
      title: '基础信息',
      fields: [
        { key: 'id', label: 'ID' },
        { key: 'name', label: '医生姓名' },
        { key: 'mobile', label: '手机号' },
        { key: 'idcard', label: '身份证' },
        { key: 'type', label: '身份', format: (_, info) => doctorTypeText(info.type) },
        { key: 'sign_type', label: '签名类型', format: (_, info) => signTypeText(info.sign_type) },
        { key: 'depart', label: '科室', format: (_, info) => info.depart?.name || '-' },
        { key: 'titleInfo', label: '职称', format: (_, info) => info.titleInfo?.name || '-' },
        { key: 'stores', label: '关联门店', format: (_, info) => storeNamesText(info) },
        { key: 'good_at', label: '擅长', multiline: true },
        { key: 'intro', label: '简介', multiline: true },
      ],
    },
    {
      title: '审核信息',
      fields: [
        { key: 'inputUser', label: '录入人', format: (_, info) => info.inputUser?.nick_name || '-' },
        { key: 'status', label: '审核状态', format: (_, info) => statusText(info.status) },
        { key: 'auditAdmin', label: '审核人', format: (_, info) => info.auditAdmin?.nick_name || '-', show: (info) => !!info.auditAdmin },
        { key: 'audit_time', label: '审核时间', format: (_, info) => formatTime(info.audit_time), show: (info) => !!info.audit_time },
        { key: 'audit_remark', label: '审核备注', show: (info) => !!info.audit_remark },
      ],
    },
    {
      title: '证照资料',
      fields: [
        { key: 'avatar', label: '头像', type: 'image' },
        { key: 'qualification', label: '资格证书', type: 'image' },
        { key: 'practicing', label: '执业证书', type: 'image' },
        { key: 'title', label: '职称证书', type: 'image' },
        { key: 'card_up', label: '身份证正面', type: 'image' },
        { key: 'card_down', label: '身份证反面', type: 'image' },
        { key: 'sign_image', label: '签名图片', type: 'image' },
      ],
    },
  ]
}

export function formatDetailFieldValue(field, info) {
  if (field.show && !field.show(info)) return null
  if (field.format) return field.format(field.key, info)
  const v = info[field.key]
  if (v == null || v === '') return '-'
  if (Array.isArray(v) && !v.length) return null
  return String(v)
}

export function getDetailImageUrls(field, info) {
  const v = info[field.key]
  if (!v) return []
  if (field.type === 'images' && Array.isArray(v)) return v.filter(Boolean)
  if (field.type === 'image' && typeof v === 'string') return [v]
  return []
}

/** 门店详情关联的医生预填列表 */
export function getLinkedDoctorInputs(info) {
  const list = info?.doctor_inputs ?? info?.doctorInputs ?? []
  return Array.isArray(list) ? list : []
}

export function getDetailFileLinks(field, info) {
  const files = info[field.key]
  if (!Array.isArray(files) || !files.length) return []
  return files.map((f) => {
    if (typeof f === 'string') return { url: f, name: f.split('/').pop() || '合同文件' }
    return { url: f.file_url || f.url || '', name: f.file_name || f.name || '合同文件' }
  }).filter((f) => f.url)
}
