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

class ExtendService {

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

		$query = MemberExtends::with('member:uid,username,nickname,avatar,level_id,level_expire_time');

		if ($request->filled('member_id')){
			$query->where('member_id', $request->input('member_id'));
		}

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

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

		$query->where($map);

		$query->orderBy($request->input('order', 'id'), $request->input('sort', 'desc'));
		if ($request->filled('page')){
			$data = [
				'total' => $query->count(),
				'page' => (int) $request->input('page', 1),
				'data' => $query->offset($request->input('offset', 0))->limit($request->input('limit', 30))->get(),
			];
		}else{
			$data = $query->limit($request->input('limit', 10))->get();
		}

		return $data;
	}

	public function getDetail($request){
		if ($request->filled('member_id')){
			$member_id = $request->input('member_id');
		} else {
			$member_id = auth('api')->user()['uid'];
		}

		$extends = MemberExtends::with('member:uid,username,nickname,avatar,level_id,level_expire_time', 'member.level')->where('member_id', $member_id)->first();

		if (!$extends){
			throw new \Exception('用户数据不存在');
		}
		if ($extends->status == 0){
			throw new \Exception('用户数据审核中');
		}
		if ($extends->status == 2){
			throw new \Exception('用户数据审核未通过');
		}

		return $extends ?? [];
	}

	public function update($request){
		$field = Fields::all();

		if ($request->filled('member_id')){
			$member_id = $request->input('member_id');
		} else {
			$member_id = auth('api')->user()['uid'];
		}

		$extends = MemberExtends::where('member_id', $member_id)->first();
		if (!$extends){
			$extends = new MemberExtends();
			$extends->member_id = $member_id;
		}

		foreach ($field as $key => $value) {
			if ($request->filled($value['name']) && $request->input($value['name']) != ''){
				$extends->{$value['name']} = $request->input($value['name']);
			}
		}

		$extends->status = 0;
		$extends->save();
		return $extends;
	}

	public function audit($request){
		$request->validate([
			'id' => 'required',
			'member_id' => 'required',
			'level_id' => 'required',
			'status' => 'required|integer|in:0,1,2',
			'level_expire_time' => 'required|date',
		], [
			'id.required' => 'ID不能为空',
			'member_id.required' => '用户ID不能为空',
			'level_id.required' => '等级ID不能为空',
			'status.required' => '审核状态不能为空',
			'status.integer' => '审核状态必须为数字',
			'status.in' => '审核状态值错误',
			'level_expire_time.required' => '等级过期时间不能为空',
			'level_expire_time.date' => '等级过期时间格式错误',
		]);

		$extends = MemberExtends::where('id', $request->input('id'))->first();

		if (!$extends){
			throw new \Exception('审核数据不存在');
		}

		$extends->status = $request->input('status');
		$extends->card_number = 'NF9' . str_pad($extends->member_id, 6, '0', STR_PAD_LEFT);

		$extends->save();

		if ($request->input('status') == 1){
			$member = $extends->member()->first();
			$member->level_id = $request->input('level_id');
			$member->level_expire_time = $request->input('level_expire_time');
			if ($extends->name){
				$member->nickname = $extends->name;
			}
			if ($extends->mobile){
				$member->mobile = $extends->mobile;
			}
			if ($extends->email){
				$member->email = $extends->email;
			}
			$member->save();
		}
		return $extends;
	}
}