<?php
require_once("conf.php");
require_once("method.php");

$method = $_SERVER["REQUEST_METHOD"];
method($method);

$data = json_decode(file_get_contents('php://input'), true);

if ($method == "GET" AND !isset($_GET['id']) AND !isset($_GET['personel_id']) AND !isset($_GET['sync']) AND !isset($_GET['company_type'])) {

    $query = $db->query("SELECT * FROM company", PDO::FETCH_ASSOC);
    if ($query->rowCount() > 0) {
        foreach ($query as $q) {
            $company[] = array(
            	"cyId" => $q["cyId"],
            	"cyName" => $q["cyName"],
            	"cyAName" => $q["cyAName"],
            	"cyAddress" => $q["cyAddress"],
            	"cyPhone" => $q["cyPhone"],
            	"cyMail" => $q["cyMail"],
            	"cyLogo" => $q["cyLogo"],
            	"cyCompanyType" => $q["cyCompanyType"],
            	"cyPersonelId" => $q["cyPersonelId"],
            	"cyCreatedAt" => $q["cyCreatedAt"],
            	"cyUpdatedAt" => $q["cyUpdatedAt"],
                "cySyncAt" => $q["cySyncAt"]
            );
        }
        $result = json_encode($company, JSON_UNESCAPED_UNICODE);
        echo $result;

    } else {
        echo "";
    }
} else if ($method == "GET" AND isset($_GET['id'])) {

    $query = $db->query("SELECT * FROM company WHERE cyId='".$_GET['id']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
		foreach ($query as $q) {
            $company[] = array(
            	"cyId" => $q["cyId"],
            	"cyName" => $q["cyName"],
            	"cyAName" => $q["cyAName"],
            	"cyAddress" => $q["cyAddress"],
            	"cyPhone" => $q["cyPhone"],
            	"cyMail" => $q["cyMail"],
            	"cyLogo" => $q["cyLogo"],
            	"cyCompanyType" => $q["cyCompanyType"],
            	"cyPersonelId" => $q["cyPersonelId"],
            	"cyCreatedAt" => $q["cyCreatedAt"],
            	"cyUpdatedAt" => $q["cyUpdatedAt"],
                "cySyncAt" => $q["cySyncAt"]
            );
        }
        $result = json_encode($company, JSON_UNESCAPED_UNICODE);
        echo $result;

    } else {
        echo "";
    }
} else if ($method == "GET" AND isset($_GET['personel_id'])) {

    $query = $db->query("SELECT * FROM company WHERE cyPersonelId='".$_GET['personel_id']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
		foreach ($query as $q) {
            $company[] = array(
            	"cyId" => $q["cyId"],
            	"cyName" => $q["cyName"],
            	"cyAName" => $q["cyAName"],
            	"cyAddress" => $q["cyAddress"],
            	"cyPhone" => $q["cyPhone"],
            	"cyMail" => $q["cyMail"],
            	"cyLogo" => $q["cyLogo"],
            	"cyCompanyType" => $q["cyCompanyType"],
            	"cyPersonelId" => $q["cyPersonelId"],
            	"cyCreatedAt" => $q["cyCreatedAt"],
            	"cyUpdatedAt" => $q["cyUpdatedAt"],
                "cySyncAt" => $q["cySyncAt"]
            );
        }
        $result = json_encode($company, JSON_UNESCAPED_UNICODE);
        echo $result;

    } else {
        echo "";
    }
} else if ($method == "GET" AND isset($_GET['company_type'])) {

    $query = $db->query("SELECT * FROM company WHERE cyCompanyType='".$_GET['company_type']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
		foreach ($query as $q) {
            $company[] = array(
            	"cyId" => $q["cyId"],
            	"cyName" => $q["cyName"],
            	"cyAName" => $q["cyAName"],
            	"cyAddress" => $q["cyAddress"],
            	"cyPhone" => $q["cyPhone"],
            	"cyMail" => $q["cyMail"],
            	"cyLogo" => $q["cyLogo"],
            	"cyCompanyType" => $q["cyCompanyType"],
            	"cyPersonelId" => $q["cyPersonelId"],
            	"cyCreatedAt" => $q["cyCreatedAt"],
            	"cyUpdatedAt" => $q["cyUpdatedAt"],
                "cySyncAt" => $q["cySyncAt"]
            );
        }
        $result = json_encode($company, JSON_UNESCAPED_UNICODE);
        echo $result;

    } else {
        echo "";
    }
} else if ($method == "GET" AND isset($_GET['sync']) AND isset($_GET['sync_personel'])) {

    $query = $db->query("SELECT * FROM company WHERE cySyncAt >= ".$_GET['sync']." AND cyPersonelId !='".$_GET['sync_personel']."'", PDO::FETCH_ASSOC);

    if ($query->rowCount() > 0) {
		foreach ($query as $q) {
            $company[] = array(
            	"cyId" => $q["cyId"],
            	"cyName" => $q["cyName"],
            	"cyAName" => $q["cyAName"],
            	"cyAddress" => $q["cyAddress"],
            	"cyPhone" => $q["cyPhone"],
            	"cyMail" => $q["cyMail"],
            	"cyLogo" => $q["cyLogo"],
            	"cyCompanyType" => $q["cyCompanyType"],
            	"cyPersonelId" => $q["cyPersonelId"],
            	"cyCreatedAt" => $q["cyCreatedAt"],
            	"cyUpdatedAt" => $q["cyUpdatedAt"],
                "cySyncAt" => $q["cySyncAt"]
            );
        }
        $result = json_encode($company, JSON_UNESCAPED_UNICODE);
        echo $result;

    } else {
        echo "";
    }
} else if ($method == "POST") {
    $currentDate =  time();
    $query = $db->prepare("INSERT INTO company SET
    	cyId=:cyId,
		cyName=:cyName,
		cyAName=:cyAName,
		cyAddress=:cyAddress,
		cyPhone=:cyPhone,
		cyMail=:cyMail,
		cyLogo=:cyLogo,
		cyCompanyType=:cyCompanyType,
		cyPersonelId=:cyPersonelId,
		cyCreatedAt=:cyCreatedAt,
		cyUpdatedAt=:cyUpdatedAt,
        cySyncAt=:cySyncAt
	");
    $insert=$query->execute(array(
		'cyId' => $data['cyId'],
		'cyName'=>$data['cyName'],
		'cyAName'=>$data['cyAName'],
		'cyAddress'=>$data['cyAddress'],
		'cyPhone'=>$data['cyPhone'],
		'cyMail'=>$data['cyMail'],
		'cyLogo'=>$data['cyLogo'],
		'cyCompanyType'=>$data['cyCompanyType'],
		'cyPersonelId' => $data['cyPersonelId'],
		'cyCreatedAt'=>$data['cyCreatedAt'],
		'cyUpdatedAt'=>$data['cyUpdatedAt'],
        'cySyncAt'=>$currentDate."000",
    ));
    if ($insert) {
        echo json_encode("ok");

    } else {
        echo json_encode("error");
    }
} else if ($method == "PUT") {
    $currentDate =  time();
    $query = $db->prepare("UPDATE company SET
		cyName=:cyName,
		cyAName=:cyAName,
		cyAddress=:cyAddress,
		cyPhone=:cyPhone,
		cyMail=:cyMail,
		cyLogo=:cyLogo,
		cyCompanyType=:cyCompanyType,
		cyPersonelId=:cyPersonelId,
		cyCreatedAt=:cyCreatedAt,
		cyUpdatedAt=:cyUpdatedAt,
        cySyncAt=:cySyncAt
		WHERE cyId=:cyId
	");
    $update=$query->execute(array(
    	'cyId' => $data['cyId'],
		'cyName'=>$data['cyName'],
		'cyAName'=>$data['cyAName'],
		'cyAddress'=>$data['cyAddress'],
		'cyPhone'=>$data['cyPhone'],
		'cyMail'=>$data['cyMail'],
		'cyLogo'=>$data['cyLogo'],
		'cyCompanyType'=>$data['cyCompanyType'],
		'cyPersonelId' => $data['cyPersonelId'],
		'cyCreatedAt'=>$data['cyCreatedAt'],
		'cyUpdatedAt'=>$data['cyUpdatedAt'],
        'cySyncAt'=>$currentDate."000",
		'cyId'=>$data['cyId']
    ));
    if ($update) {
        echo json_encode("ok");

    } else {
        echo json_encode("error");
    }
} else if ($method == "DELETE") {
    $main_query=$db->prepare("SELECT * FROM company WHERE cyId=:cy_id");
    $main_query->execute(array("cy_id"=>$_GET['id']));
    $main=$main_query->fetch(PDO::FETCH_ASSOC);

    if($main['cyCompanyType']=="customer"){
        $query_c1 = $db->prepare("DELETE FROM acente WHERE acCustomerId=:acCustomerId");
        $query_c1->execute(array(
            'acCustomerId'=>$_GET['id']
        ));

        $query_c2 = $db->prepare("DELETE FROM report WHERE rpCustomerId=:rpCustomerId");
        $query_c2->execute(array(
            'rpCustomerId'=>$_GET['id']
        ));
    }else if($main['cyCompanyType']=="exporter"){
        $query_e1 = $db->prepare("DELETE FROM acente WHERE acExporterId=:acExporterId");
        $query_e1->execute(array(
            'acExporterId'=>$_GET['id']
        ));

        $query_e2 = $db->prepare("DELETE FROM report WHERE rpExporterId=:rpExporterId");
        $query_e2->execute(array(
            'rpExporterId'=>$_GET['id']
        ));
    }

    $query = $db->prepare("DELETE FROM company WHERE cyId=:cyId");
    $update=$query->execute(array(
		'cyId'=>$_GET['id']
    ));
    if ($update) {
        echo json_encode("ok");

    } else {
        echo json_encode("error");
    }
}
