<?php

namespace App\Models\Customer;

use App\Entities\Customer\ServiceEntity;
use tidy;

class ServiceModel extends CustomerModel
{
    const TABLE = "serviceinfo";
    const PK = "uid";
    const TITLE = "code";
    protected $table            = self::TABLE;
    // protected $useAutoIncrement = false;
    protected $primaryKey       = self::PK;
    protected $returnType       = ServiceEntity::class;
    protected $allowedFields = [
        "user_uid",
        "clientinfo_uid",
        "code",
        "site",
        "type",
        "location",
        "billing",
        "amount",
        "start_at",
        "end_at",
        "history",
        "status",
        "updated_at"
    ];
    public function __construct()
    {
        parent::__construct();
    }
    public function getFormFieldRule(string $action, string $field): string
    {
        if (is_array($field)) {
            throw new \Exception(__FUNCTION__ . "=> field가 array 입니다.\n" . var_export($field, true));
        }
        switch ($field) {
            case "user_uid":
            case "clientinfo_uid":
            case "amount":
                $rule  = "required|numeric";
                break;
            case "site":
            case "type":
            case "location":
            case "status":
                $rule = "required|trim|string";
                break;
            case "billing_at":
            case "start_at":
                $rule = "required|valid_date";
                break;
            case "end_at":
                $rule = "permit_empty|valid_date";
                break;
            case "history":
                $rule = "permit_empty|trim|string";
                break;
            default:
                $rule = parent::getFormFieldRule($action, $field);
                break;
        }
        return $rule;
    }
    //입력전 코드처리
    protected function create_process(array $formDatas): string|int
    {
        $formDatas['code'] = "s" . time();
        return parent::create_process($formDatas);
    }
}
