<?php
    session_start();
    include "../../../db.php";
    date_default_timezone_set('Asia/Kolkata');
    $currentTime = date('M j, Y g:i a');

    if ( !isset($_SESSION['uid']) && !isset($_SESSION['uref']) && !isset($_SESSION['utype']) ) {
        header("Location: logout.php");
        exit();
    }
?>

<?php
    $edit_run = mysqli_query($con, "SELECT * FROM users WHERE uid = '$_SESSION[uid]'");
    $edit_row = mysqli_fetch_assoc($edit_run);


    $user_sql = "SELECT * FROM user2 where u_id='$_SESSION[uid]' LIMIT 1";
    $user_run = mysqli_query($con, $user_sql);
    while ( $user_rows = mysqli_fetch_assoc($user_run) ) {
    $sliderPath = substr($user_rows['u_flogo'], 3);
    $sliderPath = "../../login/".$sliderPath;   
    }
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <?php include "includes/meta.php"; ?>
    <?php include "includes/css.php"; ?>
    <link href="css/all.min.css" rel="stylesheet" type="text/css">
    <link href="css/alltable.css" rel="stylesheet">

</head>

<body>

    <!--==========================
  Header
  ============================-->
    <?php include "includes/header2.php"; ?>

    <!--==========================
    Intro Section
  ============================-->


    <div class="container">
        <div class="card-body">
            <h1 class="h3 mb-2 text-gray-800 text-center" style="margin-top: 90px">Profile</h1>
            <hr>
            <?php if(isset($_GET["edit_contact"])){
                $c_id = $_GET["edit_contact"];
                $eContact_sql = mysqli_query($con, "SELECT * FROM contact_table WHERE c_for = '$_SESSION[uid]' AND c_id = '$c_id' ");
                if($eContact_row = mysqli_fetch_assoc($eContact_sql)){
                ?>
            <div class="row">
                <div class="col-md-12">
                    <div class="card">
                        <div class="card-header text-center">Edit Contact Details</div>

                        <div class="card-body">
                            <div id="addForm">
                                <form method="post" enctype="multipart/form-data">
                                    <div class="row justify-content-center">
                                        <div class="col-md-6 mb-2">
                                            <label for="address"><strong>Address 1</strong></label>
                                            <input type="text" class="form-control form-control-sm mb-2" name="address"
                                                value="<?php echo $eContact_row["c_address"];?>">
                                        </div>
                                        <div class="col-md-6 mb-2">
                                            <label for="address1"><strong>Address 2</strong></label>
                                            <input type="text" class="form-control form-control-sm mb-2" name="address1"
                                                value="<?php echo $eContact_row["c_address1"];?>">
                                        </div>
                                        <div class="col-md-4 mb-2">
                                            <label for="contact"><strong>Mobile Number</strong></label>
                                            <input type="text" class="form-control form-control-sm mb-2" name="contact"
                                                value="<?php echo $eContact_row["c_mobile"];?>">
                                        </div>
                                        <div class="col-md-4 mb-2">
                                            <label for="contact2"><strong>Other Mobile Number</strong></label>
                                            <input type="text" class="form-control form-control-sm mb-2" name="contact2"
                                                value="<?php echo $eContact_row["c_mobileO"];?>">
                                        </div>
                                        <div class="col-md-4 mb-2">
                                            <label for="email"><strong>Email ID</strong></label>
                                            <input type="email" class="form-control form-control-sm mb-2" name="email"
                                                value="<?php echo $eContact_row["c_email"];?>">
                                        </div>
                                        <div class="col-md-12 mb-2">
                                            <div class="row">
                                                <div class="col-md-6 mb-2">
                                                    <font color="black">Map Link</font>
                                                    <textarea name="content" id="content" class="form-control ckeditor"
                                                        required><?php echo $eContact_row["c_map"];?></textarea>
                                                </div>
                                                <div class="col-md-6 mb-2">
                                                    <font color="black">Map Display</font>
                                                    <iframe src="<?php echo $eContact_row['c_map'];?>" width="100%"
                                                        height="100%" frameborder="0" style="border:0;"
                                                        allowfullscreen></iframe>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="col-md-12 mb-2">
                                            <label for="photo">Image Upload <span><img
                                                        src="<?php echo '../upload/blog/ck/' . $eContact_row['c_img'];?>"
                                                        class="img-fluid" style="height:50px;" alt=""></span></label>
                                            <input type="file" class="form-control" name="photo" accept="image/*">
                                        </div>
                                    </div>

                                    <button type="submit" class="btn btn-success btn-user m-2" name="edit_contact">
                                        <i class="fa fa-pencil"></i> Edit Contact
                                    </button>
                                </form>
                            </div>
                        </div>

                        <script>
                        CKEDITOR.replace('content', {
                            height: 300,
                            filebrowserUploadUrl: "upload.php"
                        });
                        </script>

                    </div>
                </div>
            </div>
            <?php } } else {?>
            <div class="row">
                <div class="col-md-6">
                    <div class="card p-2 rounded">
                        <p align="right">
                            <img src="<?php echo  $sliderPath; ?> " alt="Smiley face" height="auto" width="100">
                            <br> Name - <?php echo $edit_row["uname"]; ?>
                            <br>
                            Creat Date - <?php echo $edit_row["ucreated"]; ?> <br>
                            <a href="<?php echo $edit_row["uagreement"]; ?>" target="_blank"> View Aagreement
                            </a>

                        </p>
                        <?php if (!empty($edit_row["uphone"])) { ?>
                        <p><strong>Main Mobile -</strong> <a href="tel:+91<?php echo $edit_row["uphone"]; ?>">
                                <?php echo $edit_row["uphone"]; ?></a></p>
                        <?php } 
                                if (!empty($edit_row["uothcontact"])) { ?>
                        <p><strong>Oth Mobile -</strong> <a href="tel:+91<?php echo $edit_row["uothcontact"]; ?>">
                                <?php echo $edit_row["uothcontact"]; ?></a></p>
                        <?php } 
                                if (!empty($edit_row["utypeorg"])) { ?>
                        <p><strong>Type of Org -</strong> <?php echo $edit_row["utypeorg"]; ?></p>
                        <?php } 
                                if (!empty($edit_row["uwhatsapp"])) { ?>
                        <p><strong>WhatsApp -</strong> <a href="https://wa.me/91<?php echo $edit_row["uwhatsapp"]; ?>"
                                target="_blank">
                                <?php echo $edit_row["uwhatsapp"]; ?></a></p>
                        <?php } 
                                if (!empty($edit_row["umail"])) { ?>
                        <p><strong>Email -</strong> <a href="mailto:<?php echo $edit_row["umail"]; ?>">
                                <?php echo $edit_row["umail"]; ?></a></p>
                        <?php } 
                                if (!empty($edit_row["uweburl"])) { ?>
                        <p><strong>Website -</strong> <a href="<?php echo $edit_row["uweburl"]; ?>" target="_blank">
                                <?php echo $edit_row["uweburl"]; ?></a></p>
                        <?php } 
                                if (!empty($edit_row["ufburl"])) { ?>
                        <p><strong>Fb URL -</strong> <a href="<?php echo $edit_row["ufburl"]; ?>" target="_blank">
                                <?php echo $edit_row["ufburl"]; ?></a></p>
                        <?php } 
                                if (!empty($edit_row["utwitter"])) { ?>
                        <p><strong>Twitter URL -</strong> <a href="<?php echo $edit_row["utwitter"]; ?>"
                                target="_blank">
                                <?php echo $edit_row["utwitter"]; ?></a></p>
                        <?php } 
                                if (!empty($edit_row["uinsta"])) { ?>
                        <p><strong>Instagram URL -</strong> <a href="<?php echo $edit_row["uinsta"]; ?>"
                                target="_blank">
                                <?php echo $edit_row["uinsta"]; ?></a></p>
                        <?php } 
                                if (!empty($edit_row["uyoutube"])) { ?>
                        <p><strong>YouTube URL -</strong> <a href="<?php echo $edit_row["uyoutube"]; ?>"
                                target="_blank">
                                <?php echo $edit_row["uyoutube"]; ?></a></p>
                        <?php } 
                                if (!empty($edit_row["linkdin"])) { ?>
                        <p><strong>LinkedIn URL -</strong> <a href="<?php echo $edit_row["linkdin"]; ?>"
                                target="_blank">
                                <?php echo $edit_row["linkdin"]; ?></a></p>
                        <?php } ?>

                        <?php if (!empty($edit_row["uaddress1"]) || !empty($edit_row["uaddress2"])) { ?>
                        <p><strong>Address -</strong>
                            <?php echo $edit_row["uaddress1"] . (!empty($edit_row["uaddress2"]) ? ' &nbsp;' . $edit_row["uaddress2"] : ''); ?>
                        </p>
                        <?php } ?>

                    </div>
                    <button class="btn btn-primary m-2" onclick="toggleForm()">Show/Hide Profile Edit</button>
                    <button class="btn btn-primary m-2" onclick="contactForm()">Show/Hide Contact Edit</button>
                </div>
                <div class="col-md-6" id="profileForm" style="display: none;">
                    <div class="card p-2">
                        <p align="center" class=" btn-success btn-user m-2" name="edit_cat">
                            <i class="far fa-image"></i> Update Profile
                        </p>

                        <form class="user" method="post" enctype="multipart/form-data">
                            <h4 class="mb-3 badge badge-secondary">Personal Details</h4>
                            <div class="row justify-content-center">

                                <div class="col-md-6">
                                    <div class="mb-1 mb-sm-0">
                                        <label for="uname"><strong>Name</strong></label>
                                        <input type="text" class="form-control form-control-sm mb-2" id="uname"
                                            name="uname" value="<?php echo $edit_row["uname"]; ?>">
                                    </div>
                                    <div class="mb-1 mb-sm-0">
                                        <label for="uwhatsapp"><strong>WhatsApp No</strong></label>
                                        <input type="text" class="form-control form-control-sm mb-2" id="uwhatsapp"
                                            name="uwhatsapp" value="<?php echo $edit_row["uwhatsapp"]; ?>">
                                    </div>
                                    <div class="mb-1 mb-sm-0">
                                        <label for="uothcontact"><strong>Other Contact</strong></label>
                                        <input type="text" class="form-control form-control-sm mb-2" id="uothcontact"
                                            name="uothcontact" value="<?php echo $edit_row["uothcontact"]; ?>">
                                    </div>
                                    <div class="mb-1 mb-sm-0">
                                        <label for="umail"><strong>Email ID</strong></label>
                                        <input type="email" class="form-control form-control-sm mb-2" id="umail"
                                            name="umail" value="<?php echo $edit_row["umail"]; ?>">
                                    </div>
                                    <div class="mb-1 mb-sm-0">
                                        <label for="ufburl"><strong>Facebook URL</strong></label>
                                        <input type="text" class="form-control form-control-sm mb-2" id="ufburl"
                                            name="ufburl" value="<?php echo $edit_row["ufburl"]; ?>">
                                    </div>
                                    <div class="mb-1 mb-sm-0">
                                        <label for="utwitter"><strong>Twitter URL</strong></label>
                                        <input type="text" class="form-control form-control-sm mb-2" id="utwitter"
                                            name="utwitter" value="<?php echo $edit_row["utwitter"]; ?>">
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <div class="mb-1 mb-sm-0">
                                        <label for="uinsta"><strong>Instagram</strong></label>
                                        <input type="text" class="form-control form-control-sm mb-2" id="uinsta"
                                            name="uinsta" value="<?php echo $edit_row["uinsta"]; ?>">
                                    </div>
                                    <div class="mb-1 mb-sm-0">
                                        <label for="uyoutube"><strong>YouTube URL</strong></label>
                                        <input type="text" class="form-control form-control-sm mb-2" id="uyoutube"
                                            name="uyoutube" value="<?php echo $edit_row["uyoutube"]; ?>">
                                    </div>
                                    <div class="mb-1 mb-sm-0">
                                        <label for="linkdin"><strong>LinkedIn URL</strong></label>
                                        <input type="text" class="form-control form-control-sm mb-2" id="linkdin"
                                            name="linkdin" value="<?php echo $edit_row["linkdin"]; ?>">
                                    </div>
                                    <div class="mb-1 mb-sm-0">
                                        <label for="uaddress1"><strong>Organization Address 1</strong></label>
                                        <input type="text" class="form-control form-control-sm mb-2" id="uaddress1"
                                            name="uaddress1" value="<?php echo $edit_row["uaddress1"]; ?>">
                                    </div>
                                    <div class="mb-1 mb-sm-0">
                                        <label for="uaddress2"><strong>Organization Address 2</strong></label>
                                        <input type="text" class="form-control form-control-sm mb-2" id="uaddress2"
                                            name="uaddress2" value="<?php echo $edit_row["uaddress2"]; ?>">
                                    </div>
                                    <div class="mb-1 mb-sm-0">
                                        <label for="theme"><strong>Select Theme</strong></label>
                                        <select name="theme" id="theme" class="form-control mb-3">
                                            <option value="<?php echo $edit_row["theme"];?>">Current:
                                                <?php echo $edit_row["theme"]; ?></option>
                                            <option value="FFA500">Orange</option>
                                            <option value="3887c7">Blue</option>
                                            <option value="336600">Green</option>
                                            <option value="336699">Dark Blue</option>
                                            <option value="666633">Brown</option>
                                        </select>
                                    </div>
                                </div>

                            </div>

                            <h4 class="mb-3 badge badge-secondary">Login Details</h4>
                            <div class="form-group">
                                <div class="mb-1 mb-sm-0">
                                    <label for="upass"><strong>Password</strong></label>
                                    <input type="password" class="form-control form-control-sm mb-2" id="upass"
                                        name="upass" placeholder="Password *" value="<?php echo $edit_row["upass"]; ?>">
                                </div>
                            </div>

                            <button type="submit" class="btn btn-success btn-user m-2" name="edit_cat">
                                <i class="far fa-image"></i> Update Profile
                            </button>
                            <a href="index.php" class="btn btn-warning btn-user m-2">
                                <i class="fas fa-ban"></i> Cancel
                            </a>
                        </form>

                        <script>
                        function toggleForm() {
                            var formDiv = document.getElementById("profileForm");
                            if (formDiv.style.display === "none") {
                                formDiv.style.display = "block";
                            } else {
                                formDiv.style.display = "none";
                            }
                        }
                        </script>

                    </div>
                </div>
                <div class="col-md-12" id="contactForm" style="display: none;">
                    <div class="card">
                        <div class="card-header text-center">Contact Details</div>

                        <div class="card-body">
                            <button class="btn btn-success m-2" onclick="addForm()">Add Contact Details</button>
                            <div id="addForm" style="display:none;">
                                <form method="post" enctype="multipart/form-data">
                                    <h4 class="mb-3 badge badge-secondary">Personal Details</h4>
                                    <div class="row justify-content-center">
                                        <div class="col-md-6 mb-2">
                                            <label for="address"><strong>Address 1</strong></label>
                                            <input type="text" class="form-control form-control-sm mb-2" name="address"
                                                placeholder="Address Line One">
                                        </div>
                                        <div class="col-md-6 mb-2">
                                            <label for="address1"><strong>Address 2</strong></label>
                                            <input type="text" class="form-control form-control-sm mb-2" name="address1"
                                                placeholder="Address Line Two">
                                        </div>
                                        <div class="col-md-4 mb-2">
                                            <label for="contact"><strong>Mobile Number</strong></label>
                                            <input type="text" class="form-control form-control-sm mb-2" name="contact"
                                                placeholder="Enter Mobile Number">
                                        </div>
                                        <div class="col-md-4 mb-2">
                                            <label for="contact2"><strong>Other Mobile Number</strong></label>
                                            <input type="text" class="form-control form-control-sm mb-2" name="contact2"
                                                placeholder="Enter Mobile Number">
                                        </div>
                                        <div class="col-md-4 mb-2">
                                            <label for="email"><strong>Email ID</strong></label>
                                            <input type="email" class="form-control form-control-sm mb-2" name="email"
                                                placeholder="Enter Email Id">
                                        </div>
                                        <div class="col-md-12 mb-2">
                                            <font color="black">Map Link</font>
                                            <textarea name="content" id="content" class="form-control ckeditor"
                                                required></textarea>
                                        </div>
                                        <div class="col-md-12 mb-2">
                                            <font color="black">Upload Image</font>
                                            <input type="file" class="form-control" name="photo" accept="image/*">
                                        </div>
                                    </div>

                                    <button type="submit" class="btn btn-success btn-user m-2" name="add_contact">
                                        <i class="fa fa-plus"></i> Add Contact
                                    </button>
                                </form>
                            </div>
                        </div>

                        <?php
                        $cons_sql = mysqli_query($con, "SELECT * FROM contact_table WHERE c_for = '$_SESSION[uid]' ");
                        if(mysqli_num_rows($cons_sql)>0){
                        ?>
                        <div class="card-body mt-3">
                            <h5>Contact Details:</h5>
                            <div class="row align-items-center">
                                <?php
                                $c = 1;
                                while($cons_row = mysqli_fetch_assoc($cons_sql)){
                                    ?>
                                <div class="col-md-12 mb-2">
                                    <div class="card p-3 bg-secondery">
                                        <div class="row">
                                            <div class="col-md-5">
                                                <h5 class="m-0"><?php echo $c;?>)</h5>
                                                <p class="m-0"><strong>Address:</strong>
                                                    <?php echo $cons_row["c_address"];?></p>
                                                <p class="m-0"><strong>Address:</strong>
                                                    <?php echo $cons_row["c_address1"];?></p>
                                                <p class="m-0"><strong>Mobile:</strong>
                                                    <?php echo $cons_row["c_mobile"];?></p>
                                                <p class="m-0"><strong>Other Mobile:</strong>
                                                    <?php echo $cons_row["c_mobileO"];?></p>
                                                <p class="m-0"><strong>Email:</strong>
                                                    <?php echo $cons_row["c_email"];?></p>
                                                <p class="m-0"><strong>Upload date:</strong>
                                                    <?php echo $cons_row["c_upDate"];?></p>
                                            </div>
                                            <div class="col-md-3">
                                                <iframe src="<?php echo $cons_row['c_map'];?>" width="100%" height="200"
                                                    frameborder="0" style="border:0;" allowfullscreen></iframe>
                                            </div>
                                            <div class="col-md-3">
                                                <img src="<?php echo '../upload/blog/ck/' . $cons_row['c_img'];?>"
                                                    class="img-fluid" alt="">
                                            </div>
                                            <div class="col-md-1">
                                                <div class="mb-2">
                                                    <a class="btn btn-sm btn-primary"
                                                        href="profile_multicontact.php?edit_contact=<?php echo $cons_row["c_id"];?>"><i
                                                            class="fa fa-edit"></i></a>
                                                </div>
                                                <div>
                                                    <a class="btn btn-sm btn-danger"
                                                        href="profile_multicontact.php?del_contact=<?php echo $cons_row["c_id"];?>"><i
                                                            class="fa fa-trash"></i></a>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <?php $c++;
                                }
                                ?>

                            </div>
                        </div>
                        <?php }?>

                        <script>
                        CKEDITOR.replace('content', {
                            height: 300,
                            filebrowserUploadUrl: "upload.php"
                        });
                        </script>
                        <script>
                        function contactForm() {
                            var formDiv = document.getElementById("contactForm");
                            if (formDiv.style.display === "none") {
                                formDiv.style.display = "block";
                            } else {
                                formDiv.style.display = "none";
                            }
                        }

                        function addForm() {
                            var formDiv = document.getElementById("addForm");
                            if (formDiv.style.display === "none") {
                                formDiv.style.display = "block";
                            } else {
                                formDiv.style.display = "none";
                            }
                        }
                        </script>

                    </div>
                </div>
            </div>
            <?php }?>

        </div>
    </div>

    <?php include "includes/ft.php"; ?>
    <?php include "includes/js.php"; ?>



