<?php
session_start();
require_once "../../../config.php";
require_once "../../../validation.php";
require_once "../../../Helper.php";

require_once '../../database1.php';
require_once "../../check_admin.php";

class bulkSaveStatus extends database
{
    public function updateStatuses($data, $status)
    {
        $this->dbh = $this->connectPDO1();
        $this->dbh->beginTransaction();
        try
        {
            foreach($data AS $each)
            {
            
                $res = $this->update('tb_checkout', ['cl_status' => $status], ['cl_id' => $each]);
                if(!$res)
                {
                    $this->dbh->commit();
                    return 0;
                    exit;
                }
            }
            
            $this->dbh->commit();
            return 1;
            exit;
            
        
        }
        catch (PDOException $e) {
                // roll back transaction
                $this->dbh->rollback();
                return 0;
        }
    }
}

$obj = new bulkSaveStatus();

if(!isset($_POST['data']) || empty($_POST['data']) || !isset($_POST['status']))
{
    http_response_code(400);
    echo json_encode(array('error_text' => 'No input found'));
    exit;
}

$data = $_POST['data'];
$status = $_POST['status'];

$res = $obj->updateStatuses($data, $status);
if(!$res)
{
    http_response_code(500);
    echo json_encode(array('error_text' => 'Something Went Wrong'));
    exit;
}

http_response_code(200);
    echo json_encode(array('message' => 'Status Updated'));
    exit;
    
