<?php
declare(strict_types=1);
$configFile=__DIR__.'/config.php';if(!is_file($configFile))exit('First copy config.example.php to config.php and enter your database credentials.');$config=require $configFile;$ok='';$error='';
if($_SERVER['REQUEST_METHOD']==='POST'){try{$d=$config['db'];$pdo=new PDO("mysql:host={$d['host']};port=".($d['port']??3306).";dbname={$d['name']};charset=utf8mb4",$d['user'],$d['pass'],[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);$sql=file_get_contents(__DIR__.'/database/schema.sql');$pdo->exec($sql);$hash=password_hash($_POST['password'],PASSWORD_DEFAULT);$pdo->prepare('INSERT INTO users(name,email,password_hash,role) VALUES(?,?,?,"admin")')->execute([trim($_POST['name']),strtolower(trim($_POST['email'])),$hash]);$ok='Installation complete. Delete install.php, then sign in.';}catch(Throwable $e){$error='Installation failed: '.$e->getMessage();}}
?><!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>Install Tracker</title><link rel="stylesheet" href="assets/app.css"></head><body><main class="container"><div class="login card"><h1>Install Marketing Tracker</h1><?php if($ok):?><div class="flash"><?=htmlspecialchars($ok)?></div><a class="btn" href="index.php?r=login">Sign in</a><?php else:?><?php if($error):?><div class="flash error"><?=htmlspecialchars($error)?></div><?php endif;?><p>This creates the database tables and first administrator.</p><form method="post"><div class="field"><label>Admin name</label><input name="name" required></div><div class="field"><label>Admin email</label><input type="email" name="email" required></div><div class="field"><label>Password (minimum 12 characters)</label><input type="password" name="password" minlength="12" required></div><button class="btn">Install</button></form><?php endif;?></div></main></body></html>
