<?php

namespace app\admin\model\score;

use think\Model;
use traits\model\SoftDelete;

class Event extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'score_event';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text',
        'frequency_text',
        'way_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            if (!$row['weigh']) {
                $pk = $row->getPk();
                $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
            }
        });
    }

    
    public function getStatusList()
    {
        return config("site.type_point_program");
    }

    public function getFrequencyList()
    {
        return ['month' => __('Frequency month'), 'day' => __('Frequency day'), 'week' => __('Frequency week'), 'once' => __('Frequency once')];
    }

    public function getWayList()
    {
        return ['free' => __('Way free'), 'direct' => __('Way direct')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        $list = $this->getStatusList();
        return $list[$value] ?? '';
    }


    public function getFrequencyTextAttr($value, $data)
    {
        $value = $value ?: ($data['frequency'] ?? '');
        $list = $this->getFrequencyList();
        return $list[$value] ?? '';
    }


    public function getWayTextAttr($value, $data)
    {
        $value = $value ?: ($data['way'] ?? '');
        $valueArr = explode(',', $value);
        $list = $this->getWayList();
        return implode(',', array_intersect_key($list, array_flip($valueArr)));
    }

    protected function setWayAttr($value)
    {
        return is_array($value) ? implode(',', $value) : $value;
    }


}
