from flask import Flask, render_template, request, redirect, url_for
import sqlite3
from datetime import datetime

app = Flask(__name__)

DATABASE = "database/centre.db"


# ==========================
# CONNEXION SQLITE
# ==========================

def get_db():
    conn = sqlite3.connect(DATABASE)
    conn.row_factory = sqlite3.Row
    return conn


# ==========================
# CREATION DE LA BASE
# ==========================

def init_database():

    conn = get_db()

    cursor = conn.cursor()

    cursor.execute("""
    CREATE TABLE IF NOT EXISTS interventions(

        id INTEGER PRIMARY KEY AUTOINCREMENT,

        heure TEXT,

        type TEXT,

        adresse TEXT,

        commune TEXT,

        victime INTEGER,

        telephone TEXT,

        requérant TEXT,

        commentaire TEXT,

        statut TEXT

    )
    """)

    cursor.execute("""
    CREATE TABLE IF NOT EXISTS vehicules(

        id INTEGER PRIMARY KEY AUTOINCREMENT,

        nom TEXT,

        caserne TEXT,

        statut TEXT

    )
    """)

    cursor.execute("""
    CREATE TABLE IF NOT EXISTS personnel(

        id INTEGER PRIMARY KEY AUTOINCREMENT,

        nom TEXT,

        grade TEXT,

        caserne TEXT,

        statut TEXT

    )
    """)

    conn.commit()
    conn.close()


# ==========================
# PAGE PRINCIPALE
# ==========================

@app.route("/")
def index():

    conn = get_db()

    interventions = conn.execute("""

    SELECT *

    FROM interventions

    ORDER BY id DESC

    """).fetchall()

    conn.close()

    return render_template(
        "dashboard.html",
        interventions=interventions
    )


# ==========================
# NOUVEL APPEL
# ==========================

@app.route("/nouvel-appel", methods=["POST"])
def nouvel_appel():

    heure = datetime.now().strftime("%d/%m/%Y %H:%M")

    type_inter = request.form["type"]

    adresse = request.form["adresse"]

    commune = request.form["commune"]

    victime = request.form["victime"]

    telephone = request.form["telephone"]

    requerant = request.form["requerant"]

    commentaire = request.form["commentaire"]

    conn = get_db()

    conn.execute("""

    INSERT INTO interventions(

    heure,

    type,

    adresse,

    commune,

    victime,

    telephone,

    requérant,

    commentaire,

    statut

    )

    VALUES(?,?,?,?,?,?,?,?,?)

    """,(

        heure,

        type_inter,

        adresse,

        commune,

        victime,

        telephone,

        requerant,

        commentaire,

        "En attente"

    ))

    conn.commit()

    conn.close()

    return redirect("/")


# ==========================
# CHANGER LE STATUT
# ==========================

@app.route("/statut/<int:id>/<etat>")
def statut(id,etat):

    conn = get_db()

    conn.execute("""

    UPDATE interventions

    SET statut=?

    WHERE id=?

    """,(etat,id))

    conn.commit()

    conn.close()

    return redirect("/")


# ==========================
# SUPPRESSION
# ==========================

@app.route("/supprimer/<int:id>")
def supprimer(id):

    conn=get_db()

    conn.execute("""

    DELETE FROM interventions

    WHERE id=?

    """,(id,))

    conn.commit()

    conn.close()

    return redirect("/")


# ==========================
# PAGE ADMIN
# ==========================

@app.route("/admin")
def admin():

    conn=get_db()

    vehicules=conn.execute("""

    SELECT *

    FROM vehicules

    """).fetchall()

    personnel=conn.execute("""

    SELECT *

    FROM personnel

    """).fetchall()

    conn.close()

    return render_template(

        "admin.html",

        vehicules=vehicules,

        personnel=personnel

    )


# ==========================
# AJOUT VEHICULE
# ==========================

@app.route("/ajouter-vehicule",methods=["POST"])
def ajouter_vehicule():

    conn=get_db()

    conn.execute("""

    INSERT INTO vehicules(

    nom,

    caserne,

    statut

    )

    VALUES(?,?,?)

    """,(

    request.form["nom"],

    request.form["caserne"],

    "Disponible"

    ))

    conn.commit()

    conn.close()

    return redirect("/admin")


# ==========================
# AJOUT PERSONNEL
# ==========================

@app.route("/ajouter-pompier",methods=["POST"])
def ajouter_pompier():

    conn=get_db()

    conn.execute("""

    INSERT INTO personnel(

    nom,

    grade,

    caserne,

    statut

    )

    VALUES(?,?,?,?)

    """,(

    request.form["nom"],

    request.form["grade"],

    request.form["caserne"],

    "Disponible"

    ))

    conn.commit()

    conn.close()

    return redirect("/admin")


