<view class="page">
  <view class="hero">
    <view class="profile">
      <image wx:if="{{session.avatarUrl}}" class="avatar" src="{{session.avatarUrl}}" mode="aspectFill"></image>
      <view wx:else class="avatar fallback">{{sessionInitial}}</view>
      <view class="profile-body">
        <text class="title">{{session.name || '微信用户'}}</text>
        <text wx:if="{{session.role === 'merchant'}}" class="subtitle">当前身份：{{session.role === 'merchant' ? '商家' : '客户'}}</text>
      </view>
    </view>
  </view>

  <view class="section">
    <text class="section-title">常用资料</text>
    <view class="field">
      <text class="label">默认收货地址</text>
      <input class="input" placeholder="例如：3号宿舍楼 502" value="{{profile.defaultAddress}}" bindinput="updateAddress" />
    </view>
    <view class="field">
      <text class="label">联系电话</text>
      <input class="input" placeholder="请输入手机号" value="{{profile.contactPhone}}" bindinput="updatePhone" />
    </view>
    <button class="primary" loading="{{saving}}" bindtap="saveProfile">保存资料</button>
  </view>

  <view wx:if="{{session.role !== 'merchant'}}" class="section">
    <text class="section-title">成为商家</text>
    <text class="section-desc">填写邀请码后即可切换到商家版。</text>
    <view class="field">
      <text class="label">邀请码</text>
      <input class="input" placeholder="请输入邀请码" value="{{inviteCode}}" bindinput="updateInviteCode" />
    </view>
    <button class="primary" loading="{{switching}}" bindtap="becomeMerchant">成为商家</button>
  </view>

  <view wx:else class="section">
    <text class="section-title">商家设置</text>
    <text class="section-desc">收款码由服务器统一配置，客户支付页会展示同一个微信收款码。</text>

    <view class="qr-grid">
      <view class="qr-card" bindtap="previewQr" data-url="{{profile.paymentAccounts.wechatQr}}">
        <text class="qr-title">微信收款码</text>
        <image wx:if="{{profile.paymentAccounts.wechatQr}}" class="qr-image" src="{{profile.paymentAccounts.wechatQr}}" mode="aspectFit"></image>
        <view wx:else class="qr-placeholder">未配置</view>
      </view>

      <view class="qr-card" bindtap="previewQr" data-url="{{profile.paymentAccounts.alipayQr}}">
        <text class="qr-title">支付宝收款码</text>
        <image wx:if="{{profile.paymentAccounts.alipayQr}}" class="qr-image" src="{{profile.paymentAccounts.alipayQr}}" mode="aspectFit"></image>
        <view wx:else class="qr-placeholder">未配置</view>
      </view>
    </view>

    <button class="primary secondary" bindtap="openMerchantWorkbench">进入商家工作台</button>
    <button class="ghost" loading="{{switching}}" bindtap="switchToCustomer">切回客户版</button>
  </view>

  <view class="section">
    <button class="ghost danger" bindtap="logout">退出登录</button>
  </view>
</view>
