<?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
    Insurance 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.";
    }
}
?>

<style>
        /* General body styling */
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        /* Form container styling */
        form {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 600px;
        }

        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 20px;
        }

        /* Label styling */
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #555;
        }

        /* Input and textarea styling */
        input[type="text"],
        input[type="email"],
        input[type="number"],
        input[type="date"],
        input[type="time"],
        textarea,
        input[type="file"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 15px;
            border: 1px solid #ccc;
            border-radius: 4px;
            font-size: 16px;
        }

        /* Button styling */
        button {
            width: 100%;
            padding: 12px;
            background-color: #28a745;
            border: none;
            border-radius: 4px;
            color: #fff;
            font-size: 16px;
            cursor: pointer;
        }

        button:hover {
            background-color: #218838;
        }

        /* Success message styling */
        .success-message {
            background-color: #d4edda;
            color: #155724;
            padding: 15px;
            border-radius: 4px;
            margin-top: 20px;
            text-align: center;
            border: 1px solid #08961b;
            width: 100%;
            max-width: 600px;
        }

        /* Error message styling */
        .error-message {
            background-color: #f8d7da;
            color: #721c24;
            padding: 15px;
            border-radius: 4px;
            margin-top: 20px;
            text-align: center;
            border: 1px solid #f5c6cb;
            width: 100%;
            max-width: 600px;
        }
    </style>

</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>

        <label>Mobile No:</label>
        <input type="text" name="mobileNo" required>

        <label>Email Address:</label>
        <input type="email" name="emailAddress" required>

        <label>Type of Insurance:</label>
        <input type="text" name="typeOfInsurance" required>

        <label>Insurance Policy/Certificate No:</label>
        <input type="text" name="insuranceCertificateNo" required>

        <label>Date of Loss:</label>
        <input type="date" name="dateOfLoss" required>

        <label>Accident Location:</label>
        <input type="text" name="accidentLocation" required>

        <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>

        <label>Photograph of the Damage Property:</label>
        <input type="file" name="photograph" accept="image/*" required>
        
        <button type="submit">Submit Claim</button>
    </form>
</body>
</html>
