<?php
namespace app\admin\controller\index;

use app\admin\controller\BaseController;
use app\admin\model\SystemAdmin;
use app\admin\server\index\IndexServer;


class Index extends BaseController
{

    /**
     * Notes:获取菜单列表
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/16
     */
    public function getAdminList()
    {
        $keywords = $this->request->get('keywords');
        [$page, $limit] = $this->getPage();
        $result         = IndexServer::getAdminList($keywords,$page,$limit);
        return json_success($result, '成功');
    }

    /**
     * Notes:修改管理员
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/16
     */
    public function updateAdmin()
    {
        $admin_id        = $this->request->post('admin_id');
        $admin_real_name = $this->request->post('admin_real_name','');
        $admin_pwd       = $this->request->post('admin_pwd','');
        $agin_admin_pwd  = $this->request->post('agin_admin_pwd','');
        $admin_roles     = $this->request->post('admin_roles');
        $admin_status    = $this->request->post('admin_status');
        if (!empty($admin_pwd) && !empty($agin_admin_pwd)){
            if ($admin_pwd != $agin_admin_pwd){
                throw new \Exception('两次密码输入不一致！');
            }
        }
        $this->validateCheck('index\IndexValidate', ['admin_id' => $admin_id], 'update');
        $result          = IndexServer::updateAdmin($admin_id, $admin_real_name, $admin_pwd, $admin_roles, $admin_status);
        return json_success($result, '修改成功');

    }

    /**
     * Notes:添加管理员
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/16
     */
    public function insertAdmin()
    {
        $admin_real_name = $this->request->post('admin_real_name','');
        $admin_pwd       = $this->request->post('admin_pwd','');
        $admin_account   = $this->request->post('admin_account');
        $agin_admin_pwd  = $this->request->post('agin_admin_pwd','');
        $admin_roles     = $this->request->post('admin_roles');
        $admin_status    = $this->request->post('admin_status');
        if (!empty($admin_pwd) && !empty($agin_admin_pwd)){
            if ($admin_pwd != $agin_admin_pwd){
                throw new \Exception('两次密码输入不一致！');
            }
        }
        $this->validateCheck('index\IndexValidate', ['admin_real_name' => $admin_real_name, 'admin_pwd' => $admin_pwd, 'admin_account' => $admin_account, 'agin_admin_pwd' => $agin_admin_pwd, 'admin_roles' => $admin_roles, 'admin_status' => $admin_status], 'insert');
        $result          = IndexServer::insertAdmin($admin_account, $admin_real_name, $admin_pwd, $admin_roles, $admin_status);
        return json_success($result, '修改成功');

    }

    /**
     * Notes:删除管理员
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/16
     */
    public function delAdmin()
    {
        $admin_id = $this->request->get('admin_id');
        $this->validateCheck('index\IndexValidate', ['admin_id' => $admin_id], 'info');
        $result         = IndexServer::delAdmin($admin_id);
        if ($result){
            return json_success($result, '删除成功');
        }else{
            throw new \Exception('删除失败！');
        }

    }

    /**
     * Notes:查询管理员详情
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/13
     */
    public function adminInfo()
    {
        $admin_id = $this->request->get('admin_id');
        $this->validateCheck('index\IndexValidate', ['admin_id' => $admin_id], 'info');
        $result         = IndexServer::adminInfo($admin_id);
        return json_success($result, '成功');
    }

    /**
     * Notes:管理员启用禁用
     * @return \support\Response
     * User: ZQ
     * Date: 2022/9/13
     */
    public function updateStatus()
    {
        $admin_id = $this->request->get('admin_id');
        $admin_status = $this->request->get('admin_status');
        $this->validateCheck('index\IndexValidate', ['admin_id' => $admin_id], 'info');
        $result       = IndexServer::updateStatus($admin_id, $admin_status);
        return json_success($result, '修改成功');
    }

    /**
     * Notes:获取小程序页面地址列表
     * @return \support\Response
     * User: yym
     * Date: 2022/10/25
     */
    public function getPageList()
    {
        return json_success(IndexServer::getPageList());
    }
}
