<?php

namespace App\Filament\Resources\ProductionOrderResource\Pages;

use App\Filament\Resources\ProductionOrderResource;
use App\Models\Item;
use App\Models\Plant;
use App\Models\ProductionOrder;
use Filament\Facades\Filament;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\CreateRecord;

class CreateProductionOrder extends CreateRecord
{
    protected static string $resource = ProductionOrderResource::class;

    public function saveProductionOrder()
    {
        $plantId = trim($this->form->getState()['plant_id'] ?? '') ?? null;

        $itemId = trim($this->form->getState()['item_id'] ?? '') ?? null;

        $quantity = trim($this->form->getState()['quantity'] ?? '') ?? null;

        $startDate = trim($this->form->getState()['start_date'] ?? '') ?? null;

        $endDate = trim($this->form->getState()['end_date'] ?? '') ?? null;

        $pOrder = trim($this->form->getState()['production_order'] ?? '') ?? null;

        $fSerNo = trim($this->form->getState()['from_serial_number'] ?? '') ?? null;

        $tSerNo = trim($this->form->getState()['to_serial_number'] ?? '') ?? null;

        $createdBy = trim($this->form->getState()['created_by'] ?? '') ?? Filament::auth()->user()?->name;

        $updatedBy = trim($this->form->getState()['updated_by'] ?? '') ?? Filament::auth()->user()?->name;

        if (empty($plantId)) {
            Notification::make()
                ->title('Plant name cannot be empty!')
                ->danger()
                ->send();

            return;
        } elseif (empty($itemId)) {
            Notification::make()
                ->title('Item code cannot be empty!')
                ->danger()
                ->send();

            return;
        } elseif (empty($quantity)) {
            Notification::make()
                ->title('Quantity cannot be empty!')
                ->danger()
                ->send();

            return;
        } elseif (empty($startDate)) {
            Notification::make()
                ->title('Please choose a Start Date!')
                ->danger()
                ->send();

            return;
        } elseif (empty($endDate)) {
            Notification::make()
                ->title('Please choose a End Date!')
                ->danger()
                ->send();

            return;
        } elseif (empty($pOrder)) {
            Notification::make()
                ->title('Production order cannot be empty!')
                ->danger()
                ->send();

            return;
        } elseif (empty($fSerNo)) {
            Notification::make()
                ->title('From serial number cannot be empty!')
                ->danger()
                ->send();

            return;
        } elseif (empty($tSerNo)) {
            Notification::make()
                ->title('To serial number cannot be empty!')
                ->danger()
                ->send();

            return;
        } elseif (empty($createdBy)) {
            Notification::make()
                ->title('Created By cannot be empty!')
                ->danger()
                ->send();

            return;
        } elseif (empty($updatedBy)) {
            Notification::make()
                ->title('Updated By cannot be empty!')
                ->danger()
                ->send();

            return;
        }

        $dupProd = ProductionOrder::where('plant_id', $plantId)->where('production_order', $pOrder)->first();
        if ($dupProd) {
            Notification::make()
                ->title("Production Order '{$pOrder}' already exists in database!")
                ->danger()
                ->send();

            return;
        }

        $insert = ProductionOrder::create([
            'plant_id' => $plantId ?? null,
            'item_id' => $itemId ?? null,
            'quantity' => $quantity ?? null,
            'start_date' => $startDate ?? null,
            'end_date' => $endDate ?? null,
            'production_order' => $pOrder ?? null,
            'from_serial_number' => $fSerNo ?? null,
            'to_serial_number' => $tSerNo ?? null,
            'created_by' => $createdBy ?? null,
            'updated_by' => $updatedBy ?? null,
        ]);

        if ($insert) {
            Notification::make()
                ->title("Production Order '{$pOrder}' saved successfully.")
                ->success()
                ->send();

            $this->form->fill([
                'plant_id' => $plantId,
                'item_id' => $itemId,
                'quantity' => $quantity,
                'start_date' => $startDate,
                'end_date' => $endDate,
                'production_order' => $pOrder,
                'from_serial_number' => $fSerNo,
                'to_serial_number' => $tSerNo,
                'show_extra_fields' => true,
                'created_by' => $createdBy ?? null,
                'updated_by' => $updatedBy ?? null,
            ]);

            return;

        } else {
            Notification::make()
                ->title("Failed to save Production Order '{$pOrder}'!")
                ->danger()
                ->send();
            $this->form->fill([
                'plant_id' => $plantId,
                'item_id' => $itemId,
                'quantity' => null,
                'start_date' => null,
                'end_date' => null,
                'production_order' => null,
                'from_serial_number' => null,
                'to_serial_number' => null,
                'created_by' => $createdBy ?? null,
                'updated_by' => $updatedBy ?? null,
            ]);

            return;
        }
    }

