<?php
namespace app\api\controller;
use app\common\controller\Api;
use liuniu\UtilService;
use think\Request;
use app\common\model\training\{Training as TrainingModel,TrainingInfo,Volunteer};

class Training extends Api
{
    protected $noNeedRight = ['*'];
    public function lst()
    {

        $where = UtilService::getMore(
            [
                ['status',1],
                ['page',1],
                ['limit',10],
                ['user_id',$this->auth->getUserinfo()['id']],
            ]
        );
        $where['cid'] = $this->cid;
        return $this->success('获取成功',TrainingModel::lst($where));
    }
    public function apply(Request $request)
    {
        $where = UtilService::postMore(
            [
                ['training_id',0],
                ['user_id',$this->auth->getUserinfo()['id']],
                ['cid',$this->cid],
                ['name',''],
                ['Phone',''],
                ['num',0],
            ],$request
        );
        if($where['training_id']==0) $this->error('活动编号不能为空');
        if($where['name']=='') $this->error('姓名不能为空');
        if($where['Phone']=='') $this->error('电话不能为空');
        if($where['num']==0) $this->error('人数不能为0');
        if(TrainingInfo::Where('training_id',$where['training_id'])->where('user_id',$where['user_id'])->find()) $this->error('已报名，不能重复报名!');
        $rs = TrainingInfo::create($where);
        TrainingModel::where('id',$where['training_id'])->inc('number',$where['num'])->update();
        $this->success('创建成功',$rs);
    }
    public function mylst(Request $request)
    {
        $where = UtilService::getMore(
            [
                ['cid',$this->cid],
                ['user_id',$this->auth->getUserinfo()['id']],
                ['page',1],
                ['limit',10],
            ]
        );
        return $this->success('获取成功',TrainingInfo::lst($where));
    }
    public function view()
    {
        $id = input('id',0);
        if($id==0) $this->error('参数错误!');
        $rs = TrainingModel::info($this->cid,$id);
        if(!$rs)$this->error(TrainingModel::getErrorInfo());
        return $this->success('获取成功',$rs);
    }
}