<?php

namespace App\Exports\MME_Export\FeedingExport;


use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class FeedingExport implements FromCollection, WithMapping, WithHeadings, WithChunkReading, WithColumnFormatting, WithColumnWidths
{
  private $feedingexport;
  private $exportType;

  public function __construct($feedingexport, $exportType)
  {
    $this->feedingexport = $feedingexport;
    $this->exportType = $exportType;
    
  }
  public function collection()
  {
    return  $this->feedingexport;
  }
  public function headings(): array
  {
    
    if ($this->exportType == "register") {
      return [
        'Database',
        'Clinic Code',
        'General ID',
        'Sex',
        'FC-Reg Age',
        'FC-Reg Age(month)',
        'Feeding_ID',
        'Patient_Type',
        'Diagnosis',
        'Enter_from',
        'Distance_to_clinicMile',
        'Screening_Criteria',
        'Admission_Type',
        'Medical_complication',
        'Date_of_admission',
        'FINAL_MUAC',
        'MUAC',
        'Z_score',
        'Weight',
        'MUAC1',
        'Z_score1',
        'Weight1',
        'Clinical',
        'Others',
        'Date_of_transfer_to_ATFP',
        'Date of Outcome',
        'Outcome',
        'Clinical1',
        'Others1',
        'Hospital_Outcome',
        'Length_of_Stay_in',
        'Total_Length_of_Stay_days_',
        "Parents_Status",
        'Parent_HIV_status',
        'Caretaker',
        'Remark',
        
      ];
    } else if ($this->exportType == "follow_up") {
      return [
        'Database',
        'Clinic Code',
        'General ID',
        'Sex',
        //'Current Age',
        //'Current Age(month)',
        'Feeding_ID',
        'Date of Admission',
        'Visitdate',
        'MUAC',
        'Z_score',
        'Weight',
        'Height',
        'Clinical',
        'Other',
        'Remark',
      ];
    }
  }
  public function map($row): array
  {
    if ($this->exportType == "register") {
      return [
        $row->getConnectionName(),
        $row['Clinic Code'],
        $row['Pid'],
        $row['Gender'],
        $row['Current Agey'],
        $row['Current Agem'],
        $row["Feeding_ID"],
        $row["Patient_Type"],
        $row["Diagnosis"],
        $row["Enter_from"],
        $row["Distance_to_clinicMile"],
        $row["Screening_Criteria"],
        $row["Admission_Type"],
        $row["Medical_complication"],
        $row["Date_of_admission"],
        $row["FINAL_MUAC"],
        $row["MUAC"],
        $row["Z_score"],
        $row["Weight"],
        $row["MUAC1"],
        $row["Z_score1"],
        $row["Weight1"],
        $row["Clinical"],
        $row["Others"],
        $row["Date_of_transfer_to_ATFP"],
        $row["Date_of_Discharge"],
        $row["Outcome"],
        $row["Clinical1"],
        $row["Others1"],
        $row["Hospital_Outcome"],
        $row["Length_of_Stay_in"],
        $row["Total_Length_of_Stay_days_"],
        $row["Parents_Status"],
        $row["Parent_HIV_status"],
        $row["Caretaker"],
        $row["Remark"],
        
       
      ];
    } else if ($this->exportType == "follow_up") {
      return [
        $row->getConnectionName(),
        $row['Clinic Code'],
        $row['Pid'],
        $row['Gender'],
        //$row['Current Agey'],
        //$row['Current Agem'],
        $row['Feeding_ID'],
        $row["Date of Admission"],
        $row["Visitdate"],
        $row["MUAC"],
        $row["Z_score"],
        $row["Weight"],
        $row["Height"],
        $row["Clinical"],
        $row["Other"],
        $row["Remark"],
      ];
    }
  }
  public function columnWidths(): array
  {
    $columns = [];
    foreach (range('A', 'Z') as $char) {
      $columns[] = $char;
    }
    return array_fill_keys($columns, 15);
  }

  public function chunkSize(): int
  {
    return 5000; // Process 1,000 rows at a time
  }

  public function columnFormats(): array
  {
    if ($this->exportType == "register") {
      return [
        'O' => 'd-m-yyyy', // Date_of_admission
        'Y' => 'd-m-yyyy', // Date_of_transfer_to_ATFP
        'Z' => 'd-m-yyyy', // Date of Outcome
      ];
    } else if ($this->exportType == "follow_up") {
      return [
        'F' => 'd-m-yyyy',
        'G' => 'd-m-yyyy',
      ];
    }
  }
}
