<?php
    require_once('../.hta_config/conf.php');
    require_once('../.hta_slug/_header.php');

    if($_SERVER['REQUEST_METHOD']=='POST' && strlen($_POST['name']>3)){
        $slugText = $_POST['name'];
        $slugText = preg_replace('~[^\p{L}\p{N}_-]+~u', '-', $slugText);
        $slugText = strtolower($slugText);
        $slugText = preg_replace('~-+~', '-', $slugText);
        $slugText = trim($slugText, '-');

        $time=time();
        $sid=rand(100,360).$time.rand(100,999);// $id='360000'.$time; //360000 concatineted with 1704215388 produces 10 width base_convert with 36
        $siliconid = base_convert($sid,10,36);
        
        try {
            $conn = new PDO("mysql:host=$mariaServer;dbname=$mariaDb", $mariaUser, $mariaPass);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            
            $stmt = $conn->prepare("INSERT INTO api_tools (siliconid, name, title, description, slug, status) VALUES (:siliconid, :name, :title, :description, :slug, 0)");
            $stmt->bindParam(':siliconid',   $siliconid);
            $stmt->bindParam(':name',        $_POST['name']);
            $stmt->bindParam(':title',       $_POST['title']);
            $stmt->bindParam(':description', $_POST['description']);
            $stmt->bindParam(':slug',        $slugText);
            $stmt->execute();
            // echo '<script>window.location.href="/cart/code-base"</script>';
            // $in_page_message = "<p class='text-green' style='text-align: center;' >Thank you! Your Query has been submitted.</p>";          
        } catch (PDOException $e) { 
            $in_page_message= "<p class='text-danger'>Error: " . $e->getMessage() . "</p>";
        }
    }
?>
<div class="container-zz mx-auto my-20">
    <p>Add New Tool</p>
    <form method="post" enctype="multipart/form-data" >
        <div style="display: flex; flex-direction: column;">
            <label for="tools-name">Tools Name:</label>
            <input type="text" name="name" id="tools-name" style="" required/>
        </div>
        <div style="display: flex; flex-direction: column;">
            <label for="title">Title:</label>
            <input type="text" name="title" id="title" style="" required/>
        </div>
        <div style="display: flex; flex-direction: column;">
            <label for="description">Description:</label>
            <input type="text" name="description" id="description" style="" required/>
        </div>
        <div style="display: flex; flex-direction: column;">
            <input type="submit" name="submit" id="submit" value="Save" style=""/>
        </div>
    </form>
</div>
<style>
    .container-zz{width:100%}@media (min-width: 640px){.container-zz{max-width:640px}}@media (min-width: 768px){.container-zz{max-width:768px}}@media (min-width: 1024px){.container-zz{max-width:1024px}}@media (min-width: 1280px){.container-zz{max-width:1280px}}@media (min-width: 1536px){.container-zz{max-width:1536px}}
    .mx-auto{margin-left:auto;margin-right:auto}
    .my-20{margin-top:5rem;margin-bottom:5rem}
</style>