<?php
require '../../vendor/autoload.php';
include '../include/headscript.php';
include '../../Connections/repnew.php';

header('Content-Type: application/json');

$currentDate = date('d-m-Y');
$title = $_POST['title'];
$content = $_POST['content'];
$filterStart = $_POST['filterStart'];
$filterEnd = $_POST['filterEnd'];

$conn = new mysqli($servername, $username, $password, $database);
if ($conn->connect_error) {
    die(json_encode(["error" => "Connection failed: " . $conn->connect_error]));
}

$stmt = $conn->prepare("SELECT * from company");
$stmt->execute();
$result = $stmt->get_result();
$logoname = $result->fetch_all(MYSQLI_ASSOC);

$stmt = $conn->prepare("DELETE from reportsections");
$stmt->execute();
$stmt = $conn->prepare("INSERT INTO reportsections (
        cover_title, cover_subtitle, instructionsdetails_title, instructiondetails_text, 
        overviewstatistic_title, overviewstatistic_text, supplieristatistic_title, supplieristatistic_text, 
        analysisstatistic_title, analysisstatistic_text, analytscompounds_title, analytscompounds_text, 
        finalevaluation_title, finalevaluation_text
    ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

if ($stmt === false) {
    die(json_encode(["error" => "Prepare statement failed: " . $conn->error]));
}

$stmt->bind_param(
    'ssssssssssssss',
    $title[0],
    $content[0],
    $title[1],
    $content[1],
    $title[2],
    $content[2],
    $title[3],
    $content[3],
    $title[4],
    $content[4],
    $title[5],
    $content[5],
    $title[6],
    $content[6]
);

if (!$stmt->execute()) {
    die(json_encode(["error" => "Execute statement failed: " . $stmt->error]));
}
$stmt->close();
$conn->close();

$chartdata0 = file_get_contents('../../../resources/charts/chart0.ch');
$chartdata1 = file_get_contents('../../../resources/charts/chart1.ch');
$chartdata2 = file_get_contents('../../../resources/charts/chart2.ch');
$chartdata3 = file_get_contents('../../../resources/charts/chart3.ch');
$chartdata4 = file_get_contents('../../../resources/charts/chart4.ch');
$chartdata5 = file_get_contents('../../../resources/charts/chart5.ch');

$tmplogo = file_get_contents('../uploadlogo/' . $logoname[0]['logocompany']);
$base64logo = base64_encode($tmplogo);
$logodata = 'data:image/jpeg;base64,' . $base64logo;

$page =
    '<html>
    <head>
        <title>Financial Report</title>
        <style>
            body {
                font-family: DejaVu Sans;
            }

            #headerdate {
                font-family: DejaVu Sans;
                display: inline-block;
                width: 450px;
                text-align: right;
            }

            footer {
                display: "inline-block";
                margin-top:500px;
            }

            div > h3 {
                font-family: DejaVu Sans;
                font-size: 30px;
            }

            #filter{
                font-family: DejaVu Sans;
                font-size: 20px;
                margin-left: 50px;
            }

            #content {
                font-family: DejaVu Sans;
                font-size: 20px;
            }
        </style>
    </head>
    <body>
        <div id="page1">
            <div>
                <img
                    src=' . $logodata . '
                    alt="Brand Logo" class="logo" width="150px" height="40px"
                />
                <div id="headerdate" style="font-family: Dejavu Sans;">Date:&nbsp;' . $currentDate . '</div>
            </div>
            <div>
                <div style="text-align: center; font-size: 70px; margin-top: 200px; font-family: Dejavu Sans;">' . $title[0] . ' </div>
                <div style="margin-left: 20px; text-align: center; font-size: 30px; font-family: Dejavu Sans;">' . $content[0] . '</div>
            </div>
            <div style="margin-top: 500px;margin-left: 50px;display: inline-block;width: 40%;font-size: 15px; font-family: Dejavu Sans;"> Created by: ' . $nameuser . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date ' . $currentDate . '</div>
        </div>
        <br style="page-break-before: always;" />
        <div id="page2">
            <img
                src=' . $logodata . '
                alt="Brand Logo" class="logo" width="150px" height="40px"
            />
            <h3 style="font-family: Dejavu Sans; font-size: 30px;">' . $title[1] . '</h3>
            <p style="font-size: 20px; font-weight: bolder; font-family: Dejavu Sans;">---------------------------------------------------------------------------------</p>
            <p id = "content" style="font-family: Dejavu Sans; font-size: 20px;">' . $content[1] . '</p>
            <div>
                <p id="filtertitle" style="margin-top: 650px; font-family: Dejavu Sans;">FILTERS APPLIED</p>
                <p id="filterdata" style="font-family: Dejavu Sans;">From Date ' . $filterStart . ' to Date ' . $filterEnd . '</p>
                <p id="filterdata" style="font-family: Dejavu Sans;">Other Filters</p>
            </div>           
        </div>
        <br style="page-break-before: always;" />
        <div id="page3">
            <img
                src=' . $logodata . '
                alt="Brand Logo" class="logo" width="150px" height="40px"
            />
            <h3 style="font-family: Dejavu Sans; font-size: 30px;">' . $title[2] . '</h3>
            <p style="font-size: 20px; font-weight: bolder; font-family: Dejavu Sans;">---------------------------------------------------------------------------------</p>
            <p id = "content" style = "font-family: Dejavu Sans; font-size: 20px;"">
                ' . $content[2] . '
            </p>
            <div id="chart">
                <img src=' . $chartdata0 . ' width="700px" />
                <img src=' . $chartdata1 . ' width="700px" />
            </div>
        </div>
        <br style="page-break-before: always;" />
        <div id="page4">
            <img
                src=' . $logodata . '
                alt="Brand Logo" class="logo" width="150px" height="40px"
            />
            <h3 style="font-family: Dejavu Sans; font-size: 30px;">' . $title[3] . '</h3>
            <p style="font-size: 20px; font-weight: bolder; font-family: Dejavu Sans;">---------------------------------------------------------------------------------</p>
            <p id = "content" style = "font-family: Dejavu Sans; font-size: 20px;"">' . $content[3] . '</p>
            <div id="chart">
                <img src=' . $chartdata2 . ' width="700px" />
                <img src=' . $chartdata3 . ' width="700px" />
            </div>
        </div>
        <br style="page-break-before: always;" />
        <div id="page4">
            <img
                src=' . $logodata . '
                alt="Brand Logo" class="logo" width="150px" height="40px"
            />
            <h3 style="font-family: Dejavu Sans; font-size: 30px;">' . $title[4] . '</h3>
            <p style="font-size: 20px; font-weight: bolder; font-family: Dejavu Sans;">---------------------------------------------------------------------------------</p>
            <p id = "content" style = "font-family: Dejavu Sans; font-size: 20px;"">' . $content[4] . '</p>
            <div id="chart">
                <img src=' . $chartdata4 . ' width="700px" />
            </div>
        </div>
        <br style="page-break-before: always;" />
        <div id="page6">
            <img
                src=' . $logodata . '
                alt="Brand Logo" class="logo" width="150px" height="40px"
            />
            <h3 style="font-family: Dejavu Sans; font-size: 30px;">' . $title[5] . '</h3>
            <p style="font-size: 20px; font-weight: bolder; font-family: Dejavu Sans;">---------------------------------------------------------------------------------</p>
            <p id = "content" style = "font-family: Dejavu Sans; font-size: 20px;">' . $content[5] . '</p>
            <div id="chart">
                <img src=' . $chartdata5 . ' width="700px" />
            </div>
        </div>
        <br style="page-break-before: always;" />
        <div id="page7">
            <img
                src=' . $logodata . '
                alt="Brand Logo" class="logo" width="150px" height="40px"
            />
            <h3 style="font-family: Dejavu Sans; font-size: 30px;">' . $title[6] . '</h3>
            <p style="font-size: 20px; font-weight: bolder; font-family: Dejavu Sans;">---------------------------------------------------------------------------------</p>
            <p id = "content" style = "font-family: Dejavu Sans; font-size: 20px;">' . $content[6] . '</p>
        </div>
    </body>
    </html>';
$filePath = "../../download/";
$filename = "Report" . date('d-m-Y-H-i') . ".docx";

if (!is_dir('../../download/')) {
    mkdir('../../download/', 0777, true);
}

file_put_contents('../../download/report.html', $page);

$command = 'soffice --headless --convert-to docx:"MS Word 2007 XML" ../../download/report.html --outdir "../../download/"';

$output = shell_exec($command);
if (rename('../../download/report.docx', '../../download/Report' . date('d-m-Y-H-i') . '.docx')) {
    $response['status'] = 'success';
    $response['message'] = 'http://127.0.0.1/reportifynew/public/download/';
    $response['file'] = $filename;
} else {
    $response['status'] = 'error';
    $response['message'] = 'WORD download fail';
}
echo json_encode($response);
unlink('../../download/report.html');
