<?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 Modules\Member\Services;

use Modules\Member\Models\Fields;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;

class FieldsService {

	/**
	 * @title 字段列表
	 *
	 * @param [type] $request
	 * @return void
	 */
	public function getDataList($request) {
		$map = [];

		if ($request->filled('status')) {
			$map[] = ['status', '=', $request->input('status')];
		}

		$query = Fields::where($map);

		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->limit($request->input('limit', 10))->get();
		}

		return $data;
	}

	/**
	 * @title 创建字段
	 *
	 * @param [type] $request
	 * @return void
	 */
	public function create($request) {
		$request->validate([
			'name' => 'required|unique:cms_model_field',
			'title' => 'required',
			'type' => 'required',
		]);
		$field = new Fields();

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

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

	/**
	 * @title 更新字段
	 *
	 * @param [type] $request
	 * @return void
	 */
	public function update($request) {
		$request->validate([
			'name' => 'required|unique:cms_model_field,name,' . $request->input('id'),
			'title' => 'required',
			'type' => 'required',
		]);
		$field = Fields::find($request->input('id'));

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

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

	/**
	 * @title 删除字段
	 *
	 * @param [type] $request
	 * @return void
	 */
	public function delete($request) {
		if($request->filled('id')){
			try {
				$field = Fields::findOrFail($request->input('id'));
			} catch (\Throwable $th) {
				throw new \Exception("模型不存在！", 1);
			}
			$field->delete();
		}
		if($request->filled('ids')){
			try {
				$field = Fields::whereIn('id', $request->input('ids'));
				$field->delete();
			} catch (\Throwable $th) {
				throw new \Exception($th->getMessage(), 1);
			}
		}
		return $field;
	}

	public function getFieldList($request){
		$fields = Schema::getColumnListing('member_extends');

		$data = [];
		foreach ($fields as $value) {
			if ($value !== $request->input('name'))
			$data[] = ['title' => $value, 'values' => $value];
		}
		return $data;
	}

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

		$query = Fields::with('model:id,title,name,icon');

		if ($request->filled('model')) {
			$query->whereHas('model', function($q) use($request) {
				$q->where('name', $request->input('model'));
			});
		}

		$data = $query->get();
		return $data;
	}
}
