<?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 = 1;
    public $customerUrl;
    public $contact;
    public $contactCustomerName;

    protected $rules = [
        'serverKey' => 'required|string|max:255',
        'teamId' => 'required|integer',
        'customerSize' => 'required|integer',
        'customerTypes' => 'required',
        'company' => 'required|string|max:255',
        'branch' => 'nullable|string|max:255',
        'status' => 'required|in:Y,N',
        'licenseDate' => 'nullable|date',
        'phpVersionId' => 'required|integer',
        'currentVersion' => 'nullable|string|max:255',
        'installDate' => 'nullable|date',
        'installVersion' => 'nullable|string|max:255',
        'databaseType' => 'required',
        'ownerType' => 'nullable',
        // 'hscodeMethod' => 'required|integer|in:1,2',
        'customerUrl' => 'nullable|string|max:255',
        'contact' => 'nullable|string|max:255',
        'contactCustomerName' => 'nullable|string|max:255',
    ];
    public function mount()
    {
        $this->phpVersionId  = DB::table('master_php_ver')
            ->orderBy('id', 'asc')
            ->first()->id;

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

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

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

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

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


    public function save()
    {
        $this->validate();
        // dd($this->teamId);
        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()
    {
        $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();

        return view('livewire.pages.server-license.server-license-create');
    }
}
