<?php

namespace App\Modules\HR\Reports\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Modules\PLATFORM\Settings\Models\Branch;
use App\Modules\HR\Core\Models\Department;
use App\Modules\HR\Core\Models\Employee;
use App\Modules\HR\Payroll\Models\PayrollGroup;
use App\Modules\HR\Payroll\Models\PayrollProcess;
use App\Modules\HR\Reports\Services\ReportManager;
use App\Modules\PLATFORM\Settings\Models\PaymentMethod;

class PayrollReportController extends Controller
{
    public function payrollDetailsCrosstab(Request $request) { return $this->view('payroll-details-crosstab'); }
    public function postPayrollDetailsCrosstab(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'[0, 0, 243, 153]','landscape'); }

    public function loanAdvanceDetails(Request $request) { return $this->view('loan-advance-details'); }
    public function postLoanAdvanceDetails(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'[0, 0, 243, 153]','landscape'); }

    public function loanAdvanceSummary(Request $request) { return $this->view('loan-advance-summary'); }
    public function postLoanAdvanceSummary(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'[0, 0, 243, 153]','landscape'); }

    public function cashDenominations(Request $request) { return $this->view('cash-denominations'); }
    public function postCashDenominations(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'[0, 0, 243, 153]','landscape'); }

    public function payslip(Request $request) { return $this->view('payslip'); }
    public function postPayslip(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'[0, 0, 243, 153]','landscape'); }

    public function payrollTotalSummary(Request $request) { return $this->view('payroll-total-summary'); }
    public function postPayrollTotalSummary(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'[0, 0, 243, 153]','landscape'); }

    public function payrollSignature(Request $request) { return $this->view('payroll-signature'); }
    public function postPayrollSignature(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'[0, 0, 243, 153]','landscape'); }

    public function salaryBankFile(Request $request) { return $this->view('salary-bank-file'); }
    public function postSalaryBankFile(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'[0, 0, 243, 153]','landscape'); }

    public function epfEvemcCreator(Request $request) { return $this->view('epf-evemc-creator'); }
    public function postEpfEvemcCreator(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'[0, 0, 243, 153]','landscape'); }

    public function epfEvempCreator(Request $request) { return $this->view('epf-evemp-creator'); }
    public function postEpfEvempCreator(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'[0, 0, 243, 153]','landscape'); }

    public function etfBankFile(Request $request) { return $this->view('etf-bank-file'); }
    public function postEtfBankFile(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'[0, 0, 243, 153]','landscape'); }

    public function epfReport(Request $request) { return $this->view('epf-report'); }
    public function postEpfReport(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'[0, 0, 243, 153]','landscape'); }

    public function epfReportCrosstab(Request $request) { return $this->view('epf-report-crosstab'); }
    public function postEpfReportCrosstab(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'[0, 0, 243, 153]','landscape'); }

    public function etfReport(Request $request) { return $this->view('etf-report'); }
    public function postEtfReport(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'[0, 0, 243, 153]','landscape'); }

    public function etfReportCrosstab(Request $request) { return $this->view('etf-report-crosstab'); }
    public function postEtfReportCrosstab(Request $request , ReportManager $manager) { return $this->generate($request, $manager,'[0, 0, 243, 153]','landscape'); }


    private function view($viewName)
    {
        $branches = Branch::where('status', 1)->get();
        $departments = Department::where('status', 1)->get();
        $payrollGroup = PayrollGroup::where('status',1)->get();
        $employees = Employee::get()->map(function ($emp) {
                $name = trim($emp->first_name . ' ' . $emp->middle_name . ' ' . $emp->last_name);
                if ($emp->employee_code !== $emp->epf_no && !empty($emp->epf_no)) {
                    $emp->display_text = $emp->employee_code . ', ' . $emp->epf_no . ' - ' . $name;
                } else {
                    $emp->display_text = $emp->employee_code . ' - ' . $name;
                }
                $emp->search_text = $emp->employee_code . ' ' . $emp->epf_no . ' ' . $name;
                return $emp;
            });
        $paymentMethods = PaymentMethod::where('status',1)->get();
        $reportName = $viewName;
        $reportTitle = ucwords(str_replace('-', ' ', $viewName));

        $lastPayroll = PayrollProcess::orderBy('date_from', 'desc')->first();
        $lastPayrollDateFrom = $lastPayroll ? $lastPayroll->date_from->format('Y-m-d') : now()->startOfMonth()->format('Y-m-d');
        $lastPayrollDateTo   = $lastPayroll ? $lastPayroll->date_to->format('Y-m-d')   : now()->endOfMonth()->format('Y-m-d');

        return view('hr_reports::payroll.' . $viewName, compact(
            'branches', 'departments', 'reportName', 'reportTitle',
            'paymentMethods', 'employees', 'payrollGroup',
            'lastPayrollDateFrom', 'lastPayrollDateTo'
        ));
    }

    private function generate(Request $request, ReportManager $manager , $size,$layout)
    {
        return $manager->generate(
            $request->report_name,
            $request->all(),
            $request->format,
            $size,
            $layout
        );
    }
}
