/**
  * Remove status
  *
  * @param  int  $id
  * @return string
  */
 public function destroy($id)
 {
     $statusdevices = StatusDevice::find($id);
     if ($statusdevices != null) {
         $statusdevices->delete();
     }
     return json_encode("success");
 }
 /**
  * Display list device be assigned to
  * 
  * @return Response view
  */
 public function index()
 {
     $devices = Device::all();
     $employees = Employee::all();
     $employall = array();
     $employall += array('0' => 'None');
     foreach ($employees as $key => $value) {
         $employall += array($value->id => $value->lastname . " " . $value->firstname);
     }
     $statusall = StatusDevice::lists('status', 'id');
     return view('devices.borrow', compact('devices', 'employall', 'statusall'));
 }
 /**
  * Export to excel list device
  * 
  * @return file excel
  */
 public function exportExcel()
 {
     Excel::create('List Device', function ($excel) {
         $excel->sheet('Sheetname', function ($sheet) {
             $sheet->setBorder('A1:K1', 'thin');
             $sheet->cells('A1:K1', function ($cells) {
                 $cells->setBackground('blue');
                 $cells->setFontColor('#FFFFFF');
                 $cells->setAlignment('center');
                 $cells->setValignment('middle');
                 $cells->setFontFamily('Times New Roman');
             });
             $sheet->setFontFamily('Times New Roman');
             $sheet->setWidth(array('A' => '10', 'B' => '20', 'C' => '20', 'D' => '20', 'E' => '20', 'F' => '20', 'G' => '40', 'H' => '20', 'I' => '30', 'J' => '20', 'K' => '20'));
             $data = [];
             /*HEADER EXCEL*/
             array_push($data, array('STT', 'NAME DEVICE', 'SERIAL DEVICE', 'RECEIVE DATE', 'STATUS'));
             /*CONTENT EXCEL*/
             $device = Device::all();
             $number = 0;
             foreach ($device as $key => $value) {
                 $device[$key]->device_name = KindDevice::find($value->kind_device_id)->device_name;
                 $device[$key]->status = StatusDevice::find($value->status_id)->status;
                 $device[$key]->employee_code = Employee::find($value->employee_id)->employee_code;
                 $number++;
                 array_push($data, array($number, $value->device_name, $value->serial_device, $value->receive_date, $value->status, $value->distribution));
             }
             $sheet->fromArray($data, null, 'A1', false, false);
         });
     })->download('xls');
 }
 /**
  * Filter info in overview device
  * @return void
  */
 public function filter()
 {
     $type_id = Input::get('type_device');
     $model_id = Input::get('model_device');
     $kind_id = Input::get('kind_device');
     $status_id = Input::get('status_device');
     $os_id = Input::get('os_device');
     $contract_id = Input::get('contract_number');
     $position = Position::all();
     $types = TypeDevice::all();
     $models = ModelDevice::all();
     $kinds = KindDevice::all();
     $statuses = StatusDevice::all();
     $os = OperatingSystem::all();
     $contract = InformationDevice::all();
     /*Thuc hien cau truy van de lay du lieu sau khi filter*/
     $devices = Device::whereHas('kind_device', function ($query) use($type_id) {
         if ($type_id) {
             $query->whereHas('model_device', function ($query) use($type_id) {
                 $query->whereHas('type_devices', function ($query) use($type_id) {
                     $query->where('id', '=', $type_id);
                 });
             });
         }
     })->whereHas('kind_device', function ($query) use($model_id) {
         if ($model_id) {
             $query->whereHas('model_device', function ($query) use($model_id) {
                 $query->where('id', '=', $model_id);
             });
         }
     })->whereHas('kind_device', function ($query) use($kind_id) {
         if ($kind_id) {
             $query->where('id', '=', $kind_id);
         }
     })->whereHas('status_devices', function ($query) use($status_id) {
         if ($status_id) {
             $query->where('id', '=', $status_id);
         }
     })->whereHas('operating_system', function ($query) use($os_id) {
         if ($os_id) {
             $query->where('id', '=', $os_id);
         }
     })->whereHas('infomation_device', function ($query) use($contract_id) {
         if ($contract_id) {
             $query->where('id', '=', $contract_id);
         }
     })->get();
     foreach ($devices as $key => $value) {
         $value->device_name = $value->kind_device->device_name;
         $value->status = $value->status_devices->status;
         $employee = $value->employee;
         if ($employee) {
             $value->employee_code = $employee->employee_code;
             $value->fullname = $employee->lastname . " " . $employee->firstname;
             $value->employee_position = $employee->departments['name'];
         } else {
             $value->employee_code = "";
             $value->fullname = "";
             $value->employee_position = "";
         }
     }
     echo json_encode($devices);
 }