<?php 


session_start();

    function getProperties(){
        include '../dbconfig.php';
        $query = "Select * from properties";
        $stmt = $conn->prepare($query);
        $stmt->execute();
        $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $data=$stmt->fetchAll();
        return $data;
    }

    function getPropertiesCount(){
        include '../dbconfig.php';
        $query = "Select * from properties where property_type = 'Residential'";
        $stmt = $conn->prepare($query);
        $stmt->execute();
        $residentialCount =$stmt->rowCount();

        $query2 = "Select * from properties where property_type = 'Commercial'";
        $stmt2 = $conn->prepare($query2);
        $stmt2->execute();
        $commercialCount = $stmt2->rowCount();
       
        $query3 = "Select * from properties where property_type = 'Industrial'";
        $stmt23 = $conn->prepare($query3);
        $stmt23->execute();
        $industrialCount =$stmt23->rowCount();
        $countAll = array($residentialCount,$commercialCount,$industrialCount);
        return $countAll;
    }

    function get_data2($conn,$data){
        $query='1=1 ';
    	// $property_type=$data['property_type'];
     //    $property_status=$data['property_status'];
     //    $shop_office_godown=$data['shop_office_godown'];
     //    $sqft=$data['sqft'];
       
      //  $rest=' and 1=1';
            $checkCommercial=in_array('Commercial', $data);
    		foreach ($data as $key => $value) {
            if($key=='s_range' || $key=='e_range'){
                 $s_range=str_replace('Rs ', '', $data['s_range'])?:100000;
                 $e_range=str_replace('Rs ', '', $data['e_range'])?:100000000;
                  $s_range=str_replace(',', '', $s_range);
                 $e_range=str_replace(',', '', $e_range);
                 $rest=" and price BETWEEN ".$s_range." and ".$e_range;
            }else if($key=='sq_ft'){
                if($checkCommercial){
                    $values=explode("-",$value);
                   // print_r($values);
                    $query.=" and sq_ft BETWEEN ".$values[0]." and ".$values[1];
                }else{
           $query.=" and ".$key.">='".$value."'";
                }
       }else{
        $query.=" and ".$key."='".$value."'";
       }
        }
    	
     $sql = "SELECT * from properties where ".$query." order by id DESC ";

    $stmt = $conn->prepare($sql);
    $stmt->execute();
      $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
$data=$stmt->fetchAll();
return $data;
    }

     function getPropertyById($id)
    {
        include '../dbconfig.php';
        $query = "Select * from properties where id ='$id'";
        $stmt = $conn->prepare($query);
        $stmt->execute();
        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $data=$stmt->fetch();
        return $data;
    }

    if(isset($_GET['deleteId'])){
        include '../dbconfig.php';
        $id = $_GET['deleteId'];
        $query = "DELETE  from properties where id ='$id'";
        $stmt = $conn->prepare($query);
       $res = $stmt->execute();
       if($res){
        $_SESSION['success'] = " Property Deleted Succesfully";
        header('location:dashboard.php');
       }
    }

    if(isset($_GET['deletePicId'])){
        include '../dbconfig.php';
        $id = $_GET['deletePicId'];
        $key = $_GET['key'];
        $data = getPropertyById($id);
        $images = explode(',', $data['image']);
        unset($images[$key]);
        $newImage = implode(",",$images);
        $query = "UPDATE   properties SET `image`='$newImage' where id ='$id'";
        $stmt = $conn->prepare($query);
       $res = $stmt->execute();
       if($res){
        $_SESSION['success'] = " Image Deleted Succesfully";
        
       } else{
        $_SESSION['error'] = " some error occured , try again";
       }
       header('location:editproperty.php?id='.$id);
    }


    //      function getProjectById($id)
    // {
    //     include '../dbconfig.php';
    //     include 'connect.php';
    //     $query = "Select * from project_details where id ='$id'";
    //     $stmt = $conn->prepare($query);
    //     $stmt->execute();
    //     $stmt->setFetchMode(PDO::FETCH_ASSOC);
    //     $data=$stmt->fetch();
    //     return $data;
    // }
?>