"""修改看板HTML，根据角色隐藏成本信息"""
import re, sys
sys.stdout.reconfigure(encoding='utf-8')

html_path = r"C:\Users\Administrator\.openclaw\workspace\letaro\data_model\wang_dashboard.html"

with open(html_path, 'r', encoding='utf-8') as f:
    html = f.read()

# 1. 添加canViewCost函数 - 在unlock()函数后面
old = '''function unlock(){
  document.getElementById('loginOverlay').style.display = 'none';
  document.getElementById('app').style.display = 'block';
  document.querySelector('h1').textContent += ' · ' + ROLE_LABEL;
}'''

new = '''function unlock(){
  document.getElementById('loginOverlay').style.display = 'none';
  document.getElementById('app').style.display = 'block';
  document.querySelector('h1').textContent += ' · ' + ROLE_LABEL;
}

// 权限控制：是否显示成本信息
function canViewCost(){
  return ALLOWED === null; // 高管(boss1688) ALLOWED=null 可看成本，区域经理不可看
}'''

html = html.replace(old, new)

# 2. 修改库存总览KPI - 隐藏成本总额行
old = '''<div class="kc"><div class="l">ææ¬æ»é¢</div><div class="v">\\u00a5${n(Math.round(ov.total_cost))}</div></div>'''
new = '''{canViewCost()?`<div class="kc"><div class="l">ææ¬æ»é¢</div><div class="v">\\u00a5${n(Math.round(ov.total_cost))}</div></div>`:''}'''
html = html.replace(old, new)

# 3. 修改库存总览门店排名表头 - 移除成本列
old = '''<th>åºå­ä»¶æ°</th><th>é¶å®æ»é¢</th><th>ææ¬</th>'''
new = '''<th>åºå­ä»¶æ°</th><th>é¶å®æ»é¢</th>${canViewCost()?'<th>ææ¬</th>':''}'''
html = html.replace(old, new)

# 4. 修改门店排名行 - 隐藏成本数据
old = '''<td>\\u00a5${n(Math.round(s['é¶å®æ»é¢']))}</td>
      <td>\\u00a5${n(Math.round(s['ææ¬æ»é¢']))}</td>'''
new = '''<td>\\u00a5${n(Math.round(s['é¶å®æ»é¢']))}</td>
      ${canViewCost()?`<td>\\u00a5${n(Math.round(s['ææ¬æ»é¢']))}</td>`:''}'''
html = html.replace(old, new)

# 5. 修改全年库存按季节汇总 - 隐藏成本列
old = '''<th>å­£è</th><th>åºå­ä»¶æ°</th><th>é¶å®æ»é¢</th><th>ææ¬æ»é¢</th>'''
new = '''<th>å­£è</th><th>åºå­ä»¶æ°</th><th>é¶å®æ»é¢</th>${canViewCost()?'<th>ææ¬æ»é¢</th>':''}'''
html = html.replace(old, new)

old = '''<td>${s.season}</td><td>${n(s['åºå­ä»¶æ°'])}</td><td>\\u00a5${n(Math.round(s['é¶å®æ»é¢']))}</td><td>\\u00a5${n(Math.round(s['ææ¬æ»é¢']))}</td>'''
new = '''<td>${s.season}</td><td>${n(s['åºå­ä»¶æ°'])}</td><td>\\u00a5${n(Math.round(s['é¶å®æ»é¢']))}</td>${canViewCost()?`<td>\\u00a5${n(Math.round(s['ææ¬æ»é¢']))}</td>`:''}'''
html = html.replace(old, new)

# 6. 修改门店详情季节明细 - 隐藏成本列
old = '''<th>å­£è</th><th>åºå­ä»¶æ°</th><th>é¶å®æ»é¢</th><th>ææ¬</th><th>SKU</th>'''
new = '''<th>å­£è</th><th>åºå­ä»¶æ°</th><th>é¶å®æ»é¢</th>${canViewCost()?'<th>ææ¬</th>':''}<th>SKU</th>'''
html = html.replace(old, new)

old = '''<td>${s.season}</td><td>${n(s['åºå­ä»¶æ°'])}</td><td>\\u00a5${n(Math.round(s['é¶å®æ»é¢']))}</td><td>\\u00a5${n(Math.round(s['ææ¬æ»é¢']))}</td><td>${n(s.SKUæ°)}</td>'''
new = '''<td>${s.season}</td><td>${n(s['åºå­ä»¶æ°'])}</td><td>\\u00a5${n(Math.round(s['é¶å®æ»é¢']))}</td>${canViewCost()?`<td>\\u00a5${n(Math.round(s['ææ¬æ»é¢']))}</td>`:''}<td>${n(s.SKUæ°)}</td>'''
html = html.replace(old, new)

# 7. 修改门店详情的KPI额外成本行
old = '''{l:'ææ¬æ»é¢', v:'\\u00a5'+n(Math.round(cost))}'''
new = '''{l:'ææ¬æ»é¢', v:canViewCost()?'\\u00a5'+n(Math.round(cost)):'-隐藏-'}'''
html = html.replace(old, new)

# 8. 修改库存总览KPI - 把成本列移到canViewCost里
# 注释：库存表里的成本+门店详情里的成本已经处理了，还有一个在showInvStore里

# 保存
with open(html_path, 'w', encoding='utf-8') as f:
    f.write(html)

print("✅ 修改完成")
print(f"文件大小: {len(html)} 字节")

# 验证修改
count = html.count("canViewCost")
print(f"canViewCost 出现次数: {count}（应该>5）")
