<?php

namespace App\Helpers\Customer;

use App\Models\Customer\ClientModel;

class ClientHelper extends CustomerHelper
{
    public function __construct()
    {
        parent::__construct();
        $this->setTitleField(ClientModel::TITLE);
    }
    public function getFieldView(string $field, mixed $value, array $viewDatas, array $extras = []): string|null
    {
        switch ($field) {
            case 'name':
                $value = "<a href=\"/admin/customer/client/detail/{$viewDatas['entity']->getPK()}\">" . $value . "</a>";
                break;
            case "email":
            case "phone":
                $value = $this->getMyAuth()->isAccessRole(['security']) ? parent::getFieldView($field, $value, $viewDatas, $extras) : "***********";
                break;
            case 'account_balance':
                $value = form_label(
                    number_format(intval($value)) . "원",
                    'index',
                    [
                        "data-src" => "/admin/customer/account?clientinfo_uid={$viewDatas['entity']->getPK()}&ActionTemplate=popup",
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#modal_action_form",
                        "class"          => "btn btn-sm form-label-sm text-primary",
                        ...$extras,
                    ]
                );
                break;
            case 'coupon_balance':
                $value = form_label(
                    number_format(intval($value)) . "개",
                    'index',
                    [
                        "data-src" => "/admin/customer/coupon?clientinfo_uid={$viewDatas['entity']->getPK()}&ActionTemplate=popup",
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#modal_action_form",
                        "class"          => "btn btn-sm form-label-sm text-primary",
                        ...$extras,
                    ]
                );
                break;
            case 'point_balance':
                $value = form_label(
                    number_format(intval($value)) . "원",
                    'index',
                    [
                        "data-src" => "/admin/customer/point?clientinfo_uid={$viewDatas['entity']->getPK()}&ActionTemplate=popup",
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#modal_action_form",
                        "class"          => "btn btn-sm form-label-sm text-primary",
                        ...$extras,
                    ]
                );
                break;
            default:
                $value = parent::getFieldView($field, $value, $viewDatas, $extras);
                break;
        }
        if (is_array($value)) {
            throw new \Exception(__METHOD__ . "에서 오류: {$field}의 값이 Array형태입니다");
        }
        return $value;
    } //
    public function getListButton(string $action, string $label, array $viewDatas, array $extras = []): string
    {
        switch ($action) {
            case 'modify':
                $action = $this->getMyAuth()->isAccessRole(['security']) ? parent::getListButton($action, $label, $viewDatas, $extras) : $label;
                break;
            case 'create':
            case 'delete':
            case 'batchjob':
            case 'batchjob_delete':
                $action = $this->getMyAuth()->isAccessRole(['security']) ?  parent::getListButton($action, $label, $viewDatas, $extras) : "";
                break;
            case 'history':
                $action = form_label(
                    $label ? $label : ICONS['HISTORY'],
                    $action,
                    [
                        "data-src" => "/admin/customer/client/history?clientinfo_uid={$viewDatas['entity']->getPK()}",
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#modal_action_form",
                        "class" => "btn btn-sm btn-primary form-label-sm",
                        ...$extras
                    ]
                );
                break;
            case 'coupon':
            case 'account':
            case 'point':
                $action = form_label(
                    $label,
                    $action,
                    [
                        "data-src"       => "/admin/customer/{$action}?clientinfo_uid={$viewDatas['entity']->getPK()}&ActionTemplate=popup",
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#modal_action_form",
                        "class" => "text-primary",
                        ...$extras
                    ]
                );
                break;
            default:
                $action = parent::getListButton($action, $label, $viewDatas, $extras);
                break;
        }
        return $action;
    }
}
