<?php

namespace App\Http\Controllers;

use App\UserDetail;
use App\UserDutyCheck;
use App\UserToken;
use App\UserWorkgroup;
use App\Warehouse;
use Endroid\QrCode\QrCode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use mysql_xdevapi\Collection;
use Ramsey\Uuid\Uuid;

class QRCodeController extends Controller
{
    //进入打卡页面
    public function goGetQRCode(){
        $warehouses=Warehouse::with('userWorkgroups')->get();
        return view("personnel/checking-in/getQRcode",compact('warehouses'));
    }

    //进入进出场二维码显示页面
    public function importAndExportQRCode(){
        $importAndExportQRCodes=[];
        $userDutyCheckOne=new UserDutyCheck([
            'source'=>'进场',
            'type'=>'import',
        ]);
        $userDutyCheck=new UserDutyCheck([
            'source'=>'出场',
            'type'=>'export',
        ]);
        array_push($importAndExportQRCodes,$userDutyCheckOne,$userDutyCheck);
        $importAndExportQRCodes=$this->qrCode_refresh_time($importAndExportQRCodes);
        Cache::forget('next_refresh_time');
        return view('personnel/checking-in/importAndExportQRCode',['importAndExportQRCodes'=>$importAndExportQRCodes,'next_refresh_time'=>Cache::get('next_refresh_time'),]);

    }
    //进出场二维码刷新时间
    public function qrCode_refresh_time($importAndExportQRCodes){
        $qrCode_refresh=config('hr.qrCode_refresh');
        $next_refresh_time=date('Y-m-d H:i:s',strtotime('+'.$qrCode_refresh.'second'));
        Cache::put('next_refresh_time',$next_refresh_time);
        return $this->flushQRCode($importAndExportQRCodes);
    }
    //刷新进出场二维码方法
    public function flushQRCode($importAndExportQRCodes){
        foreach ($importAndExportQRCodes as $importAndExportQRCode){
            $time=strtotime(Cache::get('next_refresh_time'));
            $qrCode_refresh=config('hr.qrCode_refresh');
            if ($qrCode_refresh){
                $date=date('Y-m-d H:i:s', strtotime('+30second',$time));
                $url=url("personnel/checking-in/userDutyCheck/importAndExportClock?key=").$date;
                $url=$url."&&importAndExportQRCodeType=".$importAndExportQRCode->type;
            }else{
                $url=url("personnel/checking-in/userDutyCheck/importAndExportClock");
                $url=$url."?importAndExportQRCodeType=".$importAndExportQRCode->type;
            }
            $qrCode=new QrCode($url);
            if ($importAndExportQRCode->type){
//                $fileURL=base_path('public/images/QRCodeIMG/'.$importAndExportQRCode->type.'.png');
//                $qrCode->writeFile($fileURL);
                $importAndExportQRCode->src=base64_encode($qrCode->writeString());
//                $importAndExportQRCode->url=url('images/QRCodeIMG/'.$importAndExportQRCode->type.'.png').'?da='.Uuid::uuid1();
            }
        }
        return $importAndExportQRCodes;
    }
    //刷新进出场二维码页面
    public function refreshQRCode(Request $request){
        $importAndExportQRCodes=[];
        $userDutyCheckOne=new UserDutyCheck([
            'type'=>'import',
            'source'=>'进场',
        ]);
        $userDutyCheck=new UserDutyCheck([
            'type'=>'export',
            'source'=>'出场',
        ]);
        array_push($importAndExportQRCodes,$userDutyCheckOne,$userDutyCheck);
        $importAndExportQRCodes=$this->qrCode_refresh_time($importAndExportQRCodes);
        $csrfToken=csrf_token();
        return ['importAndExportQRCodes'=>$importAndExportQRCodes,'next_refresh_time'=>Cache::get('next_refresh_time'),'csrf_Token'=>$csrfToken];
    }

    //进入工作组二维码显示页面
    public function QRCode(Request $request){
        $userWorkgroupId=$request->input('userWorkgroupId');
        $userWorkgroup=UserWorkgroup::find($userWorkgroupId);
        $userWorkgroup=$this->getQRCode($userWorkgroup);
        if (!$userWorkgroup)return view('exception.404',['error'=>'未找到工作组！']);
        return view('personnel/checking-in/QRcode',['userWorkgroup'=>$userWorkgroup]);
    }

    //获取工作组二维码
    public function getQRCode($userWorkgroup){
                $url=url("personnel/checking-in/userDutyCheck/clock");
                $url=$url."?userWorkgroupID=".$userWorkgroup->id;
            $qrCode=new QrCode($url);
            if ($userWorkgroup->id){
                $fileURL=base_path('public/images/QRCodeIMG/'.$userWorkgroup->id.'.png');
                if (file_exists($fileURL)){
                    $userWorkgroup->url=url('images/QRCodeIMG/'.$userWorkgroup->id.'.png');
                }
                $qrCode->writeFile($fileURL);
                $userWorkgroup->url=url('images/QRCodeIMG/'.$userWorkgroup->id.'.png');
            }
        return $userWorkgroup;
    }


    //获取进出场二维码
//    public function getImportAndExportQRCode($importAndExportQRCodes){
//            foreach ($importAndExportQRCodes as $importAndExportQRCode){
//                $qrCode_refresh_everyday=config('hr.qrCode_refresh_everyday');
//                if ($qrCode_refresh_everyday){
//                    $date=md5(date('Y-m-d'));
//                    $url=url("personnel/checking-in/userDutyCheck/importAndExportClock?key=").$date;
//                    $url=$url."&&importAndExportQRCodeType=".$importAndExportQRCode->type;
//                }else{
//                    $url=url("personnel/checking-in/userDutyCheck/importAndExportClock");
//                    $url=$url."?importAndExportQRCodeType=".$importAndExportQRCode->type;
//                }
//                $qrCode=new QrCode($url);
//                if ($importAndExportQRCode->type){
//                    $fileURL=base_path('public/images/QRCodeIMG/'.$importAndExportQRCode->type.'.png');
//                    if (file_exists($fileURL)){
//                        $importAndExportQRCode->url=url('images/QRCodeIMG/'.$importAndExportQRCode->type.'.png');
//                        continue;
//                    }
//                    $qrCode->writeFile($fileURL);
//                    $importAndExportQRCode->url=url('images/QRCodeIMG/'.$importAndExportQRCode->type.'.png');
//                }
//            }
//        return $importAndExportQRCodes;
//    }

//    //进出场二维码刷新时间
//    public function qrCode_refresh_time($importAndExportQRCodes,$beforeTime){
//        $qrCode_refresh=config('hr.qrCode_refresh');
//        if (!Cache::has('next_refresh_time') || Cache::get('next_refresh_time')<date('Y-m-d H:i:s')){
//            $next_refresh_time=date('Y-m-d H:i:s',strtotime('+'.$qrCode_refresh.'second'));
//            Cache::put('next_refresh_time',$next_refresh_time);
//            return $this->flushQRCode($importAndExportQRCodes);
//        }
//        if ($beforeTime==Cache::get('next_refresh_time')){
//            $time=strtotime($beforeTime);
//            //刷新 更新cache
//            $next_refresh_time=date('Y-m-d H:i:s', strtotime('+'.$qrCode_refresh.'second', $time));
//            Cache::put('next_refresh_time',$next_refresh_time);
//            return $this->flushQRCode($importAndExportQRCodes);
//        }
//        return $this->getImportAndExportQRCode($importAndExportQRCodes);
//    }
}
