<?php

namespace App\Models;

use App\Models\AdminModel;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use DB;
use Illuminate\Support\Facades\Log;

class ProductAttributeModel extends AdminModel
{
    public function __construct()
    {
        $this->table               = 'product_attribute';
        $this->folderUpload        = 'product_attribute' ;
        $this->fieldSearchAccepted = ['id'];
        $this->crudNotAccepted     = ['_token'];
    }

    public function listItems($params = null, $options = null)
    {
        $result = null;

        if ($options['task'] == "admin-list-items-by-product") {
            $query = $this->select('id', 'product_id', 'attribute_id', 'value', 'value_price', 'default', 'status', 'ordering', 'created', 'created_by', 'modified', 'modified_by', DB::raw("CONCAT(`attribute_id`,`value`) AS attribute_id_value"));
            $query->orderBy('ordering', 'asc')
            ->where('product_id', $params['id']);
            $result =  $query->get()->toArray();
        }

        if ($options['task'] == "admin-list-items-by-attribute-id") {
            $query = $this->select('id', 'product_id', 'attribute_id', 'value', 'value_price', 'default', 'status', 'ordering', 'created', 'created_by', 'modified', 'modified_by', DB::raw("CONCAT(`attribute_id`,`value`) AS attribute_id_value"));
            $query->orderBy('ordering', 'asc')
            ->where('attribute_id', $params['attribute_id'])
            ->where('product_id', $params['id']);
            $result =  $query->get()->toArray();
        }

        return $result;
    }

    public function getItem($params = null, $options = null)
    {
        $result = null;

        if ($options['task'] == 'get-item') {
            $result = self::select('id', 'product_id', 'attribute_id', 'value', 'status', 'created', 'created_by', 'modified', 'modified_by', DB::raw("CONCAT(`attribute_id`,`value`) AS attribute_id_value"))->where('product_id', $params['product_id'])
            ->where('attribute_id', $params['attribute_id'])
            ->where('value', $params['value'])
            ->first();
        }

        return $result;
    }


    public function saveItem($params = null, $options = null)
    {
        if ($options['task'] == 'add-item') {
            $params['created_by'] = "hailan";
            $params['created']    = date('Y-m-d');
            self::insert($this->prepareParams($params));
        }

        if ($options['task'] == 'edit-item') {
            $params['modified_by']   = "hailan";
            $params['modified']      = date('Y-m-d');
            self::where('id', $params['id'])->update($this->prepareParams($params));
        }
    }

    public function deleteItem($params = null, $options = null)
    {
        if ($options['task'] == 'delete-item') {
            self::where('id', $params['id'])->delete();
        }
        if ($options['task'] == 'delete-items-by-product-id') {
            self::where('product_id', $params['id'])->delete();
        }
        if ($options['task'] == 'delete-items-not-exists') {
            // delete-items by product_id, attribute_id, value
            self::where('product_id', $params['product_id'])
            ->where('attribute_id', $params['attribute_id'])
            ->where('value', $params['value'])->delete();
        }
    }
}
