<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Children;
use App\Models\User;
use Validator;
use App\Models\SignUp;
use App\Models\Leave;

class ChildController extends ApiController {
	/**
	 *
	 * @api {post} /child/store 新增孩子
	 * @apiName store
	 * @apiGroup Children
	 * @apiParam {String} sex 性别 必填
	 * @apiParam {String} realname 真实姓名必填
	 * @apiParam {String} birthday 生日
	 * @apiParam {String} relation 与孩子关系 必填
	 * @apiParam {String} photo 只传入七牛返回的key
	 * @apiPermission token
	 */
	function store(Request $request) {
		$this->validate ( $request, [ 
				'sex' => 'required',
				'realname' => 'required',
				'relation' => 'required'
		] );
		if (isset ( $request->photo ) && $request->photo != '') {
			$request ['photo'] = getQiniuImgUrl ( $request->photo );
		}
		$request ['parent_id'] = $request->user_id;
		Children::Create ( $request->all () );
		return $this->success ();
	}
	/**
	 *
	 * @api {post} /child/lists 孩子列表
	 * @apiName lists
	 * @apiGroup Children
	 * @apiParam {String} page 页码 default:1
	 * @apiSuccess {int} current_page 当前页码
	 * @apiSuccess {int} total 总数
	 * @apiSuccess {list[]} data 分页数据
	 * @apiSuccess (data){int} id
	 * @apiSuccess (data){String} realname 真实姓名
	 * @apiPermission token
	 */
	function lists(Request $request) {
		$page = $request->input ( 'page', 1 );
		$children = Children::where ( 'parent_id', $request->user_id )->get ();
		return $this->success ( $children );
	}
	/**
	 *
	 * @api {post} /child/destroy 删除孩子
	 * @apiName destroy
	 * @apiGroup Children
	 * @apiParam {int} id 孩子ID
	 * @apiPermission token
	 */
	function destroy(Request $request) {
		Children::destroy ( $request->id );
		return $this->success ();
	}
	/**
	 *
	 * @api {post} /child/show 孩子详情
	 * @apiName show
	 * @apiGroup Children
	 * @apiParam {int} id 孩子ID
	 * @apiSuccess {String} realname 真实姓名
	 * @apiSuccess {String} birthday 生日
	 * @apiSuccess {String} relation 关系
	 * @apiSuccess {String} photo 图片完整链接
	 * @apiSuccess {String} is_join 是否加入托管班
	 * @apiPermission token
	 */
	function show(Request $request) {
		$child = Children::find ( $request->id );
		$count = OrgChild::where ( 'child_id', $request->id )->where ( 'status', 1 )->count ();
		$child->is_join = $count;
		return $this->success ( $child );
	}
	/**
	 *
	 * @api {post} /child/bind 申请绑定
	 * @apiName bind
	 * @apiGroup Children
	 * @apiParam {int} child_id 孩子ID 如果是注册流程中的申请，可不传
	 * @apiParam {int} mobile 教师手机号  如果此手机号不是机构教师会返回错误
	 * @apiSuccess {String} notice 提示语 弹出即可
	 */
	function bind(Request $request) {
		// 如果child_id为空,则寻找该用户的第一个孩子
		if (! $request->child_id) {
			$child = Children::where ( 'parent_id', $request->user_id )->first ();
			if (! $child)
				return $this->error ( 422, "不存在该孩子" );
			$request ['child_id'] = $child->id;
		}
		$user = User::where ( 'mobile', $request->mobile )->where ( 'type', 2 )->where ( 'oid', '!=', 0 )->first ();

		if (! isset ( $user )) {
			return $this->error ( 422, "不存在该老师" );
		}
		$org = Organize::find ( $user->oid );
		if (! isset ( $org )) {
			return $this->error ( 422, "不存在该机构" );
		}
		$count = OrgChild::where ( 'child_id', $request->child_id )->where ( 'org_id', $org->id )->count ();
		if ($count)
			return $this->error ( 422, "请勿重复申请" );
		$request ['org_id'] = $org->id;
		OrgChild::create ( $request->all () );
		return $this->success ( [ 
				'notice' => "您已成功申请加入:" . $org->name
		] );
	}
	/**
	 *
	 * @api {post} /child/update 修改孩子
	 * @apiName update
	 * @apiGroup Children
	 * @apiParam {int} id 孩子id
	 * @apiParam {String} sex 性别
	 * @apiParam {String} realname 真实姓名
	 * @apiParam {String} birthday 生日
	 * @apiParam {String} relation 与孩子关系 必填
	 * @apiParam {String} photo 只传入七牛返回的key
	 * @apiPermission token
	 */
	function update(Request $request) {
		$child = Children::find ( $request->id );
		if (! isset ( $child )) {
			return $this->error ( 422, "数据不存在" );
		}
		if (isset ( $request->photo ) && $request->photo != '') {
			$request ['photo'] = getQiniuImgUrl ( $request->photo );
		}
		$request ['parent_id'] = $request->user_id;
		$child->update ( $request->all () );
		return $this->success ();
	}

	/**
	 *
	 * @Description:在线报名
	 * @param Request $request
	 * @return string[]|unknown[]|number[]
	 */
	function signUp(Request $request) {
		SignUp::create ( $request->all () );
		return $this->success ();
	}
	/**
	 *
	 * @Description:
	 * @param Request $request
	 */
	function leave(Request $request) {
		$time = time ();
		$w = date ( 'w', $time );
		if (($w != 0 and $w != 6)) {
		} else {
			return $this->error ( 1001, '周一至周五才允许请假' );
		}
		$reason = [ 
				'生病',
				'临时有事',
				'不想来了',
				'其他原因',
				'长期病假'
		];
		$request ['reason'] = $reason [$request->reasonIdex];
		Leave::create ( $request->all () );
		// 给对应教师发送短信通知
		return $this->success ();
	}
	/**
	 * @Description:取消请假
	 * @param Request $request
	 * @return string[]|unknown[]|number[]
	 */
	function cancelLeave(Request $request) {
		$where['child_id']=$request->child_id;
		$where['class_id']=$request->class_id;
		$where['class_date']=$request->class_date;
		Leave::where($where)->delete();
		return $this->success ();
	}
}
