<?php
namespace app\admin\controller\v1;
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-09-06 15:49
// +----------------------------------------------------------------------

use app\Request;
use app\admin\controller\AuthController;
use library\services\UtilService;
use library\utils;
use app\model\admin\Admin as AdminModel;
use \app\model\admin\AdminRole;

class SiteAdmin extends AuthController
{


    /**
     * 获取管理数据
     * @param Request $request
     * @return mixed
     */
    public function info(Request $request) {
        [$id,$sassId] = UtilService::getMore([
            ['id','','empty','参数错误'],
            ['sassId','','empty','参数错误']
        ],$request,true);
        $adminInfo = AdminModel::where('id',$id)->where('sassid',$sassId)->find();
        return app('json')->success([
            'id' => $id,
            'username'  => $adminInfo['username'],
            'name'      => $adminInfo['name'],
            'avatar'    => $adminInfo['avatar'],
            'role_id'   => $adminInfo['role_id'],
            'status'    => $adminInfo['status']
        ]);
    }

    /**
     * 获取管理列表
     * @param Request $request
     */
    public function list(Request $request) {
        $pageSize = 50;
        [$page,$roleId,$keyword,$sassId] =  UtilService::getMore(
            [
                ['page',1],
                ['roleId',0],
                ['keyword',''],
                ['sassId','','empty','参数错误']
            ],$request,true
        );
        $adminRole = new AdminRole;
        $adminRole->setSassId($sassId);
        list($pageCount,$data) = (new AdminModel)
            ->getList($page,
                ['sassid'=>$sassId,'role_id'=>[$roleId,'!empty'],'username'=>[$keyword,'!empty']]
                ,$pageSize,'id desc');
        $result = UtilService::getParam([
            'id',
            'username',
            'name',
            'avatar',
            'status',
            [ 'regTime', 'time', 'date("Y-m-d",$1)'],
            [ 'lastTime', 'last_time', 'date("Y-m-d",$1)'],
            'ip',
            ['ipArea','ip',
                function($item) {
                    return  (new utils\IpLocation())->getlocation($item)['country'];
                }
            ],
            ['roleName','role_id',
                function($item) use($adminRole) {
                    return $adminRole->getRoleId($item,'name');
                }
            ]
        ],$data);
        return app('json')->success([
            'list'      => $result,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $page
        ]);
    }


    /**
     * 保存数据
     * @param Request $request
     */
    public function  save(Request $request) {
        $post = UtilService::getMore(
            [
                ['id','0'],
                ['sassId','','empty','参数错误'],
                ['name','','empty','请输入姓名'],
                ['username','','empty','请输入账户'],
                ['status','0'],
                ['role_id','','empty','请选择管理角色'],
                ['password',''],
                ['avatar','']
            ],
            $request
        );
        $bool = AdminModel::saveAdmin($post);
        if($bool)
            return app('json')->success("操作成功", []);
        else
            return app('json')->fail(AdminModel::getErrorInfo());
    }

    /**
     * 删除数据
     * @param Request $request
     */
    public function del(Request $request) {
        [$id] = UtilService::getMore(
            [
                ['id','0'],
            ],
            $request,true
        );
        AdminModel::del($id);
        return app('json')->success("删除成功", []);
    }


}