</body>

</html>



<?php
if(isset($_POST['edit_cat'])){
    $uname = mysqli_real_escape_string($con, strip_tags($_POST['uname']));
	$uwhatsapp = mysqli_real_escape_string($con, strip_tags($_POST['uwhatsapp']));
	$uothcontact = mysqli_real_escape_string($con, strip_tags($_POST['uothcontact']));
	$umail = mysqli_real_escape_string($con, strip_tags($_POST['umail']));
	$ufburl = mysqli_real_escape_string($con, strip_tags($_POST['ufburl']));
	$utwitter = mysqli_real_escape_string($con, strip_tags($_POST['utwitter']));
	$uinsta = mysqli_real_escape_string($con, strip_tags($_POST['uinsta']));
	$uyoutube = mysqli_real_escape_string($con, strip_tags($_POST['uyoutube']));
	$linkdin = mysqli_real_escape_string($con, strip_tags($_POST['linkdin']));
	$uaddress1 = mysqli_real_escape_string($con, strip_tags($_POST['uaddress1']));
	$uaddress2 = mysqli_real_escape_string($con, strip_tags($_POST['uaddress2']));
	$theme = mysqli_real_escape_string($con, strip_tags($_POST['theme']));
		
      
    $upass = mysqli_real_escape_string($con, strip_tags($_POST['upass']));

    $up_sql = "UPDATE users SET uname = '$uname', uwhatsapp = '$uwhatsapp',uothcontact = '$uothcontact',umail = '$umail',ufburl = '$ufburl',utwitter = '$utwitter',uinsta = '$uinsta',uyoutube = '$uyoutube',linkdin = '$linkdin', uaddress1 = '$uaddress1', uaddress2 = '$uaddress2', theme = '$theme',  upass = '$upass' WHERE uid = '$_SESSION[uid]'";

    $_SESSION["uname"] = $uname;

    if(mysqli_query($con, $up_sql)){
            ?>
<script>
window.location = 'profile_multicontact.php';
</script>
<?php
        }
        else{
            ?>
<script>
alert('OOPS !!!!');
setTimeout(() => {
    window.location = 'profile_multicontact.php';
}, 500);
</script>
<?php
        }
    }
