<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User\UserModel;
use App\Models\Welfare\LotteryModel;
use App\Models\Welfare\TicketModel;
use App\Models\Welfare\PrizeModel;
use App\Models\Welfare\StoreModel;

class WelfareController extends Controller
{


    /**
     * 创建抽奖活动
     * @param  Request  $request
     * @return array|\Illuminate\Http\JsonResponse
     */
    public function createLottery(Request $request)
    {
        $this->validate($request, [
            'title' => 'required|max:32',
            'drawed_at' => 'required|integer',
            'showed_at' => 'required|integer',
            'cost' => 'required|max:16',
            'cover_url' => 'required|url',
            'banner_url' => 'url',
            'count' => 'required|integer',
            'lottery_explain' => 'required',
        ], [
            'title.required' => '请输入标题',
            'title.max' => '字数不能超过32个字符',
            'cost.required' => '请输入价格',
            'cost.max' => '字数不能超过16个字符',
            'drawed_at.*' => '请选择开奖时间',
            'showed_at.*' => '清选择显示时间',
            'cover_url.*' => '上传封面',
            'banner_url.*' => '上传长图',
            'count.*' => '请填写数字',
            'lottery_explain.*' => '抽奖说明',
        ]);
        $lottery = LotteryModel::create($request->all());
        return response()->json([
            'code' => 200,
            'message' => 'OK',
            'data' => $lottery
        ]);
    }


