<?php

namespace app\adminapi\controller;

use app\adminapi\BaseController;
use support\Request;
use think\facade\Db;

class ConfigController extends BaseController
{
    public function getConfig(Request $request)
    {
        $param = $request->get();
        // $where[] = ['lang', $param['lang']];

        $list = Db::name('config')
            ->where('lang',$param['lang'])
            ->select()
            ->toArray();
        
        $result = [];

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

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

    public function updateConfig(Request $request)
    {
        $param = $request->post();

        try{
            foreach ($param as $key => $val) {
                if($key != 'lang'){
                    $data = [
                        'value' => $val,
                    ];
        
                    Db::name('config')
                        ->where('code',$key)
                        ->where('lang',$param['lang'])
                        ->update($data);
                }
            }
            return $this->Success('更新成功');
        }catch(\Exception $e){
            return $this->Error('更新失敗');
        }
    }
}
