<title>منظومة الإمبراطور عمارة 👑 - نسخة التقارير المالية</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
<script src="https://unpkg.com/html5-qrcode"></script>
<style>
:root { --gold: #d4af37; --dark: #0a1128; --green: #10b981; --red: #f43f5e; --blue: #007bff; --light-gold: #fffcf0; }
body { background: #f8fafc; color: #1e293b; font-family: 'Segoe UI', sans-serif; margin: 0; padding: 10px; padding-bottom: 50px; }
.header-box { background: var(--dark); color: white; padding: 15px; border-radius: 20px; text-align: center; border-bottom: 5px solid var(--gold); position: sticky; top: 0; z-index: 1000; }
.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-top: 10px; }
.stat-card { background: rgba(255,255,255,0.1); padding: 8px; border-radius: 10px; border: 1px solid var(--gold); }
.stat-card b { display: block; color: var(--gold); font-size: 1.1rem; }
.control-btns { display: grid; grid-template-columns: repeat(3, 1fr); gap: 5px; margin-top: 15px; }
.btn-sm { padding: 8px; font-size: 0.75rem; border-radius: 8px; border: none; cursor: pointer; color: white; font-weight: bold; }
.card { background: white; border-radius: 15px; padding: 15px; margin-top: 15px; box-shadow: 0 2px 10px rgba(0,0,0,0.05); border: 1px solid #e2e8f0; }
input, select, textarea { width: 100%; padding: 8px; margin-bottom: 8px; border: 1px solid #ddd; border-radius: 8px; box-sizing: border-box; }
/* التحضير والدرجات */
.att-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 10px 0; }
.att-item { background: #f1f5f9; border-radius: 8px; padding: 5px; border: 1px solid #cbd5e1; text-align: center; }
.att-item.active { background: #dcfce7; border-color: var(--green); }
.att-score { width: 100% !important; border: none !important; text-align: center; padding: 4px !important; font-weight: bold; background: white; border-radius: 4px !important; }
.btn { padding: 10px; border-radius: 8px; border: none; font-weight: bold; cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 5px; font-size: 0.85rem; }
.btn-gold { background: var(--gold); color: white; width: 100%; }
.btn-wa { background: #25d366; color: white; }
.btn-danger { background: var(--red); color: white; margin-top: 5px; width: 100%; }
/* شاشة التقارير */
#preview-overlay { display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.95); z-index: 10000; padding: 15px; text-align: center; overflow-y: auto; color: white; }
.report-window { background: white; color: black; border-radius: 15px; padding: 20px; text-align: right; width: 95%; max-width: 600px; margin: 20px auto; }
table { width: 100%; border-collapse: collapse; margin-top: 10px; }
table th, table td { border: 1px solid #ddd; padding: 8px; text-align: right; font-size: 0.85rem; }
table th { background: var(--dark); color: white; }
.pay-yes { color: var(--green); font-weight: bold; }
.pay-no { color: var(--red); font-weight: bold; }
</style>
<div class="header-box">
<h2 style="margin:5px;">مستر أحمد عمارة 👑</h2>
<div class="stats">
<div class="stat-card">صافي الدخل <b id="statNet">0</b></div>
<div class="stat-card">إجمالي السحب <b id="statOut">0</b></div>
</div>
<div class="control-btns">
<button class="btn-sm" style="background: var(--blue);" onclick="showPaymentReport()">📊 كشوف الدفع</button>
<button class="btn-sm" style="background: var(--green);" onclick="showTop()">⭐ الأوائل</button>
<button class="btn-sm" style="background: var(--gold);" onclick="toggleScanner()">📸 سكانر</button>
</div>
</div>
<div class="card">
<input type="text" id="newName" placeholder="اسم الطالب">
<div style="display:flex; gap:5px;">
<input type="tel" id="sPhone" placeholder="واتساب الطالب">
<input type="tel" id="pPhone" placeholder="واتساب ولي الأمر">
</div>
<select id="newGrade">
<option value="1 ثانوي (200ج)">1 ثانوي (200ج)</option>
<option value="2 ثانوي (250ج)">2 ثانوي (250ج)</option>
<option value="3 ثانوي (300ج)">3 ثانوي (300ج)</option>
<option value="3 إعدادي (170ج)">3 إعدادي (170ج)</option>
</select>
<button class="btn btn-gold" onclick="addStudent()">➕ تسجيل الطالب</button>
</div>
<input type="text" id="search" oninput="draw()" placeholder="🔍 ابحث بالاسم أو الكود..." style="margin-top:15px; border: 2px solid var(--gold);">
<div id="list"></div>
<div id="preview-overlay">
<div id="report-content" class="report-window">
</div>
<button class="btn" style="background:white; color:black; margin: 10px auto; width: 150px;" onclick="closeOverlay()">إغلاق الكشف</button>
</div>
<script>
let db = JSON.parse(localStorage.getItem('amara_v8')) || { students: [], expenses: [] };
const prices = { "3 إعدادي (170ج)": 170, "1 ثانوي (200ج)": 200, "2 ثانوي (250ج)": 250, "3 ثانوي (300ج)": 300, "book": 270 };
function save() { localStorage.setItem('amara_v8', JSON.stringify(db)); draw(); }
function addStudent() {
let n = document.getElementById('newName').value;
if(!n) return alert("اكتب الاسم");
db.students.unshift({
id: Math.floor(1000+Math.random()*8999).toString(),
n, sp: document.getElementById('sPhone').value, pp: document.getElementById('pPhone').value,
g: document.getElementById('newGrade').value, paid: false, bookPaid: false,
level: 'جيد', att_list: [0,0,0,0,0,0,0,0], scores: ["","","","","","","",""]
});
save(); document.getElementById('newName').value='';
}
// كشف الدفع الذكي
function showPaymentReport() {
let html = `<h3>📊 كشف تحصيل المصاريف</h3>
<select id="gradeFilter" onchange="generatePaymentTable(this.value)" style="margin-bottom:15px; border:2px solid var(--gold)">
<option value="">اختر المرحلة الدراسية...</option>
<option>1 ثانوي (200ج)</option>
<option>2 ثانوي (250ج)</option>
<option>3 ثانوي (300ج)</option>
<option>3 إعدادي (170ج)</option>
</select>
<div id="tables-area"></div>`;
document.getElementById('report-content').innerHTML = html;
document.getElementById('preview-overlay').style.display = 'block';
}
function generatePaymentTable(grade) {
if(!grade) return;
let students = db.students.filter(s => s.g === grade);
let paid = students.filter(s => s.paid);
let notPaid = students.filter(s => !s.paid);
let html = `<h4>✅ المسددين (${paid.length})</h4>
<table>
<thead><tr><th>الاسم</th><th>الكود</th><th>الحالة</th></tr></thead>
<tbody>`;
paid.forEach(s => html += `<tr><td>${s.n}</td><td>${s.id}</td><td class="pay-yes">تم الدفع</td></tr>`);
html += `</tbody></table><br><h4>❌ المتأخرين (${notPaid.length})</h4>
<table>
<thead><tr><th>الاسم</th><th>الكود</th><th>الحالة</th></tr></thead>
<tbody>`;
notPaid.forEach(s => html += `<tr><td>${s.n}</td><td>${s.id}</td><td class="pay-no">لم يدفع</td></tr>`);
html += `</tbody></table>
<div style="margin-top:15px; font-weight:bold; color:var(--blue);">إجمالي المبلغ المحصل لهذه المرحلة: ${paid.length * (prices[grade] || 0)} ج</div>`;
document.getElementById('tables-area').innerHTML = html;
}
function draw() {
const list = document.getElementById('list');
const q = document.getElementById('search').value.toLowerCase();
list.innerHTML = '';
let totalIn = 0;
db.students.forEach(s => {
if(s.paid) totalIn += (prices[s.g] || 0);
if(s.bookPaid) totalIn += prices.book;
if(s.n.toLowerCase().includes(q) || s.id.includes(q)) {
let attHtml = '';
for(let i=0; i<8; i++) {
attHtml += `<div class="att-item ${s.att_list[i]==1?'active':''}">
<span style="font-size:0.6rem" onclick="toggleAtt('${s.id}',${i})">ح${i+1}</span>
<input type="text" class="att-score" value="${s.scores[i]}" onchange="uScore('${s.id}',${i},this.value)">
</div>`;
}
list.innerHTML += `
<div class="card">
<div style="display:flex; justify-content:space-between; margin-bottom:5px;">
<b>${s.n}</b> <small>ID: ${s.id}</small>
</div>
<div class="att-grid">${attHtml}</div>
<div style="display:grid; grid-template-columns: 1fr 1fr; gap:5px;">
<button class="btn" style="background:${s.paid?'var(--green)':'#ccc'}; color:white;" onclick="uVal('${s.id}','paid',${!s.paid})">💵 دفع</button>
<button class="btn btn-danger" onclick="deleteStudent('${s.id}')">🗑️ حذف</button>
<button class="btn btn-wa" style="grid-column: span 2" onclick="window.open('https://wa.me/2${s.pp}')">واتساب ولي الأمر</button>
</div>
</div>`;
}
});
document.getElementById('statNet').innerText = totalIn;
}
function uScore(id, i, v) { let s=db.students.find(x=>x.id==id); s.scores[i]=v; if(v.trim()!=="") s.att_list[i]=1; save(); }
function uVal(id, k, v) { let s=db.students.find(x=>x.id==id); s[k]=v; save(); }
function toggleAtt(id, i) { let s=db.students.find(x=>x.id==id); s.att_list[i]=s.att_list[i]==1?0:1; save(); }
function deleteStudent(id) { if(confirm("حذف الطالب؟")) { db.students=db.students.filter(s=>s.id!==id); save(); } }
function closeOverlay() { document.getElementById('preview-overlay').style.display='none'; }
draw();
</script>