    /**
     * 修改活动
     * @param  Request  $request
     * @return array|\Illuminate\Http\JsonResponse
     */
    public function updateLottery(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer',
            'title' => 'max:32',
            'cost' => 'max:16',
            'drawed_at' => 'integer',
            'showed_at' => 'integer',
            'cover_url' => 'url',
            'count' => 'integer',
            'deleted_at' => 'integer',
        ], [
            'title.*' => '字数不能超过32个字符',
            'cost.*' => '字数不能超过16个字符',
            'drawed_at.*' => '请选择开奖时间',
            'showed_at.*' => '清选择显示时间',
            'cover_url.*' => '上传图片',
            'count.*' => '请填写数字',
            'lottery_explain.*' => '抽奖说明',
            'receive_explain.*' => '兑奖说明',
        ]);
        $lottery = LotteryModel::findOrFail($request->id);
        $lottery->fill($request->all());
        $lottery->save();
        return response()->json([
            'code' => 200,
            'message' => 'OK',
            'data' => $lottery
        ]);
    }


    /**
     * 抽奖活动列表
     * @param  Request  $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function lotterys(Request $request)
    {
        $per_page = $request->input('per_page', 10);
        $page = $request->input('page', 1);
        $offset = ($page - 1) * $per_page;
        $lotterys = LotteryModel::when($request->filled('draw'), function ($query) use ($request) {
            if ($request->draw) {
                return $query->where('drawed_at', '<=', time());
            } else {
                return $query->where('drawed_at', '>', time());
            }
        });
        $count = $lotterys->count();
        $list = $lotterys->orderBy('drawed_at', 'desc')->offset($offset)->limit($per_page)->get();
        foreach ($list as $lottery) {
            $lottery->ticket_count = TicketModel::where('lottery_id', $lottery->id)->count();
            $lottery->ticket_number = TicketModel::where('lottery_id', $lottery->id)->distinct()->count('uid');
        }
        return response()->json([
            'code' => 200,
            'message' => 'OK',
            'data' => [
                'list' => $list,
                'count' => $count,
                'per_page' => $per_page,
                'page' => $page
            ]
        ]);
    }


    /**
     * 兑换码列表
     * @param  Request  $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function tickets(Request $request)
    {
        $per_page = $request->input('per_page', 20);
        $page = $request->input('page', 1);
        $offset = ($page - 1) * $per_page;
        $builder = TicketModel::when($request->filled('lottery_id'), function ($query) use ($request) {
            return $query->where('lottery_id', $request->lottery_id);
        })->when($request->filled('uid'), function ($query) use ($request) {
            return $query->where('uid', $request->uid);
        })->when($request->filled('ticket_code'), function ($query) use ($request) {
            return $query->where('ticket_code', $request->ticket_code);
        })->when($request->filled('lucky'), function ($query) use ($request) {
            return $query->where('lucky', $request->lucky);
        });
        $total = $builder->count();
        $tickets = $builder->orderBy('id', 'desc')->offset($offset)->limit($per_page)->get();
        foreach ($tickets as $ticket) {
            $ticket->username = UserModel::select('uid', 'nickname', 'headimgurl')->where('uid', $ticket->uid)->first();
            $ticket->lottery = LotteryModel::select('title')->where('id', $ticket->lottery_id)->first();
        }
        return response()->json([
            'code' => 200,
            'message' => 'OK',
            'data' => [
                'list' => $tickets,
                'count' => $total,
                'per_page' => $per_page,
                'page' => $page
            ]
        ]);
    }


    /**
     * 修改ticket
     * @param  Request  $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateTicket(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer',
            'lucky' => 'boolean',
        ], [
            'id.*' => '请选择一个抽奖码',
            'lucky.*' => '请设置是否中奖',
        ]);
        $ticket = TicketModel::findOrFail($request->id);
        $ticket->fill($request->all());
        $ticket->save();
        return response()->json([
            'code' => '200',
            'message' => 'OK'
        ]);
    }


    /**
     * 商品列表
     * @param  Request  $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function stores(Request $request)
    {
        $per_page = $request->input('per_page', 10);
        $page = $request->input('page', 1);
        $offset = ($page - 1) * $per_page;
        $builder = new StoreModel();
        $total = $builder->count();
        $lotterys = $builder->orderBy('id', 'desc')->offset($offset)->limit($per_page)->get();
        return response()->json([
            'code' => 200,
            'message' => 'OK',
            'data' => [
                'list' => $lotterys,
                'count' => $total,
                'per_page' => $per_page,
                'page' => $page
            ]
        ]);
    }


    /**
     * 创建商品
     * @param  Request  $request
     * @return array|\Illuminate\Http\JsonResponse
     */
    public function createStore(Request $request)
    {
        $this->validate($request, [
            'title' => 'required|max:32',
            'cost' => 'required|max:16',
            'cover_url' => 'required|url',
            'price' => 'required|integer',
            'state' => 'boolean',
            'count' => 'required|integer',
            'receive_explain' => 'required',
        ], [
            'title.required' => '请输入标题',
            'title.max' => '字数不能超过32个字符',
            'cost.required' => '请输入价格',
            'cost.max' => '字数不能超过16个字符',
            'drawed_at.*' => '请选择开奖时间',
            'cover_url.*' => '上传封面图片',
            'price.*' => '请设置拼图数量',
            'state.*' => '请设置是否上下架',
            'count.*' => '请填写数字',
            'receive_explain.*' => '兑奖说明',
        ]);
        $store = StoreModel::create($request->all());
        return response()->json([
            'code' => 200,
            'message' => 'OK',
            'data' => $store,
        ]);
    }


    /**
     * 修改商品
     * @param  Request  $request
     * @return array|\Illuminate\Http\JsonResponse
     */
    public function updateStore(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer',
            'title' => 'max:32',
            'cost' => 'max:16',
            'cover_url' => 'url',
            'price' => 'integer',
            'state' => 'boolean',
            'count' => 'integer',
        ], [
            'id.*' => '请选择一个商品',
            'title.max' => '字数不能超过32个字符',
            'cost.max' => '字数不能超过16个字符',
            'drawed_at.*' => '请选择开奖时间',
            'cover_url.*' => '上传封面图片',
            'price.*' => '请设置拼图数量',
            'state.*' => '请设置是否上下架',
            'count.*' => '请填写数字',
        ]);
        $store = StoreModel::findOrFail($request->id);
        $store->fill($request->all());
        $store->save();
        return response()->json([
            'code' => 200,
            'message' => 'OK',
            'data' => $store,
        ]);
    }


    /**
     * 奖品兑换列表
     * @param  Request  $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function prizes(Request $request)
    {
        $per_page = $request->input('per_page', 20);
        $page = $request->input('page', 1);
        $offset = ($page - 1) * $per_page;
        $builder = PrizeModel::when($request->filled('title'), function ($query) use ($request) {
            return $query->where('title', $request->title);
        })->when($request->filled('name'), function ($query) use ($request) {
            return $query->where('name', $request->name);
        })->when($request->filled('uid'), function ($query) use ($request) {
            return $query->where('uid', $request->uid);
        })->when($request->filled("state"), function ($query) use ($request) {
            return $query->where('state', $request->state);
        });
        $total = $builder->count();
        $prizes = $builder->orderBy('id', 'desc')->offset($offset)->limit($per_page)->get();
        return response()->json([
            'code' => 200,
            'message' => 'OK',
            'data' => [
                'list' => $prizes,
                'count' => $total,
                'per_page' => $per_page,
                'page' => $page
            ]
        ]);
    }

    /**
     * 领取奖品
     * @param  Request  $request
     * @return array|\Illuminate\Http\JsonResponse
     */
    public function updatePrize(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer',
            'name' => 'max:32',
            'weixin' => 'max:32',
            'state' => 'integer',
        ], [
            'id.*' => '请选择一个',
            'name.*' => '请填写正确的姓名',
            'weixin.*' => '请填写正确的联系方式',
            'state.*' => '请设置领取状态',
        ]);
        $prize = PrizeModel::findOrFail($request->id);
        $prize->fill($request->all());
        $prize->save();
        return response()->json([
            'code' => 200,
            'message' => 'OK'
        ]);
    }
}
