<?php
session_start();
include "../../inc/config.php";
session_check();
switch ($_GET["act"]) {
  case "in":
  
  
  if (!is_dir("../../../upload/empresa")) {
              mkdir("../../../upload/empresa");
            }
  $data = array("nombre"=>$_POST["nombre"],"nit"=>$_POST["nit"],"direccion"=>$_POST["direccion"],"tel"=>$_POST["tel"],);
  
  
  
                    if (!preg_match("/.(png|jpg|jpeg|gif|bmp)$/i", $_FILES["logo"]["name"]) ) {

              echo "pastikan file yang anda pilih png|jpg|jpeg|gif";
              exit();

            } else { 
      move_uploaded_file($_FILES["logo"]["tmp_name"], "../../../upload/empresa/".$_FILES['logo']['name']);
        
            $logo = array("logo"=>$_FILES["logo"]["name"]);
              $data = array_merge($data,$logo);
            }
   
    $in = $db->insert("empresa",$data);
    
    if ($in=true) {
      echo "good";
    } else {
      return false;
    }
    break;
  case "delete":
    
    $db->deleteDirectory("../../../upload/empresa/".$db->fetch_single_row("empresa","idempresa",$_POST["id"])->logo);
    
    
    $db->delete("empresa","idempresa",$_GET["id"]);
    break;
  case "up":
   $data = array("nombre"=>$_POST["nombre"],"nit"=>$_POST["nit"],"direccion"=>$_POST["direccion"],"tel"=>$_POST["tel"],);
   
   
   
                         if(isset($_FILES["logo"]["name"])) {
                        if (!preg_match("/.(png|jpg|jpeg|gif|bmp)$/i", $_FILES["logo"]["name"]) ) {

              echo "pastikan file yang anda pilih gambar";
              exit();

            } else {
      move_uploaded_file($_FILES["logo"]["tmp_name"], "../../../upload/empresa/".$_FILES['logo']['name']);
        
              $db->deleteDirectory("../../../upload/empresa/".$db->fetch_single_row("empresa","idempresa",$_POST["id"])->logo);
              $logo = array("logo"=>$_FILES["logo"]["name"]);
              $data = array_merge($data,$logo);
            }

                         }

    
    $up = $db->update("empresa",$data,"idempresa",$_POST["id"]);
    if ($up=true) {
      echo "good";
    } else {
      return false; 
    }
    break;
  default:
    # code...
    break;
}

?>