<?php
session_start();
// include 'fun/function.php';
// $item_limit = 50;
//

//

function set_Photo($Image)
{
    global $conn;
   $Photo=saveImage($Image, UPLOAD_DIR_DATA);
  
            $insertQuery  = "INSERT INTO  Temp_Image (Name) VALUES (?)";
            if ($stmt = $conn->prepare($insertQuery))
            {
                $stmt->bind_param("s", $Photo);
                if ($stmt->execute()) 
                {
                     $response["message"] = $Photo;
                     $response["status"] = 1;
                } 
                else 
                {
                    $response["status"] = 2;
                    $response["message"] = $stmt->error;
                }
                $stmt->close();
            }
            else {
                $response["status"] = 0;
                $response["message"] = "ERROR";
            }
       
    return $response;
}

function set_multitdata($insertQuery)
{
    global $conn;
    $response = array();
        if ($conn->multi_query($insertQuery) === TRUE) {
            $response["status"] = 1;
            $response["message"] = "Added new record";
            $conn->close();
          }
    else {
        $response["status"] = 0;
        $response["message"] = "Record already exists";
    }
    return $response;
}
// function set_CaseData($RegisterNo, $Date, $CaseTypeId, $PropertyTypeId, $PropertyId, $Lat_Long, $CaseAddressId, $ImporterAddressId, $BuyerAddressId, $SellerAddressId, $GroundArea, $GroundUnitId, $BuildingArea, $BuildingAreaUnitId, $BuildingTypeId, $ContractPrice, $ProposedPrice, $OwnerTypeId, $PayTypeId, $BnakId, $OwnerNrcId, $OwnerPh, $GuestNrcId, $StateId, $GuestPh, $ProposerNrcId, $ProposerPh, $NrcConfimStateId, $RegisterStateId, $BuildingType, $OtherType, $OtherArea, $FloorType,$OtherUnit,$ImporterSt,$SellerSt,$BuyerSt,$CaseSt)
// {
//     global $conn;
//     date_default_timezone_set('Asia/Yangon'); 
//      $timeAndDate =  date('Y-m-d H:i:s');
//     $response = array();
      
//     //   $query = "SELECT max(GameId) from TblMatch";
//     //     $rr= getNumber($query);
//     $insertQuery  ="INSERT INTO TblRegister (RegisterNo, Date, CaseTypeId, PropertyTypeId, PropertyId, Lat_Long, CaseAddressId, ImporterAddressId, BuyerAddressId, SellerAddressId, GroundArea, GroundUnitId, BuildingArea, BuildingAreaUnitId, BuildingTypeId, ContractPrice, ProposedPrice, OwnerTypeId, PayTypeId, BnakId, OwnerNrcId, OwnerPh, GuestNrcId, StateId, GuestPh, ProposerNrcId, ProposerPh, NrcConfimStateId, RegisterStateId, BuildingType, OtherType, OtherArea, FloorType,OtherUnit,ImporterSt,SellerSt,BuyerSt,CaseSt) VALUES  (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
//         // $insertQuery  = "INSERT INTO tbllocationpart (UserId,Latitude,Longitude,Status,Remark,GameId,Game_Date,LocationString,TrackingString,TrackName) VALUES (?,?,?,?,?,?,?,?,?,?)";
//         if ($stmt = $conn->prepare($insertQuery))
//         {
//             $stmt->bind_param("ssssssssssssssssssssssssssssssssssssss", $RegisterNo, $timeAndDate, $CaseTypeId, $PropertyTypeId, $PropertyId, $Lat_Long, $CaseAddressId, $ImporterAddressId, $BuyerAddressId, $SellerAddressId, $GroundArea, $GroundUnitId, $BuildingArea, $BuildingAreaUnitId, $BuildingTypeId, $ContractPrice, $ProposedPrice, $OwnerTypeId, $PayTypeId, $BnakId, $OwnerNrcId, $OwnerPh, $GuestNrcId, $StateId, $GuestPh, $ProposerNrcId, $ProposerPh, $NrcConfimStateId, $RegisterStateId, $BuildingType, $OtherType, $OtherArea, $FloorType,$OtherUnit,$ImporterSt,$SellerSt,$BuyerSt,$CaseSt);
//             if ($stmt->execute()) 
//             {
//                  $response["message"] = "Success";
//                  $response["status"] = 1;
//             } 
//             else 
//             {
//                 $response["status"] = 2;
//                 $response["message"] = $stmt->error;
//             }
//             $stmt->close();
//           }
//         else {
//         $response["status"] = 0;
//         $response["message"] = "ERROR";
//         }
//         $response["id"] = $InsertedId;
//     return $response;
// }

