<?php

declare(strict_types=1);

namespace app\admin\model;

use app\common\model\traits\TimestampInteger;
use support\think\Model;

/**
 * 系统配置模型（Webman 迁移版）
 */
class Config extends Model
{
    use TimestampInteger;

    public static string $cacheTag = 'sys_config';

    protected string $table = 'config';
    protected string $pk = 'id';
    protected bool $autoWriteTimestamp = true;

    protected array $append = ['value', 'content', 'extend', 'input_extend'];
    protected array $jsonDecodeType = ['checkbox', 'array', 'selects'];
    protected array $needContent = ['radio', 'checkbox', 'select', 'selects'];

    public static function onBeforeInsert(Config $model): void
    {
        if (!in_array($model->getData('type'), $model->needContent)) {
            $model->content = null;
        } else {
            $model->content = json_encode(str_attr_to_array($model->getData('content')));
        }
        if (is_array($model->rule)) {
            $model->rule = implode(',', $model->rule);
        }
        if ($model->getData('extend') || $model->getData('inputExtend')) {
            $extend = str_attr_to_array($model->getData('extend'));
            $inputExtend = str_attr_to_array($model->getData('inputExtend'));
            if ($inputExtend) {
                $extend['baInputExtend'] = $inputExtend;
            }
            if ($extend) {
                $model->extend = json_encode($extend);
            }
        }
        $model->allow_del = 1;
    }

    public static function onAfterWrite(): void
    {
        clear_config_cache();
    }

    public function getValueAttr($value, $row)
    {
        if (!isset($row['type']) || $value == '0') return $value;
        if (in_array($row['type'], $this->jsonDecodeType)) {
            return empty($value) ? [] : json_decode($value, true);
        }
        if ($row['type'] == 'switch') {
            return (bool) $value;
        }
        if ($row['type'] == 'editor') {
            return !$value ? '' : htmlspecialchars_decode($value);
        }
        if (in_array($row['type'], ['city', 'remoteSelects'])) {
            if (!$value) return [];
            if (!is_array($value)) return explode(',', $value);
            return $value;
        }
        return $value ?: '';
    }

    public function setValueAttr(mixed $value, $row): mixed
    {
        if (in_array($row['type'], $this->jsonDecodeType)) {
            return $value ? json_encode($value) : '';
        }
        if ($row['type'] == 'switch') {
            return $value ? '1' : '0';
        }
        if ($row['type'] == 'time') {
            return $value ? date('H:i:s', strtotime($value)) : '';
        }
        if ($row['type'] == 'city') {
            if ($value && is_array($value)) {
                return implode(',', $value);
            }
            return $value ?: '';
        }
        if (is_array($value)) {
            return implode(',', $value);
        }
        return $value;
    }

    public function getContentAttr($value, $row)
    {
        if (!isset($row['type'])) return '';
        if (in_array($row['type'], $this->needContent)) {
            $arr = json_decode($value, true);
            return $arr ?: [];
        }
        return '';
    }

    public function getExtendAttr($value)
    {
        if ($value) {
            $arr = json_decode($value, true);
            if ($arr) {
                unset($arr['baInputExtend']);
                return $arr;
            }
        }
        return [];
    }

    public function getInputExtendAttr($value, $row)
    {
        if ($row && $row['extend']) {
            $arr = json_decode($row['extend'], true);
            if ($arr && isset($arr['baInputExtend'])) {
                return $arr['baInputExtend'];
            }
        }
        return [];
    }
}
