<?php
global $hooks;

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

$hooks->add_action('silex_action','the_contacts'); 
function contacts_title(){
    echo "Contact List";
}

function contacts_css() {
    ?>
	<link href="/assets/global/plugins/fancybox/source/jquery.fancybox.css" rel="stylesheet" type="text/css" />
	<link href="/assets/modul-css/profile/profile.min.css" rel="stylesheet" type="text/css">	
	<link href="/assets/global/plugins/typeahead/typeahead.css" rel="stylesheet" type="text/css" />
    <?php
}

function contacts_js() {
    ?>	
	<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/modul-js/common/contact/contact.min.js"></script>
    <?php
}

function companies_css() {
    ?>
	<link href="/assets/global/plugins/fancybox/source/jquery.fancybox.css" rel="stylesheet" type="text/css" />
	<link href="/assets/modul-css/profile/profile.min.css" rel="stylesheet" type="text/css">	
	<link href="/assets/global/plugins/typeahead/typeahead.css" rel="stylesheet" type="text/css" />
    <?php
}

function companies_js() {
    ?>	
	<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/modul-js/common/contact/company.min.js"></script>
    <?php
}



function the_contacts(){
	global $app;
	
	
	$app->post('/contacts/add-contact', function (Request $request) {
        global $db;
        $friend_id = $request->get('id');
        $user_id = $_SESSION['user_id'];
		
		$db->bind("friend_id", $friend_id);
		$db->bind("user_id", $user_id);
        // Bind the data
		$userReg = $db->query("INSERT INTO user_friend(friend_id,user_id) VALUES (:friend_id,:user_id)");
		
		$db->bind("user_id", $friend_id);
		$db->bind("friend_id", $user_id);
        // Bind the data
		$userReg = $db->query("INSERT INTO user_friend(friend_id,user_id) VALUES (:friend_id,:user_id)");
		              
		if ($userReg) {
			return new Response("SUCCESS", 200);
		} else {
			return new Response('FAILED', 200);
		}
    });
	$app->post('/contacts/get-detail', function (Request $request) {
        global $db;
        $id = $request->get('id');
        // Bind the data
        $html = getModalContact($id);
		return $html;
    });
	$app->post('/companies/addmarking', function (Request $request) {
		global $db;
        $friend_id = $request->get('friend_id');
        $company_id = $request->get('company_id');
		$marking_no = $request->get('marking_no');
		
		$db->bind("friend_id", $friend_id);
		$db->bind("company_id", $company_id);
		$db->bind("marking_no", json_encode($marking_no));
        // Bind the data
		$marking_no_upd = $db->query("UPDATE company_friend SET status = 1, marking_no = :marking_no WHERE company_id = :company_id and friend_id = :friend_id");
		
		$db->bind("friend_id", $company_id);
		$db->bind("company_id", $friend_id);
		$db->bind("marking_no", json_encode($marking_no));
        // Bind the data
		$marking_no_upd2 = $db->query("UPDATE company_friend SET status = 1, marking_no = :marking_no WHERE company_id = :company_id and friend_id = :friend_id");
		
		if ($marking_no_upd && $marking_no_upd2) {
			return new Response("SUCCESS", 200);
		} else {
			return new Response('FAILED', 200);
		}
		
		
    });
	$app->post('/companies/edit-approve-user', function (Request $request) {
        global $db;
        $id = $request->get('id');
        // Bind the data
        $html = getModalEditApproveUser($id);
		return $html;
    });
	$app->post('/companies/approve-user', function (Request $request) {
        global $db;
        $id = $request->get('id');
        // Bind the data
        $html = getModalApproveUser($id);
		return $html;
    });
	$app->post('/contacts/list', function(Request $request)  {
        $curpage = $request->get('page');
        include 'contact.list.php';
        return '';
    });
	$app->get('/contacts/user-list', function(Request $request)  {
        include 'user.list.php';
        return '';
    });
	$app->get('/contacts', function() { 
		global $hooks;
		
		$hooks->add_action('the_title',"contacts_title");
        $hooks->add_action('global_css', "contacts_css");		
        $hooks->add_action('global_js', "contacts_js");
		
		the_head();
		include 'contact.tpl.php';
		the_footer();
		typeAhead();
		return "";
	}); 
	
	$app->post('/companies/add-company', function (Request $request) {
        global $db;
        $friend_id = $request->get('id');
        $company_id = getCompany($_SESSION['user_id']);
		
		$db->bind("friend_id", $friend_id);
		$db->bind("company_id", $company_id);
        // Bind the data
		$userReg = $db->query("INSERT INTO company_friend(friend_id,company_id) VALUES (:friend_id,:company_id)");
		
		$db->bind("friend_id", $friend_id);
		$db->bind("company_id", $company_id);
        // Bind the data
		$userReg = $db->query("INSERT INTO company_friend(friend_id,company_id) VALUES (:company_id,:friend_id)");
		              
		if ($userReg) {
			return new Response("SUCCESS", 200);
		} else {
			return new Response('FAILED', 200);
		}
    });
	$app->post('/companies/get-detail', function (Request $request) {
        global $db;
        $id = $request->get('id');
        // Bind the data
        $html = getModalCompanies($id);
		return $html;
    });
	$app->post('/companies/list', function(Request $request)  {
        $curpage = $request->get('page');
        include 'companies.list.php';
        return '';
    });
	$app->get('/companies/company-list', function(Request $request)  {
        include 'company.list.php';
        return '';
    });
	$app->get('/companies', function() { 
		global $hooks;
		
		$hooks->add_action('the_title',"contacts_title");
        $hooks->add_action('global_css', "companies_css");		
        $hooks->add_action('global_js', "companies_js");
		
		the_head();
		include 'companies.tpl.php';
		the_footer();
		typeAhead();
		return "";
	}); 
}