<?php
namespace app\adminapi\controller\v1;

use app\adminapi\ApiController;
use think\facade\Db;

class Admin extends ApiController
{
    public function getAdminUsers()
    {
        if(!input('search')){
            $result=Db::name('admin')
            ->page(input('current'),input('size'))
            ->select();
    
            $total=Db::name('admin')
            ->count();    
        }else{
            $result=Db::name('admin')
            ->where('username','like','%'.input('search').'%')
            ->page(input('current'),input('size'))
            ->select();
    
            $total=Db::name('admin')
            ->where('username','like','%'.input('search').'%')
            ->count();    
        }

        if(!$result){
            $result=[];
        }

        $rtn=[
            'total' => $total,
            'data' => $result
        ];

        return $this->Success($rtn);
    }

    public function getUser(){

        $id=input('id');

        $result=Db::name('admin')
        ->field('id,username,email,status,role_id as role')
        ->where('id',$id)
        ->find();

        if(!$result){
            $result=[];
        }

        return $this->success($result);
    }

    public function addUser(){

        $req=input();

        $password_hash = password_hash($req['password'], PASSWORD_DEFAULT);

        $data=[
            'username'      => $req['username'],
            'password'      => $password_hash,
            'email'         => $req['email'],
            'action_list'   => '',
            'status'        => $req['status'],
        ];

        $result=Db::name('admin')
        ->insert($data);

        if(!$result){
            $result=[];
        }

        return $this->success($result);
    }

    public function updateUser(){

        $req=input();
        $data=[
            'username'      => $req['username'],
            'email'         => $req['email'],
            'role_id'       => $req['role'],
            'action_list'   => '',
            'status'        => $req['status'],
        ];

        if(strlen($req['password'])>0){
            $data['password']=$req['password'];
        }

        $result=Db::name('admin')
        ->where('id',$req['id'])
        ->update($data);

        if(!$result){
            $result=[];
        }

        return $this->success($result);
    }

    public function deleteUser(){
        $id=input('id');

        $result=Db::name('admin')
        ->where('id',$id)
        ->delete();

        return $this->success($result);
    }

    public function updateStatus(){
        $id=input('id');
        $status=input('status');

        $result=Db::name('admin')
        ->where('id',$id)
        ->update(['status'=>$status]);

        if(!$result){
            $result=[];
        }

        return $this->success($result);
    }


    public function getAdminLogs()
    {
        if(!input('search')){
            $result=Db::name('admin_log')
            ->page(input('current'),input('size'))
            ->order('id','desc')
            ->select();
    
            $total=Db::name('admin_log')
            ->count();    
        }else{
            $result=Db::name('admin_log')
            ->where('admin_name','like','%'.input('search').'%')
            ->page(input('current'),input('size'))
            ->order('id','desc')
            ->select();
    
            $total=Db::name('admin_log')
            ->where('admin_name','like','%'.input('search').'%')
            ->count();    
        }

        if(!$result){
            $result=[];
        }

        $rtn=[
            'total' => $total,
            'data' => $result
        ];

        return $this->Success($rtn);
    }

}
