<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class RegularContract extends Model
{
    protected $table = 'regular_contract';
    protected $primaryKey = 'contract_id';

    protected $fillable = [
        'contract_qr_id',
        'user_id',
        'user_categoryid',
        'reserve_id',
        'park_id',
        'price_parkplaceid',
        'user_securitynum',
        'reserve_date',
        'contract_reserve',
        'contract_created_at',
        'contract_updated_at',
        'contract_cancelday',
        'contract_reduction',
        'contract_periods',
        'contract_periode',
        'contract_taxid',
        'billing_amount',
        'contract_payment_day',
        'contract_money',
        'refunds',
        'refunds_comment',
        'repayment_at',
        'contact_guid',
        'contact_shop_code',
        'contract_cvs_class',
        'contract_flag',
        'settlement_transaction_id',
        'contract_seal_issue',
        'seal_reissue_request',
        'contract_permission',
        'contract_cancel_flag',
        'tag_qr_flag',
        'tag_change_flag',
        'park_position',
        'ope_id',
        'contract_manual',
        'contract_notice',
        'contract_payment_number',
        'created_at',
        'updated_at'
    ];

    public static function search($inputs)
    {
        $list = self::query();
        // Sort
        if ($inputs['sort']) {
            $list->orderBy($inputs['sort'], $inputs['sort_type']);
        }
        if ($inputs['isExport']){
            $list = $list->get();
        }else{
            $list = $list->paginate(\App\Utils::item_per_page); // Utilsクラスの定数を使用
        }
        return $list;
    }

    public static function getByPk($pk)
    {
        return self::find($pk);
    }
    
    public static function deleteByPk($arr)
    {
        return self::whereIn('contract_id', $arr)->delete();
    }
    
    //TODO 定期契約ID not found in database specs

   //TODO 解約/契約不可フラグ not found in database specs
    public function userName()
    {
        return $this->belongsTo(\App\Models\User::class,'user_id','user_seq')->first();
    }
    public function getUserType()
    {
        return $this->belongsTo(\App\Models\Usertype::class,'user_categoryid','user_categoryid')->first();
    }
    public function getPark()
    {
        return $this->belongsTo(\App\Models\Park::class,'park_id','park_id')->first();
    }
    public function getPrice()
    {
        return $this->belongsTo(\App\Models\Price::class,'price_parkplaceid','price_parkplaceid')->first();
    }
//    public function getSettlement()
//    {
//        return $this->belongsTo(SettlementTransaction::class,'settlement_transaction_id','settlement_transaction_id')->first();
//    }

    public function getOpe()
    {
        return $this->belongsTo(\App\Models\Ope::class,'ope_id','ope_id')->first();
    }
}