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


$abc = $_POST['json'];

$idata = json_decode($abc, true);


$session = $idata['session'];
$school_id = $idata['school_id'];
$notice_type = $idata['notice_type'];
$notice_detail = $idata['notice_detail'];
$class = $idata['class'];
$Added_by = $idata['Added_by'];
$image_name = $idata['image_name'];
$base = $idata['image'];
$date = date("Y-m-d");

if (!isset($myObj)) {
    $myObj = new stdClass();
}

// Decode Image
if ($base != null) {
    $binary = base64_decode($base);
} else {
    $binary = null;
}

header('Content-Type: bitmap; charset=utf-8');
$file_nm = '../../uploads/notice/' . $image_name;


// Images will be saved under 'meeting_image' folder
$file = fopen($file_nm, 'wb');

// Create File
if ($binary != null) {
    $x = fwrite($file, $binary);
}
fclose($file);


$lastContentId = $db->getVal("SELECT content_id FROM student_notice ORDER BY id DESC LIMIT 1");
$lastContentId++;


foreach ($class as $x => $x_value) {
    $aryData = array(

        "school_id" => $school_id,
        "session" => $session,
        "date" => $date,
        "notice_type" => $notice_type,
        "upload_notice" => $image_name,
        "notice_detail" => $notice_detail,
        //"class" => $x_value,
        "Added_by" => $Added_by,
        "status" => 1,
        "type" => 2,

        "content_id" => $lastContentId
    );

    $cls_name = $x_value['class_name'];
    $sec_list = $x_value['section_list'];

    $aryData['class'] = $cls_name;

    if (count($sec_list) == 0) {
        $insert_complain = $db->insertAry("student_notice", $aryData);
    } else {
        foreach ($sec_list as $sec_name){
            $aryData['section'] = $sec_name;
            $insert_complain = $db->insertAry("student_notice", $aryData);
        }
    }

}

if ($insert_complain > 0) {

    $msg = "Notice Added successfully.!";
}


if ($msg != "") {
    echo json_encode(array('status' => 1, 'msg' => 'Done', "data" => $msg));

} else {
    echo json_encode(array('status' => 0, 'msg' => 'notice not Added. Please try again later..!'));
}
