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

switch ($_GET["op"]) {

    case 'ajaxSaveUpdate':

        $cat_id = isset($_POST["cat_id"]) ? funCheckInput($_POST["cat_id"]) : "";
        $cat_ent_id = isset($_POST["cat_ent_id"]) ? funCheckInput($_POST["cat_ent_id"]) : "";
        $cat_pos_id = isset($_POST["cat_pos_id"]) ? funCheckInput($_POST["cat_pos_id"]) : "";
        $cat_name = isset($_POST["cat_name"]) ? funCheckInput($_POST["cat_name"]) : "";
        $cat_created_by = $session_user_id;

        $cat_name = ucwords(strtolower($cat_name));

        try {

            $pdo->beginTransaction();

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

            if ($isNew) {

                $cat_code = createTokenNumber('tb_categories', 'cat_code', '6');

                $sql = "INSERT INTO tb_categories 
                (cat_ent_id, cat_pos_id, cat_code, cat_name, cat_created_by)
                VALUES (:cat_ent_id, :cat_pos_id, :cat_code, :cat_name, :cat_created_by)";

                $stmt = $pdo->prepare($sql);
                $stmt->execute([
                    ':cat_ent_id'     => $cat_ent_id,
                    ':cat_pos_id'     => $cat_pos_id,
                    ':cat_code'       => $cat_code,
                    ':cat_name'       => $cat_name,
                    ':cat_created_by' => $cat_created_by
                ]);

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

                $sql = "UPDATE tb_categories 
                SET cat_name = :cat_name 
                WHERE cat_id = :cat_id";

                $stmt = $pdo->prepare($sql);
                $stmt->execute([
                    ':cat_name' => $cat_name,
                    ':cat_id'   => $cat_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 'ajaxCategoriesList':

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

        $sql = "SELECT *
			,(select pos_name FROM tb_pos WHERE pos_id = cat_pos_id) AS pos_name
             FROM tb_categories WHERE cat_ent_id = '{$ent_id}' AND cat_pos_id = '{$pos_id}' AND cat_status = '{$cat_status}';";
        // exit($sql);
        $rows = executePDOFetchAllAPI($sql);
        echo json_encode($rows);

        break;


    case 'ajaxSwitchStatus':

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

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

        break;


    case 'ajaxSelectList':

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

        $sql = "SELECT cat_id,cat_name FROM tb_categories WHERE cat_ent_id = '$ent_id' AND cat_pos_id = '$pos_id' ORDER BY cat_name ASC;";

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

        break;
}
