<?php

namespace App\Cells\Customer;

use App\Helpers\Equipment\ServerPartHelper;
use App\Services\Customer\ServiceService;
use App\Services\PaymentService;

class ServiceCell extends CustomerCell
{

  private ?PaymentService $_paymentService = null;
  public function __construct()
  {
    parent::__construct(new ServiceService());
  }
  final public function getPaymentService(): PaymentService
  {
    if (!$this->_paymentService) {
      $this->_paymentService = new PaymentService();
    }
    return $this->_paymentService;
  }

  public function detail(array $params): string
  {
    $this->getService()->setAction(__FUNCTION__);
    $this->getService()->setFormFields();
    $this->getService()->setFormFilters();
    $this->getService()->setFormRules();
    $this->getService()->setFormOptions();
    //서비스별 미납 Count
    $unPaids  = $this->getPaymentService()->getUnPaids('serviceinfo_uid', ['clientinfo_uid' => $params['clientinfo_uid']]);
    //서비스별 서버리스트
    $entities = [];
    $childServers = [];
    foreach ($this->getService()->getEntities(['clientinfo_uid' => $params['clientinfo_uid']]) as $entity) {
      $entities[] = $entity;
      $childServers[$entity->getPK()] = $this->getService()->getServerService()->getEntities(['serviceinfo_uid' => $entity->getPK()]);
    }
    $template = array_key_exists('template', $params) ? $params['template'] : __FUNCTION__;
    return view('cells/service/' . $template, [
      'serviceCellDatas' => [
        'control' => $this->getService()->getControlDatas(),
        'service' => $this->getService(),
        'unPaids' => $unPaids,
        'entities' => $entities,
        'childServers' => $childServers,
        'serverPartHelper' => new ServerPartHelper(),
      ]
    ]);
  }
}
