<?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 "email":
            case "phone":
                $value = !$this->getMyAuth()->isAccessRole(['security']) ? "***********" : parent::getFieldView($field, $value, $viewDatas, $extras);
                break;
            case 'account_balance':
                $extras = ["class" => "btn btn-link", "target" => "_self", ...$extras];
                $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" => "#index_action_form",
                        ...$extras
                    ]
                );
                break;
            case 'coupon_balance':
                $extras = ["class" => "btn btn-link", "target" => "_self", ...$extras];
                $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" => "#index_action_form",
                        ...$extras
                    ]
                );
                break;
            case 'point_balance':
                $extras = ["class" => "btn btn-link", "target" => "_self", ...$extras];
                $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" => "#index_action_form",
                        ...$extras
                    ]
                );
                break;
            default:
                $value = parent::getFieldView($field, $value, $viewDatas, $extras);
                break;
        }
        if (is_array($value)) {
            echo __METHOD__ . "에서 오류: {$field}의 값이 Array형태입니다";
            exit;
        }
        return $value;
    } //
    public function getListButton(string $action, string $label, array $viewDatas, array $extras = []): string
    {
        switch ($action) {
            case 'modify':
                if (!$this->getMyAuth()->isAccessRole(['security'])) {
                    $action = $viewDatas['entity']->getCode();
                } else {
                    $action = parent::getListButton($action, $viewDatas['entity']->getCode(), $viewDatas, $extras);
                }
                break;
            case 'create':
            case 'delete':
            case 'batchjob':
            case 'batchjob_delete':
                $action = !$this->getMyAuth()->isAccessRole(['security']) ? "" : parent::getListButton($action, $label, $viewDatas, $extras);
                break;
            case 'history':
                $extras = ["class" => "btn btn-outline btn-primary btn-circle", "target" => "_self", ...$extras];
                $action = form_label(
                    ICONS['HISTORY'],
                    $action,
                    [
                        "data-src" => "/admin/customer/clienthistory?clientinfo_uid={$viewDatas['entity']->getPK()}",
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#index_action_form",
                        ...$extras
                    ]
                );
                break;
            default:
                $action = parent::getListButton($action, $label, $viewDatas, $extras);
                break;
        }
        return $action;
    }
}
