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

switch ($_GET["op"]) {

    case 'ajaxSaveUpdate':

        $pay_type_id = isset($_POST["pay_type_id"]) ? funCheckInput($_POST["pay_type_id"]) : "";
        $pay_type_ent_id = isset($_POST["pay_type_ent_id"]) ? funCheckInput($_POST["pay_type_ent_id"]) : "";
        $pay_type_pos_id = isset($_POST["pay_type_pos_id"]) ? funCheckInput($_POST["pay_type_pos_id"]) : "";
        $pay_type_name = isset($_POST["pay_type_name"]) ? funCheckInput($_POST["pay_type_name"]) : "";
        $pay_type_name = mb_strtoupper($pay_type_name);
        $pay_type_created_by = $session_user_id;

        if (empty($pay_type_ent_id)) $pay_type_ent_id = $session_user_ent_id;
        if (empty($pay_type_pos_id)) $pay_type_pos_id = $session_user_pos_id;

        try {

            $pdo->beginTransaction();

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

            if ($isNew) {

                $sql = "INSERT INTO tb_payments_type 
                (pay_type_ent_id, pay_type_pos_id, pay_type_created_by, pay_type_name)
                VALUES (:ent_id, :pos_id, :created_by, :name)";

                $stmt = $pdo->prepare($sql);
                $stmt->execute([
                    ':ent_id'     => $pay_type_ent_id,
                    ':pos_id'     => $pay_type_pos_id,
                    ':created_by' => $pay_type_created_by,
                    ':name'       => $pay_type_name
                ]);

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

                $sql = "UPDATE tb_payments_type 
                SET pay_type_name = :name 
                WHERE pay_type_id = :id";

                $stmt = $pdo->prepare($sql);
                $stmt->execute([
                    ':name' => $pay_type_name,
                    ':id'   => $pay_type_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 'ajaxPaymentTypeList':

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

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

        break;


    case 'ajaxSelectPaymentType':

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

        $customWhere = "";
        if ($fromSection == "sell") $customWhere = "AND pay_type_status = 'Enabled'";
        if ($fromSection == "sales-report") $customWhere = "";

        $sql = "SELECT pay_type_id,pay_type_name FROM tb_payments_type WHERE pay_type_ent_id = '$ent_id' AND pay_type_pos_id = '$pos_id' $customWhere;";
        // exit($sql);

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


    case 'ajaxSwitchStatus':

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

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

        break;
}
