<?php

namespace App\Http\Controllers\CustomerService;

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

use Carbon\Carbon;
use Validator;
use PDF;

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

class CoverageController extends Controller
{

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

    function createWorkID($limit) {
                $number = WorkOrder::select('id','wo')->orderBy('created_at','DESC')->first();
		$numberOnly = str_replace('WO-','',$number->wo);
		$numberOnly++;
		return $numberOnly;

		$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 viewCoverageR()
    {
    	$id = Auth::guard('cs')->id();
		$user = Staff::with('StaffDetail')->find($id);

		$pp = count(WorkOrder::where('status','Pending Non Prelaid')->get());
		$sp = count(WorkOrder::where('status','Success Non Prelaid')->get());
		$rs = count(WorkOrder::where('status','Reschedule')->get());
		$ss = count(WorkOrder::where('status','Suspend')->get());
		$cm = count(WorkOrder::where('status','Completed')->get());

    	return view('customer-service.view_coverageR', compact('user','pp','sp','rs','ss','cm'));
    }

    public function viewCoverageB()
    {
    	$id = Auth::guard('cs')->id();
		$user = Staff::with('StaffDetail')->find($id);

		$pp = count(WorkOrder::where('status','Pending Non Prelaid')->get());
		$sp = count(WorkOrder::where('status','Success Non Prelaid')->get());
		$rs = count(WorkOrder::where('status','Reschedule')->get());
		$ss = count(WorkOrder::where('status','Suspend')->get());
		$cm = count(WorkOrder::where('status','Completed')->get());

    	return view('customer-service.view_coverageB', compact('user','pp','sp','rs','ss','cm'));
    }

    public function viewCoverageForm()
    {
    	$id = Auth::guard('cs')->id();
		$user = Staff::with('StaffDetail')->find($id);

		$pp = count(WorkOrder::where('status','Pending Non Prelaid')->get());
		$sp = count(WorkOrder::where('status','Success Non Prelaid')->get());
		$rs = count(WorkOrder::where('status','Reschedule')->get());
		$ss = count(WorkOrder::where('status','Suspend')->get());
		$cm = count(WorkOrder::where('status','Completed')->get());

    	return view('customer-service.view_coverage_form',compact('user','pp','sp','rs','ss','cm'));
    }

    public function editCoverage($cov)
    {
    	$id = Auth::guard('cs')->id();
		$user = Staff::with('StaffDetail')->find($id);

		$pp = count(WorkOrder::where('status','Pending Non Prelaid')->get());
		$sp = count(WorkOrder::where('status','Success Non Prelaid')->get());
		$rs = count(WorkOrder::where('status','Reschedule')->get());
		$ss = count(WorkOrder::where('status','Suspend')->get());
		$cm = count(WorkOrder::where('status','Completed')->get());

    	$coverage = Coverage::where('_id', $cov)->first();

    	return view('customer-service.view_coverage_edit', compact('user','pp','sp','rs','ss','cm','coverage'));
    }

    public function getCoverageR()
    {
    	$coverage = Coverage::where('Type', 'R')->get();

    	$i=0;
    	$curr = Carbon::now()->getTimestamp();
    	$nested_data = array();
    	foreach ($coverage as $a) {
        	$i++; $n1 = '';    

        	$reg_time = $a->created_at;
        	$expiry_date = $reg_time->addDays(3);    
        	$expiry_date = $expiry_date->getTimestamp(); 

        	if($curr < $expiry_date) {
        		$n1 = "New/";
        	} 
              
        	array_push($nested_data, array(
        		'index' => $n1.$i,
        		'address' => $a->building_name,
        		// 'street' => $a->street,
        		'city' => $a->city,
        		'state' => $a->state,
        		'postcode' => $a->postcode,
        		'status_building' => $a->status_building,
        		'action' => $a->_id
        	));
        }
        return \DataTables::of($nested_data)->make(true);
    }

    public function getCoverageB()
    {
    	$coverage = Coverage::where('Type', 'B')->get();

    	$i=0;
    	$curr = Carbon::now()->getTimestamp();
    	$nested_data = array();
    	foreach ($coverage as $a) {
        	$i++; $n1 = '';    

        	$reg_time = $a->created_at;
        	$expiry_date = $reg_time->addDays(3);    
        	$expiry_date = $expiry_date->getTimestamp(); 

        	if($curr < $expiry_date) {
        		$n1 = "New/";
        	} 
              
        	array_push($nested_data, array(
        		'index' => $n1.$i,
        		'address' => $a->building_name,
        		// 'street' => $a->street,
        		'city' => $a->city,
        		'state' => $a->state,
        		'postcode' => $a->postcode,
        		'status_building' => $a->status_building,
        		'action' => $a->_id
        	));
        }
        return \DataTables::of($nested_data)->make(true);
    }

    public function addCoverage(Request $request)
    {
    	$coverage = [
			'Type' => $request->get('tapp'),
			'building_name' => $request->get('name'),
			'city' => $request->get('city'),
			'state' => $request->get('state'),
			'postcode' => $request->get('postcode'),
			'installation_fee' => $request->get('fee'),
			'status_building' => $request->get('status'),
			'jmb_email' => $request->get('jmb'), 
		];

		$coverageUpdate = Coverage::create($coverage);

		return redirect()->back()->with('success_msg', 'message');
    }

    public function updateCoverage(Request $request)
    {
    	$coverage = Coverage::where('_id', $request->get('coverage_id'))->first();

		$coverage->building_name = $request->get('name');
		$coverage->city = $request->get('city');
		$coverage->state = $request->get('state');
        $coverage->status_building = $request->get('status');
		$coverage->postcode = $request->get('postcode');
		$coverage->save();

		return redirect()->back()->with('success_msg', 'Success! Update coverage '.$request->get('name'));
    }

    public function deleteCoverage(Request $request)
    {
    	$coverage = Coverage::where('_id',$request->id)->first();
		
		if(!empty($coverage)){
			$coverage->delete();
			return 'true';
		}else {
			return 'false';
		}
    }
}
