<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

include('DB.php');

$bdd = new DB();

try {

    $bdd->beginTransaction();

    $service_id = $bdd->lastId(
        "INSERT INTO service (customer_name, mobile_no, product_id,invoice_date,chassis_no,registration_no)
         VALUES (?,?,?,?,?,?)",
        [
            $_POST['customer_name'],
            $_POST['mobile_no'],
            $_POST['product_id'],
			date('Y-m-d'),
			$_POST['chassis_no'],
			empty($_POST['registration_no']) ? null : $_POST['registration_no']
        ]
    );

    $bdd->execute(
        "INSERT INTO appointment
        (service_id, service_type, service_date, time_slot, user_id)
        VALUES (?,?,?,?,?)",
        [
            $service_id,
            $_POST['service_type'],
            $_POST['service_date'],
            $_POST['time_slot'],
            $_POST['user_id']
        ]
    );

    $bdd->commit();
    echo 'success';

} catch (Exception $e) {

    $bdd->rollBack();
    echo $e->getMessage();
}

$bdd->disconnect();
