<?php

namespace Modules\Target\Exports;

use App\User;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\Exportable;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithStyles;

class ChartTargetExport implements FromView, WithColumnWidths, WithStyles
{

    /**
    * @return \Illuminate\Support\Collection
    */

    public function __construct($employees,$products,$year,$months)
    {
        $this->employees = $employees;
        $this->products = $products;
        $this->year = $year;
        $this->months = $months;
    }


     public function view(): View
    {
        return view('admin.targets.excel.chart', [
            'employees' => $this->employees,
            'products' => $this->products,
            'year' => $this->year,
            'months' => $this->months,
        ]);
    }

    public function columnWidths(): array
    {
        $columns = [];

        // أولاً: من A لحد Z
        foreach (range('A', 'Z') as $col) {
            $columns[] = $col;
        }

        // ثانياً: من AA لحد BJ
        foreach (range('A', 'B') as $first) {
            foreach (range('A', 'Z') as $second) {
                $columns[] = $first . $second;
                if ($first . $second === 'BJ') {
                    break 2; // نخرج من اللوب عند BJ
                }
            }
        }

        // تعيين عرض 25 لكل عمود
        $widths = [];
        foreach ($columns as $col) {
            $widths[$col] = 25;
        }

        return $widths;
    }





    public function styles(\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $sheet)
{
    $highestRow = $sheet->getHighestRow();

    // 1️⃣ إضافة Borders لكل الصفوف
    for ($row = 1; $row <= $highestRow; $row++) {
        $sheet->getStyle("A{$row}:BJ{$row}")
            ->getBorders()
            ->getAllBorders()
            ->setBorderStyle(Border::BORDER_THIN);
    }

    // helper function للتحقق لو فيه لون موجود
    $hasFill = function($style) {
        $color = $style->getFill()->getStartColor()->getRGB();
        return !empty($color) && strtoupper($color) !== 'FFFFFF'; // مش أبيض
    };

    // 2️⃣ الأعمدة L,M,N,X,Y,Z,AM,AN,AO,BB,BC,BD => #dadada
    $columns1 = ['L','M','N','X','Y','Z','AM','AN','AO','BB','BC','BD'];
    foreach ($columns1 as $col) {
        for ($row = 1; $row <= $highestRow; $row++) {
            $style = $sheet->getStyle("{$col}{$row}");
            if (!$hasFill($style)) { // لو مفيش لون من قبل
                $style->getFill()
                    ->setFillType(Fill::FILL_SOLID)
                    ->getStartColor()->setRGB('dadada');
            }
        }
    }

    // 3️⃣ الأعمدة AA,AB,AC,AP,AQ,AR,BE,BF,BG => #d9e2f3
    $columns2 = ['AA','AB','AC','AP','AQ','AR','BE','BF','BG'];
    foreach ($columns2 as $col) {
        for ($row = 1; $row <= $highestRow; $row++) {
            $style = $sheet->getStyle("{$col}{$row}");
            if (!$hasFill($style)) {
                $style->getFill()
                    ->setFillType(Fill::FILL_SOLID)
                    ->getStartColor()->setRGB('d9e2f3');
            }
        }
    }

    // 4️⃣ الأعمدة BH,BI,BJ => #ffcc99
    $columns3 = ['BH','BI','BJ'];
    foreach ($columns3 as $col) {
        for ($row = 1; $row <= $highestRow; $row++) {
            $style = $sheet->getStyle("{$col}{$row}");
            if (!$hasFill($style)) {
                $style->getFill()
                    ->setFillType(Fill::FILL_SOLID)
                    ->getStartColor()->setRGB('ffcc99');
            }
        }
    }
}
}
