<?php
//require_once './admin_header.php';
require_once './header_new.php';
//require_once './PHP_Code/connection.php';
?>
<?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);
}

// Filters
$filters = [];
$where = [];

if (!empty($_GET['village'])) {
    $village = $conn->real_escape_string($_GET['village']);
    $filters['village'] = $village;
    $where[] = "Village_Name = '$village'";
}
if (!empty($_GET['taluka'])) {
    $taluka = $conn->real_escape_string($_GET['taluka']);
    $filters['taluka'] = $taluka;
    $where[] = "Taluka_Name = '$taluka'";
}
if (!empty($_GET['ration_type'])) {
    $ration_type = $conn->real_escape_string($_GET['ration_type']);
    $filters['ration_type'] = $ration_type;
    $where[] = "Ration_Card_Type = '$ration_type'";
}
if (!empty($_GET['ujjwala'])) {
    $ujjwala = $conn->real_escape_string($_GET['ujjwala']);
    $filters['ujjwala'] = $ujjwala;
    $where[] = "Ujjwala_YesNo = '$ujjwala'";
}

$sql = "SELECT * FROM family_survey_data";
if (!empty($where)) {
    $sql .= " WHERE " . implode(" AND ", $where);
}
$conn->set_charset("utf8mb4");
$result = $conn->query($sql);
?>


<style>
    body {
        font-family: sans-serif;
        /*padding: 20px;*/
        background: #f4f4f4;
    }

    h2, h4 {
        text-align: center;
    }

    .print-btn {
        margin: 10px auto;
        display: block;
        padding: 10px 20px;
        background: #007bff;
        color: white;
        border: none;
        border-radius: 5px;
        font-weight: bold;
        cursor: pointer;
    }

    .print-btn:hover {
        background: #0056b3;
    }

    table {
        border-collapse: collapse;
        width: 100%;
        background: white;
        margin-top: 20px;
        font-size: 14px;
    }

    th, td {
        border: 1px solid #000 !important;
        padding: 6px 8px;
        text-align: left;
        color: black;
    }

    th {
        background-color: #e0e0e0;
    }

    /* ✅ Only show print_divtag when printing */
    @media print {
        body * {
            visibility: hidden;
        }

        #print_divtag1, #print_divtag1 * {
            visibility: visible;
        }

        #print_divtag1 {
            position: absolute;
            left: 0;
            top: 0;
            width: 100%;
        }

        table, th, td {
            border: 1px solid #000 !important;
            color: black !important;
        }

        .print-btn {
            display: none;
        }
    }
</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">

                <h4>ફેમિલી સર્વે રિપોર્ટ</h4>
                <button class="print-btn" onclick="printDiv('print_divtag1')">🖨️ પ્રિન્ટ કરો</button>
                <div class="tab-content" id="print_divtag1">
                    <div class="tab-pane tabs-animation fade active show" id="tab-content-0" role="tabpanel">
                        <div class="main-card mb-3 card">
                            <div class="col-lg-12">
                                <div class="form-row" style="margin-left: 20px;margin-top: 20px">
                                    <form method="get">
                                        <label>ગામનું નામ:
                                            <input type="text" name="village" value="<?= isset($filters['village']) ? htmlspecialchars($filters['village']) : '' ?>">
                                        </label>
                                        <label>તાલુકો:
                                            <input type="text" name="taluka" value="<?= isset($filters['taluka']) ? htmlspecialchars($filters['taluka']) : '' ?>">
                                        </label>
                                        <label>રેશન કાર્ડ પ્રકાર:
                                            <select name="ration_type">
                                                <option value="">--Select--</option>
                                                <option value="Antyoday" <?= (isset($filters['ration_type']) && $filters['ration_type'] == 'Antyoday') ? 'selected' : '' ?>>Antyoday</option>
                                                <option value="BPL" <?= (isset($filters['ration_type']) && $filters['ration_type'] == 'BPL') ? 'selected' : '' ?>>BPL</option>
                                                <option value="NFSA" <?= (isset($filters['ration_type']) && $filters['ration_type'] == 'NFSA') ? 'selected' : '' ?>>NFSA</option>
                                            </select>
                                        </label>
                                        <label>ઉજ્જ્વલા યોજના:
                                            <select name="ujjwala">
                                                <option value="">--Select--</option>
                                                <option value="હા" <?= (isset($filters['ujjwala']) && $filters['ujjwala'] == 'હા') ? 'selected' : '' ?>>હા</option>
                                                <option value="ના" <?= (isset($filters['ujjwala']) && $filters['ujjwala'] == 'ના') ? 'selected' : '' ?>>ના</option>
                                            </select>
                                        </label>
                                        <button type="submit">Filter</button>
                                        <a href="patrak_1_family_detail_report.php"><button type="button">Clear</button></a>
                                    </form>

                                </div>
                            </div>
                            <div id="print_divtag">
                                <h5 class="card-title"   style="float: none;text-align: center;margin-top: 10px">ફેમિલી સર્વે રિપોર્ટ - <?php echo date("d/m/Y"); ?></h5>

                                <table>
                                    <thead>
                                        <tr>
                                            <th>ક્રમ</th>
                                            <th>ગામ</th>
                                            <th>ફળીયુ</th>
                                            <th>સભ્ય નામ</th>
                                            <th>મહિલા</th>
                                            <th>પુરુષ</th>
                                            <th>બાળકો</th>
                                            <th>ટોટલ</th>
                                            <th>રેશન કાર્ડ</th>
                                            <th>ઉજ્જ્વલા લાભ</th>
                                            <th>પીએમ કિસાન</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php if ($result && $result->num_rows > 0): ?>
                                            <?php while ($row = $result->fetch_assoc()): ?>
                                                <tr>
                                                    <td><?= $row['Sr_No'] ?></td>
                                                    <td><?= $row['Village_Name'] ?></td>
                                                    <td><?= $row['Habitation_Name'] ?></td>
                                                    <td><?= $row['Name_of_family_member'] ?></td>
                                                    <td><?= $row['Female'] ?></td>
                                                    <td><?= $row['Male'] ?></td>
                                                    <td><?= $row['Children'] ?></td>
                                                    <td><?= $row['Total'] ?></td>
                                                    <td><?= $row['Ration_Card_Type'] ?></td>
                                                    <td><?= $row['Ujjwala_YesNo'] ?></td>
                                                    <td><?= $row['PMKisan_YesNo'] ?></td>
                                                </tr>
                                            <?php endwhile; ?>
                                        <?php else: ?>
                                            <tr><td colspan="11">ડેટા મળ્યો નથી.</td></tr>
                                        <?php endif; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>  
<script>
//function printDiv(divId) {
//    const originalContent = document.body.innerHTML;
//    const printContent = document.getElementById(divId).innerHTML;
//
//    document.body.innerHTML = printContent;
//    window.print();
//    document.body.innerHTML = originalContent;
//    window.location.reload(); // Optional
//}
//</script>

<script>
function printDiv(divId) {
    window.print(); // simplified now that CSS handles visibility
}
</script>