<?php

ini_set('display_errors', 1);
// error_reporting(0);

//require_once '../../resources/base_path_include.php';
require_once '../../../resources/base_path_include.php';
// should we not call header and ch
require_once $base_path_to_resources . "includes/config.php"; // use require for mandatory files
require_once $base_path_to_resources . 'lib/function.php';

require_once $base_path_to_resources . "lib/currency_functions.php";

//valdation file
require_once $base_path_to_resources . 'lib/validate_api_signature.php';
require_once $base_path_to_resources . 'lib/validate_function.php';
require_once $base_path . 'user_management/authenticate.php';

//creating dynamic db
require_once $base_path_to_resources . 'includes/specific_db_config.php';
$postdata = json_decode(file_get_contents("php://input"), true);

checkIfNoPhpContentsReceive();
validateApiSignature($postdata['api_signature']);
//authenticate jwt token
$jwttoken = $postdata['jwt_token'];
$jwt_validate = authenticate_jwt($jwttoken);
//then workspace_id is decoded in data of jwt use from there
$userid = $jwt_validate->data->id;
$workspace_id = $jwt_validate->data->workspace_id;
//print_r($workspace_id);die();
$role = $jwt_validate->data->role;

$username = $jwt_validate->data->username;

if ($postdata['function_type'] == "transaction_lock") {

  $imploded_uniqueids_string = "('" . implode("','", $postdata['unique_id']) . "')";
  //$unique_id = $postdata['unique_id'];
  $company_id = trim($postdata['company_id']);
  $branch_id = trim($postdata['branch_id']);
  $book_id = trim($postdata['book_id']);


 $confirmed_role_user = validatePrimaryAccess($conn, $workspace_id, $role, $userid, ['superadmin', 'admin', null]);
    if (!$confirmed_role_user){ 
        errorreturn('Access denied.');
    }else{
        $specific_db_conn = dynamicDbConnection($confirmed_role_user);
         
    }

  $res = $specific_db_conn->prepare("SELECT transactionLock FROM  act_transactiondata WHERE unique_id IN $imploded_uniqueids_string and workspace_id =? and company_id = ? and branch_id=? and book_id= ? and transactionLock=?");
  $res->execute(array($workspace_id, $company_id, $branch_id, $book_id, '0'));
  $result1 = $res->fetch();
  // $lockstatus = $result1['transaction_lock'];
  //echo $lock_status;die();

  if (count($result1) >= 1) {
    $lock_status = '1';
    $sql = "UPDATE  act_transactiondata SET transactionLock=?,lockedBy=? WHERE unique_id IN $imploded_uniqueids_string   and workspace_id= ? and company_id=? and branch_id=? and book_id= ?";
    $stmt = $specific_db_conn->prepare($sql);
    $stmt->execute(array($lock_status, $username, $workspace_id, $company_id, $branch_id, $book_id));
  }

  //   else if ($lockstatus == '0') {
  //     $lock_status = '1';
  //     $sql = "UPDATE  act_transactiondata SET transaction_lock=?,last_locked_by=?  WHERE unique_id IN $imploded_uniqueids_string   and workspace_id =? and company_id=? and branch_id=? and book_id= ?";
  //     $stmt = $specific_db_conn->prepare($sql);
  //     $stmt->execute(array($lock_status,$username, $workspace_id, $company_id,$branch_id,$book_id));
  //   }

  if ($stmt->rowCount() > 0) {
    // Update successful
    successreturn("Transaction lock status updated Successfully");
  } else {
    // Update failed
    errorreturn("Transaction lock status updated Successfully");
  }
}
