<?php

namespace App\Exports;

use App\RejectedBill;
use App\RejectedBillItem;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Gate;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

class RejectedExport extends \PhpOffice\PhpSpreadsheet\Cell\StringValueBinder implements FromCollection,ShouldAutoSize,WithColumnFormatting
,WithCustomValueBinder
{
    protected $ids;
    static public $max=100000;
    protected $rejectedBills;
    public function setIds(Array $ids)
    {
        $this->ids=$ids;
    }
    public function setRejectedBills($rejectedBills)
    {
        $this->rejectedBills=$rejectedBills;
    }
    public function collection()
    {
        if($this->ids){
            return $this->getByIds();
        }
        if($this->rejectedBills){
            return $this->getByRejectedBills();
        }
        return null;
    }
    private function getByIds(){
        $rejectedItems=RejectedBillItem::whereHas('rejectedBill',function(Builder $query){
            $query->whereIn('id',$this->ids);
            if(count($this->ids)>self::$max){
                $query->where('is_finished',false);
            }
        });
        $rejectedItems=$rejectedItems->get();
        $rejecteds = new Collection();
        if($rejectedItems->isEmpty())return $rejecteds;
        $id_owner = $rejectedItems->first()->rejectedBill['id_owner'];
        $this->prependFields($rejecteds,$id_owner);
        $rejectedItems->each(function(RejectedBillItem $item)use($rejecteds,$id_owner){
            $this->injectRecord($rejecteds,$item->rejectedBill,$item,$id_owner);
        });
        return $rejecteds;
    }
    private function getByRejectedBills(){
        $rejecteds = new Collection();
        $rejectedBills=$this->rejectedBills->get();
        if($rejectedBills->isEmpty())return $rejecteds;
        $id_owner = $rejectedBills->first()['id_owner'];
        $this->prependFields($rejecteds,$id_owner);
        $rejectedBills->each(function(RejectedBill $rejectedBill)use($rejecteds,$id_owner){
            $rejectedBill->items()->each(function (RejectedBillItem $item)use($rejecteds,$rejectedBill,$id_owner){
                $this->injectRecord($rejecteds,$rejectedBill,$item,$id_owner);
            });
        });
        return $rejecteds;
    }
    private function prependFields($collection,$id_owner){
        $fieldNames=['日期','审核号','客户名称','订单号','姓名',
            '手机','原单单号','退回单号','退回公司','到付费用',
            '是否入库','商品条码','商品名称',];
        if($id_owner==94){
            $fieldNames=array_merge($fieldNames,['寄件方省','重量',]);
        }
        $fieldNames=array_merge($fieldNames,['数量','是否正品',
            '批次号','生产日期','效期','备注','退单备注','录入人']);
        $collection->prepend($fieldNames);
        return $collection;
    }
    private function injectRecord($collection,$bill,$item,$id_owner){
        $fieldValues=[$item['created_at'],
            $bill['checked_numbers'],$bill['owner_name'],
            $bill['order_number'],$bill['sender'],
            $bill['mobile_sender'],$bill['logistic_number'],
            $bill['logistic_number_return'],$bill['logistic_name'],
            $bill['fee_collected'],$bill['is_loaded_str'],
            $item['barcode_goods'],$item['name_goods'],];
        if($id_owner==94){
            $fieldValues=array_merge($fieldValues,[$bill['common_01'],$bill['common_02']]);
        }
        $fieldValues=array_merge($fieldValues,[$item['amount'],$item['quality_label'],
            $item['batch_number'],$item['made_at'],$item['validity_at'],$item['remark'],$bill['remark'],$bill['operator_name']]);
        $collection->push($fieldValues);
        return $collection;
    }


    /**
     * @return array
     */
    public function columnFormats(): array
    {
        return [
            'B' => NumberFormat::FORMAT_TEXT,
            'C' => NumberFormat::FORMAT_TEXT,
            'D' => NumberFormat::FORMAT_TEXT,
            'E' => NumberFormat::FORMAT_TEXT,
            'F' => NumberFormat::FORMAT_TEXT,
            'G' => NumberFormat::FORMAT_TEXT,
            'H' => NumberFormat::FORMAT_TEXT,
            'L' => NumberFormat::FORMAT_TEXT,
            'N' => NumberFormat::FORMAT_TEXT,
        ];
    }
}
