<?php

namespace app\mainapp\controller;

use app\mainapp\BaseController;

use app\common\model\Activity as ActivityModel;
use app\common\model\ActivityJoin as ActivityJoinModel;

class Activity extends BaseController
{

    public function listActivity()
    {
        $ppage = input('ppage/d', 1);
        $psize = input('psize/d', 20);
        $map[] = ['status', '=', 1];
        $plist = ActivityModel::where($map)->order(['priority' => 'desc', 'id' => 'desc'])->page($ppage)->limit($psize)->select();
        page_result(0, "", [
            'plist'   => $plist,
            'pstatus' => $psize > count($plist) ? 'noMore' : 'more',
        ]);
    }

    public function detailActivity()
    {
        $id   = input('id/d', 0);
        $list = ActivityModel::findOrEmpty($id);
        if ($list->isEmpty()) {
            page_result(1, "活动不存在");
        }

        $userid      = input('userid/d', 0);
        $join_status = ActivityJoinModel::where([['user_id', '=', $userid], ['activity_id', '=', $id]])->count();

        page_result(0, "", [
            'list'        => $list,
            'join_status' => $join_status > 0 ? true : false,
        ]);
    }

    public function joinActivity()
    {
        $id     = input('id/d', 0);
        $userid = input('userid/d', 0);
        if (empty($id) || empty($userid)) {
            page_result(1, "数据错误");
        }

        $info = ActivityModel::findOrEmpty($id);
        if ($info->isEmpty()) {
            page_result(1, "活动不存在");
        }

        if (strtotime($info->start_time) < time()) {
            page_result(1, "活动报名时间已过");
        }

        $join = ActivityJoinModel::where([
            ['user_id','=',$userid],
            ['activity_id','=',$id],
            ['status','=',1],
        ])->find();
        if (!empty($join)) {
            page_result(1, "请勿重复参加");
        }

        $info->join_num++;
        $info->save();

        ActivityJoinModel::create([
            'user_id'     => $userid,
            'activity_id' => $id,
            'create_time' => time(),
        ]);

        page_result(0);
    }

    public function joinList()
    {
        $userid = input('userid/d', 0);
        if (empty($userid)) {
            page_result(1, "请先登录");
        }

        $ppage = input('ppage/d', 1);
        $psize = input('psize/d', 20);
        $map[] = ['user_id', '=', $userid];
        $plist = ActivityJoinModel::with('activity')->where($map)->order(['id' => 'desc'])->page($ppage)->limit($psize)->append(['status_text'])->select();
        page_result(0, "", [
            'plist'   => $plist,
            'pstatus' => $psize > count($plist) ? 'noMore' : 'more',
        ]);
    }

    public function cancelJoin()
    {
        $id = input('id/d', 0);
        if (empty($id)) {
            page_result(1, "数据错误");
        }

        $join = ActivityJoinModel::find($id);
        if (empty($join)) {
            page_result(1, "数据错误");
        }
        if ($join->status == 2) {
            page_result(0, "操作成功");
        }
        $join->cancel_time = time();
        $join->status = 2;
        $join->save();

        ActivityModel::where('id' , $join['activity_id'])->dec('join_num')->update();

        page_result(0, "操作成功");
    }
}