<!DOCTYPE html>
<html lang="hi">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CULT THRIFT</title>
<style>
body{background:#101010;color:#eee;font:16px system-ui;max-width:720px;margin:auto;padding:16px}
h1,h2{color:#d7b35b}
section{background:#1b1b1b;padding:16px;margin:12px 0;border-radius:12px}
input,button{display:block;width:100%;box-sizing:border-box;padding:12px;margin:8px 0;border-radius:8px}
input{background:#111;color:white;border:1px solid #555}
button{background:#d7b35b;border:0;font-weight:bold}
.hide{display:none}
.table{overflow:auto}
table{width:100%;border-collapse:collapse}
td,th{border-bottom:1px solid #555;padding:7px;white-space:nowrap}
#res{word-break:break-all}
</style>
</head>
<body>

<h1>CULT THRIFT</h1>
<p>Customer Referral • Chhindwara</p>

<button onclick="tab('join')">Customer Join</button>
<button onclick="tab('admin')">Admin</button>

<section id="join">
<h2>Registration</h2>
<form id="f">
<input id="n" placeholder="Naam" required maxlength="100">
<input id="m" placeholder="10 digit mobile" inputmode="numeric" maxlength="10" required>
<input id="a" placeholder="Gaon / area" required maxlength="100">
<input id="r" placeholder="Referral code (optional)">
<button type="submit">Register</button>
</form>
<div id="res"></div>
</section>

<section id="admin" class="hide">
<h2>Admin Login</h2>
<input id="email" type="email" placeholder="Admin email">
<input id="pw" type="password" placeholder="Password">
<button onclick="login()">Login</button>
<div id="dash" class="hide">
<h2>Customer List</h2>
<p id="count">Total customers: 0</p>
<input id="q" placeholder="Search customer" oninput="draw()">
<div class="table" id="t"></div>
<button onclick="csv()">Download CSV</button>
<button onclick="logout()">Logout</button>
</div>
</section>

<script src="https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2"></script>
<script>
const SUPABASE_URL="https://ktviluwkyhagkrrabhfq.supabase.co";
const SUPABASE_KEY="sb_publishable_pUtVq0A-brVQp-roGDaH7A_BoaWLk9n";

const db=supabase.createClient(SUPABASE_URL,SUPABASE_KEY);
const $=id=>document.getElementById(id);
let rows=[];

const params=new URLSearchParams(location.search);
if(params.get("ref")) $("r").value=params.get("ref");

function tab(id){
$("join").classList.toggle("hide",id!=="join");
$("admin").classList.toggle("hide",id!=="admin");
}

$("f").addEventListener("submit",async e=>{
e.preventDefault();
$("res").textContent="Registering...";

const {data,error}=await db.rpc("register_customer",{
p_name:$("n").value.trim(),
p_mobile:$("m").value.trim(),
p_area:$("a").value.trim(),
p_referral:$("r").value.trim().toUpperCase()||null
});

if(error){
$("res").textContent="Error: "+error.message;
return;
}

const link=location.origin+location.pathname+"?ref="+data.customer_code;
$("res").textContent="Registration complete! Customer Code: "+data.customer_code+" | Referral Link: "+link;

$("n").value="";
$("m").value="";
$("a").value="";
});

async function login(){
const {error}=await db.auth.signInWithPassword({
email:$("email").value,
password:$("pw").value
});
if(error){alert(error.message);return;}
$("dash").classList.remove("hide");
await load();
}

async function load(){
const {data,error}=await db.from("customers").select("*").order("created_at",{ascending:false});
if(error){alert(error.message);return;}
rows=data||[];
$("count").textContent="Total customers: "+rows.length;
draw();
}

function draw(){
const s=$("q").value.toLowerCase();
const filtered=rows.filter(x=>Object.values(x).join(" ").toLowerCase().includes(s));
$("t").innerHTML="";
const table=document.createElement("table");
table.innerHTML="<tr><th>Code</th><th>Name</th><th>Mobile</th><th>Area</th><th>Referral</th></tr>";
filtered.forEach(x=>{
const tr=document.createElement("tr");
[x.customer_code,x.name,x.mobile,x.area,x.referral_code||"-"].forEach(v=>{
const td=document.createElement("td");
td.textContent=v;
tr.appendChild(td);
});
table.appendChild(tr);
});
$("t").appendChild(table);
}

function csv(){
const data=[["Code","Name","Mobile","Area","Referral"],
...rows.map(x=>[x.customer_code,x.name,x.mobile,x.area,x.referral_code||""])]
.map(row=>row.map(v=>'"'+String(v??"").replaceAll('"','""')+'"').join(",")).join("\n");
const blob=new Blob([data],{type:"text/csv"});
const a=document.createElement("a");
a.href=URL.createObjectURL(blob);
a.download="cult-customers.csv";
a.click();
URL.revokeObjectURL(a.href);
}

async function logout(){
await db.auth.signOut();
$("dash").classList.add("hide");
}
</script>
</body>
</html>