<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Csv as Csv_writer;

class Outlook_export
{

    protected $fields = [
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
    ];
    protected $titles = [
        'Title'
        , 'First Name'
        , 'Middle Name'
        , 'Last Name'
        , 'Suffix'
        , 'Company'
        , 'Department'
        , 'Job Title'
        , 'Business Street'
        , 'Business Street 2'
        , 'Business Street 3'
        , 'Business City'
        , 'Business State'
        , 'Business Postal Code'
        , 'Business Country/Region'
        , 'Home Street'
        , 'Home Street 2'
        , 'Home Street 3'
        , 'Home City'
        , 'Home State'
        , 'Home Postal Code'
        , 'Home Country/Region'
        , 'Other Street'
        , 'Other Street 2'
        , 'Other Street 3'
        , 'Other City'
        , 'Other State'
        , 'Other Postal Code'
        , 'Other Country/Region'
        , 'Assistant\'s Phone'
        , 'Business Fax'
        , 'Business Phone'
        , 'Business Phone 2'
        , 'Callback'
        , 'Car Phone'
        , 'Company Main Phone'
        , 'Home Fax'
        , 'Home Phone'
        , 'Home Phone 2'
        , 'ISDN'
        , 'Mobile Phone'
        , 'Other Fax'
        , 'Other Phone'
        , 'Pager'
        , 'Primary Phone'
        , 'Radio Phone'
        , 'TTY/TDD Phone'
        , 'Telex'
        , 'Account'
        , 'Anniversary'
        , 'Assistant\'s Name'
        , 'Billing Information'
        , 'Birthday'
        , 'Business Address PO Box'
        , 'Categories'
        , 'Children'
        , 'Directory Server'
        , 'E-mail Address'
        , 'E-mail Type'
        , 'E-mail Display Name'
        , 'E-mail 2 Address'
        , 'E-mail 2 Type'
        , 'E-mail 2 Display Name'
        , 'E-mail 3 Address'
        , 'E-mail 3 Type'
        , 'E-mail 3 Display Name'
        , 'Gender'
        , 'Government ID Number'
        , 'Hobby'
        , 'Home Address PO Box'
        , 'Initials'
        , 'Internet Free Busy'
        , 'Keywords'
        , 'Language'
        , 'Location'
        , 'Manager\'s Name'
        , 'Mileage'
        , 'Notes'
        , 'Office Location'
        , 'Organizational ID Number'
        , 'Other Address PO Box'
        , 'Priority'
        , 'Private'
        , 'Profession'
        , 'Referred By'
        , 'Sensitivity'
        , 'Spouse'
        , 'User 1'
        , 'User 2'
        , 'User 3'
        , 'User 4'
        , 'Web Page'
    ];

    public function export($type = '', $tags = '')
    {
        $ci = get_instance();

        $ci->load->model('newsletter/newsletter_email_tmodel');
        if (!empty($tags)) {
            $ci->newsletter_email_tmodel->set_predicates(['tags' => $tags]);
        }
        $data = $ci->newsletter_email_tmodel->set_select('email')->fetch_all();

        $spreadsheet = new Spreadsheet();
        $spreadsheet->getProperties()->setCreator('Ciext');
        $sheet = $spreadsheet->getActiveSheet();

        foreach ($this->titles as $k => $title) {
            $letter = '';
            if ($k < count($this->fields)) {
                $letter = $this->fields[$k];
            } else {
                $letter = $this->fields[floor($k / count($this->fields)) - 1] . $this->fields[($k % count($this->fields))];
            }
            $sheet->setCellValue($letter . '1', $title);
        }
        $i = 2;
        foreach ($data as $item) {
            $sheet->setCellValue('BF' . $i, $item['email']);
            $i++;
        }

        header('Content-type: text/csv');
        header('Content-Disposition: attachment;filename="newsletter_' . $type . '_' . date('Y-m-d') . '.csv"');
        header('Cache-Control: max-age=0');

        $writer = new Csv_writer($spreadsheet);
        $writer->save('php://output');
        exit;
    }

}
