<?php

namespace App\Http\Controllers;

use App\Services\StockManagementService;
use Illuminate\Http\Request;
use DB;
use Symfony\Component\Process\Process;
class DeviceController extends Controller
{
    protected $stockManagementService;
    public function __construct(StockManagementService $stockManagementService)
    {
        $this->stockManagementService = $stockManagementService;
    }

    public function index(Request $request)
    {
        return view('/app/gold_management/ghalai_main_lot',[
            'PAGINATE_LIMIT' => PAGINATE_LIMIT,
            'status' => $request->status,
            'role' => $request->user()->role,
            'token' => session('token')
        ]);
    }
    
    public static function execute(){
        $cmd = 'secure-read-pt CFANTEK 246';
        $process = Process::fromShellCommandline($cmd);

        $processOutput = '';

        $captureOutput = function ($type, $line) use (&$processOutput) {
            $processOutput .= $line;
        };

        $process->setTimeout(null)
            ->run($captureOutput);

        /*if ($process->getExitCode()) {
            $exception = new ShellCommandFailedException($cmd . " - " . $processOutput);
            report($exception);

            throw $exception;
        }*/
        echo $processOutput;exit;

        return $processOutput;
    }
    
    public function add(Request $request)
    {
        //dd($request);exit;
        //echo '<pre>';print_r($request->user());exit;
        //secure-read-ws CFANTEK 246 26
        //secure-read-pt CFANTEK 246
        //$process = new Process(['rm', '/var/www/html/test.html']);
       // $process = new Process(['secure-read-ws', 'CFANTEK 246 26']);
        $process = new Process(['secure-read-pt', 'CFANTEK 246']);
        //echo '<pre>';print_r($process);exit;
echo $process->run();
exit;

        echo $request->user()->role;
        $devices = '';$branches='';$branchId = 0;
        if($request->user()->role == 'ADMIN'){
            $branches = DB::table('branch')->get();
            $devices = DB::table('devices')->select('id','name','code')->get();
            //echo '<pre>';print_r($branches);exit;
            $branchDevices = DB::table('branch_devices')->get();
        }
        if($request->user()->role == 'BRANCH_MANAGER'){
            $branch = DB::table('branch')->where('id',$request->user()->branch)->first();
            //echo $branch->city;exit;
            //DB::enableQueryLog();
            $devices = DB::table('devices')
            ->where('branch',$branch->city)
            ->whereNotIn('name', DB::table('branch_devices')->pluck('computer'))
            ->select('id','name','code')->get();
            //echo '<pre>';print_r(DB::getQueryLog());exit;
            //echo '<pre>';print_r($branchDevices);exit;
            $branchDevices = DB::table('branch_devices')->where('branch_id', $request->user()->branch)->get();
            $branchId = $request->user()->branch;
        }
        
        //echo '<pre>';print_r($devices);print_r($branchDevices);exit;
        return view('/app/device/add',[
            'PAGINATE_LIMIT' => PAGINATE_LIMIT,
            'status' => $request->status,
            'role' => $request->user()->role,
            'token' => session('token'),
            'devices' => $devices,
            'branchDevices' => $branchDevices,
            'branchId' => $branchId,
            'branches' => $branches
        ]);
    }
    
    public function add_computer(Request $request){
       return $this->stockManagementService->add_computer($request); 
    }
    
    public function branch_wise_computers(Request $request){
       return $this->stockManagementService->branch_wise_computers($request); 
    }
}