<?php

namespace App\Http\Controllers;

use App\Exports\InventoryBlindReceiveExcelExport;
use App\InventoryBlindReceiveExcel;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class StoreBlindReceiveController extends Controller
{
    public function index()
    {
        $excels=InventoryBlindReceiveExcel::orderBy('id','desc')->get();
        return view('store.blindReceive.excel.index',['excels'=>$excels]);
    }
    public function create()
    {

    }
    public function store(Request $request)
    {
        //
    }
    public function apiStore(Request $request)
    {
        $request=json_decode($request->getContent(),true);
        $goodses=$request['goodses']??'';
        $excelFileName=$request['filename']??'';
        if(!$goodses||!is_array($goodses)||count($goodses)==0){
            return ['result'=>'failure','fail_info'=>'没有数据提交！'];
        }
        $goodsesExcelArray=[['隔口','数量','条码','生产日期','失效日期','批次号',"唯一码"]];
        foreach ($goodses as $goods){
            array_push($goodsesExcelArray,[$goods['bin'],$goods['amount'],$goods['barcode'].' '
                ,$goods['produce_date'],$goods['valid_date'],$goods['batch_number'],$goods['unique_code']]);
        }
        $fileName=Carbon::now()->toDateTimeLocalString().microtime(true).'.xlsx';
        $fileName = str_replace( ':', '_',$fileName);
        $goodsNeateningExcel = new InventoryBlindReceiveExcel(['file' => $fileName, 'goods_amount' => count($goodses), 'name'=>$excelFileName]);
        $goodsNeateningExcel->save();
        Excel::store(new InventoryBlindReceiveExcelExport($goodsesExcelArray),$fileName,'public');
        $this->removeExpiredExcel();
        return ['result'=>'success'];
    }
    private function removeExpiredExcel(){
        $excels=InventoryBlindReceiveExcel::where('created_at','<',Carbon::now()->subDays(100)->toDateTimeString())->get();
        $excels->each(function(InventoryBlindReceiveExcel $excel){
            $excel->delete();
        });
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\InventoryBlindReceiveExcel  $goodsNeateningExcel
     * @return \Illuminate\Http\Response
     */
    public function show(InventoryBlindReceiveExcel $goodsNeateningExcel)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\InventoryBlindReceiveExcel  $goodsNeateningExcel
     * @return \Illuminate\Http\Response
     */
    public function edit(InventoryBlindReceiveExcel $goodsNeateningExcel)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\InventoryBlindReceiveExcel  $goodsNeateningExcel
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, InventoryBlindReceiveExcel $goodsNeateningExcel)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\InventoryBlindReceiveExcel  $goodsNeateningExcel
     * @return \Illuminate\Http\Response
     */
    public function destroy(InventoryBlindReceiveExcel $goodsNeateningExcel)
    {
        //
    }
}
