<?php
session_start();
include '../../assets/constant/config.php';

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

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

        $filepath = "../../assets/images/" . htmlspecialchars($_FILES["photo"]["name"], ENT_QUOTES, 'UTF-8');

        if (move_uploaded_file($_FILES["photo"]["tmp_name"], $filepath)) {
            $img = htmlspecialchars($_FILES["photo"]["name"], ENT_QUOTES, 'UTF-8');
        } else {
            echo "Error !!";
        }
//insert second photo
$filepath = "../../assets/images/" . htmlspecialchars($_FILES["photo1"]["name"], ENT_QUOTES, 'UTF-8');

        if (move_uploaded_file($_FILES["photo1"]["tmp_name"], $filepath)) {
            $img1= htmlspecialchars($_FILES["photo1"]["name"], ENT_QUOTES, 'UTF-8');
        } else {
            echo "Error !!";
        }
      

        $stmt = $conn->prepare("INSERT INTO `soul_winning`(`branch`,`name`,`gender`, `marital`,`status`,`phone_no`,`email`,`bod`,`address`,`photo`,`description`,`photo1`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");

        $stmt->execute([
           
            htmlspecialchars($_POST['branch'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['name'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['gender'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['marital'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['status'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['phone_no'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['email'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['bod'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['address'], ENT_QUOTES, 'UTF-8'),
            $img,
             htmlspecialchars($_POST['description'], ENT_QUOTES, 'UTF-8'),
            $img1,
            
          ]);

        $_SESSION['success'] = "success";

        header("location:../manage_soulwinning.php");
    }

    



// update member....

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


        // Update main photo
       $filepath = "../../assets/images/" . $_FILES["photo"]["name"];

if(move_uploaded_file($_FILES["photo"]["tmp_name"], $filepath)) 
{
//echo "<img src=".$filepath." height=200 width=300 />";
    $img=$_FILES["photo"]["name"];
} 
else 
{
echo "Error !!";
}
    
        // Update secondary photo
       if (!empty($_FILES['photo1']['tmp_name'])) {
            $file_extension = pathinfo($_FILES["photo1"]["name"], PATHINFO_EXTENSION);
            $new_filename = uniqid() . '.' . $file_extension;
            $filepath = "../assets/images/" . $new_filename;
            if (move_uploaded_file($_FILES["photo1"]["tmp_name"], $filepath)) {
                $img = $new_filename;
                @unlink("../assets/images/" . htmlspecialchars($_POST['old_photo_img'], ENT_QUOTES, 'UTF-8'));
            } else {
                // Handle failed upload
                // You might want to add error handling here
            }
        } else {
            // Keep the old photo if no new one is uploaded
            $img1 = htmlspecialchars($_POST['old_photo_img'], ENT_QUOTES, 'UTF-8');
        }
    
        // Prepare and execute the SQL update statement
        $stmt = $conn->prepare("UPDATE `soul_winning` SET `branch`=?,`name`=?,`gender`=?,`marital`=?,`status`=?, `phone_no`=?, `email`=?,`bod`=?,`address`=?,`photo`=?,`description`=?,`photo1`=? WHERE id=? ");
        $stmt->execute([
            htmlspecialchars($_POST['branch'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['name'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['gender'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['marital'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['status'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['phone_no'], ENT_QUOTES, 'UTF-8'),
           htmlspecialchars($_POST['email'], ENT_QUOTES, 'UTF-8'),
           htmlspecialchars($_POST['bod'], ENT_QUOTES, 'UTF-8'),
           htmlspecialchars($_POST['address'], ENT_QUOTES, 'UTF-8'),
          $img,
         htmlspecialchars($_POST['description'], ENT_QUOTES, 'UTF-8'),
         $img1,
         htmlspecialchars($_POST['id'], ENT_QUOTES, 'UTF-8')
        ]);
    
        $_SESSION['update'] = "update";
        header("location:../manage_soulwinning.php");
    }
    

    // delete member..

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

        $stmt = $conn->prepare("UPDATE `soul_winning` SET delete_status='1' where id=? ");

        $stmt->execute([htmlspecialchars($_POST['del_id'], ENT_QUOTES, 'UTF-8')]);

        $_SESSION['delete'] = "delete";

        header("location:../manage_soulwinning.php");
    }
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