function set_CaseDataaaab($RegisterNo, $Date, $CaseTypeId, $PropertyTypeId, $PropertyId, $Lat_Long, $CaseAddressId, $ImporterAddressId, $BuyerAddressId, $SellerAddressId, $GroundArea, $GroundUnitId, $BuildingArea, $BuildingAreaUnitId, $BuildingTypeId, $ContractPrice, $ProposedPrice, $OwnerTypeId, $PayTypeId, $BnakId, $OwnerNrcId, $OwnerPh, $GuestNrcId, $StateId, $GuestPh, $ProposerNrcId, $ProposerPh, $NrcConfimStateId, $RegisterStateId, $BuildingType, $OtherType, $OtherArea, $FloorType, $OtherUnit, $ImporterSt, $SellerSt, $BuyerSt, $CaseSt,$CaseFullAddress,$ImporterFullAddress,$BuyerFullAddress,$SellerFullAddresst)
{
    global $conn;
    date_default_timezone_set('Asia/Yangon'); 
    $timeAndDate = date('Y-m-d H:i:s');
    $response = array();

    $insertQuery = "INSERT INTO TblRegister (RegisterNo, Date, CaseTypeId, PropertyTypeId, PropertyId, Lat_Long, CaseAddressId, ImporterAddressId, BuyerAddressId, SellerAddressId, GroundArea, GroundUnitId, BuildingArea, BuildingAreaUnitId, BuildingTypeId, ContractPrice, ProposedPrice, OwnerTypeId, PayTypeId, BnakId, OwnerNrcId, OwnerPh, GuestNrcId, StateId, GuestPh, ProposerNrcId, ProposerPh, NrcConfimStateId, RegisterStateId, BuildingType, OtherType, OtherArea, FloorType, OtherUnit, ImporterSt, SellerSt, BuyerSt, CaseSt,CaseFullAddress,ImporterFullAddress,BuyerFullAddress,SellerFullAddresst) 
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

    if ($stmt = $conn->prepare($insertQuery)) {
        $stmt->bind_param("ssssssssssssssssssssssssssssssssssssssssss", 
            $RegisterNo, $timeAndDate, $CaseTypeId, $PropertyTypeId, $PropertyId, $Lat_Long, $CaseAddressId, $ImporterAddressId, $BuyerAddressId, $SellerAddressId, $GroundArea, $GroundUnitId, $BuildingArea, $BuildingAreaUnitId, $BuildingTypeId, $ContractPrice, $ProposedPrice, $OwnerTypeId, $PayTypeId, $BnakId, $OwnerNrcId, $OwnerPh, $GuestNrcId, $StateId, $GuestPh, $ProposerNrcId, $ProposerPh, $NrcConfimStateId, $RegisterStateId, $BuildingType, $OtherType, $OtherArea, $FloorType, $OtherUnit, $ImporterSt, $SellerSt, $BuyerSt, $CaseSt,$CaseFullAddress,$ImporterFullAddress,$BuyerFullAddress,$SellerFullAddresst
        );

        if ($stmt->execute()) {
            $InsertedId = mysqli_insert_id($conn);
            $response["message"] = "Success";
            $response["status"] = 1;
            $response["id"] = $InsertedId;
        } else {
            $response["status"] = 2;
            $response["message"] = $stmt->error;
        }

        $stmt->close();
    } 
    
    else {
        $response["status"] = 0;
        $response["message"] = "ERROR: " . $conn->error;
    }

    return $response;
}
function set_CaseData($RegisterNo, $Date, $CaseTypeId, $PropertyTypeId, $PropertyId, $Lat_Long, $CaseAddressId, $ImporterAddressId, $BuyerAddressId, $SellerAddressId, $GroundArea, $GroundUnitId, $BuildingArea, $BuildingAreaUnitId, $BuildingTypeId, $ContractPrice, $ProposedPrice, $OwnerTypeId, $PayTypeId, $BnakId, $OwnerNrcId, $OwnerPh, $GuestNrcId, $StateId, $GuestPh, $ProposerNrcId, $ProposerPh, $NrcConfimStateId, $RegisterStateId, $BuildingType, $OtherType, $OtherArea, $FloorType,$OtherUnit,$ImporterSt,$SellerSt,$BuyerSt,$CaseSt,$OnlineImage,$CaseFullAddress,$ImporterFullAddress,$BuyerFullAddress,$SellerFullAddresst)
{
    global $conn;
    date_default_timezone_set('Asia/Yangon'); 
     $timeAndDate =  date('Y-m-d H:i:s');
    $response = array();
      
    //   $query = "SELECT max(GameId) from TblMatch";
    //     $rr= getNumber($query);
    $insertQuery  ="INSERT INTO TblRegister (RegisterNo, Date, CaseTypeId, PropertyTypeId, PropertyId, Lat_Long, CaseAddressId, ImporterAddressId, BuyerAddressId, SellerAddressId, GroundArea, GroundUnitId, BuildingArea, BuildingAreaUnitId, BuildingTypeId, ContractPrice, ProposedPrice, OwnerTypeId, PayTypeId, BnakId, OwnerNrcId, OwnerPh, GuestNrcId, StateId, GuestPh, ProposerNrcId, ProposerPh, NrcConfimStateId, RegisterStateId, BuildingType, OtherType, OtherArea, FloorType,OtherUnit,ImporterSt,SellerSt,BuyerSt,CaseSt,CaseFullAddress,ImporterFullAddress,BuyerFullAddress,SellerFullAddress) VALUES  (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
       if ($stmt = $conn->prepare($insertQuery))
        {
            $stmt->bind_param("ssssssssssssssssssssssssssssssssssssssssss", $RegisterNo, $timeAndDate, $CaseTypeId, $PropertyTypeId, $PropertyId, $Lat_Long, $CaseAddressId, $ImporterAddressId, $BuyerAddressId, $SellerAddressId, $GroundArea, $GroundUnitId, $BuildingArea, $BuildingAreaUnitId, $BuildingTypeId, $ContractPrice, $ProposedPrice, $OwnerTypeId, $PayTypeId, $BnakId, $OwnerNrcId, $OwnerPh, $GuestNrcId, $StateId, $GuestPh, $ProposerNrcId, $ProposerPh, $NrcConfimStateId, $RegisterStateId, $BuildingType, $OtherType, $OtherArea, $FloorType,$OtherUnit,$ImporterSt,$SellerSt,$BuyerSt,$CaseSt,$CaseFullAddress,$ImporterFullAddress,$BuyerFullAddress,$SellerFullAddresst);
            if ($stmt->execute()) {
            $InsertedId = mysqli_insert_id($conn);
           $Result[] = set_multituploadImage($OnlineImage,$InsertedId);
            $ResultId =$Result["status"];
            if($ResultId==1)
            {
                 $response["message"] = "Success";
                $response["status"] = 1;
                $response["id"] = $InsertedId;
            }
            else
            {
                 $response["message"] = "Success With No PhotoUpload";
                $response["status"] = 1;
                $response["id"] = $InsertedId;
            }
           
        } else {
            $response["status"] = 2;
            $response["message"] = $stmt->error;
        }
            $stmt->close();
          }
        else {
         $response["status"] = 0;
        $response["message"] = "ERROR: " . $conn->error;
        }
        // $response["id"] = $InsertedId;
    return $response;
}

