<?php

require_once "config.php";

date_default_timezone_set("asia/Kuala_Lumpur");

 "property id: ". $property_id = $_POST["property_id"];
$policy_year = $_POST["policy_year"];
$policy_no = $_POST["policy_no"];
$policy_start_cover = $_POST["policy_start_cover"];
$policy_end_cover = $_POST["policy_end_cover"];
$policy_total_cover = $_POST["policy_total_cover"];
$policy_type_of_used = $_POST["policy_type_of_used"];
 "FILE ATTACHMENT: ".$policy_file_attachment = $_POST["policy_file_attachment"];
$cukaipintu_year = $_POST["cukaipintu_year"];
$cukaipintu_account_no = $_POST["cukaipintu_account_no"];
$cukaipintu_charges = $_POST["cukaipintu_charges"];
$cukaipintu_duedate = $_POST["cukaipintu_duedate"];
$cukaipintu_file_attachment = $_POST["cukaipintu_file_attachment"];
$cukaitanahpetak_year = $_POST["cukaitanahpetak_year"];
$cukaitanahpetak_account_no = $_POST["cukaitanahpetak_account_no"];
$cukaitanahpetak_charges = $_POST["cukaitanahpetak_charges"];
$cukaitanahpetak_duedate = $_POST["cukaitanahpetak_duedate"];
$cukaitanahpetak_file_attachment = $_POST["cukaitanahpetak_file_attachment"];
$MF_charges = $_POST["MF_charges"];
$SF_charges = $_POST["SF_charges"];
$MFSF_duedate = $_POST["MFSF_duedate"];
$MFSF_file_attachment = $_POST["MFSF_file_attachment"];
$iwk_account_no = $_POST["iwk_account_no"];
$iwk_charges = $_POST["iwk_charges"];
$iwk_duedate = $_POST["iwk_duedate"];
$iwk_file_attachment = $_POST["iwk_file_attachment"];
$mv_year = $_POST["mv_year"];
$mv_RM = $_POST["mv_RM"];

    $currentDirectory = getcwd();
    $uploadDirectory = "Uploads/FileProperties/";

    $errors = []; // Store errors here

	
	 "policy_file_attachment: ".$fileName = $_FILES['policy_file_attachment']['name'];
	$fileName_1 = $_FILES['cukaipintu_file_attachment']['name'];
	$fileName_2 = $_FILES['cukaitanahpetak_file_attachment']['name'];
	$fileName_3 = $_FILES['MFSF_file_attachment']['name'];
	$fileName_4 = $_FILES['iwk_file_attachment']['name'];
    $fileSize = $_FILES['policy_file_attachment']['size'];
    $fileTmpName  = $_FILES['policy_file_attachment']['tmp_name'];
    $fileTmpName_1  = $_FILES['cukaipintu_file_attachment']['tmp_name'];
    $fileTmpName_2  = $_FILES['cukaitanahpetak_file_attachment']['tmp_name'];
    $fileTmpName_3  = $_FILES['MFSF_file_attachment']['tmp_name'];
    $fileTmpName_4  = $_FILES['iwk_file_attachment']['tmp_name'];
    $fileType = $_FILES['policy_file_attachment']['type'];
    $fileExtension = strtolower(end(explode('.',$fileName)));
    $fileExtension_1 = strtolower(end(explode('.',$fileName_1)));
    $fileExtension_2 = strtolower(end(explode('.',$fileName_2)));
    $fileExtension_3 = strtolower(end(explode('.',$fileName_3)));
    $fileExtension_4 = strtolower(end(explode('.',$fileName_4)));

   // $uploadPath = $currentDirectory . $uploadDirectory . basename($fileName); 
    $uploadPath =  $uploadDirectory . basename($fileName); 
    $uploadPath_1 =  $uploadDirectory . basename($fileName_1); 
    $uploadPath_2 =  $uploadDirectory . basename($fileName_2); 
    $uploadPath_3 =  $uploadDirectory . basename($fileName_3); 
    $uploadPath_4 =  $uploadDirectory . basename($fileName_4); 

    if (isset($_POST['submit'])) {

      if (empty($errors)) {
        $didUpload = move_uploaded_file($fileTmpName, $uploadPath);
        $didUpload_1 = move_uploaded_file($fileTmpName_1, $uploadPath_1);
        $didUpload_2 = move_uploaded_file($fileTmpName_2, $uploadPath_2);
        $didUpload_3 = move_uploaded_file($fileTmpName_3, $uploadPath_3);
        $didUpload_3 = move_uploaded_file($fileTmpName_4, $uploadPath_4);

        if ($didUpload) {
          echo "The file " . basename($fileName) . " has been uploaded";
          echo "The file " . basename($fileName_1) . " has been uploaded";
          echo "The file " . basename($fileName_2) . " has been uploaded";
          echo "The file " . basename($fileName_3) . " has been uploaded";
          echo "The file " . basename($fileName_4) . " has been uploaded";
        } else {
          echo "An error occurred. Please contact the administrator.";
        }
      } else {
        foreach ($errors as $error) {
          echo $error . "These are the errors" . "\n";
        }
      }
	
	 $SQL_fireinsurance = "insert into insuran_kebakaran 
				(id,
				property_id,
				policy_year,
				policy_no,
				policy_start_cover,
				policy_end_cover,
				policy_total_cover,
				policy_type_of_used,
				policy_file_attachment,
				created_at,
				updated_at
				)
				values(
				'',
				'".$property_id."',
				'".$policy_year."',
				'".$policy_no."',
				'".$policy_start_cover."',
				'".$policy_end_cover."',
				'".$policy_total_cover."',
				'".$policy_type_of_used."',
				'".$fileName."',
				'".date('Y-m-d H:i:s')."',
				'".date('Y-m-d H:i:s')."'
				)
	";

	$runSQL_fireinsurance = mysqli_query($link, $SQL_fireinsurance);
	if($runSQL_fireinsurance === FALSE){
		echo "Error: " . $SQL_fireinsurance . "<br>" . mysqli_error($link);
	}
	
	$SQL_cukaipintu = "insert into cukai_pintu
				(id,
				property_id,
				cukaipintu_year,
				cukaipintu_account_no,
				cukaipintu_charges,
				cukaipintu_duedate,
				cukaipintu_file_attachment,
				created_at,
				updated_at
				)
				values(
				'',
				'".$property_id."',
				'".$cukaipintu_year."',
				'".$cukaipintu_account_no."',
				'".$cukaipintu_charges."',
				'".$cukaipintu_duedate."',
				'".$fileName_1."',
				'".date("Y-m-d H:i:s")."',
				'".date("Y-m-d H:i:s")."'
				)
	";
	
	$runSQL_cukaipintu = mysqli_query($link, $SQL_cukaipintu);
	if($runSQL_cukaipintu === FALSE){
		echo "Error: " . $SQL_cukaipintu . "<br>" . mysqli_error($link);
	}
	
	$SQL_cukaitanahpetak = "insert into cukai_tanahpetak
				(id,
				property_id,
				cukaitanahpetak_year,
				cukaitanahpetak_account_no,
				cukaitanahpetak_charges,
				cukaitanahpetak_duedate,
				cukaitanahpetak_file_attachment,
				created_at,
				updated_at
				)
				values(
				'',
				'".$property_id."',
				'".$cukaitanahpetak_year."',
				'".$cukaitanahpetak_account_no."',
				'".$cukaitanahpetak_charges."',
				'".$cukaitanahpetak_duedate."',
				'".$fileName_2."',
				'".date("Y-m-d H:i:s")."',
				'".date("Y-m-d H:i:s")."'
				)
	";
	$runSQL_cukaitanahpetak = mysqli_query($link, $SQL_cukaitanahpetak);
	if($runSQL_cukaitanahpetak === FALSE){
		echo "Error: " . $SQL_cukaitanahpetak . "<br>" . mysqli_error($link);
	}
	
	$SQL_mntnfee_snkgfund = "insert into mntnfee_snkgfund
				(id,
				property_id,
				MF_charges,
				SF_charges,
				MFSF_duedate,
				MFSF_file_attachment,
				created_at,
				updated_at
				)
				values(
				'',
				'".$property_id."',
				'".$MF_charges."',
				'".$SF_charges."',
				'".$MFSF_duedate."',
				'".$fileName_3."',
				'".date("Y-m-d H:i:s")."',
				'".date("Y-m-d H:i:s")."'
				)
	";
	$runSQL_mntnfee_snkgfund = mysqli_query($link, $SQL_mntnfee_snkgfund);
	if($runSQL_mntnfee_snkgfund === FALSE){
		echo "Error: " . $SQL_mntnfee_snkgfund . "<br>" . mysqli_error($link);
	}
	
	$SQL_iwk = "insert into iwk
				(id,
				property_id,
				iwk_account_no,
				iwk_charges,
				iwk_due_date,
				iwk_file_attachment,
				created_at,
				updated_at
				)
				values(
				'',
				'".$property_id."',
				'".$iwk_account_no."',
				'".$iwk_charges."',
				'".$iwk_duedate."',
				'".$fileName_4."',
				'".date("Y-m-d H:i:s")."',
				'".date("Y-m-d H:i:s")."'
				)
	";
	$runSQL_iwk = mysqli_query($link, $SQL_iwk);
	if($runSQL_iwk === FALSE){
		echo "Error: " . $SQL_iwk . "<br>" . mysqli_error($link);
	}
	
	$SQL_nilaipasaran = "insert into nilai_pasaran
					(id,
					property_id,
					mv_year,
					mv_RM,
					created_at,
					updated_at
					)
					values(
					'',
					'".$property_id."',
					'".$mv_year."',
					'".$mv_RM."',
					'".date("Y-m-d H:i:s")."',
					'".date("Y-m-d H:i:s")."'
					)
	";

	$runSQL_nilaipasaran = mysqli_query($link, $SQL_nilaipasaran);
	if($runSQL_nilaipasaran === FALSE){
		echo "Error: " . $SQL_nilaipasaran . "<br>" . mysqli_error($link);
	}
	header("Location: properties_viewPropertyRecord.php?property_id=" . $property_id);
}
?>