<?php require_once __DIR__.'/lib/v7_bootstrap.php';$uid=trim((string)($_GET['user_id']??$_POST['user_id']??''));$flash=['ok'=>'','err'=>''];
if($_SERVER['REQUEST_METHOD']==='POST' && isset($_POST['_action'])){itz_v7_csrf($csrf);try{$act=(string)$_POST['_action'];if($act==='tag'&&$uid!==''){$k=preg_replace('/[^a-z0-9_.-]/i','',(string)$_POST['tag_key']);if($k==='')throw new RuntimeException('Tag key لازم است');$v=(string)($_POST['tag_value']??'1');$s=$pdo->prepare('INSERT INTO itz_v7_user_tags(user_id,tag_key,tag_value,created_at,updated_at) VALUES(:u,:k,:v,:created_at,:updated_at) ON DUPLICATE KEY UPDATE tag_value=VALUES(tag_value),updated_at=VALUES(updated_at)');$s->execute([':u'=>$uid,':k'=>$k,':v'=>$v,':created_at'=>time(),':updated_at'=>time()]);$flash['ok']='Tag ذخیره شد.';}if($act==='task'&&$uid!==''){$s=$pdo->prepare('INSERT INTO itz_v7_admin_tasks(title,description_text,user_id,priority_name,source_name,created_at,updated_at) VALUES(:t,:d,:u,:p,:s,:created_at,:updated_at)');$s->execute([':t'=>(string)$_POST['title'],':d'=>(string)($_POST['description']??''),':u'=>$uid,':p'=>(string)($_POST['priority']??'normal'),':s'=>'customer360',':created_at'=>time(),':updated_at'=>time()]);$flash['ok']='Task ساخته شد.';}}catch(Throwable $e){$flash['err']=$e->getMessage();}}
$data=$uid!==''?$v7->customer360($uid):[];$tasks=[];if($uid!==''){$s=$pdo->prepare('SELECT * FROM itz_v7_admin_tasks WHERE user_id=:u ORDER BY id DESC LIMIT 20');$s->execute([':u'=>$uid]);$tasks=$s->fetchAll(PDO::FETCH_ASSOC)?:[];}
?>
<!doctype html><html lang="fa" dir="rtl"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Customer 360 | iTzAYDiN-BOT</title><link rel="stylesheet" href="css/theme.css"><link rel="stylesheet" href="css/admin-extra.css"><link rel="stylesheet" href="css/ay-controls.css"><script src="js/theme.js" defer></script><style>.cgrid{display:grid;grid-template-columns:1.2fr .8fr;gap:12px}.score{width:100px;height:100px;border-radius:50%;display:grid;place-items:center;border:8px solid color-mix(in srgb,var(--accent) 35%,var(--border-soft));font-size:28px;font-weight:900}.tags{display:flex;gap:6px;flex-wrap:wrap}.tags span{padding:5px 8px;border-radius:999px;background:color-mix(in srgb,var(--accent) 10%,transparent);color:var(--accent)}@media(max-width:800px){.cgrid{grid-template-columns:1fr}}</style></head><body><section id="container"><?php include 'header.php';?><section id="main-content"><div class="wrapper"><div class="page-head"><div><div class="page-head__title">🧠 Customer 360</div><div class="page-head__sub">یک نمای عملیاتی از مشتری، خریدها، Tagها و Taskها</div></div></div><form method="get" class="card" style="display:flex;gap:8px"><input class="form-control" name="user_id" placeholder="Telegram User ID" value="<?=itz_v7_h($uid)?>"><button class="btn btn-primary">باز کردن پروفایل</button></form><?php if($flash['ok']):?><div class="alert alert-success"><?=itz_v7_h($flash['ok'])?></div><?php endif;?><?php if($flash['err']):?><div class="alert alert-danger"><?=itz_v7_h($flash['err'])?></div><?php endif;?>
<?php if($uid!=='' && $data):?><div class="cgrid"><div class="card"><div style="display:flex;justify-content:space-between;align-items:center"><div><small>User ID</small><h2><?=itz_v7_h($uid)?></h2><div class="tags"><?php foreach($data['signals'] as $x):?><span><?=itz_v7_h($x)?></span><?php endforeach;?><?php foreach($data['tags'] as $k=>$v):?><span><?=itz_v7_h($k.'='.$v)?></span><?php endforeach;?></div></div><div class="score"><?=intval($data['score'])?></div></div><pre style="white-space:pre-wrap;direction:ltr;font-size:11px;max-height:220px;overflow:auto"><?=itz_v7_h(json_encode($data['user'],JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT))?></pre></div><div class="card"><form method="post"><input type="hidden" name="_csrf" value="<?=itz_v7_h($csrf)?>"><input type="hidden" name="_action" value="tag"><input type="hidden" name="user_id" value="<?=itz_v7_h($uid)?>"><h3>Tag</h3><input class="form-control" name="tag_key" placeholder="vip / churn_risk / reseller"><input class="form-control" name="tag_value" placeholder="value" value="1"><button class="btn btn-outline btn-sm">ذخیره Tag</button></form><hr><form method="post"><input type="hidden" name="_csrf" value="<?=itz_v7_h($csrf)?>"><input type="hidden" name="_action" value="task"><input type="hidden" name="user_id" value="<?=itz_v7_h($uid)?>"><h3>Task ادمین</h3><input class="form-control" name="title" placeholder="عنوان" required><textarea class="form-control" name="description" placeholder="توضیح"></textarea><select class="form-control" name="priority"><option>normal</option><option>high</option><option>urgent</option></select><button class="btn btn-primary btn-sm">ساخت Task</button></form></div></div>
<div class="cgrid" style="margin-top:12px"><div class="card"><div class="card__head"><div class="card__title">آخرین فاکتورها</div></div><div class="table-wrap"><table class="display app-table"><thead><tr><th>ID</th><th>Status</th><th>Price</th><th>Location</th></tr></thead><tbody><?php foreach($data['invoices'] as $i):?><tr><td><?=itz_v7_h($i['id_invoice']??'')?></td><td><?=itz_v7_h($i['Status']??'')?></td><td><?=itz_v7_h($i['price']??'')?></td><td><?=itz_v7_h($i['Service_location']??'')?></td></tr><?php endforeach;?></tbody></table></div></div><div class="card"><div class="card__head"><div class="card__title">Taskها</div></div><?php foreach($tasks as $t):?><div style="padding:9px;border-bottom:1px solid var(--border-soft)"><b><?=itz_v7_h($t['title'])?></b><small style="display:block"><?=itz_v7_h($t['status_name'].' · '.$t['priority_name'])?></small></div><?php endforeach;?></div></div><?php endif;?>
</div></section></section></body></html>
