<?php

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


        $name_exist = $this->selectWhereDlt($this->subcategory_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->subcategory_tb VALUES (null,
    '" . mysqli_real_escape_string($this->connectDb(), $unique_id) . "',
    '" . mysqli_real_escape_string($this->connectDb(), $category_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('SubCategory');
    }

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

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

        $name_exist = $this->selectWhere($this->subcategory_tb, 'name', $name, 'category_id', $category_id);
        if(count($name_exist) > 0 && $name_exist[0]['unique_id'] !== $id){
            return $this->errorMsgs(16);
        }

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

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


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

        //  $product_check = $this->selectWhere($this->product_tb, 'subcategory_id', $id);
        //  if (count($product_check) > 0) {
        //      return ['error' => 1, 'msg' => 'Error! Subcategory already has a product.'];
        //  }
 
        //  $purchase_check = $this->selectWhere($this->purchase_tb, 'subcategory_id', $id);
        //  if (count($purchase_check) > 0) {
        //      return ['error' => 1, 'msg' => 'Error! Purchase has been created for this subcategory.'];
        //  }

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

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

$subCategory = new SubCategory();
