<?php

namespace common\models;

use backendApi\modules\v1\models\Admin;
use Yii;
use common\libs\logging\operate\valueType\Config as ValueTypeConfig;

/**
 * This is the model class for table "{{%AD}}".
 *
 * @property string $ID
 * @property string $TITLE 标题
 * @property string $IMAGE 图片地址
 * @property int $TYPE 类型
 * @property string $LID 广告位ID
 * @property string $CONTENT 内容
 * @property string $COUNTRY_ID 国家ID
 * @property int $SORT 排序
 * @property int $STATUS 状态
 * @property string $CREATE_ADMIN 创建管理员
 * @property string $UPDATE_ADMIN 更新管理员
 * @property int $CREATED_AT 创建时间
 * @property int $UPDATED_AT 更新时间
 */
class Ad extends \common\components\ActiveRecord
{
    const TYPE_LINK = 1;        // 外链
    const TYPE_ARTICLE = 2;     // 文章

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%AD}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['TITLE', 'IMAGE', 'TYPE', 'LID', 'CREATE_ADMIN', 'CREATED_AT', 'COUNTRY_ID'], 'required'],
            [['TYPE', 'SORT', 'STATUS', 'CREATED_AT', 'UPDATED_AT'], 'integer'],
            [['ID', 'LID', 'CREATE_ADMIN', 'UPDATE_ADMIN'], 'string', 'max' => 32],
            [['TITLE'], 'string', 'max' => 48],
            [['IMAGE', 'CONTENT'], 'string', 'max' => 1000],
            [['TITLE'], 'unique'],
            [['ID'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'TITLE' => '标题',
            'COUNTRY_ID' => '国家ID',
            'IMAGE' => '图片地址',
            'TYPE' => '类型',
            'LID' => '广告位ID',
            'CONTENT' => '内容',
            'SORT' => '排序',
            'STATUS' => '状态',
            'CREATE_ADMIN' => '创建管理员',
            'UPDATE_ADMIN' => '更新管理员',
            'CREATED_AT' => '创建时间',
            'UPDATED_AT' => '更新时间',
        ];
    }

    /**
     * 操作日志记录条件
     * @return array
     */
    public function attrLabelsWithLogType(){
        return [
            'TITLE' => '标题',
            'IMAGE' => '图片地址',
            'TYPE' => [
                'label' => '类型',
                'type' => function($data){
                    $value = $data['value'];
                    return $value==1 ? '外链' : '文章';
                },
            ],
            'LID' => [
                'label' => '广告位',
                'type' => function($data){
                    $value = $data['value'];
                    $adLocation = AdLocation::findOneAsArray('ID=:ID', [':ID'=>$value], 'LOCATION_NAME');
                    return $adLocation['LOCATION_NAME'];
                },
            ],
            'CONTENT' => '内容',
            'SORT' => '排序',
            'CREATE_ADMIN' => [
                'label' => '创建人',
                'type' => function($data){
                    $value = is_array($data) && isset($data['value']) ? $data['value'] : '';
                    $result = Admin::findOneAsArray('ID=:ID', [':ID'=>$value], 'ADMIN_NAME');
                    return !empty($result) ? $result['ADMIN_NAME'] : '';
                },
            ],
            'UPDATE_ADMIN' => [
                'label' => '修改人',
                'type' => function($data){
                    $value = is_array($data) && isset($data['value']) ? $data['value'] : '';
                    $result = Admin::findOneAsArray('ID=:ID', [':ID'=>$value], 'ADMIN_NAME');
                    return !empty($result) ? $result['ADMIN_NAME'] : '';
                },
            ],
            'CREATED_AT' => [
                'label' => '创建时间',
                'type' => ValueTypeConfig::DATE_TIME_TYPE,
            ],
            'UPDATED_AT' => [
                'label' => '修改时间',
                'type' => ValueTypeConfig::DATE_TIME_TYPE,
            ],
        ];
    }
}
