<?php

namespace common\models\forms;

use common\models\LogAdminLogin;
use Yii;

class LogAdminLoginForm extends \yii\base\Model
{
    const KEY_LOG_COLUMN = 'key_log';
    public $_id;
    public $adm_name;
    public $ip;
    public $created_at;
    public $user_agent;
    public $period_num;
    public $opt_type;
    public $success_times;
    public $fail_times;
    public $device;
    public $request_route;
    public $return_result;
    /**
     * DB
     * @return \yii\db\Connection the database connection used by this AR class.
     * @throws \yii\base\InvalidConfigException
     */
    public static function getDb()
    {
        return Yii::$app->get('mongodb');
    }

    /**
     * @inheritdoc
     */
    public function rules(){
        return [
            [['adm_name', 'ip', 'request_route'], 'required'],
            [['created_at', 'success_times', 'period_num',  'fail_times'], 'filter', 'filter' => function($value){
                return ($value) ? $value : 0;
            }],
            [['adm_name', 'ip', 'opt_type'], 'string', 'max' => 16],
            [['_id', 'return_result', 'device',], 'safe'],
            [['user_agent', 'request_route'], 'string', 'max' => 1000],
        ];
    }
    /**
     * @inheritdoc
     */
    public function beforeValidate() {
        return parent::beforeValidate();
    }
    /**
     * @inheritdoc
     */
    public function scenarios() {
        return parent::scenarios();
    }
    /**
     * @inheritdoc
     */
    public function attributeLabels(){
        return [
            '_id' => 'ID',
            'adm_name' => '对象编号',
            'ip' => '操作ip',
            'created_at' => '创建时间',
            'user_agent' => '浏览器特征',
            'period_num' => '期数',
            'opt_type' => '操作类型',
            'success_times' => '登陆成功次数',
            'fail_times' => '登录失败次数',
            'device' => '客户端',
            'request_route' => '请求路径',
            'return_result' => '返回内容',
        ];
    }

    /**
     * 添加
     * @return bool|LogAdminLogin
     */
    public function add(){
        if(!$this->validate()){
            return false;
        }
        $model = new LogAdminLogin();
        $model->adm_name = $this->adm_name;
        $model->ip = $this->ip;
        $model->created_at = intval($this->created_at);
        $model->user_agent = $this->user_agent;
        $model->period_num = intval($this->period_num);
        $model->opt_type = $this->opt_type;
        $model->success_times = intval($this->success_times);
        $model->fail_times = intval($this->fail_times);
        $model->device = $this->device;
        $model->request_route = $this->request_route;
        $model->return_result = $this->return_result;

        if(!$model->save()){
            return false;
        }
        return $model;
    }

    /**
     * 删除
     * @param $id
     * @return array|bool|null|\yii\mongodb\ActiveRecord
     * @throws \yii\db\StaleObjectException
     */
    public function delete($id){
        $row = LogAdminLogin::find()->select(['adm_name'])->where(['_id'=>$id])->one();
        if(!$row){
            $this->setError('要删除的日志不存在');
            return false;
        }
        if(!$row->delete()){
            $this->setError('日志删除失败');
        }
        return $row;
    }

}
