<?php
namespace common\models\forms;

use common\components\Model;
use common\helpers\user\Info;
use common\libs\logging\operate\UserOperate;
use common\models\DeclarationLevel;
use common\models\User;
use common\models\UserInfo;

/**
 * Login form
 */
class UserForm extends Model
{
    public $userId;
    public $userName;
    public $zcPv;
    public $zcAmount;
    public $conUserName;
    public $recUserName;
    public $conUid;
    public $recUid;
    public $location;
    public $idCard;
    public $allData;   // 批量报单的全部报单数据

    //个人资料
    public $nation;
    public $realName;
    public $mobile;
    public $openBank;
    public $bankAddress;
    public $bankNo;
    //修改密码
    public $password;
    public $oldPassword;
    public $verifyPassword;
    public $payPassword;

    private $_conUid;
    private $_recUid;
    private $_limit = 1000;

    private $_conTopUid;
    private $_conTopDeep;
    private $_recTopUid;
    private $_recTopDeep;

    public function init() {
        parent::init();
        $this->userOperateLogger = new UserOperate([
            'fetchClass' => User::class,
        ]);
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['userId', 'userName', 'zcPv', 'zcAmount', 'conUserName', 'recUserName','conUid', 'recUid', 'location','nation','realName', 'mobile','openBank','bankAddress','bankNo'], 'trim'],
            [['userId', 'userName', 'zcPv', 'zcAmount', 'conUid', 'recUid', 'location'], 'required', 'on'=>'addWithUid'],
            [['userId', 'userName', 'zcPv', 'zcAmount', 'conUserName', 'recUserName', 'location'], 'required', 'on'=>['addWithUserName', 'addByAdmin']],
            [['idCard', 'allData'], 'required', 'on'=>['addWithUserName']],
            [['userName'], 'required', 'on'=>['validateUser', 'noLoginModifyPassword']],
            [['conUid'], 'required', 'on'=>['validateCon', 'validateAddWithUid']],
            [['conUserName'], 'required', 'on'=>['validateConName', 'validateAddWithName']],
            [['recUid'], 'required', 'on'=>['validateRec', 'validateAddWithUid']],
            [['recUserName'], 'required', 'on'=>['validateRecName', 'validateAddWithName']],
            [['conUid', 'location'], 'required', 'on'=>['validateLocation', 'validateAddWithUid']],
            [['conUserName', 'location'], 'required', 'on'=>['validateLocationConName', 'validateAddWithName']],

