<?php

namespace App\Http\Livewire\Pages\ServerLicense;

use GuzzleHttp\Exception\RequestException;
use App\Models\ConfSmartupdate;
use App\Models\MasterPhpVer;
use App\Models\TabPatchFile;
use Livewire\Component;
use GuzzleHttp\Client;
use App\Models\ConfServerLicense;
use Illuminate\Support\Facades\DB;
class ServerLicenseCreate extends Component
{

    public $phpVersions = [];
    public $teams = [];
    public $sizings = [];
    public $customerTypes = [];
    public $dbTypes = [];
    public $ownerTypes = [];
    
    public $serverKey;
    public $teamId;
    public $customerSize;
    public $customerType;
    public $company;
    public $branch;
    public $status;
    public $licenseDate;
    public $phpVersionId;
    public $currentVersion;
    public $installDate;
    public $installVersion;
    public $databaseType;
    public $ownerType;
    public $hscodeMethod;
    public $customerUrl;
    public $contact;
    public $contactCustomerName;
   

    // protected $rules = [
    //     'SNKEY' => 'required|string|max:255',
    //     'NBTEAM' => 'required|integer',
    //     'CUSTOMERSIZE' => 'required|integer',
    //     'CUSTOMERTYPE' => 'required|integer',
    //     'COMPANY' => 'required|string|max:255',
    //     'BRANCH' => 'nullable|string|max:255',
    //     'STATUS' => 'required|in:Y,N',
    //     'LICENSEDATE' => 'required|date',
    //     'PHP_VERSION_ID' => 'required|integer',
    //     'CUR_VERSION' => 'required|string|max:255',
    //     'INSTALL_DATE' => 'required|date',
    //     'INSTALL_VERSION' => 'required|string|max:255',
    //     'DATABASETYPE' => 'required|integer',
    //     'OWNERTYPE' => 'required|integer',
    //     'HSCODEMETHOD' => 'required|integer|in:1,2',
    //     'CUSTOMERURL' => 'required|string|max:255',
    //     'CONTACT' => 'required|string|max:255',
    //     'CONTACTCUSTOMERNAME' => 'required|string|max:255',
    // ];
    public function mount()
    {
        $this->phpVersions  = DB::table('master_php_ver')
            ->orderBy('id', 'asc')
            ->get();

        $this->teams = DB::table('master_team')
            ->orderBy('id', 'asc')
            ->get();

        $this->sizings = DB::table('master_size')
            ->orderBy('id', 'asc')
            ->get();

        $this->customerTypes  = DB::table('master_sizeType')
            ->orderBy('id', 'asc')
            ->get();

        $this->dbTypes  = DB::table('master_dbtype')
            ->orderBy('id', 'asc')
            ->get();

        $this->ownerTypes  = DB::table('conf_server_ownertype')
            ->orderBy('owntypeid', 'asc')
            ->get();
    }

   
    public function save()
    {
        // $this->validate();

        ConfServerLicense::create([
            'SNKEY' => $this->serverKey,
            'NBTEAM' => $this->teamId,
            'CUSTOMERSIZE' => $this->customerSize,
            'CUSTOMERTYPE' => $this->customerType,
            'COMPANY' => $this->company,
            'BRANCH' => $this->branch,
            'STATUS' => $this->status,
            'LICENSEDATE' => $this->licenseDate,
            'PHP_VERSION_ID' => $this->phpVersionId,
            'CUR_VERSION' => $this->currentVersion,
            'INSTALL_DATE' => $this->installDate,
            'INSTALL_VERSION' => $this->installVersion,
            'DATABASETYPE' => $this->databaseType,
            'OWNERTYPE' => $this->ownerType,
            'HSCODEMETHOD' => $this->hscodeMethod,
            'CUSTOMERURL' => $this->customerUrl,
            'CONTACT' => $this->contact,
            'CONTACTCUSTOMERNAME' => $this->contactCustomerName,
        ]);
    
        return redirect()->route('server-license.index')->with('message', 'Server License saved successfully.');
    }
    

    public function loadPage($page)
    {
        $this->emit('menuChanged', $page);
    }
    public function render()
    {
        return view('livewire.pages.server-license.server-license-create');
    }
}
