<?php

namespace app\admin\model\school\activity;

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

class Activity extends Model
{

    use SoftDelete;

    

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

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

    // 追加属性
    protected $append = [
        'start_time_text',
        'end_time_text',
        'sign_start_time_text',
        'sign_end_time_text',
        'status_text',
        'cancel_type_text',
        'recommend_text',
        'hot_text',
        'new_text',
        'add_type_text',
        'feel_text',
        'auth_status_text',
        'auth_time_text',
        'show_text',
        'canceltime_text'
    ];

    public function getShowList()
    {
        return ['1' => __('Show 1'), '2' => __('Show 2')];
    }


    public function getShowTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['show']) ? $data['show'] : '');
        $list = $this->getShowList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function getCateList(){
        return \app\admin\model\school\activity\Cate::column("name", 'id');
    }

    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 ['1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '4' => __('Status 4'), '5' => __('Status 5'), '-1' => __('Status -1')];
    }

    public function getCancelTypeList()
    {
        return ['1' => __('Cancel_type 1'), '2' => __('Cancel_type 2')];
    }

    public function getRecommendList()
    {
        return ['0' => __('Recommend 0'), '1' => __('Recommend 1')];
    }

    public function getHotList()
    {
        return ['0' => __('Hot 0'), '1' => __('Hot 1')];
    }

    public function getPlatformList(){
        return ['wechat_miniapp' => __('Platform wechat_miniapp'), 'tt_miniapp' => __('Platform tt_miniapp')];
    }

    protected function setPlatformAttr($value)
    {
        //如果是数组则转成字符串逗号拼接
        $value = is_array($value) ? implode(',', $value) : $value;
        return $value;
    }

    public function getNewList()
    {
        return ['0' => __('New 0'), '1' => __('New 1')];
    }

    public function getAddTypeList()
    {
        return ['1' => __('Add_type 1'), '2' => __('Add_type 2')];
    }

    public function getFeelList()
    {
        return ['0' => __('Feel 0'), '1' => __('Feel 1')];
    }

    public function getAuthStatusList()
    {
        return ['0' => __('Auth_status 0'), '1' => __('Auth_status 1'), '2' => __('Auth_status 2')];
    }


    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getSignStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sign_start_time']) ? $data['sign_start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getSignEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sign_end_time']) ? $data['sign_end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


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


    public function getCancelTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['cancel_type']) ? $data['cancel_type'] : '');
        $list = $this->getCancelTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getRecommendTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['recommend']) ? $data['recommend'] : '');
        $list = $this->getRecommendList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getHotTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['hot']) ? $data['hot'] : '');
        $list = $this->getHotList();
        return isset($list[$value]) ? $list[$value] : '';
    }





    public function getNewTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['new']) ? $data['new'] : '');
        $list = $this->getNewList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getAddTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['add_type']) ? $data['add_type'] : '');
        $list = $this->getAddTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getFeelTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['feel']) ? $data['feel'] : '');
        $list = $this->getFeelList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getAuthStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['auth_status']) ? $data['auth_status'] : '');
        $list = $this->getAuthStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getAuthTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['auth_time']) ? $data['auth_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCanceltimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['canceltime']) ? $data['canceltime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setSignStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setSignEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setAuthTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCanceltimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function admin()
    {
        return $this->belongsTo('app\admin\model\Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
