<?php

namespace App\Cells\Equipment;


use App\Entities\Equipment\ServerEntity;

class ServerPartCell extends EquipmentCell
{
  public function __construct()
  {
    parent::__construct(service('equipment_serverpartservice'));
  }

  public function parttable(array $params): string
  {
    $this->getService()->getActionForm()->form_init_process(__FUNCTION__);
    if (!array_key_exists('serverinfo_uid', $params)) {
      return "서버정보를 정의하셔야합니다.";
    }
    if (!array_key_exists('types', $params)) {
      return "부품정보 형태(Types) 리스트를 정의하셔야합니다.";
    }
    //서버정보
    $serverEntity = service('equipment_serverservice')->getEntity($params['serverinfo_uid']);
    if (!$serverEntity instanceof ServerEntity) {
      return "[{$params['serverinfo_uid']}]의 서버정보를 확인할수없습니다..";
    }
    //PartType별 Entities
    $entities = [];
    foreach ($params['types'] as $type) {
      $entities[$type] = [];
      $entities[$type][] = $this->getService()->getEntities(['serverinfo_uid' => $serverEntity->getPK(), 'type' => $type]);
    }
    $template = array_key_exists('template', $params) ? $params['template'] : __FUNCTION__;
    return view('cells/serverpart/' . $template, [
      'serverPartCellDatas' => [
        'formFilters' => $this->getService()->getActionForm()->getFormFilters(),
        'formOptions' => $this->getService()->getActionForm()->getFormOptions(),
        'helper' => $this->getService()->getHelper(),
        'serverinfo_uid' => $params['serverinfo_uid'],
        'types' => $params['types'],
        'serverEntity' => $serverEntity,
        'entities' => $entities,
      ],
    ]);
  }
}
