<?php
session_start();

if (!isset($_SESSION['userID'])) {
    header("Location: sign-in.php");
    exit();
}

require_once "SqlConnection/MySqlConnection.php";

$invoiceNumber = $_POST['invoiceNumber'];
$date = $_POST['date'];
$customerName = $_POST['customerName'];
$customerAddress1 = $_POST['customerAddress1'];
$serviceType1 = $_POST['serviceType1'];
$customerAddress2 = $_POST['customerAddress2'];
$serviceType2 = $_POST['serviceType2'];
$customerAddress3 = $_POST['customerAddress3'];
$serviceType3 = $_POST['serviceType3'];

$number1 = $_POST['number1'];
$desc1 = $_POST['desc1'];
$days1 = $_POST['days1'];
$rate1 = $_POST['rate1'];
$total1 = $_POST['total1'];

$number2 = $_POST['number2'];
$desc2 = $_POST['desc2'];
$days2 = $_POST['days2'];
$rate2 = $_POST['rate2'];
$total2 = $_POST['total2'];

$number3 = $_POST['number3'];
$desc3 = $_POST['desc3'];
$days3 = $_POST['days3'];
$rate3 = $_POST['rate3'];
$total3 = $_POST['total3'];

$number4 = $_POST['number4'];
$desc4 = $_POST['desc4'];
$days4 = $_POST['days4'];
$rate4 = $_POST['rate4'];
$total4 = $_POST['total4'];

$number5 = $_POST['number5'];
$desc5 = $_POST['desc5'];
$days5 = $_POST['days5'];
$rate5 = $_POST['rate5'];
$total5 = $_POST['total5'];

$number6 = $_POST['number6'];
$desc6 = $_POST['desc6'];
$days6 = $_POST['days6'];
$rate6 = $_POST['rate6'];
$total6 = $_POST['total6'];

$number7 = $_POST['number7'];
$desc7 = $_POST['desc7'];
$days7 = $_POST['days7'];
$rate7 = $_POST['rate7'];
$total7 = $_POST['total7'];

$number8 = $_POST['number8'];
$desc8 = $_POST['desc8'];
$days8 = $_POST['days8'];
$rate8 = $_POST['rate8'];
$total8 = $_POST['total8'];

$payment = $_POST['payment'];
$hst = $_POST['hst'];
$subtotal = $_POST['subtotal'];
$totalTax = $_POST['totalTax'];

$sqlValues = array();
    $sqlValues[] = "('$invoiceNumber', '1', '$desc1', '$days1', '$rate1', '$total1')";
    $sqlValues[] = "('$invoiceNumber', '2', '$desc2', '$days2', '$rate2', '$total2')";
    $sqlValues[] = "('$invoiceNumber', '3', '$desc3', '$days3', '$rate3', '$total3')";
    $sqlValues[] = "('$invoiceNumber', '4', '$desc4', '$days4', '$rate4', '$total4')";
    $sqlValues[] = "('$invoiceNumber', '5', '$desc5', '$days5', '$rate5', '$total5')";
    $sqlValues[] = "('$invoiceNumber', '6', '$desc6', '$days6', '$rate6', '$total6')";
    $sqlValues[] = "('$invoiceNumber', '7', '$desc7', '$days7', '$rate7', '$total7')";
    $sqlValues[] = "('$invoiceNumber', '8', '$desc8', '$days8', '$rate8', '$total8')";

    $sqlquery1 = "INSERT INTO invoices(invoiceNumber, date, customerName, customerAddress, serviceType, payment, hst, subtotal, totalTax) 
    VALUES ('$invoiceNumber', '$date', '$customerName', '$customerAddress1 $customerAddress2 $customerAddress3', '$serviceType1 $serviceType2 $serviceType3', '$payment', '$hst', '$subtotal', '$totalTax');INSERT INTO invoicestable(invoiceNumber, no, description, days, price, total) VALUES " . implode(", ", $sqlValues);

    $result = mysqli_multi_query($conn, $sqlquery1);




