<?php
require "global.ajax.php";

switch ($_GET["op"]) {

    case 'ajaxSaveUpdate':

        $prov_id = isset($_POST["prov_id"]) ? funCheckInput($_POST["prov_id"]) : "";
        $prov_ent_id = isset($_POST["prov_ent_id"]) ? funCheckInput($_POST["prov_ent_id"]) : "";
        $prov_pos_id = isset($_POST["prov_pos_id"]) ? funCheckInput($_POST["prov_pos_id"]) : "";
        $prov_name = isset($_POST["prov_name"]) ? funCheckInput($_POST["prov_name"]) : "";
        $prov_address = isset($_POST["prov_address"]) ? funCheckInput($_POST["prov_address"]) : "";
        $prov_city = isset($_POST["prov_city"]) ? funCheckInput($_POST["prov_city"]) : "";
        $prov_state = isset($_POST["prov_state"]) ? funCheckInput($_POST["prov_state"]) : "";
        $prov_zip_code = isset($_POST["prov_zip_code"]) ? funCheckInput($_POST["prov_zip_code"]) : "";
        $prov_country = isset($_POST["prov_country"]) ? funCheckInput($_POST["prov_country"]) : "";
        $prov_phone = isset($_POST["prov_phone"]) ? funCheckInput($_POST["prov_phone"]) : "";
        $prov_phone2 = isset($_POST["prov_phone2"]) ? funCheckInput($_POST["prov_phone2"]) : "";
        $prov_email = isset($_POST["prov_email"]) ? funCheckInput($_POST["prov_email"]) : "";
        $prov_fiscal_number = isset($_POST["prov_fiscal_number"]) ? funCheckInput($_POST["prov_fiscal_number"]) : "";

        if (empty($prov_ent_id)) $prov_ent_id = $session_user_ent_id;
        if (empty($prov_pos_id)) $prov_pos_id = $session_user_pos_id;

        try {

            $pdo->beginTransaction();

            $isNew = empty(trim($prov_id));

            if ($isNew) {

                $stmt = $pdo->prepare("INSERT INTO tb_providers (
                    prov_ent_id, prov_pos_id, prov_name, prov_address,
                    prov_city, prov_state, prov_zip_code, prov_country,
                    prov_phone, prov_phone2, prov_email, prov_fiscal_number, prov_created_by)
                    VALUES (
                        :prov_ent_id, :prov_pos_id, :prov_name, :prov_address,
                        :prov_city, :prov_state, :prov_zip_code, :prov_country,
                        :prov_phone, :prov_phone2, :prov_email, :prov_fiscal_number, :created_by
                    )");

                $stmt->execute([
                    ':prov_ent_id' => $prov_ent_id,
                    ':prov_pos_id' => $prov_pos_id,
                    ':prov_name' => $prov_name,
                    ':prov_address' => $prov_address,
                    ':prov_city' => $prov_city,
                    ':prov_state' => $prov_state,
                    ':prov_zip_code' => $prov_zip_code,
                    ':prov_country' => $prov_country,
                    ':prov_phone' => $prov_phone,
                    ':prov_phone2' => $prov_phone2,
                    ':prov_email' => $prov_email,
                    ':prov_fiscal_number' => $prov_fiscal_number,
                    ':created_by' => $session_user_id
                ]);

                $response = array("message" => $translate['saved_successfully'], "status" => "success");
            } else {

                $stmt = $pdo->prepare("UPDATE tb_providers SET
                prov_name = :prov_name,
                prov_address = :prov_address,
                prov_city = :prov_city,
                prov_state = :prov_state,
                prov_zip_code = :prov_zip_code,
                prov_country = :prov_country,
                prov_phone = :prov_phone,
                prov_phone2 = :prov_phone2,
                prov_email = :prov_email,
                prov_fiscal_number = :prov_fiscal_number
                WHERE prov_id = :prov_id");

                $stmt->execute([
                    ':prov_name' => $prov_name,
                    ':prov_address' => $prov_address,
                    ':prov_city' => $prov_city,
                    ':prov_state' => $prov_state,
                    ':prov_zip_code' => $prov_zip_code,
                    ':prov_country' => $prov_country,
                    ':prov_phone' => $prov_phone,
                    ':prov_phone2' => $prov_phone2,
                    ':prov_email' => $prov_email,
                    ':prov_fiscal_number' => $prov_fiscal_number,
                    ':prov_id' => $prov_id
                ]);

                $response = array("message" => $translate['updated_successfully'], "status" => "success");
            }

            $pdo->commit();
        } catch (\RuntimeException | \InvalidArgumentException $e) {
            if ($pdo->inTransaction()) $pdo->rollBack();
            $response = array("status" => "error", "message" => "Database error. {$translate['execution_failed_please_contact_the_system_vendor_for_technical_assistance']} <br>" . $e->getMessage(), "redirect" => true);
        } catch (\PDOException $e) {
            if ($pdo->inTransaction()) $pdo->rollBack();
            $response = array("status" => "error", "message" => "Database error. {$translate['execution_failed_please_contact_the_system_vendor_for_technical_assistance']} <br>" . $e->getMessage(), "redirect" => true);
        }

        echo json_encode($response);

        break;


    case 'ajaxProvidersList':

        $ent_id = isset($_GET["ent_id"]) ? funCheckInput($_GET["ent_id"]) : "";
        $pos_id = isset($_GET["pos_id"]) ? funCheckInput($_GET["pos_id"]) : "";
        $prov_status = isset($_GET["prov_status"]) ? funCheckInput($_GET["prov_status"]) : "";

        $sql = "SELECT *
			,(select pos_name FROM tb_pos WHERE pos_id = prov_pos_id) AS pos_name
             FROM tb_providers WHERE prov_ent_id = '{$ent_id}' AND prov_pos_id = '{$pos_id}' AND prov_status = '{$prov_status}';";
        // exit($sql);
        $rows = executePDOFetchAllAPI($sql);
        echo json_encode($rows);

        break;


    case 'ajaxSwitchStatus':

        $prov_id = isset($_POST["prov_id"]) ? funCheckInput($_POST["prov_id"]) : "";
        $status = isset($_POST["status"]) ? funCheckInput($_POST["status"]) : "";

        $sql = "UPDATE tb_providers SET prov_status = '{$status}' WHERE prov_id = '$prov_id';";
        $result = executeQueryPDO($sql, $translate['successfully_executed'], $translate['execution_failed']);
        echo json_encode($result);

        break;


    case 'ajaxSelectProvider':

        $ent_id = isset($_GET["ent_id"]) ? funCheckInput($_GET["ent_id"]) : "";
        $prov_id = isset($_GET["prov_id"]) ? funCheckInput($_GET["prov_id"]) : "";

        $prov_ent_id = $ent_id ? $ent_id : $session_user_ent_id;

        $whereProvId = "";
        if (!empty($prov_id)) $whereProvId = "AND prov_id = {$prov_id}";

        $sql = "SELECT
            prov_id
            ,prov_name
            FROM tb_providers
            WHERE prov_ent_id = {$prov_ent_id}
            $whereProvId
            GROUP BY prov_id ORDER BY prov_name ASC;";

        // exit($sql);

        $rows = executePDOFetchAllAPI($sql);
        echo json_encode($rows);

        break;
}
