<?php
global $hooks;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

$hooks->add_action('silex_action','the_notice'); 
function notice_title(){
    echo "Notice";
}


// Define the CSS 
function notice_css() {
    ?>
	<link href="/assets/global/plugins/select2/css/select2.min.css" rel="stylesheet" type="text/css" />
	<link href="/assets/global/plugins/select2/css/select2-bootstrap.min.css" rel="stylesheet" type="text/css" />
	<link href="/assets/global/plugins/bootstrap-fileinput/bootstrap-fileinput.css" rel="stylesheet" type="text/css" />
	<link href="/assets/global/plugins/bootstrap-select/css/bootstrap-select.css" rel="stylesheet" type="text/css" />
	<link href="/assets/global/plugins/jquery-multi-select/css/multi-select.css" rel="stylesheet" type="text/css" />
	<?php
}

// Define the CSS 
function notice_js() {
    ?>	
	<script src="/assets/global/plugins/jquery-validation/js/jquery.validate.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/datatables/datatables.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/datatables/plugins/bootstrap/datatables.bootstrap.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/bootstrap-switch/js/bootstrap-switch.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/bootstrap-wizard/jquery.bootstrap.wizard.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/bootstrap-select/js/bootstrap-select.min.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/jquery-multi-select/js/jquery.multi-select.js" type="text/javascript"></script>
	<script src="/assets/global/plugins/select2/js/select2.full.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="/assets/modul-js/common/notice/notice.js"></script>
    <?php
}

function the_notice(){
	global $app;
	
	$app->get('/notice', function (Request $request) {
		global $hooks;
		
		$hooks->add_action('the_title',"notice_title");
		$hooks->add_action('global_css',"notice_css");
		$hooks->add_action('global_js',"notice_js");
		
		the_head();
		include 'notice.tpl.php';
				
		the_footer();
		
		return "";
	}); 
	$app->post('/notice/list', function(Request $request)  {
        $curpage = $request->get('page');
        include 'notice.list.php';
        return '';
    });
	$app->post('/notice/companies', function(Request $request)  {
        $curpage = $request->get('page');
        include 'companies.list.php';
        return '';
    });	
	$app->post('/notice/contacts', function(Request $request)  {
        $curpage = $request->get('page');
        include 'contact.list.php';
        return '';
    });
	$app->post('/notice/add-notice', function (Request $request) {
        global $db;
        $user_id = $_SESSION['user_id'];
		
        $company = $request->get('company');
		if($company == null){
			$company = array();
		}
		$contact = $request->get('contact');
		if($contact == null){
			$contact = array();
		}
        $message = $request->get('message');
		
		$db->bind("message", $message);
		$db->bind("chat_type", "1");
		$db->bind("from_id", $_SESSION['user_id']);
		
		$createMessage  = $db->query("INSERT INTO notice_messages(message,chat_type,from_id) VALUES (:message,:chat_type,:from_id)");
		$message_id = $db->lastInsertId();
		
		foreach ($company as $key => $value) {
			$db->bind("company_id", $value);
			$getCompany = $db->query("SELECT * FROM user_company WHERE company_id = :company_id and position = 1");
			foreach($getCompany as $key => $user_company){
				array_push($contact, $user_company['user_id']);
			}
		}
		
		foreach ($contact as $key => $value) {
			$db->bind("owner_id", $value);
			$checkRoom = $db->query("SELECT * FROM notice_rooms WHERE owner_id = :owner_id and room_type = 'general'");
			if(count($checkRoom) == 0){				
				$db->bind("owner_id", $value);
				$createRoom  = $db->query("INSERT INTO notice_rooms(chat_name,owner_id,room_type) VALUES ('General', :owner_id, 'general')");
				$room_id = $db->lastInsertId();
			}else{
				$room_id = $checkRoom[0]["room_id"];
			} 
			
			$db->bind("user_id", $_SESSION['user_id']);
			$db->bind("room_id", $room_id);
			$checkMembers = $db->query("SELECT * FROM notice_members WHERE user_id = :user_id and room_id = :room_id");
			if(count($checkMembers) == 0){	
				$db->bind("user_id", $_SESSION['user_id']);
				$db->bind("room_id", $room_id);
				$createMembers  = $db->query("INSERT INTO notice_members(room_id,user_id,status) VALUES (:room_id, :user_id, 1)");
			}
			
			$db->bind("user_id", $value);
			$db->bind("room_id", $room_id);
			$checkMembers = $db->query("SELECT * FROM notice_members WHERE user_id = :user_id and room_id = :room_id");
			if(count($checkMembers) == 0){	
				$db->bind("user_id", $value);
				$db->bind("room_id", $room_id);
				$createMembers  = $db->query("INSERT INTO notice_members(room_id,user_id,status) VALUES (:room_id, :user_id, 1)");
			}
			
			$db->bind("room_id", $room_id);
			$db->bind("message_id", $message_id);
			$createMembers  = $db->query("INSERT INTO notice_room_message(room_id,message_id) VALUES (:room_id, :message_id)");
			

			
			$db->bind("room_id", $room_id);
			$db->bind("message_id", $message_id);
			$db->bind("user_id", $value);
			$createUnread  = $db->query("INSERT INTO notice_unread(room_id,message_id, user_id) VALUES (:room_id, :message_id, :user_id)");
			
		}
		
               
		
		if ($createMessage) {
			return new Response("SUCCESS", 200);
		} else {
			return new Response('FAILED', 200);
		}
    });
}