<?php

class Product extends basicFunction
{
    public function create($category_id, $subcategory_id, $status, $type, $includes, $balance, $price)
    {
        $vals = [$category_id, $subcategory_id, $status, $type, $includes, $balance, $price];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(1);
        }


        $unique_id = $this->rand_id();
        $date_created = $this->todayDate();
        $query = "INSERT INTO $this->product_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(), $subcategory_id) . "',
    '" . mysqli_real_escape_string($this->connectDb(), $status) . "',
    '" . mysqli_real_escape_string($this->connectDb(), $type) . "',
    '" . mysqli_real_escape_string($this->connectDb(), $includes) . "',
    '" . mysqli_real_escape_string($this->connectDb(), $balance) . "',
    '" . mysqli_real_escape_string($this->connectDb(), $price) . "',
    '" . mysqli_real_escape_string($this->connectDb(), null) . "',
    '" . 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('Product');
    }

    public function update($id, $category_id, $subcategory_id, $status, $type, $includes, $balance, $price)
    {
        $vals = [$category_id, $subcategory_id, $status, $type, $includes, $balance, $price];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(1);
        }


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

        $fields = ['status', 'type', 'includes', 'balance', 'price'];
        $values = [$status, $type, $includes, $balance, $price];
        $update = $this->update_tbs($this->product_tb, $fields, $values, $id);
        if ($update['error'] == 1) {
            return $update;
        }

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


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

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

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

        return $this->dltSuccessMsg();
    }

    public function upload_log($file, $id)
    {
        $vals = [$file];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(12);
        }
        $product = $this->selectWhereDlt($this->product_tb, 'unique_id', $id);
        if (count($product) < 1) {
            return $this->errorMsgs(15);
        }
        if ($file !== null) {

            $chk_extension = $this->chk_file_extension($this->valid_img_extensions, $file);
            if (!$chk_extension) {
                return $this->errorMsgs(12);
            }
            $file_name = $this->rand_id();
            $upload_img = $this->file_upload_custom($file, '../to/assets/logs/', $this->file_max_size, $file_name);
            if ($upload_img['error'] == 1) {
                return $upload_img;
            }
            $update_image = $this->update_where($this->product_tb, ['file_name'], [$upload_img['file_name']], 'unique_id', $id);
            if ($update_image['error'] == 1) {
                return $update_image;
            } else {
                unlink('../to/assets/logs/' . $product[0]['file_name']);

                return $this->successMsg2('File Upload');
            }
        } else {
            return $this->errorMsgs(15);
        }
    }
}

$product = new Product();
