<?php
include("../../db.php");
header('Content-Type: application/json');

$json = json_decode($_POST['json'], true);

$session = $json['session'];
$data = $json['data'];

$status = 0;
$msg = '';
$temp = 0;
foreach ($data as $dt) {

    $ary = array(
        "date" => "$dt[date]",
        "month" => "$dt[month]",
        "year" => "$dt[year]",
        "type" => "$dt[type]",
        "remark" => "$dt[remark]",
        "session" => "$session"
    );

    $checkdate = $db->getRow("select * from calendar where date='$dt[date]' ");

    if (count($checkdate) > 1) {
        $flgUp = $db->updateAry("calendar", $ary, "where id='$checkdate[id]' ");
        $temp = 1;
    } else {
        $flgIn = $db->insertAry("calendar", $ary);
        $temp = 1;
    }

    if ($temp > 0) {
        $status = 1;
        $msg = "Data inserted successfully.";
    } else {
        $msg = "Something went wrong. Please try again.";
    }

}

echo json_encode(array('status' => $status, 'msg' => "$msg"));