<?php
include("../../db.php");
error_reporting(E_ALL);
header("Access-Control-Allow-Origin: * ");

/*
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
*/

$abc = $_POST['json'];
$idata = json_decode($abc, true);
$session = $idata['session'];
$school_id = $idata['school_id'];
$added_by = $idata['added_by'];
$student_data = $idata['student_data'];

$type = $idata['type'];

$status = 0;

foreach ($student_data as $student) {

    $attendanceData = array(
        "idno" => $student['stu_id'],
        "session" => "$session",
        "school_id" => $school_id,
        "added_by" => $added_by,
    );

    if ($student['term1_att'] != "") {
        $attendanceData['present'] = $student['term1_att'];
    }

    if ($student['teacher_rem_term1'] != "") {
        $attendanceData['remark'] = $student['teacher_rem_term1'];
    }


    $any = $db->getVal("Select id from attendance_remark where idno='" . $student['stu_id'] . "' and type = '$type' and session='" . $session . "'");

    if ($any != '' || $any != null) {
        $attendance = $db->updateAry("attendance_remark", $attendanceData, "where idno='" . $student['stu_id'] . "' and type = '$type' and school_id='" . $school_id . "' and session='" . $session . "'");
        $status = 1;
    } else {
        $attendanceData['class'] = $student['class'];
        $attendanceData['section'] = $student['section'];

        $attendanceData['type'] = $type;

        $attendance1 = $db->insertAry("attendance_remark", $attendanceData);
        $status = 1;
    }

}

echo json_encode(array('status' => $status));