function set_multituploadImage($OnlineImage,$CaseID)
{
    global $conn;
    $values = [];
    $params = [];
    $savedItems = [];
    foreach ($OnlineImage as $item) {
        if (!isset($item['ImagePath'], $item['CaseImageType'])) {
            continue;
        }

        $ImagePath = $item['ImagePath'];
        $ImageType = $item['ImageType'];
        $CaseImageType = $item['CaseImageType'];
      
        $CaseId = $CaseID;

        $Photo = saveImage($ImagePath, UPLOAD_DIR_DATA); // You must ensure this function returns saved image path
        $values[] = "(?, ?, ?)";
        array_push($params, $Photo, $ImageType, $CaseId);

        $savedItems[] = [
            'ImagePath' => $Photo,
            'ImageType' => $ImageType,
            'CaseImageType' => $CaseImageType,
            'CaseId' => $CaseId
        ];
    }

    if (empty($values)) {
        return [
            "status" => 2,
            "message" => "No valid images to insert"
        ];
    }

    $sql = "INSERT INTO TblImage (ImagePath,CaseImageType, ImageType, CaseId) VALUES " . implode(", ", $values);

    $stmt = $conn->prepare($sql);

    if (!$stmt) {
        return [
            "status" => 2,
            "message" => "SQL prepare error: " . $conn->error
        ];
    }

    // Generate type string dynamically
    $types = str_repeat("sssi", count($savedItems)); // s = string, s = string, i = int
    $stmt->bind_param($types, ...$params);

    if ($stmt->execute()) {
        $response = [
            "status" => 1,
            "message" => "Images inserted successfully",
            "data" => $savedItems
        ];
    } else {
        $response = [
            "status" => 2,
            "message" => "Insert failed: " . $stmt->error
        ];
    }

    $stmt->close();
    $conn->close();

    return $response;
}

