<?php

namespace app\admin\model\fastim;

use think\Model;

/*
 * FastIm配置模型-基于fastadmin系统配置的精简版
 */

class Config extends Model
{
    // 表名,不含前缀
    protected $name = 'fastim_config';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;
    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    // 追加属性
    protected $append = [
        'extend_html'
    ];

    public function getExtendHtmlAttr($value, $data)
    {
        $result = preg_replace_callback("/\{([a-zA-Z]+)\}/", function ($matches) use ($data) {
            if (isset($data[$matches[1]])) {
                return $data[$matches[1]];
            }
        }, $data['extend']);
        return $result;
    }

    /**
     * 读取分类分组列表
     * @return array
     */
    public static function getConfigGroup()
    {
        $configGroup     = [];
        $configGroupTemp = \think\Db::name('fastim_config')->where('name', 'configgroup')->value('value');
        $configGroupTemp = json_decode($configGroupTemp, true);
        foreach ($configGroupTemp as $k => $v) {
            $configGroup[$k]['name']  = $k;
            $configGroup[$k]['title'] = __($v);
            $configGroup[$k]['list']  = [];
        }
        return $configGroup;
    }

    /**
     * 按分组读取配置列表
     * @param int $userSettings 是否是用户设置
     * @param int $userExclusive 是否是用户专有设置
     * @param int $uniappAvailable 是否是uniapp专有设置
     * @return array
     */
    public static function getConfigListByGroup($userSettings, $userExclusive, $uniappAvailable = null)
    {
        $configList = self::getConfigGroup();

        $where = [];
        if ($userSettings !== null) {
            $where['user_settings'] = $userSettings;
        }
        if ($userExclusive !== null) {
            $where['user_exclusive'] = $userExclusive;
        }
        if ($uniappAvailable !== null) {
            $where['uniapp_available'] = $uniappAvailable;
        }

        $imConfig = collection(self::where($where)->order('weigh desc,id desc')->select())->toArray();
        foreach ($imConfig as $key => $value) {
            if (!isset($configList[$value['group']]) || $value['name'] == 'configgroup') {
                continue;
            }
            $value['title'] = __($value['title']);
            if (in_array($value['type'], ['select', 'selects', 'checkbox'])) {
                $value['value'] = explode(',', $value['value']);
            }
            $value['content']                      = json_decode($value['content'], true);
            $value['tip']                          = htmlspecialchars($value['tip']);
            $configList[$value['group']]['list'][] = $value;
        }

        $index = 0;
        foreach ($configList as $key => &$value) {
            if (count($value['list'])) {
                $value['active'] = !$index ? true : false;
                $index++;
            } else {
                unset($configList[$key]);
            }
        }
        return $configList;
    }

    public static function getArrayData($data)
    {
        if (!isset($data['value'])) {
            $result = [];
            foreach ($data as $index => $datum) {
                $result['field'][$index] = $datum['key'];
                $result['value'][$index] = $datum['value'];
            }
            $data = $result;
        }
        $fieldarr = $valuearr = [];
        $field    = isset($data['field']) ? $data['field'] : (isset($data['key']) ? $data['key'] : []);
        $value    = isset($data['value']) ? $data['value'] : [];
        foreach ($field as $m => $n) {
            if ($n != '') {
                $fieldarr[] = $field[$m];
                $valuearr[] = $value[$m];
            }
        }
        return $fieldarr ? array_combine($fieldarr, $valuearr) : [];
    }
}