?>

<?php
if(isset($_POST['add_contact'])){
	
    // Escape user input to prevent SQL Injection
    $contact = mysqli_real_escape_string($con, strip_tags($_POST['contact']));
    $contact2 = mysqli_real_escape_string($con, strip_tags($_POST['contact2']));
    $email = mysqli_real_escape_string($con, strip_tags($_POST['email']));
    $address = mysqli_real_escape_string($con, strip_tags($_POST['address']));
    $address1 = mysqli_real_escape_string($con, strip_tags($_POST['address1']));
    $map = $_POST['content']; // CKEditor content
    $x = $_SESSION["uid"]; 

    // Handle Image Upload
    $target_dir = "../upload/blog/ck/";
    $photo_name = "";

    if (!empty($_FILES["photo"]["name"])) {
        $photo_ext = pathinfo($_FILES["photo"]["name"], PATHINFO_EXTENSION);
        $photo_name = time() . "_" . uniqid() . "." . $photo_ext; // Unique file name
        $target_file = $target_dir . $photo_name;

        // Check if the directory exists, if not, create it
        if (!is_dir($target_dir)) {
            mkdir($target_dir, 0777, true);
        }

        // Move the uploaded file
        if (!move_uploaded_file($_FILES["photo"]["tmp_name"], $target_file)) {
            echo "<script>alert('Image upload failed!'); window.history.back();</script>";
            exit;
        }
    }

    // Insert into database
    $sql = "INSERT INTO contact_table (c_for, c_address, c_address1, c_mobile, C_mobileO c_email, c_map, c_img, c_upDate) 
            VALUES ('$x', '$address', '$address1', '$contact', '$contact2', '$email', '$map', '$photo_name', NOW())";

    if(mysqli_query($con, $sql)) {
        echo "<script>window.location = 'profile_multicontact.php';</script>";
    } else {
        echo "<script>alert('OOPS !!!!'); setTimeout(() => { window.location = 'profile_multicontact.php'; }, 500);</script>";
    }
}
?>
<?php
if(isset($_GET['del_contact'])){
	$c_id = mysqli_real_escape_string($con, $_GET['del_contact']);

    // Fetch the image name before deleting the record
    $query = "SELECT c_img FROM contact_table WHERE c_id = '$c_id'";
    $result = mysqli_query($con, $query);
    $row = mysqli_fetch_assoc($result);

    if ($row) {
        $image_path = "../upload/blog/ck/" . $row['c_img'];

        // Delete the image file if it exists
        if (!empty($row['c_img']) && file_exists($image_path)) {
            unlink($image_path);
        }

        // Delete the record from the database
        $delete_sql = "DELETE FROM contact_table WHERE c_id = '$c_id'";
        if (mysqli_query($con, $delete_sql)) {
            echo "<script>window.location = 'profile_multicontact.php';</script>";
        } else {
            echo "<script>alert('Failed to delete contact!'); window.history.back();</script>";
        }
    } else {
        echo "<script>alert('Record not found!'); window.history.back();</script>";
    }
}
?>

