<?php

namespace app\admin\controller;

use app\admin\BaseController;

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

class Activity extends BaseController
{

    public function index()
    {
        return view('activity/index');
    }

    public function listActivity()
    {
        $limit = input('limit');
        $page  = input('page');

        $where = [];
        $title = input('title', '');
        if (!empty($title)) {
            $where[] = ['title', 'like', "%{$title}%"];
        }
        $status = input('status', 0);
        if (!empty($status)) {
            $where[] = ['status', '=', $status];
        }

        $list  = ActivityModel::order(['priority' => 'desc', 'id' => 'desc'])->where($where)->limit($limit)->page($page)->select()->append(['status_text']);
        $count = ActivityModel::where($where)->count();
        if ($count == 0) {
            exit(json_encode([
                'code' => 1,
                'msg'  => "未查询到数据",
            ]));
        }
        exit(json_encode([
            'code'  => 0,
            'msg'   => "",
            'count' => $count,
            'data'  => $list,
        ]));
    }

    public function form()
    {
        $id   = input('id/d, 0');
        $info = ActivityModel::findOrEmpty($id);
        return view('activity/form', [
            'info' => $info,
        ]);
    }

    public function edit()
    {
        $id = input('id/d');
        if (empty($id)) {
            ActivityModel::create([
                'title'       => input('title/s'),
                'main_image'  => input('main_image/s'),
                'content'     => input('content/s'),
                'address'     => input('address/s'),
                'start_time'  => strtotime(input('start_time/s')),
                'create_time' => time(),
                'priority'    => input('priority/d'),
                'status'      => input('status/d') == 1 ? 1 : 2,
            ]);
        } else {
            ActivityModel::update([
                'id'         => $id,
                'title'      => input('title/s'),
                'main_image' => input('main_image/s'),
                'content'    => input('content/s'),
                'address'    => input('address/s'),
                'start_time' => strtotime(input('start_time/s')),
                'priority'   => input('priority/d'),
                'status'     => input('status/d') == 1 ? 1 : 2,
            ]);
        }
        exit(json_encode([
            'code' => 0,
        ]));
    }

    public function del()
    {
        $id    = input('id/d');
        $count = ActivityJoinModel::where('activity_id', $id)->where('status', 1)->count();
        if ($count > 0) {
            exit(json_encode([
                'code' => 1,
                'msg'  => "已有报名的活动无法删除",
            ]));
        }

        ActivityModel::where('id', $id)->delete();
        ActivityJoinModel::where('activity_id', $id)->delete();
        exit(json_encode([
            'code' => 0,
            'msg'  => "",
        ]));
    }

    public function joinList()
    {
        return view('activity/joinlist');
    }

    public function listJoin()
    {
        $limit = input('limit');
        $page  = input('page');

        $where  = [];
        $status = input('status', 0);
        if (!empty($status)) {
            $where[] = ['status', '=', $status];
        }

        $list  = ActivityJoinModel::with(['user'])->order(['create_time' => 'desc'])->where($where)->limit($limit)->append(['status_text'])->page($page)->select();
        $count = ActivityJoinModel::where($where)->count();
        if ($count == 0) {
            exit(json_encode([
                'code' => 1,
                'msg'  => "未查询到数据",
            ]));
        }
        foreach ($list as $v) {
            $v['realname'] = $v['user']['realname'];
            $v['mobile']   = $v['user']['mobile'];
        }
        exit(json_encode([
            'code'  => 0,
            'msg'   => "",
            'count' => $count,
            'data'  => $list,
        ]));
    }
}