            [['nation','realName'], 'required', 'on'=>'modifyProfile'],
            [['oldPassword','verifyPassword'], 'required','on' => ['modifyPassword', 'modifyPasswordPay', 'noLoginModifyPassword']],
            [['password'], 'required','on' => ['modifyPassword', 'noLoginModifyPassword']],
            [['payPassword'], 'required','on' => ['modifyPasswordPay']],
            ['verifyPassword', 'compare', 'compareAttribute' => 'password' ,'message'=>'两次登录密码输入不一致' ,'on' => ['modifyPassword', 'noLoginModifyPassword']],
            ['verifyPassword', 'compare', 'compareAttribute' => 'payPassword' ,'message'=>'两次支付密码输入不一致' ,'on' => ['modifyPasswordPay']],
            [['conUid'], 'isConUid', 'on' => ['addWithUserName', 'addWithUid', 'addByAdmin']],
            [['recUid'], 'isRecUid', 'on' => ['addWithUserName', 'addWithUid', 'addByAdmin']],
            [['conUserName'], 'isCon', 'on' => ['addWithUserName', 'addWithUid', 'addByAdmin']],
            [['recUserName'], 'isRec', 'on' => ['addWithUserName', 'addWithUid', 'addByAdmin']],
            [['location'], 'isLocation', 'on' => ['addWithUserName', 'addWithUid', 'addByAdmin']],
            //[['idCard'], 'isSameNetwork'],
            [['zcPv', 'zcAmount'], 'price', 'on' => ['addWithUserName', 'addWithUid', 'addByAdmin']],
            [['zcPv'], 'isMinDecLevel', 'on' => ['addWithUserName', 'addWithUid', 'addByAdmin']],
        ];
    }

    public function attributeLabels()
    {
        return [
            'userId' => '会员ID',
            'nation' => '民族',
            'idCard' => '身份证号',
            'openBank' => '银行名称',
            'bankAddress' => '开户支行',
            'bankNo' => '银行账号',
            'verifyPassword' => '确认密码',
            'oldPassword' => '原密码',
            'password' => '登录密码',
            'payPassword' => '支付密码',
        ];
    }

    /**
     * 修改个人资料
     * @return User|null
     */
    public function modifyProfile(){
        if(!$this->validate()){
            return null;
        }
        $this->userOperateLogger->beforeUpdate(\Yii::$app->user->id,'ID',['select'=>'NATION,OPEN_BANK,BANK_NO,BANK_ADDRESS']);

        $uid = \Yii::$app->user->id;
        $model = User::findOne(['ID'=>$uid]);
        $model->NATION = $this->nation;
        $model->OPEN_BANK = $this->openBank;
        $model->BANK_NO = $this->bankNo;
        $model->BANK_ADDRESS = $this->bankAddress;
        if(!$model->save()){
            $this->addErrors($model->getErrors());
            return null;
        }

        User::updateBaseInfoToRedis($uid);

        $this->userOperateLogger->afterUpdate(\Yii::$app->user->id,'ID',['select'=>'NATION,OPEN_BANK,BANK_NO,BANK_ADDRESS']);
        $this->userOperateLogger->clean()->save([
            'optType' => '会员修改资料',
            'userId' => \Yii::$app->user->id,
            'userName' => Info::getUserNameByUserId(\Yii::$app->user->id),
        ]);
        return $model;
    }
    /**
     * 修改密码
     */
    public function modifyPassword(){
        if(!$this->validate()){
            return null;
        }
        $uid = \Yii::$app->user->id;
        $model = User::findOne(['ID'=>$uid]);
        if ( !$model->validatePassword($this->oldPassword) ) {
            $this->addError('modifyPassword', '原登录密码错误');
            return false;
        }
        $model->PASSWORD_HASH = \Yii::$app->security->generatePasswordHash($this->password);
        if(!$model->save()){
            $this->addErrors($model->getErrors());
            return false;
        }
        return true;
    }
    /**
     * 修改支付密码
     */
    public function modifyPasswordPay(){
        if(!$this->validate()){
            return null;
        }
        $uid = \Yii::$app->user->id;
        $model = User::findOne(['ID'=>$uid]);
        if ( !$model->validatePasswordPay($this->oldPassword) ) {
            $this->addError('modifyPasswordPay', '原支付密码错误');
            return false;
        }
        $model->PAY_PASSWORD = \Yii::$app->security->generatePasswordHash($this->payPassword);
        if(!$model->save()){
            $this->addErrors($model->getErrors());
            return false;
        }
        return true;
    }


    /**
     * 不登录修改密码
     */
    public function noLoginModifyPassword(){
        if(!$this->validate()){
            return null;
        }
        $model =  User::findOne(["USER_NAME"=>$this->userName]);
        if( !$model ) {
            $this->addError('noLoginModifyPassword', '不存在的用户名');
            return false;
        }
        if ( !$model->validatePassword($this->oldPassword) ) {
            $this->addError('noLoginModifyPassword', '原登录密码错误');
            return false;
        }
        $model->PASSWORD_HASH = \Yii::$app->security->generatePasswordHash($this->password);
        $model->IS_MODIFY_PASSWORD = 0;
        if(!$model->save()){
            $this->addErrors($model->getErrors());
            return false;
        }
        return true;
    }

    /**
     * @param $attribute
     * @param $params
     */
    public function isConUid($attribute, $params){
        $this->_conUid = $this->conUid;
    }

     /**
     * @param $attribute
     * @param $params
     */
    public function isRecUid($attribute, $params){

        $this->_recUid = $this->recUid;
    }

    /**
     * @param $attribute
     * @param $params
     */
    public function isCon($attribute, $params){
        
    }

    /**
     * @param $attribute
     * @param $params
     */
    public function isRec($attribute, $params){
        $this->_recUid = $this->recUid;
        
    }

    /**
     * @param $attribute
     * @param $params
     */
    public function isLocation($attribute, $params){
        
    }

    /**
     * 校验注册PV必须要大于最低级别要求
     * @param $attribute
     * @param $params
     */
    public function isMinDecLevel($attribute, $params){
        
    }
}
