<?php

namespace App\Models\Char;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use DB;
use App\Models\Char\CharBase;

class CharItem extends Model
{
    protected $connection = "Char";
    protected $table = "Table_CharItem";
    protected $primaryKey = "CharItemID";
    protected $dates = [];
    protected $fillable =
            [
              "Owner", "Slot", "Deleted", "ItemSerial", "StrRecordKind", "Account", "RecId", "Amount", "ClientSlot", "ClientEquip", "Durability",
              "ReinforceLevel", "SellerDbKey", "SellerName", "SellPrice", "RegisterDate", "SoldDate", "bSold", "MailDbKey", "Belong", "RemoveBelongCount",
              "Quality", "QUalityEffect", "LinkItemSerial", "Storage", "FellowDbKey", "Opened", "Toggle", "SSProperty", "SSEnabled01", "SSRecId01", "SSEnabled02",
              "SSRecId02", "SSEnabled03", "SSRecId03", "StrPlus", "DexPlus", "IntPlus", "VtlPlus", "MtlPlus", "Color", "SubColor", "ProducerName", "ROEffect01",
              "ROEffectValue01", "ROEffect02", "ROEffectValue02", "ROEffect03", "ROEffectValue03", "ROEffect04", "ROEffectValue04", "ROEffect05", "ROEffectValue05",
              "ROEffect06", "ROEffectValue06", "SealedFellowEffect01", "SealedFellowEValue01", "SealedFellowEffect02"
            ];

    public $timestamps = false;

    public static function UPDATEINVENTORYORDER($CharID = - 1)
    {
        $Characters = null;
        $totalAccounts = 0;
        if($CharID == -1){
            $Characters = CharBase::get();
        }
        else{
            $Characters = CharBase::where('DBKey', $CharID)->get();
        }

        foreach ($Characters as $Character) {
            $slot = 0;
            $Items = self::where('Owner', $Character->DBKey)->get();
            foreach ($Items as $item) {
                $item->Slot = $slot;
                $item->update();
                $slot++;
            }
            $totalAccounts++;
        }
        return $totalAccounts;
    }

    //mn = market, in = inventario
    public static function CHECKINVENTORYORDER($CharID = - 1, $StrRecordKind = 'in')
    {
        $ID = 0;
        $Data[] = [];
        $totalAccounts = 0;
        $Characters = null;
        if($CharID == -1){
            $Characters = CharBase::get();
        }
        else{
            $Characters = CharBase::where('DBKey', $CharID)->get();
        }

        foreach ($Characters as $key => $Character) {
            $check = true;
            $slot = 0;
            $Items = self::where('Owner', $Character->DBKey)->where('StrRecordKind', $StrRecordKind)->get();
            foreach ($Items as $item) {
                if($item->Slot != $slot){

                    $item->Slot = $slot;
                    $item->update();

                    $Data[$ID] = [
                        'account' => $item->Owner,
                        'Name' => $Character->Name
                    ];
                    $ID++;
                }
                $slot++;
            }
        }

        return $Data;
    }


}
