<?php

class Category extends basicFunction
{
    public function create($name)
    {
        $vals = [$name];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(1);
        }


        $name_exist = $this->selectWhereDlt($this->category_tb, 'name', $name);
        if (count($name_exist) > 0) {
            return $this->errorMsgs(16);
        }

        $unique_id = $this->rand_id();
        $date_created = $this->todayDate();
        $query = "INSERT INTO $this->category_tb VALUES (null,
    '" . mysqli_real_escape_string($this->connectDb(), $unique_id) . "',
    '" . mysqli_real_escape_string($this->connectDb(), $name) . "',
    '" . mysqli_real_escape_string($this->connectDb(), 0) . "',
    '" . mysqli_real_escape_string($this->connectDb(), $date_created) . "',
    '" . mysqli_real_escape_string($this->connectDb(), $date_created) . "') ";
        mysqli_close($this->connectDb());
        if (!$this->sqlQuery($query)) {
            return $this->errorMsgs(14);
        }
        return $this->successMsg('Category');
    }

    public function update($id, $name)
    {
        $vals = [$name];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(1);
        }

        // $purchase_check = $this->selectWhere($this->purchase_tb, 'category_id', $id);
        // if (count($purchase_check) > 0) {
        //     return ['error' => 1, 'msg' => 'Error! Purchase has been created for this category.'];
        // }

        $name_exist = $this->selectWhere($this->category_tb, 'name', $name);
        if (count($name_exist) > 0) {
            return $this->errorMsgs(16);
        }

        $fields = ['name'];
        $values = [$name];
        $update = $this->update_tbs($this->category_tb, $fields, $values, $id);
        if ($update['error'] == 1) {
            return $update;
        }

        return $this->successMsg3('Category Updated');
    }


    public function delete($id)
    {
        $vals = [$id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }

        // $subcategory_check = $this->selectWhere($this->subcategory_tb, 'category_id', $id);
        // if (count($subcategory_check) > 0) {
        //     return ['error' => 1, 'msg' => 'Error! Category already has a sub category.'];
        // }

        // $purchase_check = $this->selectWhere($this->purchase_tb, 'category_id', $id);
        // if (count($purchase_check) > 0) {
        //     return ['error' => 1, 'msg' => 'Error! Purchase has been created for this category.'];
        // }

        $fields = ['is_deleted'];
        $values = ['1'];
        $category = $this->update_tbs($this->category_tb, $fields, $values, $id);
        if ($category['error'] == 1) {
            return $category;
        }

        $fields = ['is_deleted'];
        $values = ['1'];
        $subcategory = $this->update_where($this->subcategory_tb, $fields, $values, 'category_id', $id);
        if ($subcategory['error'] == 1) {
            return $subcategory;
        }

        // $fields = ['is_deleted'];
        // $values = ['1'];
        // $product = $this->update_where($this->product_tb, $fields, $values, 'category_id', $id);
        // if ($product['error'] == 1) {
        //     return $product;
        // }
        return $this->dltSuccessMsg();
    }

    public function get_sub_categories($category_id)
    {
        $vals = [$category_id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }
        $subcategory = $this->selectWhereDlt($this->subcategory_tb, 'category_id', $category_id);
        return $subcategory;
    }
}

$category = new Category();
