<?php

namespace App\Http\Controllers;

use App\Imports\InventoryCompareImport;
use App\InventoryCompare;
use App\Services\OwnerService;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Http;
use Maatwebsite\Excel\Facades\Excel;

class InventoryCompareController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

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

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

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

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\InventoryCompare  $inventoryCompare
     * @return \Illuminate\Http\Response
     */
    public function destroy(InventoryCompare $inventoryCompare)
    {
        //
    }
    function inventoryCompare(Request $request,OwnerService $ownerService){
        if (!Gate::allows('库存管理-库存-库存对比')){return redirect(url('/')); }
        $owners = $ownerService->getIntersectPermitting();
        $inventoryCompares=app('InventoryCompareService')->getInventoryCompare($request->all());
        $param = $request->input();
        return view('inventory.statement.inventoryCompare',compact('owners','inventoryCompares','param'));
    }
    function importExcel(Request $request){
        if (!Gate::allows('库存管理-库存-库存对比')){return redirect(url('/')); }
//        $owner_id=$request->owner_id;
//        if(!$owner_id) return '<h1 class="text-danger">导入Excel失败<br><p style="color: red">您还未选择相应货主!</p></h1>';
        $fileSuffix = $request->file()['file']->getClientOriginalExtension();
        if (in_array($fileSuffix, ['xlsx', 'xlsm', 'xltx', 'xltm', 'xls', 'xlt', 'ods', 'ots', 'slk', 'xml', 'gnumeric', 'htm', 'html', 'csv', 'tsv'])) {
            ini_set('max_execution_time', 2100);
            ini_set('memory_limit', '512M');
            $extension = $request->file()['file']->getClientOriginalExtension();
            $extension[0] = strtoupper($extension[0]);
            Excel::import(new InventoryCompareImport(), $request->file('file')->path(), null, $extension);
            if (Cache::has('error')) {
                return '<h1 class="text-danger">导入Excel失败<br><p style="color: red">' . Cache::pull('error') . '</p></h1>';
            } else {
                $exception = Cache::get('exception');
//                if ($exception){
                    $a = '';
                    for ($i = 0; $i < count($exception); $i++) {
                        $a .= implode(',', $exception[$i]) . '&#10';
                    };
                    app('LogService')->log(__METHOD__, __FUNCTION__, json_encode($request->toArray()), Auth::user()['id']);
                    return '<h1 class="text-danger">导入Excel成功<br><textarea style="width: 50%;height: 50%">' . $a . '</textarea></h1>';
//                }else {
//                    return '<h1 class="text-danger">导入Excel成功</h1>';
//                }

            }
        } else {
            return '<h1 class="text-danger">失败<br><p style="color: red">不支持该文件类型</p></h1>';
        }
    }

    public function exportInventoryCompare(Request $request){
        if (!Gate::allows('库存管理-库存-库存对比')){return redirect(url('/')); }
        if ($request->checkAllSign){
            $params = $request->input();
            unset($params['checkAllSign']);
            $sql=app('InventoryCompareService')->getSql($params);
        }else{
            $sql=app('InventoryCompareService')->getSql(['id'=>$request->data]);
        }
        $post = Http::post(config('go.export.url'),['type'=>'inventoryCompare','sql'=>$sql]);
        if ($post->status() == 500){
            throw new Exception($post->header("Msg"));
        }
        return response($post,200, [
            "Content-type"=>"application/octet-stream",
            "Content-Disposition"=>"attachment; filename=库存对比记录-".date('ymdHis').'.xlsx',
        ]);
    }
}
