<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
header("Content-Type: application/json; charset=UTF-8");

// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);

include_once("lib/init.php");

$method = $_GET["method"];
// $method_type = $_GET["method_type"];
// $method_id = $_GET["method_id"];

if (isset($_GET["method_type"])) {
    $method_type = $_GET["method_type"];
} else {
    $method_type = null;
}

if (isset($_GET["method_id"])) {
    $method_id = $_GET["method_id"];
} else {
    $method_id = null;
}

// echo '<pre>', print_r($_GET), '</pre>';exit;



// var_dump($method);exit;

$result = "";

$post_data = (object) json_decode(file_get_contents("php://input"));
//$post_data = json_decode(json_encode($_POST));


$dashboard = new Dashboard();
$simple_list = new SimpleList();
$user = new User();
$price_list = new PriceList();
$profit = new Profit();
$work_order = new WorkOrder();
$debit_credit = new DebitCredit();
$not_found_order = new NotFoundOrder();
$not_paid_order = new NotPaidOrder();
$report = new Report();
$report_new = new ReportNew();
$notes = new Notes();
$not_found_report = new NotFoundReport();

if (count((array)$post_data) < 1) {
    $result = unknown_method("Invalid JSON");
} else {
    if (!isset($post_data->usr_auth)) {
        if ($method == "user") {
            if ($method_type == "login") {
                $result = $user->user_login($post_data);
            } else if ($method_type == "forget-pass") {
                $result = $user->user_reset_code($post_data);
            } else if ($method_type == "verify-account" || $method_type == "reset-account") {
                $result = $user->user_verification_code($method_type, $post_data);
            } else {
                $result = unknown_method();
            }
        } else {
            $result = unknown_method();
        }
    } else {
        $usr_type_via_auth = $user->user_authentication($post_data->usr_auth);
        if ($usr_type_via_auth["status"]) {
            if ($method == "dashboard") {
                if ($method_type == null) {
                    $result = $dashboard->dashboard_generate($post_data->card, $post_data->value);
                } else if ($method_type == "profitable_work_code") {
                    $result = $dashboard->get_top_5_profitable_work_code();
                } else if ($method_type == "unprofitable_work_code") {
                    $result = $dashboard->get_top_5_nonprofit_work_code();
                }
                
                else if ($method_type == "inspectors_profits") {
                    $result = $dashboard->get_inspectors_profits();
                }
                
                else if ($method_type == "inspector_completed_orders") {
                    $result = $dashboard->get_top_5_inspectors_completed_orders();
                } else if ($method_type == "top_states") {
                    $result = $dashboard->get_top_5_states();
                } else if ($method_type == "yearly_profit") {
                    $result = $dashboard->yearly_profit();
                } else {
                    $result = unknown_method();
                }
                
            } else if ($method == "list") {
                if ($method_type == "user") {
                    $result = $simple_list->simple_user_list();
                }else if ($method_type == "inspector") {
                    $result = $simple_list->simple_inspector_list();
                }else if ($method_type == "work-code") {
                    $result = $simple_list->simple_work_code_list();
                } else if ($method_type == "zip-code") {
                    $result = $simple_list->simple_zip_list();
                } else {
                    $result = unknown_method();
                }
            } else if ($method == "user") {
                if ($method_type == null) {
                    $result = $user->user_list($post_data, $usr_type_via_auth["type"]);
                } else if ($method_type == "add" || $method_type == "edit" || $method_type == "update" || $method_type == "change-status" || $method_type == "delete") {
                    $result = $user->user_manage($method_type, $post_data, $usr_type_via_auth["type"]);
                } else if ($method_type == "resend-verification-mail") {
                    $result = $user->user_verification_mail($post_data, $usr_type_via_auth["type"]);
                } else if ($method_type == "profile-details") {
                    $result = $user->user_profile_details($post_data);
                } else if ($method_type == "details") {
                    $result = $user->user_details($post_data);
                } else if ($method_type == "settings") {
                    $result = $user->user_settings($post_data, $usr_type_via_auth["type"]);
                } else if ($method_type == "change-image") {
                    $result = $user->user_change_image($post_data);
                }else if ($method_type == "upload-documents") {
                    $result = $user->user_upload_documents($post_data);
                }else if ($method_type == "delete-documents") {
                    $result = $user->user_delete_documents($post_data);
                }else if ($method_type == "get-inspector-documents") {
                    $result = $user->user_get_inspector_documents($post_data);
                }else if ($method_type == "upload-file") {
                    $result = $user->user_upload_image($post_data);
                }else if ($method_type == "update-password") {
                    $result = $user->user_update_password($post_data);
                } else {
                    $result = unknown_method();
                }
            } else if ($method == "price-list") {
                //echo "Method is    ".$method . "<hr/> and method type is    ".$method_type.'<hr/>';die;
                if ($method_type == null) {
                    $result = $price_list->price_list();
                } else if ($method_type == "add" || $method_type == "edit" || $method_type == "delete") {
                    $result = $price_list->price_manage($method_type, $post_data);
                } else if ($method_type == "details") {
                    $result = $price_list->price_details($post_data);
                } else if ($method_type == "upload") {
                    $result = $price_list->price_upload($post_data);
                } else if ($method_type == "download-price-list-csv") {
                    $result = $price_list->download_price_list_csv($post_data);
                } else if ($method_type == "erase-price-list") {
                    $result = $price_list->erase_price_list($post_data);
                } else if ($method_type == "price-list-manage-bulk") {
                    $result = $price_list->price_list_manage_bulk($post_data);
                } 
                
                else {
                    $result = unknown_method();
                }
                //
            } else if ($method == "profit") {
                if ($method_type == null) {
                    $result = $profit->profit_list($post_data);
                } else if ($method_type == "add" || $method_type == "edit" || $method_type == "delete") {
                    $result = $profit->profit_manage($method_type, $post_data);
                } else if ($method_type == "details") {
                    $result = $profit->profit_details($post_data);
                } else if ($method_type == "upload") {
                    $result = $profit->profit_upload($post_data);
                } else if ($method_type == "download-profit") {
                    $result = $profit->profit_download($post_data);
                } else {
                    $result = unknown_method();
                }
            } else if ($method == "work-order") {
                if ($method_type == null) {
                    $result = $work_order->work_order_list($post_data);
                } else if ($method_type == "add" || $method_type == "edit" || $method_type == "delete") {
                    $result = $work_order->work_order_manage($method_type, $post_data);
                } else if ($method_type == "details") {
                    $result = $work_order->work_order_details($post_data);
                } else if ($method_type == "upload") {
                    $result = $work_order->work_order_upload($post_data);
                } else if ($method_type == "download") {
                    $result = $work_order->work_order_download($post_data);
                } else {
                    $result = unknown_method();
                }
            } else if ($method == "debit-credit") {
                if ($method_type == null) {
                    $result = $debit_credit->debit_credit_list();
                } else if ($method_type == "add" || $method_type == "edit" || $method_type == "archive" || $method_type == "delete") {
                    $result = $debit_credit->debit_credit_manage($method_type, $post_data);
                } else if ($method_type == "details") {
                    $result = $debit_credit->debit_credit_details($post_data);
                } else if ($method_type == "download-debit-credit-csv") {
                    $result = $debit_credit->download_debit_credit_csv($post_data);
                }else {
                    $result = unknown_method();
                }
            } else if ($method == "not-found-order") {
                if ($method_type == null) {
                    $result = $not_found_order->not_found_order_list($post_data);
                } else if ($method_type == "edit") {
                    $result = $not_found_order->not_found_order_manage_bulk($post_data);
                } else {
                    $result = unknown_method();
                }
            } else if ($method == "not-paid-order") {
                if ($method_type == null) {
                    $result = $not_paid_order->not_paid_order_list($post_data);
                } else if ($method_type == "download-not-paid-csv") {
                    $result = $not_paid_order->download_not_paid_csv($post_data);
                }  else {
                    $result = unknown_method();
                }
            } else if ($method == "report") {
                if ($method_type == null) {
                    $result = $report->report_list($post_data);
                } else if ($method_type == "send-report") {
                    $result = $report->report_mail($post_data);
                } else if ($method_type == "download-report") {
                    $result = $report->report_download($post_data);
                } 
                
                else if($method_type == "credit-report"){
                    $result=$report->report_credit_only_generate($post_data);
                }
                
                else {
                    $result = unknown_method();
                }
            }else if ($method == "report-new") {
                if ($method_type == null) {
                    $result = $report_new->report_list($post_data);
                } else if ($method_type == "send-report") {
                    $result = $report_new->report_mail($post_data);
                } else if ($method_type == "download-report") {
                    $result = $report_new->report_download($post_data);
                } else if($method_type == "credit-report"){
                    $result=$report_new->report_credit_only_generate($post_data);
                }else {
                    $result = unknown_method();
                }
            }else if ($method == "note") {
                if ($method_type == null) {
                    $result = $notes->listing($post_data);
                }else if ($method_type == "save") {
                    $result = $notes->save($post_data);
                } else if($method_type == "delete"){
                    $result=$notes->delete($post_data);
                }else if($method_type == "archive"){
                    $result=$notes->archive($post_data);
                }elseif($method_type == "details"){
                    $result=$notes->get_single($post_data);
                }else if($method_type == "archived"){
                    $result=$notes->get_archived($post_data);
                }else if($method_type == "history"){
                    $result=$notes->get_history($post_data);
                }else {
                    $result = unknown_method();
                }
            }else if ($method == "not-found-report") {
                if ($method_type == null) {
                    $result = $not_found_report->not_found_report_list($post_data);
                } else if ($method_type == "download-not-found-report") {
                    $result = $not_found_report->not_found_report_download($post_data);
                } else if ($method_type == "assign-alias") {
                    $result = $not_found_report->assign_alias($post_data);
                } else {
                    $result = unknown_method();
                }
            } else {
                $result = unknown_method();
            }
        } else {
            $result = unknown_method("Invalid Auth Token");
        }
    }
}

function unknown_method($msg = "Method Not available"): array
{
    return [
        "status_code" => 404,
        "status" => false,
        "msg" => $msg
    ];
}

echo json_encode($result);