<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $nameOfInsured = htmlspecialchars($_POST['nameOfInsured']);
    $mobileNo = htmlspecialchars($_POST['mobileNo']);
    $emailAddress = htmlspecialchars($_POST['emailAddress']);
    $typeOfInsurance = htmlspecialchars($_POST['typeOfInsurance']);
    $insuranceCertificateNo = htmlspecialchars($_POST['insuranceCertificateNo']);
    $dateOfLoss = htmlspecialchars($_POST['dateOfLoss']);
    $accidentLocation = htmlspecialchars($_POST['accidentLocation']);
    $accidentTime = htmlspecialchars($_POST['accidentTime']);
    $detailsOfAccident = htmlspecialchars($_POST['detailsOfAccident']);
    $estimatedClaimAmount = htmlspecialchars($_POST['estimatedClaimAmount']);

    // Email recipient
    $to = "claim@senainsurance.com";
    $subject = "Claim Intimation Form Submission";

    // Email message
    $message = "
    Name of Insured: $nameOfInsured\n
    Mobile No: $mobileNo\n
    Email Address: $emailAddress\n
    Type of Insurance: $typeOfInsurance\n
    Policy/Certificate No: $insuranceCertificateNo\n
    Date of Loss: $dateOfLoss\n
    Accident Location: $accidentLocation\n
    Accident Time: $accidentTime\n
    Details of Accident: $detailsOfAccident\n
    Estimated Claim Amount of Tk.: $estimatedClaimAmount\n
    ";

    // File attachment handling
    $boundary = md5(time());
    $headers = "From: Website Submission <no-reply@senainsurance.com>\r\n";
    $headers .= "Reply-To: $emailAddress\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";

    $emailBody = "--$boundary\r\n";
    $emailBody .= "Content-Type: text/plain; charset=UTF-8\r\n";
    $emailBody .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $emailBody .= $message . "\r\n\r\n";

    if (isset($_FILES['photograph']) && $_FILES['photograph']['error'] == 0) {
        $fileName = $_FILES['photograph']['name'];
        $fileType = $_FILES['photograph']['type'];
        $fileData = file_get_contents($_FILES['photograph']['tmp_name']);
        $fileEncoded = chunk_split(base64_encode($fileData));

        $emailBody .= "--$boundary\r\n";
        $emailBody .= "Content-Type: $fileType; name=\"$fileName\"\r\n";
        $emailBody .= "Content-Disposition: attachment; filename=\"$fileName\"\r\n";
        $emailBody .= "Content-Transfer-Encoding: base64\r\n\r\n";
        $emailBody .= $fileEncoded . "\r\n\r\n";
    }

    $emailBody .= "--$boundary--";

    // Send email
    if (mail($to, $subject, $emailBody, $headers)) {
        $_SESSION['certificate'] = nl2br($message);
        header("Location: success.php");
        exit();
    } else {
        echo "There was an error sending your claim. Please try again.";
    }
}
if (isset($_FILES['photograph']) && $_FILES['photograph']['error'] == 0) {
    $fileName = $_FILES['photograph']['name'];
    $fileType = $_FILES['photograph']['type'];
    $fileData = file_get_contents($_FILES['photograph']['tmp_name']);
    $fileEncoded = chunk_split(base64_encode($fileData));

    $emailBody .= "--$boundary\r\n";
    $emailBody .= "Content-Type: $fileType; name=\"$fileName\"\r\n";
    $emailBody .= "Content-Disposition: attachment; filename=\"$fileName\"\r\n";
    $emailBody .= "Content-Transfer-Encoding: base64\r\n\r\n";
    $emailBody .= $fileEncoded . "\r\n\r\n";
}

// Handle optional additional document
if (isset($_FILES['additionalDocument']) && $_FILES['additionalDocument']['error'] == 0) {
    $fileName = $_FILES['additionalDocument']['name'];
    $fileType = $_FILES['additionalDocument']['type'];
    $fileData = file_get_contents($_FILES['additionalDocument']['tmp_name']);
    $fileEncoded = chunk_split(base64_encode($fileData));

    $emailBody .= "--$boundary\r\n";
    $emailBody .= "Content-Type: $fileType; name=\"$fileName\"\r\n";
    $emailBody .= "Content-Disposition: attachment; filename=\"$fileName\"\r\n";
    $emailBody .= "Content-Transfer-Encoding: base64\r\n\r\n";
    $emailBody .= $fileEncoded . "\r\n\r\n";
}

?>


<!-- Header Banner -->
<div class="header-banner">
    <img src="https://senainsurance.com/images/print/header-banner.jpg" alt="Company Banner">
</div>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Claim Intimation Form</title>
    <link rel="stylesheet" href="style.css">

</head>
<body>
    <h1>Claim Intimation Form</h1>
    <form action="claim_form.php" method="post" enctype="multipart/form-data">
        <label>Name of Insured:</label>
        <input type="text" name="nameOfInsured" required>

        <!-- Mobile No and Email Address in the same row -->
        <div class="form-row">
            <div>
                <label>Mobile No:</label>
                <input type="text" name="mobileNo" required>
            </div>
            <div>
                <label>Email Address:</label>
                <input type="email" name="emailAddress" required>
            </div>
        </div>

        <!-- Type of Insurance and Insurance Policy/Certificate No in the same row -->
        <div class="form-row">
            <div>
                <label>Type of Insurance:</label>
                <input type="text" name="typeOfInsurance" required>
            </div>
            <div>
                <label>Policy / Certificate No:</label>
                <input type="text" name="insuranceCertificateNo" required>
            </div>
        </div>

        <!-- Date of Loss and Accident Location in the same row -->
        <div class="form-row">
            <div>
                <label>Date of Loss:</label>
                <input type="date" name="dateOfLoss" required>
            </div>
            <div>
                <label>Accident Location:</label>
                <input type="text" name="accidentLocation" required>
            </div>
        </div>

        <label>Accident Time:</label>
        <input type="time" name="accidentTime" required>

        <label>Details of Accident:</label>
        <textarea name="detailsOfAccident" rows="4" required></textarea>

        <label>Estimated Claim Amount of Tk.:</label>
        <input type="number" name="estimatedClaimAmount" required>

         <!-- File attachments in the same row -->
<div class="form-row">
    <div>
        <label>Photograph of the Damaged Property:</label>
        <input type="file" name="photograph" accept="image/*" required>
    </div>
    <div>
        <label>Additional Document (if any):</label>
        <input type="file" name="additionalDocument" accept=".jpg,.jpeg,.png,.pdf">
    </div>
</div>

        <button type="submit">Submit Claim</button>
    </form>
</body>
</html>