<?php

if (file_exists('../../' . $_REQUEST['tenant'] . '/config.php')) {
    require_once('../../' . $_REQUEST['tenant'] . '/config.php');
}
if (file_exists('/config.php')) {
    require_once('/config.php');
}
$clientNumber = $_REQUEST['clientnumber'];
$_REQUEST['itemset'] = explode(',', $_REQUEST['itemset']);
$obs = $_REQUEST['obs'];

if (strtolower($obs) == "none") {
    //echo "PASS LOADED".$obs;
    $stat = "PASS";
    $cx['result'] = "PASS";
    $obs = "None";
} else { //echo "OBS LOADED".$obs;
    $stat = "OBS";
    $cx['result'] = "OBS";
    $obs = $obs;
}

$cx['reptype'] = $_REQUEST['examType'];
$cx['adefect'] = "None";
$cx['bdefect'] = "None";

foreach ($_REQUEST['itemset'] as $_REQUEST['item']) {

    $stmt2 = $GLOBALS['con']->prepare("SELECT `clientnumber`,`code`,`linked`,`currdate` FROM `itemdetails` WHERE `itemnumber`=?") or die(mysqli_error($GLOBALS['con']));
    $stmt2->bind_param('i', $_REQUEST['item']);
    $stmt2->execute();
    $rc2 = $stmt2->get_result();
    $stmt2->close();
    $cx2 = mysqli_fetch_array($rc2);
    $clientnumber = $cx2['clientnumber'];
    $chain = explode(":", $cx2['linked']);

    $e = '';
    $formClass = '';
    $repnumber = strtotime('now');

    $fn = mysqli_query($GLOBALS['con'], "SELECT * FROM `dd_plant` WHERE `code`='" . $cx2['code'] . "' ");
    $code = mysqli_fetch_array($fn);
    $freq = $code['frequency'] . ' months';
    $freqNo = $code['frequency'];
    $a = "None";
    $b = "None";

    $startDate = $cx2['currdate'];
    $next = date('d-m-Y', strtotime('+' . $freq, strtotime($_REQUEST['date'])));

    $dt = strtotime($_REQUEST['date']);
    $nx = strtotime($next);
    $nw = strtotime('now');


    $stmt = $GLOBALS['con']->prepare("INSERT INTO `reportitems` VALUES(NULL,'" . $repnumber . "',NULL,?,?,?,?,0,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)") or die(mysqli_error($GLOBALS['con']));
    $stmt->bind_param(
        'iiiiissssssssssssssssssssssssssssssssssssssssssssssssssss',

        $startDate,
        $dt,
        $nx,
        $nw,
        $clientnumber,
        $_REQUEST['item'],

        $_REQUEST['surveyor'],
        $a,
        $b,
        $obs,
        $cx['pnareason'],

        $cx['partialReason'],
        $cx['ffcomments'],
        $cx['code'],
        $cx['viewed'],
        $cx['history'],

        $cx['changeScheme'],
        $cx['resultOfExam'],
        $cx['lastReportSeen'],
        $cx['examType'],
        $cx['partsCovered'],
        $cx['lease_or_hire'],

        $cx['partsInaccessible'],
        $cx['conditionInternal'],
        $cx['conditionExternal'],
        $cx['conditionFittings'],
        $cx['protectiveDevices'],

        $cx['9b1'],
        $cx['9b2'],
        $cx['9b3'],
        $cx['processConditions'],
        $cx['class'],

        $cx['ebond'],
        $cx['ires'],
        $cx['volts'],
        $cx['nature'],
        $cx['test_procedure'],

        $cx['process_conditions'],
        $cx['effective'],
        $cx['particulars'],
        $cx['other_comments'],
        $cx['usage_comments'],

        $cx['noise'],
        $cx['intended_performance'],
        $cx['achieving_perfomance'],
        $cx['repair_details'],
        $cx['rack'],

        $cx['rackRun'],
        $cx['beam'],
        $cx['accessories'],
        $cx['handling'],
        $cx['stability'],

        $cx['clutch'],
        $cx['safety'],
        $cx['frequency'],
        $_REQUEST['examType'],
        $stat
    );
    $stmt->execute();
    echo $stmt->error;
    $insertID = mysqli_insert_id($GLOBALS['con']);
    $stmt->close();

    $stmt = $GLOBALS['con']->prepare("UPDATE `itemdetails` SET `currdate`=?,`duedate`=?,`scheme_number_alt`=?,`history`=?, `lastReport`=? WHERE `itemnumber`=?") or die(mysqli_error($GLOBALS['con']));
    $stmt->bind_param('iisssi', $sd, $nx, $cx['scheme_number_alt'], $cx['history'], $cx['result'], $_REQUEST['item']);
    $stmt->execute();
    $stmt->close();

    sleep(1);

    //echo print_r($cx);

}
$reps = json_encode($_REQUEST['itemset']);
$now = strtotime('now');
$c = mysqli_query($GLOBALS['con'], "SELECT * FROM `rapid_history` WHERE `clientnumber`='" . $clientnumber . "'") or die($e = "CHAECK FAILURE: " . mysqli_error($GLOBALS['con']));
$n = mysqli_num_rows($c);
if ($n == 0) {
    $c = mysqli_query($GLOBALS['con'], "INSERT INTO `rapid_history` VALUES(NULL,'" . $clientnumber . "','" . $chain[0] . "','" . $reps . "','" . $now . "')") or die($e = "INSERT FAILURE: " . mysqli_error($GLOBALS['con']));
} else {
    $f = mysqli_fetch_array($c);
    $stmt = mysqli_query($GLOBALS['con'], "UPDATE `rapid_history` SET `chain`='" . $chain[0] . "', `reps`='" . $reps . "', `date`='" . $now . "' WHERE `id`='" . $f['id'] . "'") or die(mysqli_error($GLOBALS['con']));
}


//genSchedule($cx['clientnumber']);
//echo $_REQUEST['date']."-".$_REQUEST['examType']."-";
echo "success|" . $repnumber;