<?php
session_start();  
include("configa.php"); // ✅ ডাটাবেস কানেকশন যুক্ত করা হলো
$conn->set_charset("utf8mb4");

if ($conn->connect_error) {
    die(json_encode([
        "success" => false,
        "message" => "ডাটাবেস কানেক্ট করতে ব্যর্থ: " . $conn->connect_error
    ], JSON_UNESCAPED_UNICODE));
}

if (isset($_POST['data']) && is_array($_POST['data'])) {
    $records = $_POST['data'];
    $updated = 0;

    foreach ($records as $record) {
        // ইনপুট স্যানিটাইজ
        $id = intval($record['id']);
        $ward = $conn->real_escape_string($record['ward']);
        $holdingNo = $conn->real_escape_string($record['holdingNo']);
        $name = $conn->real_escape_string($record['name']);
        $fatherHusband = $conn->real_escape_string($record['fatherHusband']);
        $homeName = $conn->real_escape_string($record['homeName']);
        $address = $conn->real_escape_string($record['address']);
        $mobile = $conn->real_escape_string($record['mobile']);
        $pesha = $conn->real_escape_string($record['pesha']);
        $khanarDhoron = $conn->real_escape_string($record['khanarDhoron']);
        $khanarMullo = floatval($record['khanarMullo']);
        $purus = intval($record['purus']);
        $chele = intval($record['chele']);
        $mohila = intval($record['mohila']);
        $meye = intval($record['meye']);
        $korAropMullo = floatval($record['korAropMullo']);
        $tofsileeKor = floatval($record['tofsileeKor']);
        $bokeya = floatval($record['bokeya']);
        $bokeyaPorishod = floatval($record['bokeyaPorishod']);
        $date_update = date('Y-m-d');

        $sql = "
            UPDATE holding_form SET
                ward='$ward',
                holdingNo='$holdingNo',
                name='$name',
                fatherHusband='$fatherHusband',
                homeName='$homeName',
                address='$address',
                mobile='$mobile',
                pesha='$pesha',
                khanarDhoron='$khanarDhoron',
                khanarMullo=$khanarMullo,
                purus=$purus,
                chele=$chele,
                mohila=$mohila,
                meye=$meye,
                korAropMullo=$korAropMullo,
                tofsileeKor=$tofsileeKor,
                bokeya=$bokeya,
                bokeyaPorishod=$bokeyaPorishod,
                date_update='$date_update'
            WHERE id=$id
        ";

        if ($conn->query($sql) === TRUE) {
            $updated++;
        }
    }

    echo json_encode([
        "success" => true,
        "updated" => $updated,
        "message" => "$updated টি রেকর্ড সফলভাবে আপডেট হয়েছে।"
    ], JSON_UNESCAPED_UNICODE);
} else {
    echo json_encode([
        "success" => false,
        "message" => "কোনো রেকর্ড পাওয়া যায়নি অথবা ডেটা ফরম্যাট ঠিক নয়।"
    ], JSON_UNESCAPED_UNICODE);
}

$conn->close();
?>
