<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithHeadings;

class SearchTokenExport implements FromCollection, WithHeadings, ShouldAutoSize
{

    public function headings(): array
    {
        $dataArray = [
            'Client ID',
            'POP',
            'Call Receive Time',
            'Complain Details',
            'Complain Code',
            'Ping (Packet Loss)',
            'Package',
            'Optical Power',
            'ONU',
            'Mobile Number',
            'Creator Name',
            'Assign Time',
            '1st Response',
            'Noc Comments',
            'Remarks (Only Type of Problem)',
            'Client Feedback',
            'Solved By',
            'Issue Type',
            'Solve Time',
            // 'Department Head Comments',
            // 'Head Of Oparetion',
            // 'Md Sir Comments',
            // 'Care Line Inchange Comments',
            // 'Tania',
            // 'Mona'
        ];

        if(checkSettings('extra_column_in_token_export') == 'enable'){
            $dataArray[] = 'Department Head Comments';
            $dataArray[] = 'Head Of Oparetion';
            $dataArray[] = 'Md Sir Comments';
            $dataArray[] = 'Care Line Inchange Comments';
            $dataArray[] = 'Tania';
            $dataArray[] = 'Mona';

        }

        return $dataArray;
    }
    protected $data;

    function __construct($data)
    {
        $this->data = $data;
    }

    public function collection()
    {
        // dd($this->data);
        $items = [];
        foreach ($this->data as $item) {
            // dd($item);

            $assignedNames = $item->assignUsers
                ? $item->assignUsers->map(fn($a) => $a->employee->name ?? null)->filter()->implode(', ')
                : '';

            $row = [
                $item->clientId,
                $item->clients->pops->popname ?? '',
                $item->created_at ? date('Y-m-d H:i:s', strtotime($item->created_at)) : '',
                $item->description ?? '',
                $item->Code->tokenCode ?? '',
                $item->ping_loss ?? '',
                $item->clients->packages->package_name ?? '',
                $item->optical_power ?? '',
                (isset($item->onu_status) && $item->onu_status === 'online')
                    ? ($item->onu_power ?? '')
                    : ($item->onu_status ?? ''),
                "'" . ($item->reporterContact ?? ''),
                $item->creator->name ?? $item->createByClient ?? '',
                $item->assign_time ? date('Y-m-d H:i:s', strtotime($item->assign_time)) : '',
                $item->employees->name ?? $assignedNames,
                $item->remarks ?? '',
                $item->remarks ?? '',
                $item->client_feedback ?? '',
                $item->closer->name ?? '',
                $item->issue_type ?? '',
                $item->close_time ? date('Y-m-d H:i:s', strtotime($item->close_time)) : '',
              
            ];

            if(checkSettings('extra_column_in_token_export') == 'enable'){
                // Must add extra columns to the SAME row (match headings count)
                $row[] = '';
                $row[] = '';
                $row[] = '';
                $row[] = '';
                $row[] = '';
                $row[] = '';
            }
            $items[] = $row;
        }
        return collect($items);
    }
}
