<?php 
$query="SELECT * FROM ".$prefix2."company";
$companyType=mysqli_query($linkid,$query);
$companyno=mysqli_num_rows($companyType);
$mydata=mysqli_fetch_assoc($companyType);
?>

<div id="contentwrapper">
  <div class="main_content">
    <nav>
      <div id="jCrumbs" class="breadCrumb module">
        <ul>
          <li> <a href="#"><i class="icon-home"></i></a> </li>
          <li> <a href="#">Setup</a> </li>
          <li> <a href="index.php?tab=company">Company</a> </li>
        </ul>
      </div>
    </nav>
    <div class="row-fluid">
      <div class="span6">
        <?php
		if(isset($_POST['submit']))
			{	
			
			$name=$_POST['name'];
			$address=$_POST['address'];
			$email=$_POST['email'];
			$phone=$_POST['phone'];
			$fax=$_POST['fax'];	
			$mobile=$_POST['mobile'];	
			$web=$_POST['web'];	
			$avatar=$_POST['avatar'];						
			//echo $code.$name.$prefix."Hello";
			
						
			//$upload_dir=$_SERVER['DOCUMENT_ROOT']."/eduMIS/ict/img/company/";
			$upload_dir=$_SERVER['DOCUMENT_ROOT']."/invoice/img/company/";
			// Image uploading for student///////////////////////////////////						
			$image_1_name = $_FILES['file_1']['name'];
			$image_1_type = $_FILES['file_1']['type'];
			$image_1_tmp_name = $_FILES['file_1']['tmp_name'];	
			
			
			$ext= explode('.',$image_1_name);
			$image_1_name_1=$image_1_name;			
			$filename = $upload_dir . $image_1_name_1;
			
			if($image_1_name_1=="")
			  $logo=$avatar;			  
			elseif($image_1_name_1!="")
			  $logo=$image_1_name_1;  
			  
		
	
	
	if ((!empty($_FILES['file_1']['name'])) and (isset($_FILES['file_1']['name'])))
	{
		
         if ((($_FILES['file_1']['type'] == "image/gif")

               || ($_FILES['file_1']['type'] == "image/jpeg")

               || ($_FILES['file_1']['type'] == "image/pjpeg")

               || ($_FILES['file_1']['type'] == "image/png")

               || ($_FILES['file_1']['type'] == "image/jpg")

               || ($_FILES['file_1']['type'] == "image/x-png")))

                         {

                                            
                                      $uploadedfile = $_FILES['file_1']['tmp_name'];

                                      $size = getimagesize($uploadedfile);

                                      $type = $size['mime'];

                                      $width = $size[0];

                                      $height = $size[1];
																

                                                if($height > '100' || $width > '100')

                                                    { 

                                                              $newwidth=80;

                                                              $newheight=80;

                                                              $tmp=imagecreatetruecolor($newwidth,$newheight);

                                                              $filename = $upload_dir . $image_1_name_1;
																				
																				
																		

                                                                                
																				

                                                                                    if($size[2] == IMAGETYPE_GIF)

                                                                                        {


                                                                                            $src = imagecreatefromgif($uploadedfile);
                                                                                            imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,				                                                                                            $newheight,$width,$height);
                                                                                            imagegif($tmp,$filename,100);
																						
                                                                                        }

                                                                                    elseif($size[2] == IMAGETYPE_JPEG)

                                                                                        {

                                                                                            $src = imagecreatefromjpeg($uploadedfile);
                                                                                            imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,                                                                                            $newheight,$width,$height);
                                                                                            imagejpeg($tmp,$filename,100);
																							

                                                                                        }

                                                                                    elseif($size[2] == IMAGETYPE_PNG) 

                                                                                        {

                                                                                            $src = imagecreatefrompng($uploadedfile);
                                                                                            imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,                                                                                            $newheight,$width,$height);
                                                                                            imagepng($tmp,$filename,9);
																							
																							
                                                                                        }

                                                                                

                                                                            imagedestroy($src);

                                                                            imagedestroy($tmp);
															                 }

                                                               else
															   {
																move_uploaded_file($uploadedfile, "$filename");																	 
															   }

                                                        }

                                                

                                        
                                    else 
									{ 
									echo  "<script>alert('invalid file extension')</script>";
									return;
									}
									
									
	}
						
						// end of Image uploading for student///////////////////////////////////
						
						
						if($companyno==1)
						{
						$query = "UPDATE ".$prefix2."company SET name='$name',address='$address',phone='$phone',email='$email',fax='$fax',mobile='$mobile',web='$web',logo='$logo'";
						$result=mysqli_query($linkid,$query) or die(mysqli_error());
						}
						else{				
						$query = "INSERT INTO ".$prefix2."company values (null,'$name','$address','$phone','$email','$fax','$mobile','$web','$logo',1,'$uid')";
						$result=mysqli_query($linkid,$query) or die(mysqli_error());		
						}
						if($result){
						?>
        <div class="alert alert-success"> <a data-dismiss="alert" class="close">x</a> <strong>Company</strong> is saved.&nbsp;&nbsp; </div>
        <script type="text/javascript" language="javascript">
							  setTimeout(function () {
								 window.location.href= 'index.php?tab=company-setup'; // the redirect goes here
								},3000);
							  </script>
        <?php 
						
						 
        
						 }
						}
					 //unlink($filename1);
						 unset($_POST);
						 $_POST['submit']='';
						 $_POST='';
					?>
        <h3 class="heading">Company Setup</h3>
        <form class="form_validation_reg" id="form_validation_reg" action=""  method="post" enctype="multipart/form-data">
		<input type="hidden" name="avatar" id="avatar" class="span12" value="<?php echo $mydata['logo']?>"/>
          <div class="formSep">
            <div class="row-fluid">
              <div class="span4">
                <label>Name <span class="f_req">*</span></label>
                <input type="text" name="name" id="name" class="span12" value="<?php echo $mydata['name']?>"/>
              </div>
              <div class="span4">
                <label>Company Logo: <?php echo $mydata['logo']?></label>
              </div>
              <div class="span4">
                <label>Logo </label>
                <input type="file" name="file_1" id="file_1" class="span12" />
              </div>
            </div>
          </div>
          <div class="formSep">
            <div class="row-fluid">
              <div class="span4">
                <label>Phone</label>
                <input type="text" name="phone" id="phone" class="span12" value="<?php echo $mydata['phone']?>" />
              </div>
			    <div class="span4">
                <label>Mobile</label>
                <input type="text" name="mobile" id="mobile" class="span12" value="<?php echo $mydata['mobile']?>" />
              </div>
              <div class="span4">
                <label>Address <span class="f_req">*</span></label>
                <textarea cols="" name="address" id="address"><?php echo $mydata['address']?></textarea>
              </div>
            </div>
          </div>
          <div class="formSep">
            <div class="row-fluid">
              <div class="span4">
                <label>Email</label>
                <input type="text" name="email" id="email" class="span12"  value="<?php echo $mydata['email']?>"/>
              </div>
              <div class="span4">
                <label>Fax</label>
                <input type="text" name="fax" id="fax" class="span12" value="<?php echo $mydata['fax']?>" />
              </div>
			  <div class="span4">
                <label>Website</label>
                <input type="text" name="web" id="web" class="span12" value="<?php echo $mydata['web']?>" />
              </div>
            </div>
          </div>
          <div class="form-actions">
            <input class="btn btn-inverse" name="submit" type="submit" value="Save Form" />
            <button class="btn">Cancel</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>
