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

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

class Site extends ApiController
{
    public function getSiteConfig(){
        $result = Db::name('site_config')
        ->where('parent_id','<>',0)
        ->select();

        foreach($result as $key => $val){
            $rtn[$val['code']]=$val['value'];
        }

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

    public function setSiteConfig(){
        $data = input();
        unset($data['version']);
        unset($data['controller']);
        unset($data['action']);
        try{
            foreach($data as $key => $val){
                Db::name('site_config')
                ->where('code',$key)
                ->update(['value'=>$val]);
            }    
        }catch(\Exception $e){
            return $this->error('更新失敗');        
        }

        return $this->success('更新成功');        
    }

    public function getAgents(){
        $result = Db::name('agent')
        ->select();

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