<?php
namespace frontend\models;

use Yii;

class AdminApi extends BaseApi
{
    public $apiUrl = 'admin';

    /**
     * 管理员列表
     * @param array $data
     * @return array
     */
    public function getList($data)
    {
        $result = $this->post($this->apiUrl . '/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 add($data)
    {
        $result = $this->post($this->apiUrl . '/add', $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 checkUsername($data)
    {
        $result = $this->post($this->apiUrl . '/check-username', $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 getAdminById($id)
    {
        $result = $this->post($this->apiUrl . '/get-admin-by-id', ['id' => $id]);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 编辑管理员
     * @param array $data
     * @return array
     */
    public function edit($data)
    {
        $result = $this->post($this->apiUrl . '/edit', $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 login($data)
    {
        $data['login_ip'] = (string) Yii::$app->request->getUserIP();
        $result = $this->post($this->apiUrl . '/login', $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 updatePassword($data)
    {
        $result = $this->post($this->apiUrl . '/update-password', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 获取用户组别列表
     */
    public function getRoleList($data = [])
    {
        $result = $this->get('/role/get-role-list-for-admin', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }

    /**
     * 通过用户ID获取权限列表
     */
    public function getAdminRoleByUserId($data = [])
    {
        $result = $this->get('/admin-role/get-grant-by-user-id', $data);
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }
    
    /**
     * 获取通用信息
     * @return array
     */
    public function getCommonInfo()
    {
        $result = $this->get('/admin/get-common-info');
        if ($result['code'] == 1) {
            return $this->returnArray(1, $result['data']);
        } else {
            return $this->returnArray(0, [], $result['message']);
        }
    }
    
}

