<?php

namespace App\Http\Controllers\Endline;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use Carbon\Carbon;

use Illuminate\Support\Facades\DB;

use Illuminate\Support\Facades\Storage;

use Illuminate\Support\Str;

use Illuminate\Support\Facades\Http;

use Illuminate\Support\Facades\Log;

use Illuminate\Support\Facades\Mail;

use Illuminate\Support\Facades\Session;

use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;

class EndlineController extends Controller
{
    public function endline()
    {
        $lines = DB::connection('sqlsrv')->select("
            SELECT 
                cc_no,
                output_type,
                group_name,
                date_from,
                date_to,
                unit_id,
                line_no,
                allocated_line_id
            FROM CHENNAI_ERP.dbo.GP_line_groups
            WHERE allocated_line_id != ''
        ");

        //dd($data);
        return view('Quality.Endline', compact('lines'));
    }

    public function endline_tablepage()
    {
        return view('Quality.Endline_tablepage');
    }

    Public function endlinetablepage()
    {
       return view('Quality.Endline_tablepage');
    }

    public function endline_selection()
    {
        return view('Quality.Endline_selection');
    }

    public function endline_defectselection()
    {
        return view('Quality.Endline_defectselection');
    }

    public function endline_finalselection()
    {
        return view('Quality.Endline_finalselection');
    }

    public function checkerDetails(Request $request)
    {
        $request->validate([
            'employee_id' => 'required|numeric'
        ]);

        $employee = DB::connection('sqlsrv')->select(
            "SELECT 
                Emplno   AS employee_id,
                Name     AS name,
                Deptcode AS department
            FROM VAAHINI_ERP_GAINUP.dbo.employeemas
            WHERE Emplno = ?",
            [$request->employee_id]
        );

        if (!$employee) {
            return response()->json([
                'success' => false,
                'message' => 'Employee not found'
            ]);
        }

        Session::put('endline_checker', [
            'employee_id' => $employee[0]->employee_id ?? null,
            'name' => $employee[0]->name ?? null,
            'department' => $employee[0]->department ?? null,
        ]);

        return response()->json([
            'success' => true,
            'data' => $employee[0]
        ]);
    }

}