<?php

namespace App\Http\Controllers\Inventory;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Inventory\Item;
use Crypt;
use App\Branch;
use App\Inventory\BillOfQuantity;
use App\Inventory\BOQItems;
use App\Inventory\InventoryUnit;
use App\Inventory\Usertoproject;
use App\Inventory\PurchaseOrder;
use App\Inventory\VendorQoutation;
use App\User;
use App\Designation;
use Illuminate\Contracts\Encryption\DecryptException;
use App\Http\Requests\InventoryRequests\BOQRequest;
use PDF;

class BOQItemsController extends Controller
{
    //

    public function view()
    {

    	 $branch_obj  = new Branch;
       //$unit_obj    = new InventoryUnit;
       //$boq         = new BillOfQuantity;
       $user = new User;
       //$designation = new Designation;
       //$vendor_type = new VendorType;
       //$designation_list = $designation->getDesignationList();

       $auser_list = $user->getUserListByType(['Employee']);
    	 $branch      = $branch_obj->getBranchList();
       //$unit_list   = $unit_obj->getAllInventoryUnit();
       //$floor_list  = $boq->getBOQListByColumn('floor');

      return view('inventory_module.usertoproject.manage',
      	[
      		'branch'    =>  $branch,
          'auser_list'=>$auser_list,
      	]
        );

    }



    public function getProjects($id)
    {

      $id = decrypt($id);
      $branch_obj  = new Branch;
      $branch      = $branch_obj->getBranchList();

      $view = 'List of Projects </br>';
      foreach ($branch as $rows)
      {
        //dd($rows->user_id);
        $count = Usertoproject::where('user_id', $id)->where('branch_id', $rows->id)->count();
        if($count > 0)
        {
          $view .= '<input type="checkbox" checked name="projects[]"  value="'.Crypt::encrypt($rows->id).'"></option>  '.$rows->name.'</br>';
        }
        else {
          $view .= '<input type="checkbox" name="projects[]"  value="'.Crypt::encrypt($rows->id).'"></option>  '.$rows->name.'</br>';
        }

      }
      return $view;

        }

        public function save(Request $request)
        {

          $id = decrypt($request->user_id);

          $user_projects = Usertoproject::where('user_id',$id)->get();

          foreach($user_projects as $up)
          {
            $up->delete();
          }

          if($request->projects)
          {
          foreach ($request->projects as $rows)
          {
            $project_id = decrypt($rows);
            $pu = new Usertoproject;
            $pu->branch_id = $project_id;
            $pu->user_id = $id;
            $pu->save();

          }
          }
          parent::successMessage('Updated.');
           return redirect()->back();

          }



          public function qoutationlist($id)
          {
          	 $id = decrypt($id);

             $users = User::where('id', $id)->count();
             if($users > 0)
            {
            $qoutations = VendorQoutation::where('user_id', $id)->get();
            return view('inventory_module.supplier.qoutationList',
            	[
            		'qoutations'    =>  $qoutations,
                'user_id' => $id
            	]
              );
            }
            else{
              parent::errorMessage('Something went wrong.');
               return redirect()->back();
            }

          }


          public function qoutationSubmit(BOQRequest $request)
          {
          	 $id = decrypt($request->user_id);

            $qoutations = new VendorQoutation();
            $qoutations->user_id = $id;
            $qoutations->remarks = $request->remarks;
            if(isset($request->file))
            {
                $qoutations->file = $this->uploadfile($request->file);
            }
            $qoutations->save();

            parent::successMessage('Qoutation Added.');
            return redirect()->back();
          }

          public function uploadfile($file)
          {
              if((isset($file) && trim(strlen($file)) > 0))
              {
                  $file =  \EF::uploadFile($file);

              }
              else
              {
                  $file = "";
              }


              return $file;
          }

}
