<?php

namespace App\Models\Char;

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

class CharCashItem_OutputBox extends Model
{
    protected $connection = "Char";
    protected $table = "Table_CharCashItem_OutputBox";
    protected $primaryKey = "ItemDBIndex";
    protected $dates = ["CreateDate", "DeleteDate"];
    protected $fillable =
        [
            "ItemDBIndex",
            "Owner",
            "Kind",
            "RecId",
            "Amount",
            "strChargeNo",
            "Deleted",
            "CreateDate",
            "DeleteDate",
            "GaveCharName",
            "Confirm",
            "Period",
            "Price",
            "evPType",
            "Comment"
        ];

    public $timestamps = false;

    public static function SENDITEMOUTPUTBOX($request)
    {

        if ($request['Owner'] == "") {

            $chars = CharBase::get();
            foreach ($chars as $char) {
                if ($char->Online == 1) {
                    $send = new CharCashItem_OutputBox();
                    $send->Owner = $char->DBKey;
                    $send->Kind = $request['Kind'];
                    $send->RecId = trim(strtolower($request['RecId']));
                    $send->Amount = $request['Amount'];
                    $send->strChargeNo = "*";
                    $send->Deleted = 0;
                    $send->CreateDate = now();
                    $send->DeleteDate = now();
                    $send->GaveCharName = "*";
                    $send->Confirm = 0;
                    $send->Period = $request['Period'];
                    $send->Price = 0;
                    $send->evPType = $request['evPType'];
                    $send->Comment = $request['Comment'];
                    $send->save();
                }
            }

        } else {
            $send = new CharCashItem_OutputBox();
            $send->Owner = $request['Owner'];
            $send->Kind = $request['Kind'];
            $send->RecId = trim(strtolower($request['RecId']));
            $send->Amount = $request['Amount'];
            $send->strChargeNo = "*";
            $send->Deleted = 0;
            $send->CreateDate = now();
            $send->DeleteDate = now();
            $send->GaveCharName = "*";
            $send->Confirm = 0;
            $send->Period = $request['Period'];
            $send->Price = 0;
            $send->evPType = $request['evPType'];
            $send->Comment = $request['Comment'];
            $send->save();
        }
    }
}