<?php
if(isset($_POST['edit_contact'])) {
    $contact = mysqli_real_escape_string($con, strip_tags($_POST['contact']));
    $contact2 = mysqli_real_escape_string($con, strip_tags($_POST['contact2']));
    $email = mysqli_real_escape_string($con, strip_tags($_POST['email']));
    $address = mysqli_real_escape_string($con, strip_tags($_POST['address']));
    $address1 = mysqli_real_escape_string($con, strip_tags($_POST['address1']));
    $map = $_POST['content']; // CKEditor content
    $x = $_SESSION["uid"];

    // Handle Image Upload
    $target_dir = "../upload/blog/ck/";
    $photo_name = "";

    if (!empty($_FILES["photo"]["name"])) {
        $photo_ext = pathinfo($_FILES["photo"]["name"], PATHINFO_EXTENSION);
        $photo_name = time() . "_" . uniqid() . "." . $photo_ext; // Unique file name
        $target_file = $target_dir . $photo_name;

        // Check if the directory exists, if not, create it
        if (!is_dir($target_dir)) {
            mkdir($target_dir, 0777, true);
        }

        // Move the uploaded file
        if (!move_uploaded_file($_FILES["photo"]["tmp_name"], $target_file)) {
            echo "<script>alert('Image upload failed!'); window.history.back();</script>";
            exit;
        }
    }
    
    if (isset($_POST['edit_contact']) && isset($_GET['edit_contact'])) {
        $edit_id = mysqli_real_escape_string($con, $_GET['edit_contact']);
        
        // Fetch existing image
        $query = mysqli_query($con, "SELECT c_img FROM contact_table WHERE c_id = '$edit_id'");
        $row = mysqli_fetch_assoc($query);
        $old_image = $row['c_img'];
        
        if (!empty($photo_name)) { // If new image uploaded, delete old image
            if (!empty($old_image) && file_exists($target_dir . $old_image)) {
                unlink($target_dir . $old_image);
            }
        } else {
            $photo_name = $old_image; // Keep old image if no new image is uploaded
        }
        
        // Update record
        $sql = "UPDATE contact_table SET c_address = '$address', c_address1 = '$address1', c_mobile = '$contact', c_mobileO = '$contact2', c_email = '$email', c_map = '$map', c_img = '$photo_name', c_upDate = NOW() WHERE c_id = '$edit_id'";
    }
    
    if(mysqli_query($con, $sql)) {
        echo "<script>window.location = 'profile_multicontact.php';</script>";
    } else {
        echo "<script>alert('OOPS !!!!'); setTimeout(() => { window.location = 'profile_multicontact.php'; }, 500);</script>";
    }
}
?>