if ($result) {
    // header("Location: createInvoice.php");
    //Construct the URL for invoice.php with the required parameters
    $invoiceUrl = 'invoice.php';
    $invoiceUrl .= '?invoiceNumber=' . urlencode($invoiceNumber);
    $invoiceUrl .= '&date=' . urlencode($date);
    $invoiceUrl .= '&customerName=' . urlencode($customerName);
    $invoiceUrl .= '&customerAddress1=' . urlencode($customerAddress1);
    $invoiceUrl .= '&serviceType1=' . urlencode($serviceType1);
    $invoiceUrl .= '&customerAddress2=' . urlencode($customerAddress2);
    $invoiceUrl .= '&serviceType2=' . urlencode($serviceType2);
    $invoiceUrl .= '&customerAddress3=' . urlencode($customerAddress3);
    $invoiceUrl .= '&serviceType3=' . urlencode($serviceType3);
    $invoiceUrl .= '&number1=' . urlencode($number1);
    $invoiceUrl .= '&desc1=' . urlencode($desc1);
    $invoiceUrl .= '&days1=' . urlencode($days1);
    $invoiceUrl .= '&rate1=' . urlencode($rate1);
    $invoiceUrl .= '&total1=' . urlencode($total1);

    $invoiceUrl .= '&number2=' . urlencode($number2);
    $invoiceUrl .= '&desc2=' . urlencode($desc2);
    $invoiceUrl .= '&days2=' . urlencode($days2);
    $invoiceUrl .= '&rate2=' . urlencode($rate2);
    $invoiceUrl .= '&total2=' . urlencode($total2);

    $invoiceUrl .= '&number3=' . urlencode($number3);
    $invoiceUrl .= '&desc3=' . urlencode($desc3);
    $invoiceUrl .= '&days3=' . urlencode($days3);
    $invoiceUrl .= '&rate3=' . urlencode($rate3);
    $invoiceUrl .= '&total3=' . urlencode($total3);

    $invoiceUrl .= '&number4=' . urlencode($number4);
    $invoiceUrl .= '&desc4=' . urlencode($desc4);
    $invoiceUrl .= '&days4=' . urlencode($days4);
    $invoiceUrl .= '&rate4=' . urlencode($rate4);
    $invoiceUrl .= '&total4=' . urlencode($total4);

    $invoiceUrl .= '&number5=' . urlencode($number5);
    $invoiceUrl .= '&desc5=' . urlencode($desc5);
    $invoiceUrl .= '&days5=' . urlencode($days5);
    $invoiceUrl .= '&rate5=' . urlencode($rate5);
    $invoiceUrl .= '&total5=' . urlencode($total5);

    $invoiceUrl .= '&number6=' . urlencode($number6);
    $invoiceUrl .= '&desc6=' . urlencode($desc6);
    $invoiceUrl .= '&days6=' . urlencode($days6);
    $invoiceUrl .= '&rate6=' . urlencode($rate6);
    $invoiceUrl .= '&total6=' . urlencode($total6);

    $invoiceUrl .= '&number7=' . urlencode($number7);
    $invoiceUrl .= '&desc7=' . urlencode($desc7);
    $invoiceUrl .= '&days7=' . urlencode($days7);
    $invoiceUrl .= '&rate7=' . urlencode($rate7);
    $invoiceUrl .= '&total7=' . urlencode($total7);

    $invoiceUrl .= '&number8=' . urlencode($number8);
    $invoiceUrl .= '&desc8=' . urlencode($desc8);
    $invoiceUrl .= '&days8=' . urlencode($days8);
    $invoiceUrl .= '&rate8=' . urlencode($rate8);
    $invoiceUrl .= '&total8=' . urlencode($total8);

    $invoiceUrl .= '&payment=' . urlencode($payment);
    $invoiceUrl .= '&hst=' . urlencode($hst);
    $invoiceUrl .= '&subtotal=' . urlencode($subtotal);
    $invoiceUrl .= '&totalTax=' . urlencode($totalTax);

    

    // Use JavaScript to open a new tab with the invoice.php URL
    echo '<script>
            var newTab = window.open("' . $invoiceUrl . '", "_blank");
            newTab.focus();
            window.location.href = "invoicesData.php";
          </script>';

    echo $invoiceUrl;
    echo "Invoice Saved";
} else {
    echo "Error while saving a new invoice";
}
?>
