<?php

namespace App\Http\Controllers\API;

use Illuminate\Http\Request;
use App\Http\Requests\RegisterRequest;
use App\Http\Controllers\Api\BaseController;
use Illuminate\Support\Facades\Auth;

use App\Staff;
use App\Model\StaffDetail;
use App\Model\Docket;
use App\Model\WorkOrder;
use App\Model\Company;
use App\Model\Form;
use App\Model\PackageDetail;
use App\Model\Subscriber;
use App\Model\FormStatus;

use App\Jobs\StoreDocket;

use Carbon\Carbon;
use File;

class DocketMobile extends BaseController
{

	function random_code($limit) {
	    return substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, $limit);
	}

	function createDocketID($limit){
		$allowedNumbers = range(0, 9);
		shuffle($allowedNumbers);
		$digits = array_rand($allowedNumbers, $limit);
		$number = '';
		
		foreach($digits as $d){
			$number .= $allowedNumbers[$d];
		}
		$unique_id = $number;
		return $unique_id;
	}

	public function createDocket(Request $request){

		$loginUser = Staff::with('StaffDetail')->where('api_token', $request->api_token)->first();
		if(!empty($loginUser)) {

			$wo = WorkOrder::where('wo',$request->wo)->first();

			if(!empty($wo)){

				$woD = Docket::where('work_order_id', $request->wo)->first();
				if(empty($woD)){
					$subs = Subscriber::where('_id',$wo->_id)->first();
					$destinationPath = 'document/'.$subs->subscriber_id;

					 // create folder/dir if not exist
			        if(!File::exists(public_path().'/'.$destinationPath)){
			            File::makeDirectory(public_path().'/'.$destinationPath,0777,true);
			        }

			        $uploaded = public_path().'/'.$destinationPath;
			        // Signature
			        $signature_docket = $request->data_uris;
			        $signature_docket = explode(",", $signature_docket)[1];
			        //convert signature_form
			        $data = base64_decode($signature_docket);
			        $file = $uploaded."/signature_docket.png";
			        $savetoServer = file_put_contents($file,$data);
			        
			        if (($savetoServer === false) || ($savetoServer == -1)) {
			            return $this->sendError('', "Couldn't save signature to jpeg");
			        }else {

			        	do {
							$do = 'DO-'.$this->createDocketID(4);
						} while (Docket::where("docket_id", "=", $do)->first() instanceof Docket);

						$signature = '/'.$destinationPath.'/signature_docket.png';
						$this->dispatch(new StoreDocket($request->all(), $do, $wo->nature_work, $loginUser->_id, $signature));

						// $docket = new Docket();
						// $docket->work_order_id = $request->wo;
						// $docket->docket_id = $do;
						// $docket->nature_work = $wo->nature_work;
						// $docket->installer_id = $loginUser->_id;
						// $docket->end_job = Carbon::now()->toDateTimeString();
						// $docket->router_serial_number = $request->router_serial_number;
						// $docket->mac_router = $request->mac_router;
						// $docket->cable_read = $request->cable_read;
						// $docket->condition = $request->condition;
						// $docket->Note = $request->Note;
						// $docket->Rating1 = $request->Rating1;
						// $docket->Rating2 = $request->Rating2;
						// $docket->Rating3 = $request->Rating3;
						// $docket->customer_signature = '/'.$destinationPath.'/signature_docket.png';

						// $wo->docket()->save($docket);

						// $wo->status = 'Completed';
						// $wo->docket_id = $do;
						// $wo->save();

						// $fm = Form::where('_id',$wo->_id)->first();
						// $stat = new FormStatus();
						// $stat->form_id = $fm->_id;
						// $stat->status_id = 8;
						// $stat->date = new \MongoDB\BSON\UTCDateTime(time()*1000);
						// $stat->status = 'Completed';
						// $stat->desc = 'This work order already been completed';
						// $fm->formstatus()->save($stat);

						return $this->sendResponse($do, 'Create Docket Successfully');
					}
					
				}else{
					return $this->sendError('', 'Work Order Already Existed!');
				}
			}else{
				return $this->sendError('', 'Work Order Not Exist!');
			}
		}else {
			return $this->sendError('Invalid User', 'User Not Exist!');
		}
	}
}
