<?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'])){

        // Validate and sanitize input data
        $branch = $_POST['branch'];
        $user_name = $_POST['user_name'];
        $employee_name = $_POST['employee_name'];
        $payroll_date = $_POST['payroll_date'];
        $total_pay = $_POST['total_pay'];
        $total_deduction = $_POST['total_deduction'];
        $net_pay = $_POST['net_pay'];
        $payment_method = $_POST['payment_method'];
        $bank_name = $_POST['bank_name'];
        $acc_no = $_POST['acc_no'];
        $decription = $_POST['decription'];
        $paid_amount = $_POST['paid_amount'];
        $comment = $_POST['comment'];
        $recurring = $_POST['recurring'];

       // Loop through each extra time and time in late
        foreach($_POST['extra_time'] as $key => $extra_time) {
            // Check if Timein_late exists for the current index
            $timein_late = isset($_POST['Timein_late'][$key]) ? $_POST['Timein_late'][$key] : null;

            // Insert data into the database
            $stmt = $conn->prepare("INSERT INTO `payroll` (branch, user_name, extra_time, Timein_late, employee_name, payroll_date, total_pay, total_deduction, net_pay, payment_method, bank_name, acc_no, decription, paid_amount, comment, recurring) VALUES (:branch, :user_name, :extra_time, :Timein_late, :employee_name, :payroll_date, :total_pay, :total_deduction, :net_pay, :payment_method, :bank_name, :acc_no, :decription, :paid_amount, :comment, :recurring)");

            $stmt->bindParam(':branch', $branch);
            $stmt->bindParam(':user_name', $user_name);
            $stmt->bindParam(':extra_time', $extra_time);
            $stmt->bindParam(':Timein_late', $timein_late);
            $stmt->bindParam(':employee_name', $employee_name);
            $stmt->bindParam(':payroll_date', $payroll_date);
            $stmt->bindParam(':total_pay', $total_pay);
            $stmt->bindParam(':total_deduction', $total_deduction);
            $stmt->bindParam(':net_pay', $net_pay);
            $stmt->bindParam(':payment_method', $payment_method);
            $stmt->bindParam(':bank_name', $bank_name);
            $stmt->bindParam(':acc_no', $acc_no);
            $stmt->bindParam(':decription', $decription);
            $stmt->bindParam(':paid_amount', $paid_amount);
            $stmt->bindParam(':comment', $comment);
            $stmt->bindParam(':recurring', $recurring);

            $stmt->execute();
        }

        // Calculate and insert the total expense for the current branch
        $stmt = $conn->prepare("UPDATE payroll SET total_payroll = (SELECT SUM(total_pay) FROM payroll WHERE branch = :branch) WHERE branch = :branch");
        $stmt->bindParam(':branch', $branch);
        $stmt->execute();

        // Redirect to a success page
        header("Location: ../manage_payroll.php");
        exit();
    }

//update payroll

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


    
    $id = $_POST['id'];
    $branch = $_POST['branch'];
    $user_name = $_POST['user_name'];
    // $admins = $_POST['admin']; // Array of admin names
    $extra_times = $_POST['extra_time']; // Array of extra times
    $timein_lates = $_POST['Timein_late']; // Array of time in late
    $employee_name = $_POST['employee_name'];
    $payroll_date = $_POST['payroll_date'];
    $total_pay = $_POST['total_pay'];
    $total_deduction = $_POST['total_deduction'];
    $net_pay = $_POST['net_pay'];
    $payment_method = $_POST['payment_method'];
    $bank_name = $_POST['bank_name'];
    $acc_no = $_POST['acc_no'];
    $decription = $_POST['decription'];
    $paid_amount = $_POST['paid_amount'];
    $comment = $_POST['comment'];
    $recurring = $_POST['recurring'];
$recur_type = $_POST['recur_type'];
$recur_frequency = $_POST['recur_frequency'];
$startdate = $_POST['startdate'];
$enddate = $_POST['enddate'];



foreach($extra_times as $key => $extra_time) {
            $Timein_late = $timein_lates[$key];

  // Update payroll table
    $stmt = $conn->prepare("UPDATE `payroll` SET `branch`=:branch,`user_name`=:user_name,`employee_name`=:employee_name,`payroll_date`=:payroll_date,`extra_time`=:extra_time,`Timein_late`=:Timein_late,`total_pay`=:total_pay,`total_deduction`=:total_deduction,`net_pay`=:net_pay,`payment_method`=:payment_method,`bank_name`=:bank_name,`acc_no`=:acc_no,`decription`=:decription,`paid_amount`=:paid_amount,`comment`=:comment,`recurring`=:recurring,`recur_frequency`=:recur_frequency,`recur_type`=:recur_type,`startdate`=:startdate,`enddate`=:enddate  WHERE id = :id");
   

             $stmt->bindParam(':branch', $branch);
            $stmt->bindParam(':user_name', $user_name);
            // $stmt->bindParam(':admin', $admin); // Bind admin name
            $stmt->bindParam(':extra_time', $extra_time); // Bind extra time
            $stmt->bindParam(':Timein_late', $timein_late); // Bind time in late

            $stmt->bindParam(':employee_name', $employee_name);
            $stmt->bindParam(':payroll_date', $payroll_date);
            $stmt->bindParam(':total_pay', $total_pay);
            $stmt->bindParam(':total_deduction', $total_deduction);
            $stmt->bindParam(':net_pay', $net_pay);
            $stmt->bindParam(':payment_method', $payment_method);
            $stmt->bindParam(':bank_name', $bank_name);
            $stmt->bindParam(':acc_no', $acc_no);
            $stmt->bindParam(':decription', $decription);
            $stmt->bindParam(':paid_amount', $paid_amount);
            $stmt->bindParam(':comment', $comment);
            $stmt->bindParam(':recurring', $recurring);
$stmt->bindParam(':recur_frequency', $recur_frequency);
$stmt->bindParam(':recur_type', $recur_type);
$stmt->bindParam(':startdate', $startdate);
$stmt->bindParam(':enddate', $enddate);




             $stmt->bindParam(':id', $id);
    $stmt->execute();

    // // Update valuation table
    // $stmt2 = $conn->prepare("UPDATE `asset` SET valuation_date = :valuation_date, value_amount = :value_amount WHERE id = :id");
    // foreach ($valuation_dates as $index => $valuation_date) {
    //     $stmt2->bindParam(':valuation_date', $valuation_date);
    //     $stmt2->bindParam(':value_amount', $value_amounts[$index]);
    //     $stmt2->bindParam(':id', $id);
    //     $stmt2->execute();
    // }

}
    // Redirect to a success page
    header("Location: ../manage_payroll.php");
    exit();
}

//delete rollpay


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

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

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

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

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

 } catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
?>