<?php
class Mdtype extends MY_Controller
{
    var $db;
    public function index()
    {
        echo "MD RK TYPE API";
    }

    public function __construct()
    {
        parent::__construct();
    }

    function listtype()
    {
        try {
            if (!$this->isLogin) {
                $this->sys_error("Invalid Token");
                exit;
            }

            $prm = $this->sys_input;

            $search = "";
            if (isset($prm["search"])) {
                $search = trim($prm["search"]);
                if ($search != "") {
                    $search = "%" . $prm["search"] . "%";
                } else {
                    $search = "%%";
                }
            }

            $sql = "SELECT 
                    Rk_TypeID,
                    Rk_TypeCode,
                    Rk_TypeName,
                    Rk_TypeIsActive,
                    '' as rownumber
                    FROM rk_type
                    WHERE Rk_TypeIsActive = 'Y'
                    AND (Rk_TypeCode LIKE ? OR Rk_TypeName LIKE ?)
                    ORDER BY Rk_TypeID DESC";
            $query = $this->db->query($sql, [$search, $search]);
            if (!$query) {
                $this->sys_error_db("type list", $this->db);
                exit;
            }

            $rows = $query->result_array();

            $sql_tot = "SELECT count(Rk_TypeID) as total
                FROM rk_type
                WHERE Rk_TypeIsActive = 'Y'
                AND (Rk_TypeCode LIKE ? OR Rk_TypeName LIKE ?)";

            $query_tot = $this->db->query($sql_tot, [$search, $search]);
            $total_list = 0;
            if ($query_tot) {
                $total_list = $query_tot->result_array()[0]['total'];
            } else {
                $this->sys_error_db("type count", $this->db);
                exit;
            }

            foreach ($rows as $k => $v) {
                $rows[$k]['rownumber'] = $k + 1;
            }

            $result = array(
                "total" => $total_list,
                "records" => $rows
            );
            $this->sys_ok($result);
        } catch (Exception $exc) {
            $message = $exc->getMessage();
            $this->sys_error($message);
        }
    }

    function deleteType()
    {
        try {
            if (!$this->isLogin) {
                $this->sys_error("Invalid Token");
                exit;
            }

            $prm = $this->sys_input;
            $userid = $this->sys_user['M_UserID'];
            $typeid = $prm['typeid'];

            $sql = "UPDATE rk_type SET 
            Rk_TypeIsActive = 'N',
            Rk_TypeM_UserID = ?,
            Rk_TypeLastUpdated = now()
            WHERE Rk_TypeID = ?";

            $query = $this->db->query(
                $sql,
                array($userid, $typeid)
            );
            if (!$query) {
                $this->sys_error_db("update type");
                exit;
            }

            $result = array(
                "total" => 1,
                "data" => array('status' => 'OK')
            );
            $this->sys_ok($result);
        } catch (Exception $exc) {
            $message = $exc->getMessage();
            $this->sys_error($message);
        }
    }

    function addType()
    {
        try {
            if (!$this->isLogin) {
                $this->sys_error("Invalid Token");
                exit;
            }

            $this->db->trans_begin();
            $prm = $this->sys_input;
            $userid = $this->sys_user['M_UserID'];
            $typecode = $prm["typecode"];
            $typename = $prm["typename"];

            $sql = "INSERT INTO `rk_type` (
            Rk_TypeCode,
            Rk_TypeName,
            Rk_TypeIsActive,
            Rk_TypeM_UserID,
            Rk_TypeCreated
            ) VALUES (?,?,'Y',?,NOW())";

            $qry = $this->db->query($sql, [
                $typecode,
                $typename,
                $userid
            ]);

            if (!$qry) {
                $this->db->trans_rollback();
                $this->sys_error_db("type insert error", $this->db);
                exit;
            }

            $this->db->trans_commit();
            $result = array("total" => 1, "records" => array("xid" => 0));
            $this->sys_ok($result);
        } catch (Exception $exc) {
            $message = $exc->getMessage();
            $this->sys_error($message);
        }
    }

    function editType()
    {
        try {
            if (!$this->isLogin) {
                $this->sys_error("Invalid Token");
                exit;
            }

            $this->db->trans_begin();
            $prm = $this->sys_input;
            $userid = $this->sys_user['M_UserID'];
            $typecode = $prm["typecode"];
            $typename = $prm["typename"];
            $typeid = $prm["typeid"];

            $sql = "UPDATE rk_type SET
            Rk_TypeCode = ?,
            Rk_TypeName = ?,
            Rk_TypeM_UserID = ?,
            Rk_TypeLastUpdated = NOW()
            WHERE Rk_TypeID = ?";
            $qry = $this->db->query($sql, [
                $typecode,
                $typename,
                $userid,
                $typeid
            ]);

            if (!$qry) {
                $this->db->trans_rollback();
                $this->sys_error_db("type error edit", $this->db);
                exit;
            }

            $this->db->trans_commit();
            $result = array("total" => 1, "records" => array("typeID" => $typeid));
            $this->sys_ok($result);
        } catch (Exception $exc) {
            $message = $exc->getMessage();
            $this->sys_error($message);
        }
    }
}
