<?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["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 !!";
        }
      // $campaign_id=$_SESSION['id'];

        $stmt = $conn->prepare("INSERT INTO `income`(`branch`,`incometype`,`amount`,`date`,`description`,`photo1`) VALUES (?,?,?,?,?,?)");

        $stmt->execute([
           
            htmlspecialchars($_POST['branch'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['incometype'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['amount'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['date'], ENT_QUOTES, 'UTF-8'),
            // htmlspecialchars($_POST['recurring'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['description'], ENT_QUOTES, 'UTF-8'),
            $img1,
           
          
        ]);


        // Calculate and insert the total expense for the current branch
        $branch = htmlspecialchars($_POST['branch'], ENT_QUOTES, 'UTF-8');
        $stmt = $conn->prepare("UPDATE income SET total_income = (SELECT SUM(amount) FROM expense WHERE branch = :branch) WHERE branch = :branch");
        $stmt->bindParam(':branch', $branch);
        $stmt->execute();

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

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

    
// update ....

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


    
        // 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 `income` SET `branch`=?,`incometype`=?,`amount`=?,`date`=?,`description`=?, `photo1`=? WHERE id=? ");
        $stmt->execute([
            htmlspecialchars($_POST['branch'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['incometype'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['amount'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['date'], ENT_QUOTES, 'UTF-8'),
            // htmlspecialchars($_POST['recurring'], ENT_QUOTES, 'UTF-8'),
            htmlspecialchars($_POST['description'], ENT_QUOTES, 'UTF-8'),
          
         $img1,
         
     htmlspecialchars($_POST['id'], ENT_QUOTES, 'UTF-8')
        ]);
    
        $_SESSION['update'] = "update";
        header("location:../manage_income.php");
    }
    

    // delete member..

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

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

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

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

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