<?php
namespace backend\models;

use Yii;

class MemberApi extends BaseApi
{
    public $apiUrl = 'member';

    /**
     * 获取用户信息
     * @param int $id
     * @param null|string $fields
     * @return array
     */
    public function getMemberInfo($id, $fields = null)
    {
        $data['id'] = $id;
        if ($fields != null) {
            if (is_array($fields)) {
                $fields = implode(',', $fields);
            }
            $data['fields'] = $fields;
        }
        $result = $this->get($this->apiUrl . '/info', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data'], '获取用户信息成功');
        } else {
            return $this->returnArray(0, [], '获取用户信息失败');
        }
    }

    /**
     * 切换MT4账号
     * @param int $id
     * @param int $login
     * @return array
     */
    public function switchMt4user($id, $login)
    {
        $result = $this->get($this->apiUrl . '/switch', ['id' => $id, 'login' => $login]);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data'], '切换MT4账户成功');
        } else {
            return $this->returnArray(0, [], '切换MT4账户失败');
        }
    }

    /**
     * 获取上级代理商
     * @param int $id
     * @return array
     */
    public function getIbs($id = 0)
    {
        $id = intval($id) ? intval($id) : Yii::$app->user->id;
        $result = $this->get($this->apiUrl . '/get-ibs', ['id' => $id]);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data'], '获取上级代理商成功');
        } else {
            return $this->returnArray(0, [], '获取上级代理商失败');
        }
    }

    /**
     * 检测邮箱是否已注册
     * @param string $email
     * @return array
     */
    public function checkEmailExist($email)
    {
        $result = $this->get($this->apiUrl . '/checkemailexist', ['email' => $email]);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data'], '邮箱未被注册');
        } else {
            return $this->returnArray(0, [], '邮箱已被注册');
        }
    }

    /**
     * 检测身份证是否已注册
     * @param string $idNo
     * @return array
     */
    public function checkIdNoExist($idNo)
    {
        $result = $this->get($this->apiUrl . '/checkidnoexist', ['id_no' => $idNo]);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data'], '身份证未被注册');
        } else {
            return $this->returnArray(0, [], '身份证已被注册');
        }
    }

    /**
     * 详情页
     * @param array $data
     * @return array
     */
    public function getView($data)
    {
        $data['member_id']  = Yii::$app->user->id;
        $result = $this->post($this->apiUrl . '/get-view', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 用户列表
     * @param array $data
     * @return array
     */
    public function getList($data)
    {
        $result = $this->get($this->apiUrl . '/list', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 更新用户信息
     * @param int $id
     * @param array $data
     * @return array
     */
    public function updateMember($id, $data)
    {
        $data['id'] = $id;
        $result = $this->post($this->apiUrl . '/update', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 锁定用户
     * @param $id
     * @param $isEnable
     * @return array
     */
    public function updateMemberEnable($id, $isEnable)
    {
        $data['is_enable'] = $isEnable;
        return static::updateMember($id, $data);
    }

    /**
     * 修改用户MT4账号
     * @param int $id
     * @param string $logins
     * @return array
     */
    public function updateMemberLogins($id, $logins)
    {
        $data['logins'] = $logins;
        return static::updateMember($id, $data);
    }

    /**
     * 修改用户密码
     * @param int $id
     * @param string $password
     * @return array
     */
    public function updateMemberPassword($id, $password)
    {
        $data['password'] = $password;
        return static::updateMember($id, $data);
    }

    /**
     * 后台代理商列表
     * @param array $data
     * @return array
     */
    public function getAdminIbList($data)
    {
        $result = $this->post($this->apiUrl . '/admin-ib-list', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }
    
    /**
     * 后台添加代理商
     * @param array $data
     * @return array
     */
    public function addAdminIb($data)
    {
        $data['ip'] = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
        $result = $this->post($this->apiUrl . '/add-admin-ib', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 后台代理商名下客户列表
     * @param array $data
     * @return array
     */
    public function getAdminIbUsersList($data)
    {
        $result = $this->post($this->apiUrl . '/admin-ib-users-list', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 后台代理商详情页
     * @param array $data
     * @return array
     */
    public function getAdminIbView($data)
    {
        $data['member_id']  = Yii::$app->user->id;
        $result = $this->post($this->apiUrl . '/get-admin-ib-view', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 后台管理员列表
     * @param array $data
     * @return array
     */
    public function getAdminAdminList($data)
    {
        $result = $this->post($this->apiUrl . '/admin-admin-list', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 后台添加管理员
     * @param array $data
     * @return array
     */
    public function addAdminAdmin($data)
    {
        $data['ip'] = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
        $result = $this->post($this->apiUrl . '/add-admin-admin', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 后台编辑管理员
     * @param array $data
     * @return array
     */
    public function editAdminAdmin($data)
    {
        $data['ip'] = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
        $result = $this->post($this->apiUrl . '/edit-admin-admin', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }
    
    /**
     * 删除用户
     * @param int $id
     * @return array
     */
    public function deleteMember($id)
    {
        $data['id'] = $id;
        $result = $this->post($this->apiUrl . '/delete', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 通过logins获取member_id
     */
    public function getIdByLogin($login)
    {
        $data['login'] = $login;
        $result = $this->get($this->apiUrl . '/get-id-by-login', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }
}

