<?php

namespace App\Exports;

use App\Models\OrderDevice;
use App\Order;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;

class RentOrderExport implements FromCollection, WithMapping,WithHeadings,WithColumnFormatting
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection(){
        $request = request();
        $order = new OrderDevice();
        $items = $order->orderBy('created_at', 'desc');
        $items = $items->whereNull('inner_device_id');
        if($request->input('work_point_id')){
                $items->whereHas('inner_device',function ($query) use ($request) {
                $query->whereHas('work_point_id',$request->input('work_point_id'));
            });
        }

        if ($request->input('number'))
        {
            $items->whereHas('inner_device',function ($query){
                $query->where('number','like','%'.request('number').'%');
            });
        }
        if ($request->input('spec_id'))
        {
            $items->where('spec_id',$request->input('spec_id'));
        }
        if ($request->input('device_id'))
        {
            $items->where('device_id',$request->input('device_id'));
        }
        if ($request->input('project_id'))
        {
            $items->whereHas('project',function ($query){
                $query->where('id','=',request('project_id'));
            });
        }
        if ($request->input('device_name_id'))
        {
            $items->where('device_name_id',$request->input('device_name_id'));
//            $items->whereHas('inner_device',function ($query) use ($request) {
//                $query->whereHas('device_name_id',$request->input('device_name_id'));
//            });
        }

//        array:6 [▼
//  "device_id" => "1"
//  "device_name_id" => "1"
//  "project_id" => null
//  "spec_id" => "1"
//  "type" => "rent"
//  "work_point_id" => null
//]
        $items = $items->get();
        foreach ($items as $item) {
            $item->user_name = $item->user ? $item->user->name : '';
            $item->project_name = $item->project ? $item->project->name : '';
            $item->work_point_name = $item->order ? ($item->order->workPoint ? $item->order->workPoint->name : '') : '';
            $item->money = ($item->price * $item->quantity) / 100;
            $item->device_type_name = $item->device ? $item->device->name : '';
            $item->device_name_name = $item->device_name ? $item->device_name->name : '';
            $item->order_number = $item->order ? "\t".$item->order->order_number : '';
            $item->spec_name = $item->spec ? $item->spec->name : '';
            $item->rent_type_name = $item->rent_type ? $item->rent_type->name : '';
            $item->price = $item->price / 100;
        }
        return $items;
    }

    public function map($row): array
    {
        return [
            $row->order_id,
            $row->order_number,
            $row->project_name,
            $row->work_point_name,
            $row->device_type_name,
            $row->device_name_name,
            $row->spec_name,
            $row->rent_type_name,
            $row->price,
            $row->quantity,
            $row->start_date,
            $row->end_date,
            $row->money,
        ];
    }

    public function headings(): array
    {
        return [
            '订单ID',
            '订单编号',
            '在用项目',
            '工点',
            '租赁设备类型',
            '租赁设备名称',
            '设备规格型号',
            '租赁方式',
            '租赁单价',
            '租赁计数',
            '租赁开始时间',
            '租赁结束时间',
            '租赁总金额',
        ];
    }
    /**
     * @return array
     */
    public function columnFormats(): array
    {
        return [
            'B' => NumberFormat::FORMAT_TEXT,
        ];
    }
}
