<?php ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);

if (file_exists('../../../' . $_POST['tenant'] . '/config.php')) {
    require_once('../../../' . $_POST['tenant'] . '/config.php');
}

if (file_exists('/config.php')) {
    require_once('/config.php');
}

require_once('elec_distro_functions.php');

if (isset($_POST['edit_board'])) {
    // Retrieve and sanitize POSTed values as needed.
    $board_id                  = $_POST['board_id'];
    $clientnumber              = $_POST['clientnumber'];
    $locnumber                 = $_POST['siteList'];
    $board_name                = $_POST['board_name'];
    $reference                 = $_POST['reference'];
    $supply_from               = $_POST['supply_from'];
    $polarity_confirmed        = isset($_POST['polarity_confirmed']) ? 1 : 0;
    $phase_sequence_confirmed  = isset($_POST['phase_sequence_confirmed']) ? 1 : 0;
    $manufacturer              = $_POST['manufacturer'];
    $board_type                = $_POST['board_type'];
    $surge_protection_type = isset($_POST['surge_protection_type'])
    ? $_POST['surge_protection_type']
    : null;
$surge_protection_part_no = $_POST['surge_protection_part_no'] ?? '';
$surge_protection_functional = isset($_POST['surge_protection_functional']) ? 1 : 0;
$surge_protection_imax = $_POST['surge_protection_imax'] ?? 0;
    $bs_type                   = $_POST['bs_type'];
    $rating                    = $_POST['rating'];
    $poles                     = $_POST['poles'];
    $associated_rcd            = $_POST['associated_rcd'];
    $trip_current              = $_POST['trip_current'];
    $trip_time_1x              = $_POST['trip_time_1x'];
    $mccb_type                 = $_POST['mccb_type'];
    $zdb                       = $_POST['zdb'];
    $pfc_pscc                  = $_POST['pfc_pscc'];
    $voltage                   = $_POST['voltage'];
    $installation_date         = $_POST['installation_date'];
    $circuit_mode              = $_POST['circuit_mode'];

    $updated = update_distro(
        $board_id,
        $clientnumber,
        $locnumber,
        $board_name,
        $reference,
        $supply_from,
        $polarity_confirmed,
        $phase_sequence_confirmed,
        $manufacturer,
        $board_type,
        $surge_protection_type,
        $surge_protection_part_no,
        $surge_protection_functional,
        $surge_protection_imax,
        $bs_type,
        $rating,
        $poles,
        $associated_rcd,
        $trip_current,
        $trip_time_1x,
        $mccb_type,
        $zdb,
        $pfc_pscc,
        $voltage,
        $installation_date,
        $circuit_mode,

    );

    if ($updated) {
        echo json_encode(['success' => true, 'clientnumber' => $clientnumber]);
    } else {
        echo json_encode(['success' => false, 'message' => 'Failed to update distribution board.']);
    }
}

if (ob_get_level() > 0) {
    ob_end_flush();
}
?>