<?php
//require_once './admin_header.php';
require_once './header_new.php';
//require_once './PHP_Code/connection.php';
// DB connection
$host = 'localhost';
$user = 'rwkwmulp_cattlefarm';
$pass = 'Rudra@123';
$dbname = 'rwkwmulp1_adimjuth_songadh'; // change this
$conn = new mysqli($host, $user, $pass, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $fields = [];
    $values = [];

    foreach ($_POST as $key => $value) {
        $fields[] = "`$key`";
        $values[] = "'" . $conn->real_escape_string($value) . "'";
    }

    $sql = "INSERT INTO family_survey_data (" . implode(',', $fields) . ") VALUES (" . implode(',', $values) . ")";
    // $conn->set_charset("utf8mb4");
    if ($conn->query($sql)) {
        $message = "<p class='success'>✔️ Entry added successfully.</p>";
    } else {
        $message = "<p class='error'>❌ Error: " . $conn->error . "</p>";
    }
}
?>

<style>
/*    * {
        box-sizing: border-box;
    }*/
/*    body {
        font-family: sans-serif;
        margin: 0;
        padding: 20px;
        background: #f5f7fa;
    }*/
    h2 {
        text-align: center;
    }

    form {
        max-width: 960px;
        margin: auto;
        background: #fff;
        padding: 20px;
        border-radius: 10px;
        box-shadow: 0 4px 8px rgba(0,0,0,0.05);
    }

    .form-grid {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
        gap: 16px;
    }

    .form-group {
        display: flex;
        flex-direction: column;
    }

    label {
        font-weight: bold;
        margin-bottom: 4px;
    }

    input[type="text"], select {
        padding: 8px;
        border: 1px solid #ccc;
        border-radius: 5px;
    }

    button {
        margin-top: 20px;
        padding: 12px 20px;
        background: #007bff;
        border: none;
        color: white;
        font-weight: bold;
        border-radius: 5px;
        cursor: pointer;
    }

    button:hover {
        background: #0056b3;
    }

    .success {
        color: green;
        text-align: center;
        font-weight: bold;
    }
    .error {
        color: red;
        text-align: center;
        font-weight: bold;
    }

    @media (max-width: 600px) {
        form {
            padding: 10px;
        }
        button {
            width: 100%;
        }
    }
</style>

<div class="app-main__outer">
    <div class="app-main__inner" style="padding-top: 30px;padding-bottom: 0px;padding-left: 0px;padding-right: 30px !important">
        <div class="tab-content">
            <div class="tab-pane tabs-animation fade show active" id="tab-content-0" role="tabpanel">
                <h2>ફેમિલી સર્વે એન્ટ્રી ફોર્મ</h2>
                <?= isset($message) ? $message : '' ?>

                <form method="POST">
                    <div class="form-grid">
                        <?php
                        $fields = [
                            'Sr_No', 'Village_Name', 'Habitation_Name', 'Gram_Panchayat_Name', 'LGD_Code',
                            'Revenue_Village_Name', 'Taluka_Name', 'District_Name', 'Total_HHs', 'Total_Population',
                            'PTGs_HHs', 'PTGs_Population', 'Name_of_family_member', 'Female', 'Male', 'Children', 'Total',
                            'Kachchaa', 'Pakka', 'Land_Type', 'Water_Source', 'Electricity_YesNo', 'Electricity_Customer_No',
                            'Working_YesNo', 'RationCard_YesNo', 'Ration_Card_No', 'Ration_Card_Type', 'RationCard_Holder_YesNo',
                            'Employment_ID', 'Employment_Type', 'Own_Land_YesNo', 'AadeshPatra_YesNo', 'AdhikartPatra_YesNo',
                            'Aadhar_YesNo', 'Aadhar_No', 'PMJAY_Card_YesNo', 'PMJAY_Card_No',
                            'No_of_Jandhan_missing_members', 'Caste_Certificate_Missing_Count',
                            'Ujjwala_YesNo', 'PMKisan_YesNo', 'Other_YN1', 'Other_YN2'
                        ];

                        $yesno = ['' => 'Select', 'હા' => 'હા', 'ના' => 'ના'];

                        foreach ($fields as $field) {
                            echo "<div class='form-group'>";
                            echo "<label for='$field'>$field</label>";

                    //         if (str_contains($field, 'YesNo')) {
                    //             echo "<select name='$field' id='$field'>";
                    //             foreach ($yesno as $k => $v) {
                    //                 echo "<option value='$k'>$v</option>";
                    //             }
                    //             echo "</select>";
                    //         } elseif (str_contains($field, 'Type') && $field !== 'Employment_Type') {
                    //             echo "<select name='$field' id='$field'>
                    //     <option value=''>Select</option>
                    //     <option value='Antyoday'>Antyoday</option>
                    //     <option value='BPL'>BPL</option>
                    //     <option value='NFSA'>NFSA</option>
                    //     <option value='Other'>Other</option>
                    //   </select>";
                    //         } else {
                    //             echo "<input type='text' name='$field' id='$field'>";
                    //         }
                    
                            if (strpos($field, 'YesNo') !== false) {
    echo "<select name='$field' id='$field'>";
    foreach ($yesno as $k => $v) {
        echo "<option value='$k'>$v</option>";
    }
    echo "</select>";
} elseif (strpos($field, 'Type') !== false && $field !== 'Employment_Type') {
    echo "<select name='$field' id='$field'>
        <option value=''>Select</option>
        <option value='Antyoday'>Antyoday</option>
        <option value='BPL'>BPL</option>
        <option value='NFSA'>NFSA</option>
        <option value='Other'>Other</option>
    </select>";
} else {
    echo "<input type='text' name='$field' id='$field'>";
}
                            echo "</div>";
                        }
                        ?>
                    </div>
                    <button type="submit">ડેટા સબમિટ કરો</button>
                </form>

            </div>
        </div>
    </div>
</div>

