<?php

namespace app\admin\model\school\classes\activity;

use app\admin\model\manystore\Shop;
use app\manystore\model\Manystore;
use think\Model;
use traits\model\SoftDelete;

class Activity extends Model
{

    use SoftDelete;

    

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

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

    // 追加属性
    protected $append = [
        'address_type_text',
        'start_time_text',
        'end_time_text',
        'sign_start_time_text',
        'sign_end_time_text',
        'status_text',
        'recommend_text',
        'hot_text',
        'new_text',
        'selfhot_text',
        'expirestatus_text',
        'add_type_text',
        'has_expire',
        'has_sign_expire',
    ];

    //不用审核允许修改的字段
    public $no_auth_fields = ["status","weigh","recommend","hot","new","selfhot","feel","start_time","end_time","sign_start_time","sign_end_time","longitude","latitude","address_city","province","city","district","address","address_detail","address_type","title",'headimage','images','content',"price"];

    public function getHasSignExpireAttr($value, $data)
    {
        $sign_start_time =  (isset($data['sign_start_time']) ? $data['sign_start_time'] : '');
        $sign_end_time =  (isset($data['sign_end_time']) ? $data['sign_end_time'] : '');

        if(!$sign_end_time) return '2';

        if( $sign_end_time < time()) {
            return '1';
        }else{
            return '2';
        }

    }




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


    public function getHasExpireAttr($value, $data)
    {
        $end_time =  (isset($data['end_time']) ? $data['end_time'] : '');
        if(!$end_time) return '2';

        if( $end_time < time()) {
            return '1';
        }else{
            return '2';
        }

    }

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

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

    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }

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

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

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

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

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

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


    public function getAddressTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['address_type']) ? $data['address_type'] : '');
        $list = $this->getAddressTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    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 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 getSelfhotTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['selfhot']) ? $data['selfhot'] : '');
        $list = $this->getSelfhotList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getExpirestatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['expirestatus']) ? $data['expirestatus'] : '');
        $list = $this->getExpirestatusList();
        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] : '';
    }

    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);
    }


    public function manystore()
    {
        return $this->belongsTo(Manystore::class, 'manystore_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function shop()
    {
        return $this->belongsTo(Shop::class, 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
