<?php
namespace backend\models\forms;

use backend\models\MemberApi;
use yii\base\Model;
use common\helpers\Idcard;

class MemberForm extends Model
{
    public $id;
    public $is_enable;
    public $logins;
    public $pwd;
    public $username;
    public $name;
    public $idNo;
    public $address;
    public $mobile;
    public $gender;
    public $birthday;
    public $in_time;
    public $ib_old_login_name;
    public $password;
    public $id_no;
    public $ref_id;
    public $group_sn;

    /**
     * @param int $id
     * @param array $config
     * @param bool $is_add
     * @throws \Exception
     */
    public function __construct($id, array $config = [], $is_add = false)
    {
        // 如果是添加，则跳过赋值过程
        if ($is_add) {
            return;
        }
        
        $id = intval($id);
        if ($id <= 0) {
            throw new \Exception('用户不存在');
        }
        $api = new MemberApi();
        $result = $api->getMemberInfo($id);
        if ($result['code'] == 1) {
            $data = $result['data'];
            $this->id = $id;
            $this->is_enable = $data['is_enable'];
            $this->logins = $data['logins'];
            $this->username = $data['username'];
            $this->name = $data['name'];
            $this->idNo = $data['id_no'];
            $this->address = $data['address'];
            $this->mobile = $data['mobile'];
            $this->gender = $data['gender'];
            $this->birthday = $data['birthday'];
            $this->in_time = $data['in_time'];
            $this->group_sn = $data['group_sn'];
        } else {
            throw new \Exception($result['message']);
        }
    }

    public function rules()
    {
        return [
            [['is_enable', 'logins', 'pwd', 'username', 'name', 'idNo', 'address'], 'safe'],
            
            // add_admin_ib
            [['ib_old_login_name', 'logins', 'username', 'password', 'name', 'ref_id', 'group_sn'], 'required', 'on' => ['add_admin_ib']],
            ['username', 'email', 'on' => ['add_admin_ib']],
            ['password', 'string', 'min' => 6, 'on' => ['add_admin_ib']],
            ['id_no', function ($attribute, $params) {
                $idcard = Idcard::getInstance();
                if (!$idcard->isChinaIDCard($this->$attribute)) {
                    $this->addError($attribute, '身份证不正确');
                }
            }, 'on' => ['add_admin_ib']], // 缺省状态下，行内验证器不会在关联特性的输入值为空或该特性已经在其他验证中失败的情况下起效。 若你想要确保该验证器始终启用的话，你可以在定义规则时，酌情将 skipOnEmpty 以及 skipOnError属性设为 false


            // add_admin_member
            [[ 'logins', 'username', 'password','name'], 'required', 'on' => ['add_admin_member']],
            ['username', 'email', 'on' => ['add_admin_member']],
            ['password', 'string', 'min' => 6, 'on' => ['add_admin_member']],
            ['id_no', function ($attribute, $params) {
                $idcard = Idcard::getInstance();
                if (!$idcard->isChinaIDCard($this->$attribute)) {
                    $this->addError($attribute, '身份证不正确');
                }
            }, 'on' => ['add_admin_member']],

            // add_admin_admin
            [['username', 'name', 'password', 'mobile'], 'required', 'on' => ['add_admin_admin']],
            ['password', 'string', 'min' => 6, 'on' => ['add_admin_admin']],
            ['mobile', function ($attribute, $params) {
                if (!preg_match('/^1\d{10}/', $this->$attribute)) {
                    $this->addError($attribute, '手机号码不正确');
                }
            }, 'on' => ['add_admin_admin']],

            // edit_admin_admin
            [['id', 'name', 'mobile'], 'required', 'on' => ['edit_admin_admin']],
            ['password', 'string', 'min' => 6, 'on' => ['edit_admin_admin']],
            ['mobile', function ($attribute, $params) {
                if (!preg_match('/^1\d{10}/', $this->$attribute)) {
                    $this->addError($attribute, '手机号码不正确');
                }
            }, 'on' => ['edit_admin_admin']],
        ];
    }

