<?php
require_once("class.restframework.php");
require_once("class.company.php");
/// int rest api
$restapi = new RestFrameWork();
#######
# company
$user = $restapi->auth();

$data_object = file_get_contents("php://input");
###############3
$company = new Company();
//////////////////////
if (!empty($data_object)) {
    //covert json object to array   
    $data = json_decode($data_object);
    //////////////////////////////
    /////////////////////
    if (!$restapi->purge("company_id")) {
        echo $restapi->JsonResponse(array("message" => "Incomplete data request", "success" => false), 400);
    } else {
        ////
        if (
            empty($data->company_name) &&
            empty($data->contacts) &&
            empty($data->physical_address) &&
            empty($data->logo) &&
            empty($data->slogan) &&
            empty($data->website) &&
            empty($data->api_key) &&
            empty($data->lic_key) &&
            empty($data->lic_file) &&
            empty($data->country_id) &&
            empty($data->currecy_id) &&
            empty($data->language_id) &&
            empty($data->date_format_id) &&
            empty($data->decimal_separator_id) &&
            empty($data->athousand_separator_id) &&
            empty($data->tin) &&
            empty($data->has_been_shutdown) &&
            empty($data->collects_taxes) &&
            empty($data->is_disabled)
        ) {
            echo $restapi->JsonResponse(array("message" => "Atleast provide one field to update", "success" => false), 400);
        } else {
            //////
            if (empty(!$data->email) && !$restapi->isEmailValid($data->email)) {
                /////
                echo $restapi->JsonResponse(array("message" => "Invalid email address", "success" => false), 400);
            } else if (empty(!$data->website) && !$restapi->isWebsiteValid($data->website)) {
                /////
                echo $restapi->JsonResponse(array("message" => "Invalid website", "success" => false), 400);
            } else {
                $company_id = $restapi->request("company_id");
                //ARRAY data 
                $data = json_decode($data_object, true);
                $company = $company->UpdateCompany($company_id, $data);
                if ($company) {
                    echo $restapi->JsonResponse(
                        array(
                            "message" => "Updated successfully",
                            "success" => true,
                            "company" => $company
                        )
                    );
                } else {
                    echo $restapi->JsonResponse(array("message" => "Something went wrong please try again laiter", "success" => false), 400);
                }
            }
        }
    }
} else {
    $restapi->JsonResponse(array("message" => "Incomplete data request", "success" => false), 400);
}
