
<?php

session_start();

if (!isset($_SESSION["id"])) {
    echo json_encode(["ok" => false, "error_code" => 100]);
    exit();
}

if (!isset($_POST["id"])) {
    echo json_encode(["ok" => false, "error_code" => 101]);
    exit();
}
include("../../autoload.php");
$users_function = new \Functions\Users();
$admins_function = new \Functions\Admins();
$logs_function = new \Functions\Logs();
$persian_date = new \Classes\PersianDate();


$admin_info = $admins_function->get_by_id($_SESSION["id"])[0];

$root = "api/show_bank_logs.php";
if (in_array($root, json_decode($admin_info->permissions))) {
    $logs = $logs_function->get_by_type(["add_bank","update_bank"], $_POST["id"]);
    $output = "";
    if(is_array($logs)){
        
        foreach($logs as $log){
            $output .= '<div class="card faq-box"
              style="color : #ffffff78 !importnat;">
                            <div class="card-body">
                                <div class="faq-icon">
                                    <i 
                                    class="bg-transparent  dripicons-pencil h1 icon-one"
                                  
                                    ></i>
                                </div>';
            $output .= '<p class="font-14 text-center">'.$log->description.'</p>'; 
            
            if($log->type == "update_bank"){
                $old_logs = json_decode($log->befor,true);
                $new_logs = json_decode($log->after,true);
                $i = 0;
                foreach($new_logs as $key_log => $value_log){ 
                    if($old_logs["$key_log"] !== $value_log){
                        
                        $change_fa = "";
                        $old_data = $old_logs["$key_log"];
                        $new_data = $value_log;
                        switch($key_log){
                            case "name":
                                $change_fa = "نام بانک";
                                break;
                            case "first_balance":
                                $change_fa = "موجودی اولیه";
                                break;
                            case "balance":
                                $change_fa = "موجودی";
                            
                                break;
                                
                                default:
                                    continue;
                        }
                        $output .= '<p class="font-10"><i class="dripicons-minus"></i>&nbsp &nbsp'.$change_fa ." از $old_data به $new_data تغییر کرد</p>";
                        
                }
            }
            }
            $output .= '<p class="font-12"><i class="fas fa-user"></i>&nbsp &nbspتوسط '.$admins_function->get_by_id($log->submitter)[0]->name.'</p>';
            $output .= '<p class="font-12"><i class="fas fa-clock"></i>&nbsp &nbspزمان '. $persian_date->jdate("Y/m/d H:i",$log->time).'</p>';
            $output .= '</div></div>';
        }
        
            echo json_encode(["ok" => true, "output" => $output]);
    exit();
    }else {
    echo json_encode(["ok" => false, "error_code" => 102]);
    exit();
}
} else {
    echo json_encode(["ok" => false, "error_code" => 103]);
    exit();
}
?>