<?php

namespace app\common\model\style;

use app\common\model\BaseModel;
use think\Model;
use traits\model\SoftDelete;

class HomeImages extends BaseModel
{

    use SoftDelete;

    

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

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

    // 追加属性
    protected $append = [
        'type_text',
        'showtype_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 getTypeList()
    {
        return ['in' => __('Type in'), 'out' => __('Type out')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getShowtypeList()
    {
        return ['image' => __('Showtype image'), 'video' => __('Showtype video')];
    }


    public function getShowtypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['showtype']) ? $data['showtype'] : '');
        $list = $this->getShowtypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }



    public function getImageAttr($value, $data)
    {
        if (!empty($value)) return cdnurl($value, true);
    }



    public static function allList($page, $limit,$params=[]){


        $sort = "weigh desc,id desc";
//        if(!empty($params['status'])){
//            $params['status'] = '1';
//        }
        return (new self)->getBaseList($params, $page, $limit,$sort);
    }


}