    /**
     * @inheritdoc
     * @return array
     */
    public function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios['add_admin_ib'] = ['ib_old_login_name', 'logins', 'username',  'password', 'name', 'id_no', 'address', 'ref_id' , 'group_sn'];
        $scenarios['add_admin_member'] = ['logins', 'username',  'password', 'name', 'id_no', 'address'];
        $scenarios['add_admin_admin'] = ['username', 'name', 'password', 'mobile'];
        $scenarios['edit_admin_admin'] = ['id', 'name', 'password', 'mobile'];
        return $scenarios;
    }

    /**
     * @inheritdoc
     * @return array
     */
    public function attributeLabels()
    {
        if (in_array($this->getScenario(), ['add_admin_ib'])) {
            return [
                'ib_old_login_name' => '用户名',
                'logins' => '返佣账户',
                'username' => '电子邮箱',
                'mobile' => '手机',
                'password' => '密码',
                'name' => '姓名',
                'id_no' => '证件号码',
                'address' => '地址',
                'ref_id' => '上级代理',
                'group_sn' => 'MT4开户编号',
            ];
        }

        if (in_array($this->getScenario(), ['add_admin_admin'])) {
            return [
                'username' => '用户名',
                'name' => '姓名',
                'password' => '密码',
                'mobile' => '手机',
            ];
        }

        if (in_array($this->getScenario(), ['edit_admin_admin'])) {
            return [
                'password' => '新密码',
                'name' => '姓名',
                'mobile' => '手机',
            ];
        }
        
        return [];
    }

    public function updatePassword($pwd)
    {
        if ($pwd == null || trim($pwd) === '') {
            $this->addError('pwd', '密码不能为空');
            return false;
        }
        $api = new MemberApi();
        $result = $api->updateMemberPassword($this->id, $pwd);
        if ($result['code'] == 1) {
            return true;
        } else {
            return false;
        }
    }

    public function updateIsEnable($enable)
    {
        if (!is_numeric($enable) || !in_array($enable, [0, 1])) {
            $this->addError('is_enable', '参数错误');
            return false;
        }
        $api = new MemberApi();
        $result = $api->updateMemberEnable($this->id, $enable);
        if ($result['code'] == 1) {
            return true;
        } else {
            return false;
        }
    }

    public function updateLogins($logins)
    {
        if ($logins == null || trim($logins) === '') {
            $this->addError('logins', 'MT4账户不能为空');
            return false;
        }
        $api = new MemberApi();
        $result = $api->updateMemberLogins($this->id, $logins);
        if ($result['code'] == 1) {
            return true;
        } else {
            return false;
        }
    }

    public function updateInfo($post)
    {
        $data = [];
        if (isset($post['username']) && trim($post['username']) !== '') {
            $data['username'] = trim($post['username']);
        }
        if (isset($post['name']) && trim($post['name']) !== '') {
            $data['name'] = trim($post['name']);
        }
        if (isset($post['idNo']) && trim($post['idNo']) !== '') {
            $data['id_no'] = trim($post['idNo']);
        }
        if (isset($post['address']) && trim($post['address']) !== '') {
            $data['address'] = trim($post['address']);
        }
        if (isset($post['mobile']) && trim($post['mobile']) !== '') {
            $data['mobile'] = trim($post['mobile']);
        }
        if (empty($data)) {
            $this->addError('id', '数据为空');
            return false;
        }
        $api = new MemberApi();
        $result = $api->updateMember($this->id, $data);
        if ($result['code'] == 1) {
            return true;
        } else {
            return false;
        }
    }

    public function delete()
    {
        $api = new MemberApi();
        $result = $api->deleteMember($this->id);
        if ($result['code'] == 1) {
            return true;
        } else {
            return false;
        }
    }
}