<?php
include("../../db.php");
header("Access-Control-Allow-Origin: * ");
header('Content-Type: application/json');

date_default_timezone_set("Asia/Kolkata");

// $class = $_REQUEST['class'];
$section = trim($_REQUEST['section']);
$current_date = date('d-m-Y');

//$sql = $db->getRows("select * from liveclass_meet where date >= '$current_date' order by date DESC");

$sql = $db->getRows("select * from liveclass_meet ORDER BY id DESC");

// $result = mysql_query($sql);
// while ($row = mysql_fetch_assoc($result))
foreach ($sql as $row) {

    $d = $row["date"];
    $t = $row["time"];

    $endtime = $row["endtime"];


    if (strtotime($current_date) <= strtotime($d)) {

    }else{
        continue;
    }


    $livetime = strtotime("$d $t");
    $curtime = time();
    $difference = $curtime - $livetime;

    if ($difference >= -120 && $difference <= 2400) {
        $flag = 1;
    } else {
        $flag = 0;
    }


    ///////
    ///
    ///


    $starttime = strtotime("$d $t");
    $finishtime = strtotime("$d $endtime");

    $curtime  = time();

    $livetime = $starttime - $curtime;
    $classend = $finishtime - $curtime;

    if($livetime < 0 && $classend > 0){
        $flag = 1;
    } else {
        $flag = 0;
    }

    ///
    ///



    $meet_account = $db->getRow("select * from meet_account where teacher_id='$row[teacher_id]' ");

    $sectionAry = $row['section'];
    if ($section != "") {
        $sectionAry = json_decode($row['section'], true);

        if ($sectionAry != "") {
            if (in_array("$section", $sectionAry)) {
                $tname = $row['teacher_id'];
                $techer = $db->getVal("select name from staff where idno='$tname'");

                $jsonn[] = array("id" => $row['id'], "teacher_id" => $row['teacher_id'], "teacher_name" => $techer, "class" => $row['class'], "section" => $row['section'],
                    "date" => $row['date'], "time" => $row['time'], "chapter" => $row['chapter'], "topic" => $row['topic'], "url" => $row['url'],
                    "status" => $row['status'], "zoom_meeting_id" => $row['zoom_meeting_id'], "flag" => $flag, 'sdk_key' => $meet_account['sdk_key'],
                    'sdk_secret' => $meet_account['sdk_secret'],'duration'=>$row['duration'], 'endtime'=>$row['endtime']);

                $json['status'] = 1;
                $json['msg'] = "successful";
                $json['data'] = $jsonn;

            }
        } else {
            $tname = $row['teacher_id'];
            $techer = $db->getVal("select name from staff where idno='$tname'");

            $jsonn[] = array("id" => $row['id'], "teacher_id" => $row['teacher_id'], "teacher_name" => $techer, "class" => $row['class'], "section" => $row['section'],
                "date" => $row['date'], "time" => $row['time'], "chapter" => $row['chapter'], "topic" => $row['topic'],
                "url" => $row['url'], "status" => $row['status'], "zoom_meeting_id" => $row['zoom_meeting_id'], "flag" => $flag,
                'sdk_key' => $meet_account['sdk_key'], 'sdk_secret' => $meet_account['sdk_secret'],'duration'=>$row['duration'], 'endtime'=>$row['endtime']);

            $json['status'] = 1;
            $json['msg'] = "successful";
            $json['data'] = $jsonn;
        }

    } else {

        $tname = $row['teacher_id'];
        $techer = $db->getVal("select name from staff where idno='$tname'");

        $jsonn[] = array("id" => $row['id'], "teacher_id" => $row['teacher_id'], "teacher_name" => $techer, "class" => $row['class'], "section" => $row['section'],
            "date" => $row['date'], "time" => $row['time'], "chapter" => $row['chapter'], "topic" => $row['topic'],
            "url" => $row['url'], "status" => $row['status'], "zoom_meeting_id" => $row['zoom_meeting_id'],
            "flag" => $flag, 'sdk_key' => $meet_account['sdk_key'], 'sdk_secret' => $meet_account['sdk_secret'],'duration'=>$row['duration'], 'endtime'=>$row['endtime']);

        $json['status'] = 1;
        $json['msg'] = "successful";
        $json['data'] = $jsonn;
    }

}


if (is_array($jsonn) && count($jsonn) > 0) {
    echo json_encode($json);
} else {
    echo json_encode(array('status' => 0, 'msg' => 'data not found..!'));
}



