<?php

namespace App\Jobs;

use App\Models\Contract;
use App\Models\Hostel;
use App\Models\HostelFee;
use App\Models\MoneyInfo;
use App\User;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use PhpOffice\PhpWord\SimpleType\NumberFormat;
use PhpOffice\PhpWord\Style\Border;

class ExportMoneyInfo implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    protected $ownerId;
    protected $date;
    protected $status;
    protected $contracts;

    public function __construct($ownerId, Carbon $date, $contracts = null)
    {
        //
        $this->ownerId = $ownerId;
        $this->date = $date;
        $this->contracts = $contracts;
        //    $this->status = $status;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //

        $contractItems = $this->contracts;
        $month = $this->date;
        $owner = User::find($this->ownerId);

        $typeDisplay = $owner->type_display_money_info;
        $name = 'Hoa-don';

        return \Excel::create($name . '-' . time(), function ($excel) use ($owner, $typeDisplay, $month, $contractItems) {
            $hostels = Hostel::query()
                ->where('owner_id', $this->ownerId)
                ->get();
            foreach ($hostels as $hostel) {
                $contracts = Contract::query()
//                    ->when($request->input('status') != -1, function($q) {
//                        $q->where('status',  \request()->input('status'));
//                    }, function($q) {
//                        $q->where('status', '<>', Contract::LIQUIDATED);
//                    })
                    ->where('hostel_id', $hostel->id)
                    ->with([
                        'room',
                        'hostel'
                    ]);
                $contracts = $contracts
                    ->when(!empty($contractItems->count()), function ($q) use ($contractItems) {
                        $q->whereIn('id', $contractItems->pluck('id')->toArray());
                    })
                    ->get()
                    ->sortBy('hostel.name')
                    ->sortBy('room.name');

                $fees = HostelFee::query()
                    ->where('hostel_id', $hostel->id)
                    ->get();


                if ($contracts->count() <= 0) {
                    continue;
                }


                $excel->sheet(mb_strimwidth(str_replace('/', '', $hostel->name), 0, 15, "..."), function ($sheet) use ($contracts, $fees, $typeDisplay, $month) {
//                    $sheet->setAutoSize(false);
//                    $sheet->setWidth(array(
//                        'T'     =>  30,
//                    ));

//                    $sheet->setColumnFormat(array(
//                        'E' => \PHPExcel_Cell_DataType::TYPE_NUMERIC,
//                        'F' =>  \PHPExcel_Cell_DataType::TYPE_NUMERIC,
//                    ));

                    $sheet->setStyle(array(
                        'font' => array(
                            'name' => 'Calibri',
                            'size' => 12,
                        ),
                    ));
                    $sheet->loadView('admin2.money.export_money_info', compact('contracts', 'fees', 'typeDisplay', 'month'));
                });
            }
        })->download('xlsx');
        //->store('xlsx', storage_path('excel/exports'));
    }
}
