<?php
/**
 * Created by PhpStorm.
 * User: 中闽 < 1464674022@qq.com >
 * Date: 2020/2/4
 * Time: 12:47
 */

namespace app\common\model;

use think\Model;

class Config extends Model
{
    protected $autoWriteTimestamp = false;

    const STATUS_OPEN = 1;//开启
    const STATUS_CLOSE = 0;//关闭

    const STATUS_HIDE = 1;//隐藏的配置
    const STATUS_SHOW = 0;//显示的配置

    const TYPE_INPUT = 0;//固定值
    const TYPE_SELECT = 1;//多选
    const TYPE_RADIO = 2;//单选

    const TYPES = [
        self::TYPE_INPUT => '固定值',
        self::TYPE_SELECT => '多选值',
        self::TYPE_RADIO => '单选值',
    ];

    //value_text
    public function getValueTextAttr($value, $data)
    {
        if ($this->getData('type') == self::TYPE_INPUT) {
            return $this->value;
        } elseif ($this->getData('type') == self::TYPE_SELECT) {
            $names = (new ConfigOption())->where('pid', $this->id)->where('status', ConfigOption::STATUS_OPEN)->column('name');
//            return implode(',', $names);
            return "已配置了" . count($names) . "个值";
        } elseif ($this->getData('type') == self::TYPE_RADIO) {
            return (new ConfigOption())->where('pid', $this->id)->where('single_status', ConfigOption::STATUS_OPEN)->column('name');
        } else {
            return '';
        }
    }

    //type_text
    public function getTypeTextAttr($value, $data)
    {
        return self::TYPES[$this->getData('type')];
    }

    //tab_text
    public function getTabTextAttr($value, $data)
    {
        if (!$this->tab) {
            return "";
        } else {
            return $this->tab->getData('name')??'';
        }
    }

    //json配置
    public function getJsonConfig()
    {
        $config = json_decode($this->remark, true);
        return is_array($config) ? $config : [];
    }

    //关联分组标签
    public function tab()
    {
        return $this->belongsTo('ConfigTab', 'tab_id');
    }
}
