<?php

namespace common\models\forms;

use common\models\LogUserHandle;
use Yii;

class LogUserHandleForm extends \yii\base\Model
{
    public $_id;
    public $user_id;
    public $user_name;
    public $ip;
    public $created_at;
    public $request_route;
    public $opt_type;
    public $save_before_content;
    public $save_after_content;
    public $key_log;
    public $opt_obj_id;
    public $opt_obj_name;
    public $remark;
    public $user_agent;
    public $period_num;
    public $is_batch;
    public $device;

    /**
     * @inheritdoc
     */
    public function rules(){
        return [
            [['user_id', 'user_name', 'ip', 'created_at', 'request_route', 'opt_type', 'save_before_content', 'save_after_content', 'key_log', 'opt_obj_id', 'opt_obj_name', 'remark', 'user_agent', 'period_num', 'is_batch', 'device'], 'trim'],
            [['user_id', 'user_name', 'ip', 'request_route', 'opt_type'], 'required'],
            [['created_at', 'key_log', 'period_num', 'is_batch'], 'filter', 'filter' => function($value){
                return ($value) ? $value : 0;
            }],
            [['user_id', 'opt_obj_id'], 'string', 'max' => 32],
            [['user_name', 'ip', 'opt_obj_name'], 'string', 'max' => 16],
            [['_id', 'opt_type', 'save_before_content', 'save_after_content', 'device'], 'safe'],
            [['user_agent', 'request_route', 'remark'], 'string', 'max' => 1000],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels(){
        return [
            '_id' => 'ID',
            'user_id' => '操作人id',
            'user_name' => '操作人',
            'ip' => '操作ip',
            'created_at' => '创建时间',
            'request_route' => '请求路由',
            'opt_type' => '操作类型',
            'save_before_content' => '保存前的内容',
            'save_after_content' => '保存后的内容',
            'key_log' => '关键日志',
            'opt_obj_id' => '操作对象id',
            'opt_obj_name' => '操作对象名称',
            'remark' => '备注',
            'user_agent' => '浏览器特征',
            'period_num' => '期数',
            'is_batch' => '批量保存',
            'device' => '客户端',
        ];
    }

    /**
     * 添加
     * @return bool|LogUserHandle
     */
    public function add(){
        if(!$this->validate()){
            return false;
        }
        $model = new LogUserHandle();
        $model->user_id = $this->user_id;
        $model->user_name = $this->user_name;
        $model->ip = $this->ip;
        $model->created_at = intval($this->created_at);
        $model->request_route = $this->request_route;
        $model->opt_type = $this->opt_type;
        $model->save_before_content = $this->save_before_content;
        $model->save_after_content = $this->save_after_content;
        $model->key_log = $this->key_log;
        $model->opt_obj_id = $this->opt_obj_id;
        $model->opt_obj_name = $this->opt_obj_name;
        $model->remark = $this->remark;
        $model->user_agent = $this->user_agent;
        $model->period_num = intval($this->period_num);
        $model->is_batch = intval($this->is_batch);
        $model->device = $this->device;

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