<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2024 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace App\Services\System;

use App\Models\System\Client;

class ClientService {

	public function getDataList($request){
		$map = [];

		if($request->filled('title')){
			$map[] = ['title', 'like', '%' . $request->input('title') . '%'];
		}

		$query = Client::where($map)->orderBy('id', 'asc');

		if($request->filled('page')){
			$data = [
				'total' => $query->count(),
				'page' => $request->input('page', 1),
				'data' => $query->offset($request->input('offset', 0))->limit($request->input('limit', 10))->get(),
			];
		}else{
			$data = $query->get();
		}
		return $data;
	}

	public function create($request){
		$request->validate([
			'title' => 'required',
		]);
		$client = new Client();

		foreach ($client->setFilterFields($request->all()) as $key => $value) {
			$client->$key = $value;
		}

		$client->app_id = '';
		$client->secret = '';
		$client->redirect = '';
		$client->save();

		return $client;
	}

	public function update($request){
		$request->validate([
			'id' => 'required',
			'title' => 'required',
		], [
			'id.required' => '参数错误',
			'title.required' => '请输入标题',
		]);

		$client = Client::where('id', $request->input('id'))->first();

		$client->title = $request->input('title');
		$client->status = $request->input('status', 0);

		$client->save();
		return $client;
	}

	public function delete($request){
		$request->validate([
			'id' => 'required',
		], [
			'id.required' => '参数错误',
		]);

		$client = Client::where('id', $request->input('id'))->first();

		$client->delete();
		return $client;
	}
}