# ==========================
# LANCEMENT
# ==========================

if __name__ == "__main__":

    init_database()

    app.run(

        host="0.0.0.0",

        port=5000,

        debug=True

    )

    /* =========================
   RESET
========================= */

*{
    margin:0;
    padding:0;
    box-sizing:border-box;
    font-family:Segoe UI,Arial,sans-serif;
}

body{
    background:#edf2f7;
    color:#2d3748;
}

/* =========================
   SIDEBAR
========================= */

.sidebar{

    position:fixed;

    left:0;

    top:0;

    width:250px;

    height:100vh;

    background:#12384d;

    color:white;

    overflow:auto;

    box-shadow:4px 0 12px rgba(0,0,0,.15);

}

.logo{

    padding:30px;

    text-align:center;

    background:#0f2d3d;

}

.logo h2{

    font-size:32px;

    margin-bottom:8px;

}

.logo p{

    color:#cfd8dc;

}

.sidebar a{

    display:block;

    color:white;

    text-decoration:none;

    padding:18px 25px;

    transition:.2s;

    border-left:4px solid transparent;

}

.sidebar a:hover{

    background:#1b4d68;

    border-left:4px solid #2ecc71;

}

/* =========================
   CONTENU
========================= */

.main{

    margin-left:250px;

    padding:25px;

}

/* =========================
   TOPBAR
========================= */

.topbar{

    background:white;

    padding:20px;

    border-radius:10px;

    display:flex;

    justify-content:space-between;

    align-items:center;

    box-shadow:0 5px 15px rgba(0,0,0,.08);

    margin-bottom:25px;

}

.topbar h1{

    font-size:28px;

    color:#12384d;

}

.heure{

    font-weight:bold;

    color:#2ecc71;

    font-size:18px;

}

/* =========================
   CARTES
========================= */

.cards{

    display:grid;

    grid-template-columns:repeat(4,1fr);

    gap:20px;

    margin-bottom:25px;

}

.card{

    background:white;

    padding:25px;

    border-radius:12px;

    box-shadow:0 5px 15px rgba(0,0,0,.08);

    text-align:center;

}

.card h3{

    color:#546e7a;

    margin-bottom:10px;

}

.card p{

    font-size:34px;

    font-weight:bold;

    color:#12384d;

}

/* =========================
   COLONNES
========================= */

.conteneur{

    display:grid;

    grid-template-columns:35% 65%;

    gap:20px;

}

/* =========================
   PANELS
========================= */

.panel{

    background:white;

    border-radius:10px;

    box-shadow:0 5px 15px rgba(0,0,0,.08);

    padding:20px;

}

.panel h2{

    margin-bottom:20px;

    color:#12384d;

}

/* =========================
   FORMULAIRE
========================= */

input,

textarea,

select{

    width:100%;

    padding:12px;

    margin-bottom:15px;

    border:1px solid #dcdcdc;

    border-radius:6px;

    outline:none;

}

textarea{

    resize:vertical;

    min-height:120px;

}

button{

    width:100%;

    background:#1abc9c;

    color:white;

    border:none;

    padding:15px;

    border-radius:8px;

    cursor:pointer;

    font-size:16px;

    transition:.2s;

}

button:hover{

    background:#16a085;

}

/* =========================
   TABLEAU
========================= */

table{

    width:100%;

    border-collapse:collapse;

}

th{

    background:#12384d;

    color:white;

    padding:14px;

}

td{

    padding:12px;

    border-bottom:1px solid #ececec;

}

tr:hover{

    background:#f7fafc;

}

/* =========================
   ETATS
========================= */

.etat{

    display:inline-block;

    padding:6px 12px;

    border-radius:20px;

    background:#2ecc71;

    color:white;

    font-size:13px;

}

/* =========================
   ACTIONS
========================= */

td a{

    text-decoration:none;

    font-size:20px;

    margin-right:12px;

}

/* =========================
   RESPONSIVE
========================= */

@media(max-width:1200px){

.cards{

grid-template-columns:repeat(2,1fr);

}

.conteneur{

grid-template-columns:1fr;

}

}

@media(max-width:700px){

.sidebar{

width:100%;

height:auto;

position:relative;

}

.main{

margin-left:0;

}

.cards{

grid-template-columns:1fr;

}

.topbar{

flex-direction:column;

gap:15px;

}

}