function set_Flightdata_Only($UserId,$Latitude,$Longitude,$Status,$Remark,$LocationString,$TrackingString,$TrackName,$GameId)
{
    global $conn;
    date_default_timezone_set('Asia/Yangon'); 
     $timeAndDate =  date('Y-m-d H:i:s');
    $response = array();
      
    //   $query = "SELECT max(GameId) from TblMatch";
    //     $rr= getNumber($query);
        $insertQuery  = "INSERT INTO tbllocationpart (UserId,Latitude,Longitude,Status,Remark,GameId,Game_Date,LocationString,TrackingString,TrackName) VALUES (?,?,?,?,?,?,?,?,?,?)";
        if ($stmt = $conn->prepare($insertQuery))
        {
            $stmt->bind_param("ssssssssss", $UserId,$Latitude,$Longitude,$Status,$Remark,$GameId,$timeAndDate,$LocationString,$TrackingString,$TrackName);
            if ($stmt->execute()) 
            {
                 $response["message"] = "Success";
                 $response["status"] = 1;
            } 
            else 
            {
                $response["status"] = 2;
                $response["message"] = $stmt->error;
            }
            $stmt->close();
          }
        else {
        $response["status"] = 0;
        $response["message"] = "ERROR";
        }
    return $response;
}
function set_Name($UserId,$Latitude,$Longitude,$Status,$Remark,$LocationString,$TrackingString,$TrackName,$GameId)
{
    global $conn;
    date_default_timezone_set('Asia/Yangon'); 
     $timeAndDate =  date('Y-m-d H:i:s');
    $response = array();
      
    //   $query = "SELECT max(GameId) from TblMatch";
    //     $rr= getNumber($query);
    // INSERT INTO `TblName` (`NameId`, `Name`) VALUES (NULL, 'tt');
        $insertQuery  = "INSERT INTO `TblName` (`Name`)  VALUES (?)";
        if ($stmt = $conn->prepare($insertQuery))
        {
            $stmt->bind_param("s", $Name);
            if ($stmt->execute()) 
            {
                 $response["message"] = "Success";
                 $response["status"] = 1;
            } 
            else 
            {
                $response["status"] = 2;
                $response["message"] = $stmt->error;
            }
            $stmt->close();
          }
        else {
        $response["status"] = 0;
        $response["message"] = "ERROR";
        }
    return $response;
}








