<?php
function theGroup() {
    global $db;
    $groups = $db->query("SELECT * FROM groups WHERE status = 1");
    $group  = "";
    foreach ($groups as $key => $value) {
        $group .= "<option value='" . $value['group_id'] . "'>" . $value['name'] . '</option>';
    }
    return $group;
}
function delete_directory($dirname) {
         if (is_dir($dirname))
           $dir_handle = opendir($dirname);
	 if (!$dir_handle)
	      return false;
	 while($file = readdir($dir_handle)) {
	       if ($file != "." && $file != "..") {
	            if (!is_dir($dirname."/".$file))
	                 unlink($dirname."/".$file);
	            else
	                 delete_directory($dirname.'/'.$file);
	       }
	 }
	 closedir($dir_handle);
	 rmdir($dirname);
	 return true;
}

function getStatusFriendCompany($company_id){
    global $db;
    $db->bind('company_id', $company_id);
    $db->bind('friend_id', $_SESSION['company_id']);
    $groups = $db->query("SELECT * FROM company_friend WHERE company_id = :company_id and  friend_id = :friend_id and status = 0");
	
    $db->bind('company_id', $company_id);
    $db->bind('friend_id', $_SESSION['company_id']);
    $groups2 = $db->query("SELECT * FROM company_friend WHERE company_id = :friend_id and  friend_id = :company_id  and status = 0");
	
	if(count($groups) > 0 && count($groups2) > 0){
		return true;
	}else{
		return false;
	}
}
function groupName($group_id) {
    global $db;
    $groups = $db->query("SELECT * FROM groups WHERE group_id = :group_id");
    return $groups[0]['name'];
}
function groupExist($group_id) {
    global $db;
    $db->bind('group_id', $group_id);
    $groups = $db->query("SELECT * FROM groups WHERE group_id = :group_id");
    return (count($groups) > 0 ? true : false);
}
function getProfileData($user_id, $data) {
    global $db;
    $db->bind("user_id", $user_id);
    $profile = $db->query("SELECT * FROM user_detail WHERE user_id=:user_id");
    return (count($profile) > 0 ? $profile[0][$data] : "");
}
function get_data($user_id, $kolom) {
    global $db;
    $data = "";
    $db->bind("user_id", $user_id);
    $data = $db->query("SELECT * FROM users WHERE user_id = :user_id;");
    if ($data) {
        return $data[0][$kolom];
    } else {
        return "0";
    }
}
function get_branch($branch_id, $kolom) {
    global $db;
    $data = "";
    $db->bind("branch_id", $branch_id);
    $data = $db->query("SELECT * FROM branches WHERE branch_id = :branch_id;");
    if ($data) {
        return $data[0][$kolom];
    } else {
        return "0";
    }
}
function getGroup($id, $kol) {
    global $db;
    $data = "";
    $db->bind("idprod", $id);
    $data = $db->query("SELECT * FROM groups WHERE groups_id = :idprod;");
    return $data[0][$kol];
}
function userExist($username) {
    global $db;
    $db->bind("username", $username);
    $row = $db->query("SELECT * FROM users WHERE username = :username");
    return (count($row) > 0 ? true : false);
}
function userIDExist($user_id) {
    global $db;
    $db->bind("user_id", $user_id);
    $row = $db->query("SELECT * FROM users WHERE user_id = :user_id");
    return (count($row) > 0 ? true : false);
}
function getCompany($user_id) {
    global $db;
    $db->bind('user_id', $user_id);
    $result = $db->query("SELECT * FROM user_company WHERE user_id = :user_id");
    return (count($result) > 0 ? $result[0]["company_id"] : 0);
}
function getBranch($user_id) {
    global $db;
    $db->bind('user_id', $user_id);
    $result = $db->query("SELECT * FROM user_company WHERE user_id = :user_id");
    return (count($result) > 0 ? $result[0]["branch_id"] : 0);
}
function getBranchData($branch_id, $data) {
    global $db;
    $db->bind('branch_id', $branch_id);
    $result = $db->query("SELECT * FROM branches WHERE branch_id = :branch_id");
    return (count($result) > 0 ? $result[0][$data] : "");
}
function getAllBranches($company_id) {
    global $db;
    $db->bind('company_id', $company_id);
    $result = $db->query("SELECT * FROM branches WHERE company_id = :company_id");
    return $result;
}
function getCompanyGroup($company_id) {
    global $db;
    $db->bind('company_id', $company_id);
    $result = $db->query("SELECT * FROM user_company WHERE company_id = :company_id and position = 1");
    return get_data($result[0]["user_id"], "group_id");
}
function getCompanyPosition($user_id) {
    global $db;
    $db->bind('user_id', $user_id);
    $result = $db->query("SELECT * FROM user_company WHERE user_id = :user_id");
    return (count($result) > 0 ? $result[0]["position"] : 0);
}
function getCompanyData($id, $type, $data) {
    global $db;
    if ($type == "user_id") {
        $company_id = getCompany($id);
        $db->bind("company_id", $company_id);
        $company = $db->query("SELECT * FROM companies WHERE company_id=:company_id");
    } else {
        $company_id = $id;
        $db->bind("company_id", $company_id);
        $company = $db->query("SELECT * FROM companies WHERE company_id=:company_id");
    }
    return (count($company) > 0 ? $company[0][$data] : "");
}
function showCompanyImage($id, $type) {
    global $db;
    if ($type == "user_id") {
        $company_id = getCompany($id);
        if ($company_id == 0) {
            return "http://www.placehold.it/200/EFEFEF/AAAAAA&amp;text=no+image";
        } else {
            $db->bind("company_id", $company_id);
            $profile = $db->query("SELECT * FROM companies WHERE company_id=:company_id");
            if (count($profile) > 0 && ($profile[0]['logo'] <> null || $profile[0]['logo'] <> "")) {
                return $profile[0]['logo'];
            } else {
                return "http://www.placehold.it/200/EFEFEF/AAAAAA&amp;text=no+image";
            }
        }
    } else {
        $company_id = $id;
        if ($company_id == 0) {
            return "http://www.placehold.it/200/EFEFEF/AAAAAA&amp;text=no+image";
        } else {
            $db->bind("company_id", $company_id);
            $profile = $db->query("SELECT * FROM companies WHERE company_id=:company_id");
            if (count($profile) > 0 && ($profile[0]['logo'] <> null || $profile[0]['logo'] <> "")) {
                return $profile[0]['logo'];
            } else {
                return "http://www.placehold.it/200/EFEFEF/AAAAAA&amp;text=no+image";
            }
        }
    }
}
function showCompanyRegCertImage($id, $type) {
    global $db;
    if ($type == "user_id") {
        $company_id = getCompany($id);
        if ($company_id == 0) {
            return "http://www.placehold.it/200/EFEFEF/AAAAAA&amp;text=no+image";
        } else {
            $db->bind("company_id", $company_id);
            $profile = $db->query("SELECT * FROM companies WHERE company_id=:company_id");
            if (count($profile) > 0 && ($profile[0]['reg_cert'] <> null || $profile[0]['reg_cert'] <> "")) {
                return $profile[0]['reg_cert'];
            } else {
                return "http://www.placehold.it/200/EFEFEF/AAAAAA&amp;text=no+image";
            }
        }
    } else {
        $company_id = $id;
        if ($company_id == 0) {
            return "http://www.placehold.it/200/EFEFEF/AAAAAA&amp;text=no+image";
        } else {
            $db->bind("company_id", $company_id);
            $profile = $db->query("SELECT * FROM companies WHERE company_id=:company_id");
            if (count($profile) > 0 && ($profile[0]['reg_cert'] <> null || $profile[0]['reg_cert'] <> "")) {
                return $profile[0]['reg_cert'];
            } else {
                return "http://www.placehold.it/200/EFEFEF/AAAAAA&amp;text=no+image";
            }
        }
    }
}
function checkFriend($user_id, $friend_id) {
    global $db;
    $db->bind('user_id', $user_id);
    $db->bind('friend_id', $friend_id);
    $result = $db->query("SELECT * FROM user_friend WHERE user_id = :user_id and friend_id = :friend_id");
    return (count($result) > 0 ? 1 : 0);
}
function getCompanyFriend($company_id, $friend_id) {
    global $db;
    $db->bind('company_id', $company_id);
    $db->bind('friend_id', $friend_id);
    $result = $db->query("SELECT * FROM company_friend WHERE company_id = :company_id and friend_id = :friend_id");
    return (count($result) > 0 ? 1 : 0);
}
function getModalApproveUser($company_id){
	global $db;
    $db->bind("company_id", $company_id);
    $result = $db->query("SELECT * FROM branches WHERE company_id = :company_id");
		$html = 
			"<input type='hidden' id='company_id' name='company_id' value='".$company_id."' />
			<input type='hidden' id='friend_id' name='friend_id' value='".$_SESSION['company_id']."' />";
	foreach($result as $key => $value){
		$html .= "
			<div class='form-group'>
				<label class='control-label col-md-3'>" . $value['branch_name'] . "</label>
				<div class='col-md-9'>
					<input class='form-control' name='marking_no[".$value['branch_id']."]' placeholder='Marking Number' value='".getMarkingNo($company_id, $_SESSION['company_id'], $value['branch_id'])."'>
					<p>".$value['branch_address'].",</p>
					<p>".$value['branch_zip']." ".$value['branch_city'].",</p>
					<p>".$value['branch_state'].", ".$value['branch_country']."</p>
				</div>
			</div>
			";
	}
	return $html;
	
	
	
}
function getModalEditApproveUser($company_id){
	global $db;
    $db->bind("company_id", $company_id);
    $result = $db->query("SELECT * FROM branches WHERE company_id = :company_id");
		$html = 
			"<input type='hidden' id='company_id' name='company_id' value='".$company_id."' />
			<input type='hidden' id='friend_id' name='friend_id' value='".$_SESSION['company_id']."' />";
	foreach($result as $key => $value){
		$html .= "
			<div class='form-group'>
				<label class='control-label col-md-3'>" . $value['branch_name'] . "</label>
				<div class='col-md-9'>
					<input class='form-control' name='marking_no[".$value['branch_id']."]' placeholder='Marking Number' value='".getMarkingNo($company_id, $_SESSION['company_id'], $value['branch_id'])."'>
					<p>".$value['branch_address'].",</p>
					<p>".$value['branch_zip']." ".$value['branch_city'].",</p>
					<p>".$value['branch_state'].", ".$value['branch_country']."</p>
				</div>
			</div>
			";
	}
	return $html;
	
	
	
}
function getModalContact($user_id) {
    global $db;
    $db->bind("user_id", $user_id);
    $data = $db->query("SELECT * FROM user_detail join users on user_detail.user_id = users.user_id join groups on users.group_id = groups.group_id
                                 WHERE user_detail.user_id = :user_id");
    $html = "";
    foreach ($data as $key => $value) {
        $salt         = "picard59fJepLkm6Gu5dDV";
        $encrypted_id = base64_encode($salt . $value["user_id"] . $salt);
        $html         = "
    <div class='portlet light'>
        <!-- SIDEBAR USERPIC -->
        <div class='profile-userpic'>
            <img src='" . showImage($value["user_id"]) . "' class='img-responsive' alt=''> </div>
        <!-- END SIDEBAR USERPIC -->
        <!-- SIDEBAR USER TITLE -->
        <div class='profile-usertitle'>
            <div class='profile-usertitle-name'> " . ucwords($value["first_name"]) . " </div>
            <div class='profile-usertitle-job'> " . ucwords(strtolower($value["name"])) . " </div>
        </div>
        <!-- END SIDEBAR USER TITLE -->
        <!-- SIDEBAR BUTTONS -->
        <div class='profile-userbuttons'>
            " . (checkFriend($_SESSION['user_id'], $value["user_id"]) > 0 ? "" : "<button type='button' class='btn btn-circle green btn-sm add' id='add' data-id='" . $value["user_id"] . "'>Add Friend</button>") . "
            <a href='/user-profile/" . $encrypted_id . "' class='btn btn-circle btn-primary btn-sm'>VIEW PROFILE</a>
			
            <!--<button type='button' class='btn btn-circle red btn-sm'>Share</button>-->
        </div>
        <!-- END SIDEBAR BUTTONS -->
        <!-- SIDEBAR MENU -->
        <div class='profile-usermenu'>
            
        </div>
        <table style='width:100%'>
            <tbody>
            <tr>
				<td><label class='control-label'>Phone No </label></td>
				<td><label class='control-label'>: " . $value["username"] . "</label></td>
			</tr>
			
            <tr>
				<td><label class='control-label'>Email</label></td>
                <td><label class='control-label'>: " . $value["email"] . "</label></td>             
            </tr>
			
            <tr>
				<td><label class='control-label'>Occupation</label></td>
				<td><label class='control-label'>: " . ucwords($value["name"]) . " </label></td>              
            </tr>
			
            <tr>
				<td><label class='control-label'>Company Name</label></td>
				<td><label class='control-label'>: " . ($_SESSION['company_id'] <> 0 ? getCompanyData($value["user_id"], "user_id", "name") : "N/A") . " </label></td>              
            </tr>
                
            </tbody></table>
        <!-- END MENU -->
    </div>";
    }
    return $html;
}
function getModalCompanies($company_id) {
    global $db;
    $db->bind("company_id", $company_id);
    $data = $db->query("SELECT * FROM companies
                                 WHERE company_id = :company_id");
    $html = "";
    foreach ($data as $key => $value) {
        $salt         = "picard59fJepLkm6Gu5dDV";
        $encrypted_id = base64_encode($salt . $value["company_id"] . $salt);
        $html         = "
    <div class='portlet light'>
        <!-- SIDEBAR USERPIC -->
        <div class='profile-userpic'>
            <img src='" . showCompanyImage($value["company_id"], "company_id") . "' class='img-responsive' style='width:20%;height:auto;' alt=''> </div>
        <!-- END SIDEBAR USERPIC -->
        <!-- SIDEBAR USER TITLE -->
        <div class='profile-usertitle'>
            <div class='profile-usertitle-name'> " . $value["name"] . " </div>
        </div>
        <!-- END SIDEBAR USER TITLE -->
        <!-- SIDEBAR BUTTONS -->
        <div class='profile-userbuttons'>
            " . (getCompanyFriend(getCompany($_SESSION['user_id']), $value["company_id"]) > 0 ? (getStatusFriendCompany($value["company_id"]) == true ? "<button type='button' class='btn btn-circle green btn-sm' disabled> Forwarder Request Sent</button>" : "") : "<button type='button' class='btn btn-circle green btn-sm add' id='add' data-id='" . $value["company_id"] . "'>Add Forwarder  <i class='fa fa-user-plus'></i></button>") . "
            <a href='/company-profile/" . $encrypted_id . "' class='btn btn-circle btn-primary btn-sm'>VIEW PROFILE</a>
            <button type='button' class='btn btn-circle red btn-sm'>Share</button>            
        </div>
        <!-- END SIDEBAR BUTTONS -->
        <!-- SIDEBAR MENU -->
        <div class='form form-horizontal'>
            <div class='form-group '>
                <label for='name' class='control-label col-md-4'>Office No</label>
                <div class='col-md-8'>
                    <p class='form-control-static'>" . $value["company_no"] . "</p>
                </div>
            </div>
            <div class='form-group '>
                <label for='name' class='control-label col-md-4'>Contact Person</label>
                <div class='col-md-8'>
                    <p class='form-control-static'>" . $value["contact_person"] . "</p>
                </div>
            </div>
            <div class='form-group '>
                <label for='name' class='control-label col-md-4'>Contact Person No</label>
                <div class='col-md-8'>
                    <p class='form-control-static'>" . $value["contact_person_no"] . "</p>
                </div>
            </div>
        </div>
        
    </div>";
    }
    return $html;
}
function getModalWarehouse($warehouse_id) {
    global $db;
    $db->bind("warehouse_id", $warehouse_id);
    $data = $db->query("SELECT * FROM warehouses
                                 WHERE warehouse_id = :warehouse_id");
    $html = "";
    foreach ($data as $key => $value) {
        $html = "
        <input type='hidden' id='id_edit' name='id_edit' value='" . $value['warehouse_id'] . "' />
        <div class='form-group '>
            <label for='warehouse_name_edit' class='control-label col-md-3'>Warehouse Name</label>
            <div class='col-md-9'>
                <input class='form-control ' name='name_edit' id='name_edit' type='text' value='" . $value['name'] . "'/>
            </div>
        </div>
        <div class='form-group '>
            <label for='warehouse_address_edit' class='control-label col-md-3'>Warehouse Address</label>
            <div class='col-md-9'>
                <input class='form-control ' name='address_edit' id='address_edit' type='text' value='" . $value['address'] . "'/>
            </div>
        </div>
        <div class='form-group '>
            <label for='warehouse_city_edit' class='control-label col-md-3'>Warehouse City</label>
            <div class='col-md-9'>
                <input class='form-control ' name='city_edit' id='city_edit' type='text' value='" . $value['city'] . "'/>
            </div>
        </div>
        <div class='form-group '>
            <label for='warehouse_postcode_edit' class='control-label col-md-3'>Warehouse Postcode</label>
            <div class='col-md-9'>
                <input class='form-control ' name='zip_edit' id='zip_edit' type='text' value='" . $value['zip'] . "'/>
            </div>
        </div>
        <div class='form-group '>
            <label for='warehouse_state_edit' class='control-label col-md-3'>Warehouse State</label>
            <div class='col-md-9'>
                <input class='form-control ' name='state_edit' id='state_edit' type='text' value='" . $value['state'] . "'/>
            </div>
        </div>
        <div class='form-group '>
            <label for='warehouse_country_edit' class='control-label col-md-3'>Warehouse Country</label>
            <div class='col-md-9'>
                <input class='form-control ' name='country_edit' id='country_edit' type='text' value='" . $value['country'] . "'/>
            </div>
        </div>
        <div class='form-group '>
            <label for='contact_person' class='control-label col-md-3'>Contact Person</label>
            <div class='col-md-9'>
                <input class='form-control ' name='contact_person' id='contact_person_edit' type='text' value='" . $value['contact_person'] . "'/>
            </div>
        </div>    
        <div class='form-group '>
            <label for='contact_person_no' class='control-label col-md-3'>Contact Person No</label>
            <div class='col-md-9'>
                <input class='form-control ' name='contact_person_no' id='contact_person_no_edit' type='text' value='" . $value['contact_person_no'] . "'/>
            </div>
        </div>
        <div class='form-group '>
            <label for='warehouse_notice_edit' class='control-label col-md-3'>Warehouse Notice</label>
            <div class='col-md-9'>
                <input class='form-control ' name='notice_edit' id='notice_edit' type='text' value='" . $value['notice'] . "'/>
            </div>
        </div>".
		($_SESSION['group_id'] == 2 ?
		"<div class='form-group'>
            <label  for='branch' class='control-label col-md-3'>Branch</label>
            <div class='col-md-9'>
                <div class='input-group select2-bootstrap-prepend'>
                    <select id='branch_edit' name='branch_edit' class='form-control select2-allow-clear'>
                        <option></option>
                        " . getAllBranch(getCompany($_SESSION['user_id'])) . "
                    </select>
                </div>
            </div>
        </div>" :
		"<input name='branch_edit' id='branch_edit' value='".getBranch($_SESSION['user_id'])."' hidden type='hidden' style='display:none;' />"
		);
    }
    return $html;
}
function getModalBranch($branch_id) {
    global $db;
    $db->bind("branch_id", $branch_id);
    $data = $db->query("SELECT * FROM branches
                                 WHERE branch_id = :branch_id");
    $html = "";
    foreach ($data as $key => $value) {
        $html = "
        <input type='hidden' id='id_edit' name='id_edit' value='" . $value['branch_id'] . "' />
        <div class='form-group '>
            <label for='name_edit' class='control-label col-md-3'>Branch Name</label>
            <div class='col-md-9'>
                <input class='form-control ' name='name_edit' id='name_edit' type='text' value='" . $value['branch_name'] . "'/>
            </div>
        </div>
        <div class='form-group '>
            <label for='address_edit' class='control-label col-md-3'>Branch Address</label>
            <div class='col-md-9'>
                <input class='form-control ' name='address_edit' id='address_edit' type='text' value='" . $value['branch_address'] . "'/>
            </div>
        </div>
        <div class='form-group '>
            <label for='city_edit' class='control-label col-md-3'>Branch City</label>
            <div class='col-md-9'>
                <input class='form-control ' name='city_edit' id='city_edit' type='text' value='" . $value['branch_city'] . "'/>
            </div>
        </div>
        <div class='form-group '>
            <label for='zip_edit' class='control-label col-md-3'>Branch Postcode</label>
            <div class='col-md-9'>
                <input class='form-control ' name='zip_edit' id='zip_edit' type='text' value='" . $value['branch_zip'] . "'/>
            </div>
        </div>
        <div class='form-group '>
            <label for='state_edit' class='control-label col-md-3'>Branch State</label>
            <div class='col-md-9'>
                <input class='form-control ' name='state_edit' id='state_edit' type='text' value='" . $value['branch_state'] . "'/>
            </div>
        </div>
        <div class='form-group '>
            <label for='country_edit' class='control-label col-md-3'>Branch Country</label>
            <div class='col-md-9'>
                <input class='form-control ' name='country_edit' id='country_edit' type='text' value='" . $value['branch_country'] . "'/>
            </div>
        </div>
        <div class='form-group '>
            <label for='contact_person_edit' class='control-label col-md-3'>Contact Person</label>
            <div class='col-md-9'>
                <input class='form-control ' name='contact_person_edit' id='contact_person_edit' type='text' value='" . $value['contact_person'] . "'/>
            </div>
        </div>
        <div class='form-group '>
            <label for='contact_person_no_edit' class='control-label col-md-3'>Contact Person No</label>
            <div class='col-md-9'>
                <input class='form-control ' name='contact_person_no_edit' id='contact_person_no_edit' type='text' value='" . $value['contact_person_no'] . "'/>
            </div>
        </div>";
    }
    return $html;
}
function getModalStaff($id) {
    global $db;
    $db->bind("id", $id);
    $data = $db->query("SELECT * FROM user_company
                                 WHERE id = :id");
    $html = "";
    foreach ($data as $key => $value) {
        if ($value["position"] == 1) {
            $position = "Admin";
        } elseif ($value["position"] == 2) {
            $position = "Branch Admin";
        } else {
            $position = "Staff";
        }
        $html = "
        <input type='hidden' id='id_edit' name='id_edit' value='" . $value['id'] . "' />
        <div class='form-group '>
            <label class='control-label col-md-4'>Name</label>
            <div class='col-md-8'>
                <p class='form-control-static '>" . getProfileData($value['user_id'], "first_name") . " " . getProfileData($value['user_id'], "last_name") . "</p>
            </div>
        </div>
        <div class='form-group'>
            <label class='control-label col-md-4'>Position</label>
            <div class='col-md-8'>
                <select id='position_edit' name='position_edit' class='form-control'>
                    <option " . ($value["position"] == 1 ? "selected" : "") . " value='1'>Admin</option>
                    <option " . ($value["position"] == 2 ? "selected" : "") . " value='2'>Branch Admin</option>
                    <option " . ($value["position"] == 3 ? "selected" : "") . " value='3'>Staff</option>
                </select>
            </div>
        </div>
        <div class='form-group'>
            <label class='control-label col-md-4'>Branch</label>
            <div class='col-md-8'>
                <select id='branch_edit' name='branch_edit' class='form-control'>
                    " . getAllBranch($value['company_id']) . "
                </select>
            </div>
        </div>";
    }
    return $html;
}
function getAllBranch($company_id) {
    global $db;
    $db->bind("company_id", $company_id);
    $data = $db->query("select * from branches where company_id = :company_id");
    $html = "";
    foreach ($data as $key => $value) {
        $html .= "<option value='" . $value['branch_id'] . "'>";
        $html .= $value['branch_name'] . " - " . $value['branch_city'] . ", " . $value['branch_state'];
        $html .= "</option>";
    }
    return $html;
}
function getAllFriendNoCompany($user_id) {
    global $db;
    $group_id = get_data($_SESSION['user_id'], "group_id");
    $db->bind("user_id", $user_id);
    $db->bind("group_id", $group_id);
    $data = $db->query("select * from user_friend join user_detail on user_friend.friend_id = user_detail.user_id join users on user_friend.friend_id = users.user_id where user_friend.user_id = :user_id and users.group_id = :group_id and user_friend.friend_id not in (select user_id from user_company where 1)");
    $html = "";
    foreach ($data as $key => $value) {
        $html .= "<option value='" . $value['friend_id'] . "'>";
        $html .= $value['first_name'] . " " . $value['last_name'] . " - (" . $value['username'] . ")";
        $html .= "</option>";
    }
    return $html;
}
function getAllFriendinCompany($user_id) {
    global $db;
    $company_id = getCompany($user_id);
    if ($company_id <> 0) {
        $db->bind("user_id", $user_id);
        $db->bind("company_id", $company_id);
        $data = $db->query("select * from user_friend join user_detail on user_friend.friend_id = user_detail.user_id join users on user_friend.friend_id = users.user_id where user_friend.user_id = :user_id and user_friend.friend_id in (select user_id from user_company where company_id = :company_id )");
        $html = "";
        foreach ($data as $key => $value) {
            $html .= "<option value='" . $value['friend_id'] . "'>";
            $html .= $value['first_name'] . " " . $value['last_name'] . " - (" . $value['username'] . ")";
            $html .= "</option>";
        }
    }
    return $html;
}
function getAllFriends($user_id) {
    global $db;
    $db->bind("user_id", $user_id);
    $data = $db->query("select * from user_friend join user_detail on user_friend.friend_id = user_detail.user_id join users on user_friend.friend_id = users.user_id where user_friend.user_id = :user_id");
    $html = "";
    foreach ($data as $key => $value) {
        $html .= "<option value='" . $value['friend_id'] . "'>";
        $html .= $value['first_name'] . " " . $value['last_name'] . " - (" . $value['username'] . ")";
        $html .= "</option>";
    }
    return $html;
}
function getAllCompanyFollowed($user_id) {
    global $db;
    $company_id = getCompany($user_id);
    if ($company_id <> 0) {
        $db->bind("company_id", $company_id);
        $data = $db->query("select * from company_friend join companies on company_friend.friend_id = companies.company_id where company_friend.company_id = :company_id");
        $html = "";
        foreach ($data as $key => $value) {
            $html .= "<option value='" . $value['company_id'] . "'>";
            $html .= $value['name'];
            $html .= "</option>";
        }
    }
    return $html;
}
function getAllForwarderCompanyFollowed($user_id) {
    global $db;
    $company_id = getCompany($user_id);
    if ($company_id <> 0) {
        $db->bind("company_id", $company_id);
        $data = $db->query("select * from company_friend join companies on company_friend.friend_id = companies.company_id join users on company_friend.friend_id = users.user_id where company_friend.company_id = :company_id and users.group_id = 1");
        $html = "";
        foreach ($data as $key => $value) {
            $html .= "<option value='" . $value['company_id'] . "'>";
            $html .= $value['name'];
            $html .= "</option>";
        }
    }
    return $html;
}
function get_warehouse($warehouse_id, $kolom) {
    global $db;
    $db->bind("warehouse_id", $warehouse_id);
    $data = $db->query("SELECT * FROM warehouses WHERE warehouse_id = :warehouse_id;");
    if ($data) {
        return $data[0][$kolom];
    } else {
        return "0";
    }
}


function getStatusCompanyFriend($company_id){
    global $db;
    $db->bind('company_id', $company_id);
    $groups = $db->query("SELECT * FROM company_friend WHERE status = 0 and company_id = :company_id ");
	
	return count($groups);
}

function getRoomUnread($room_id){
    global $db;
    $db->bind("room_id", $room_id);
    $db->bind("user_id", $_SESSION['user_id']);
    $row = $db->query("SELECT * FROM notice_unread WHERE room_id = :room_id and user_id = :user_id and status = 0;");
	
	return (count($row) > 0 ? count($row) : "");
}

function getOrderUnread(){
    global $db;
    $db->bind("user_id", $_SESSION['user_id']);
    $row = $db->query("SELECT * FROM notice_unread join notice_rooms on notice_unread.room_id = notice_rooms.room_id WHERE notice_unread.user_id = :user_id and notice_unread.status = 0 and notice_rooms.order_id IS NOT null");

	return (count($row) > 0 ? count($row) : "");
}

function getSOUnread(){
	global $db;
    $db->bind("user_id", $_SESSION['user_id']);
	$row = $db->query("SELECT * FROM shipping_order_unread WHERE status = 0 and user_id = :user_id");

	return (count($row) > 0 ? count($row) : "");
}

function getContactUnread(){
	global $db;
    $db->bind("user_id", $_SESSION['user_id']);
	$row = $db->query("SELECT * FROM user_friend WHERE status = 0 and user_id = :user_id");
	
	return (count($row) > 0 ? count($row) : "");
	
}

function getGeneralUnread() {
    global $db;
    $db->bind("user_id", $_SESSION['user_id']);
    $data = $db->query("SELECT * FROM notice_unread join notice_rooms on notice_unread.room_id = notice_rooms.room_id WHERE notice_unread.user_id = :user_id and room_type = 'general' and notice_unread.status = 0");
    return (count($data) > 0 ? count($data) : "");
}
function getHelpUnread() {
    global $db;
    $db->bind("user_id", $_SESSION['user_id']);
    $data = $db->query("SELECT * FROM notice_unread join notice_rooms on notice_unread.room_id = notice_rooms.room_id WHERE notice_unread.user_id = :user_id and room_type = 'help' and notice_unread.status = 0");
    return (count($data) > 0 ? count($data) : "");
}
function GeneralGetToID($message_id) {
    global $db;
    $html  = "";
    $count = 1;
    $db->bind("message_id", $message_id);
    $db->bind("owner_id", $_SESSION['user_id']);
    $data = $db->query("SELECT * FROM notice_rooms WHERE room_id in (SELECT room_id FROM notice_room_message WHERE message_id = :message_id)  and owner_id <> :owner_id and chat_name = 'General' and room_type = 'general' ");
    foreach ($data as $key => $value) {
        $html .= getProfileData($value['owner_id'], "first_name") . " " . getProfileData($value['owner_id'], "last_name");
        if ($count <> count($data)) {
            $html .= ", ";
        }
        $count++;
    }
    return $html;
}
function getAdminChatCount($from_id) {
    global $db;
    $db->bind("to_id", 1);
    $db->bind("from_id", $from_id);
    $data = $db->query("SELECT notice_messages.from_id, count(notice_messages.from_id) as unread FROM notice_members JOIN notice_messages ON notice_members.message_id = notice_messages.message_id WHERE notice_members.status = 0 and notice_messages.from_id = :from_id and notice_members.to_id = :to_id GROUP BY notice_messages.from_id ORDER BY COUNT(notice_members.message_id)");
    $html = "";
    if ($data) {
        $html .= '<span class="badge badge-danger">' . $data[0]['unread'] . '</span>';
    }
    return $html;
}
function getAdminChatList() {
    global $db;
    $db->bind("to_id", 1);
    $data = $db->query("SELECT notice_messages.from_id FROM notice_members JOIN notice_messages ON notice_members.message_id = notice_messages.message_id WHERE notice_members.to_id = :to_id GROUP BY notice_messages.from_id ORDER BY COUNT(notice_members.message_id)");
    $html = "";
    if (count($data) > 0) {
        foreach ($data as $key => $value) {
            $html .= "<li id = '" . $value['from_id'] . "'>";
            $html .= get_Data($value['from_id'], "username") . " " . getAdminChatCount($value['from_id']);
            $html .= "</li>";
        }
    }
    return $html;
}
function showImage($user_id) {
    global $db;
    $db->bind("user_id", $user_id);
    $profile = $db->query("SELECT * FROM user_detail WHERE user_id=:user_id");
    if (count($profile) > 0 && ($profile[0]['profile_picture'] <> null || $profile[0]['profile_picture'] <> "")) {
        return $profile[0]['profile_picture'];
    } else {
        return "http://www.placehold.it/200/EFEFEF/AAAAAA&amp;text=no+image";
    }
}
?>

<?php
function typeAhead() { 
?>
<script src="/assets/global/plugins/typeahead/typeahead.bundle.min.js" type="text/javascript"></script>
<script src="/assets/global/plugins/typeahead/handlebars.min.js" type="text/javascript"></script>

<?php
}

function getMarkingNo($company_id, $friend_id, $branch_id){
	global $db;
	$db->bind("company_id", $company_id);
	$db->bind("friend_id", $friend_id);
	$companyfriend = $db->query("SELECT * FROM company_friend WHERE company_id = :company_id and friend_id = :friend_id");
    
	if ($companyfriend[0]['marking_no'] <> null){
		$marking_no = json_decode($companyfriend[0]['marking_no']);
		foreach($marking_no as $branch => $branch_marking){
			if ($branch == $branch_id){
				return $branch_marking;
			}
		}
	}
	
    
}

function getOrderNumber(){
	global $db;
	
	$purchaseorders = $db->query("SELECT * FROM order_count WHERE 1");
	
	$current_orderno = count($purchaseorders) + 1;
	
	return sprintf('%05d', $current_orderno);
}

function listFolderFiles($dir) {
    $x = array();
    foreach (new DirectoryIterator($dir) as $fileInfo) {
        if (!$fileInfo->isDot() && !strpos($fileInfo->getFilename(), '.')) {
            $x[] = $fileInfo->getFilename();
        }
    }
    return $x;
}

?>