<?php
namespace App\Http\Controllers\Form;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

use App\Mail\CustomerInvoice;
use App\Jobs\SendCustomerInvoice;

use Carbon\Carbon;
use Validator;
use File;

use App\Model\Coverage;
use App\Model\Product;

use App\Model\Form;
use App\Model\WorkOrder;
use App\Model\Subscriber;
use App\Model\PackageDetail;
use App\Model\FormStatus;

class FormController extends Controller
{
	function createWorkID(){
		$number = WorkOrder::select('id','wo')->orderBy('created_at','DESC')->first();
		$numberOnly = str_replace('WO-','',$number->wo);
		info('before WO'.$numberOnly);
		$numberOnly++;
		info('after WO'.$numberOnly);
		return $numberOnly;
	}

	public function updateStatus($f_id){
		$fm = Form::where('_id',$f_id)->first();
		if(!empty($fm)){
			$fm->status_email = 'verified';
			$fm->save();

    		$formH = new FormStatus;
    		$formH->form_id = $fm->_id;
			$formH->status_id = 2;
			$formH->date = new \MongoDB\BSON\UTCDateTime(time()*1000);
			$formH->status = 'Verified';
			$formH->desc = 'Email already been verified by Customer Service';

			$fm->formstatus()->save($formH);

			do {
				$woID = 'WO-'.$this->createWorkID();
			} while (WorkOrder::where("wo", "=", $woID)->first() instanceof WorkOrder);

			$work_order = new WorkOrder;
			$work_order->img_url = '/assets/img/activation_icon_nRead1.png';
			$work_order->wo = $woID;
			$work_order->nature_work = '';
			$work_order->sub_category = '';
			$work_order->dateTimeStart = '';
			$work_order->dateTimeEnd = '';
			$work_order->contractor_id = '';
			$work_order->installer_id = '';
			$work_order->onu = '';
			$work_order->router = '';
			$work_order->need_phone = '';
			$work_order->no_phone = '';
			$work_order->pppoe_username = '';
			$work_order->pppoe_password = '';
			$work_order->docket_id = '';
			$work_order->remarks_custservice = '';
			$work_order->remarks_installer = '';
			$work_order->status = 'Pending Contractor';
			$work_order->created_by = '';

			$fm->workorder()->save($work_order);
			return redirect('/customer-service/new/customer/list');
		}
	}
}