    public function printProductionOrder()
    {
        $pOrder = trim($this->form->getState()['production_order'] ?? '') ?? null;

        $plantId = trim($this->form->getState()['plant_id'] ?? '') ?? null;

        if (empty($plantId)) {
            Notification::make()
                ->title('Plant name cannot be empty!')
                ->danger()
                ->send();

            return;
        } elseif (empty($pOrder)) {
            Notification::make()
                ->title('Production order cannot be empty!')
                ->danger()
                ->send();

            return;
        }

        $pOrderExists = ProductionOrder::where('plant_id', $plantId)->where('production_order', $pOrder)->first();

        if (! $pOrderExists) {
            Notification::make()
                ->title("Production Order '{$pOrder}' does not exist to get print!")
                ->danger()
                ->send();

            return;
        } else {
            return redirect()->route('production-orders.print', ['production_order' => $pOrder]);
        }
    }

    public function printItemSerial()
    {
        $pOrder = trim($this->form->getState()['production_order'] ?? '') ?? null;

        $plantId = trim($this->form->getState()['plant_id'] ?? '') ?? null;

        $plantCode = Plant::where('id', $plantId)->value('code');

        if (empty($plantId)) {
            Notification::make()
                ->title('Plant name cannot be empty!')
                ->danger()
                ->send();
            return;
        } elseif (empty($pOrder)) {
            Notification::make()
                ->title('Production order cannot be empty!')
                ->danger()
                ->send();
            return;
        }

        $pOrderExists = ProductionOrder::where('plant_id', $plantId)->where('production_order', $pOrder)->first();

        if (! $pOrderExists) {
            Notification::make()
                ->title("Production Order '{$pOrder}' does not exist to get print!")
                ->danger()
                ->send();
            return;
        } else {
            return redirect()->route('production-orders.printItemSerial', ['production_order' => $pOrder, 'plant_code' => $plantCode]);
        }
    }

    public function printPanel()
    {
        $pOrder = trim($this->form->getState()['production_order'] ?? '') ?? null;

        $plantId = trim($this->form->getState()['plant_id'] ?? '') ?? null;

        $plantCode = Plant::where('id', $plantId)->value('code');

        if (empty($plantId)) {
            Notification::make()
                ->title('Plant name cannot be empty!')
                ->danger()
                ->send();

            return;
        } elseif (empty($pOrder)) {
            Notification::make()
                ->title('Production order cannot be empty!')
                ->danger()
                ->send();
            return;
        }

        $pOrderExists = ProductionOrder::where('plant_id', $plantId)->where('production_order', $pOrder)->first();

        if (! $pOrderExists) {
            Notification::make()
                ->title("Production Order '{$pOrder}' does not exist to get print!")
                ->danger()
                ->send();

            return;
        } else {
            return redirect()->route('production-orders.printpanel', ['production_order' => $pOrder, 'plant_code' => $plantCode]);
        }
    }

    public function exportSerialNo(){
        $pOrder = trim($this->form->getState()['production_order'] ?? '') ?? null;

        $plantId = trim($this->form->getState()['plant_id'] ?? '') ?? null;

        $itemId = trim($this->form->getState()['item_id'] ?? '') ?? null;

        $fromSerNo = trim($this->form->getState()['from_serial_number'] ?? '') ?? null;

        $toSerNo = trim($this->form->getState()['to_serial_number'] ?? '') ?? null;

        $plantCode = Plant::where('id', $plantId)->value('code');

        // $itemCode = Item::where('id', $itemId)->value('code');

        if (empty($plantId)) {
            Notification::make()
                ->title('Plant name cannot be empty!')
                ->danger()
                ->send();

            return;
        } elseif (empty($pOrder)) {
            Notification::make()
                ->title('Production order cannot be empty!')
                ->danger()
                ->send();
            return;
        }
        elseif (empty($fromSerNo)) {
            Notification::make()
                ->title('From serial number cannot be empty!')
                ->danger()
                ->send();
            return;
        }
        elseif (empty($toSerNo)) {
            Notification::make()
                ->title('To serial number cannot be empty!')
                ->danger()
                ->send();
            return;
        }

        $pOrderExists = ProductionOrder::where('plant_id', $plantId)->where('production_order', $pOrder)->first();

        if (! $pOrderExists) {
            Notification::make()
                ->title("Production Order '{$pOrder}' does not exist to get print!")
                ->danger()
                ->send();

            return;
        } else {
            return redirect()->route('production-orders.exportSerial', ['production_order' => $pOrder, 'plant_code' => $plantCode, 'from_serial_no' => $fromSerNo, 'to_serial_no' => $toSerNo]);
        }
    }

    protected function getFormActions(): array
    {
        return [];
    }
}
