<?php
class Mdcategory extends MY_Controller
{
    var $db;
    public function index()
    {
        echo "MD ITEM CATEGORY API";
    }

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

    function listcategory()
    {
        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 
                    itemCategoryID,
                    itemCategoryName,
                    itemCategoryRk_TypeID,
                    itemCategoryIsActive,
                    Rk_TypeID,
                    Rk_TypeCode,
                    Rk_TypeName
                    FROM item_category
                    LEFT JOIN rk_type ON itemCategoryRk_TypeID = Rk_TypeID AND Rk_TypeIsActive = 'Y'
                    WHERE itemCategoryIsActive = 'Y'
                    AND (itemCategoryName LIKE ?)
                    ORDER BY itemCategoryID DESC";
            $query = $this->db->query($sql, [$search]);
            if (!$query) {
                $this->sys_error_db("type list", $this->db);
                exit;
            }

            $rows = $query->result_array();

            $sql_tot = "SELECT count(*) as total
                FROM item_category
                LEFT JOIN rk_type ON itemCategoryRk_TypeID = Rk_TypeID AND Rk_TypeIsActive = 'Y'
                WHERE itemCategoryIsActive = 'Y'
                AND (itemCategoryName LIKE ?)";

            $query_tot = $this->db->query($sql_tot, [$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 gettype()
    {
        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
            FROM rk_type
            WHERE Rk_TypeIsActive = 'Y'
            AND (Rk_TypeCode LIKE ? OR Rk_TypeName LIKE ?)
            ORDER BY Rk_TypeName ASC";
            $query = $this->db->query($sql, [$search, $search]);
            if (!$query) {
                $this->sys_error_db("type list", $this->db);
                exit;
            }

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

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

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

            $sql = "UPDATE item_category SET 
            itemCategoryIsActive = 'N',
            itemCategoryM_UserID = ?,
            itemCategoryLastUpdated = NOW()
            WHERE itemCategoryID = ?";

            $query = $this->db->query(
                $sql,
                array($userid, $categoryid)
            );
            if (!$query) {
                $this->sys_error_db("delete category");
                exit;
            }

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

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

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

            $sql = "INSERT INTO `item_category` (
            itemCategoryName,
            itemCategoryRk_TypeID,
            itemCategoryIsActive,
            itemCategoryM_UserID,
            itemCategoryCreated
            ) VALUES (?,?,'Y',?,NOW())";

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

            if (!$qry) {
                $this->db->trans_rollback();
                $this->sys_error_db("category 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 editCategory()
    {
        try {
            if (!$this->isLogin) {
                $this->sys_error("Invalid Token");
                exit;
            }

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

            $sql = "UPDATE item_category SET
            itemCategoryName = ?,
            itemCategoryRk_TypeID = ?,
            itemCategoryM_UserID = ?,
            itemCategoryLastUpdated = NOW()
            WHERE itemCategoryID = ?";
            $qry = $this->db->query($sql, [
                $categoryname,
                $typeid,
                $userid,
                $categoryid
            ]);

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

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