<?php 
namespace backend\models;

use common\helpers\Idcard;
use common\helpers\Utils;
use Yii;

/**
 * This is the model class for table "crm_open".
 *
 * @property integer $id
 * @property integer $open_type
 * @property integer $type
 * @property string $name
 * @property string $id_card
 * @property string $email
 * @property string $mobile
 * @property string $id_card_file_path0
 * @property string $id_card_file_path1
 * @property integer $in_time
 * @property string $rid
 * @property integer $mt4_login
 * @property string $mt4_login_pwd
 * @property string $mt4_view_pwd
 * @property integer $member_id
 * @property string $address
 * @property integer $level
 */
class Open extends \yii\db\ActiveRecord
{

    /**
     * @var array
     */
    public static $typeTextMap = [
        0 => '等待',
        1 => '处理中',
        2 => '完成',
        3 => '拒绝',
    ];
    
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'crm_open';
    }

    /**
     * @return \yii\db\Connection the database connection used by this AR class.
     */
    public static function getDb()
    {
        return Yii::$app->get('dbXcrm');
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['open_type', 'type', 'in_time', 'mt4_login', 'member_id', 'level'], 'integer'],
            [['name', 'id_card', 'email', 'in_time'], 'required'],
            [['name', 'id_card', 'email', 'id_card_file_path0', 'id_card_file_path1', 'rid', 'mt4_login_pwd', 'mt4_view_pwd'], 'string', 'max' => 255],
            [['address'], 'string', 'max' => 3000],
            ['id_card', 'checkIdCard', 'on' => ['frontend']],
            ['email', 'checkEmail', 'on' => ['frontend']],
           // ['mobile', 'checkMobile'],
        ];
    }
    
    public function checkEmail($attribute, $params = [])
    {
        if (!$this->hasErrors()) {
            $where = [
                'and',
                ['=', 'email', $this->email],
                ['in', 'type', [0, 1, 2]],
            ];
            $exists = static::find()->where($where)->count();
            if (Member::checkEmailExist($this->email)) {
                $this->addError($attribute, '邮箱' . $this->email . '已被占用');
            }
        }
    }

    public function checkIdCard($attribute, $params = [])
    {
        if (!$this->hasErrors()) {
            if (Idcard::getInstance()->isChinaIDCard($this->id_card) == false) {
                $this->addError($attribute, '身份证号码格式错误');
            }
            $where = [
                'and',
                ['=', 'id_card', $this->email],
                ['in', 'type', [0, 1, 2]],
            ];
            $exists = static::find()->where($where)->count();
            if (Member::checkIdNoExist($this->id_card)) {
                $this->addError($attribute, '身份证' . $this->id_card . '已被占用');
            }
        }
    }

    public function checkMobile($attribute, $params = [])
    {
        if (!$this->hasErrors()) {
            if (Utils::checkPhoneNumber($this->mobile) == false) {
                $this->addError($attribute, '手机号码格式错误');
            }
        }
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'open_type' => 'Open Type',
            'type' => 'Type',
            'name' => 'Name',
            'id_card' => 'Id Card',
            'email' => 'Email',
            'mobile' => 'Mobile',
            'id_card_file_path0' => 'Id Card File Path0',
            'id_card_file_path1' => 'Id Card File Path1',
            'in_time' => 'In Time',
            'rid' => 'Rid',
            'mt4_login' => 'Mt4 Login',
            'mt4_login_pwd' => 'Mt4 Login Pwd',
            'mt4_view_pwd' => 'Mt4 View Pwd',
            'member_id' => 'Member ID',
            'address' => 'Address',
            'level' => 'Level',
        ];
    }

    /**
     * @param string $email
     * @return bool
     */
    public static function checkEmailExist($email)
    {
        return static::find()->where(['email' => $email, 'type' => 0])->exists();
    }

    /**
     * @param string $idNo
     * @return bool
     */
    public static function checkIdNoExist($idNo)
    {
        return static::find()
            ->where(['id_card' => $idNo, 'open_type' => 2])
            ->andWhere(['<>', 'type', 3])
            ->exists();
    }
    
    /**
     * 开户数量
     * @param int $type 0申请，1处理中，2完成，3拒绝
     * @return int
     */
    public static function countByType($type)
    {
        if (!is_numeric($type)) {
            return 0;
        }
        return static::find()->where(['type' => $type])->count();
    }
    
}
