<?php

namespace App\Http\Controllers\Admin;

use App\Exports\LotteryMembersExport;
use App\Exports\LotteryWinMembersExport;
use App\Facades\CommonService;
use App\Http\Controllers\Controller;
use App\Http\Response\ResponseJson;
use App\Models\Activity;
use App\Models\Lottery;
use App\Models\LotteryAward;
use App\Models\LotteryMember;
use App\Models\RegularActivity;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;

class LotteryController extends Controller
{
    use ResponseJson;

    public function index()
    {
        $lotteries = Lottery::with("lotteryable")->keyword()->orderByDesc('id')->paginate();
        foreach ($lotteries as $lottery) {
            $type = '';
            $lottery_name = '';
            if ($lottery->lotteryable_type == RegularActivity::class) {
                $type = Lottery::REGULAR_ACTIVITY_TYPE;
                $lottery_name = $lottery->lotteryable?$lottery->lotteryable->title:"";
            }elseif ($lottery->lotteryable_type == Activity::class) {
                $type = Lottery::ACTIVITY_TYPE;
                $lottery_name = $lottery->lotteryable?$lottery->lotteryable->title:"";
            }
            $lottery->type = $type;
            $lottery->lottery_name  = $lottery_name;
            unset($lottery->lotteryable);
        }
        return $this->success('ok', $lotteries);
    }

    public function store(Request $request)
    {
        DB::beginTransaction();
        $data = $request->all();
        $lottery = Lottery::create($data);
        $lottery->path = config('app.url').'/award/#/newLuckyDraw?id='.$lottery->id;
        $lottery->new_path = config('app.url').'/award/#/newLuckyDrawUpdate?id='.$lottery->id;

        //生成二维码
        $lottery->qrcode = CommonService::makeQrcode($lottery->path);
        $lottery->new_qrcode = CommonService::makeQrcode($lottery->new_path);

        $apply_path = config('app.url').'/h5/#/luckyDrawData?id='.$lottery->id;
        $lottery->apply_qrcode = CommonService::makeQrcode($apply_path);;

        $lottery->update(['path'=>$lottery->path, 'new_path'=>$lottery->new_path,  'qrcode'=>$lottery->qrcode, "new_qrcode"=>$lottery->new_qrcode, 'apply_qrcode'=>$lottery->apply_qrcode]);
        DB::commit();
        return $this->success('ok');
    }

    public function show(Request $request, Lottery $lottery)
    {
        $lottery->awards;
        return $this->success('ok', $lottery);
    }

    public function update(Request $request, Lottery $lottery)
    {
        $data = $request->all();
        $lottery->update($data);
        return $this->success('ok');
    }

    public function destroy(Request $request, Lottery $lottery)
    {
        if ($lottery->lotteryable) return $this->failure("关联其他活动，不支持删除");
        $lottery->delete();
        return $this->success('ok');
    }

    public function members(Request $request, Lottery $lottery)
    {
        $members = $lottery->members()->keyword()->orderByDesc('id');
        $export = $request->export;
        if ($export) {
            $members = $members->get();
            return Excel::download(new LotteryMembersExport($members), '抽奖用户.xlsx');
        }else {
            $members = $members->paginate();
        }
        return $this->success('ok', $members);
    }

    public function storeMember(Request $request, Lottery $lottery)
    {
        $data = $request->all();
        if (isset($data['user_id'])) {//搜索用户
            $user = User::where('mobile', $data['mobile'])->first();
            $data['mobile'] = $user->mobile;
            $data['name'] = $user->name;
        }elseif (isset($data['mobile'])) {//添加姓名
            $user = User::where('mobile', $data['mobile'])->first();
            if(empty($user)) {
                $user = User::create($data);
            }
            $data['user_id'] = $user->id;
        }else {
            return $this->failure("缺少参数");
        }
        $member = $lottery->members()->create($data);
        return $this->success('ok', $member);
    }

    public function deleteMember(Request $request, LotteryMember $member)
    {
        $member->delete();
        return $this->success('ok');
    }

    public function winningMembers(Request $request, Lottery $lottery)
    {
        $members = $lottery->winMembers()->with('award')->whereHas('award')->keyword()->orderByDesc('id');
        $export = $request->export;
        if ($export) {
            $members = $members->get();
            return Excel::download(new LotteryWinMembersExport($members),'中奖用户.xlsx');
        }else {
            $members = $members->paginate();
        }
        return $this->success('ok', $members);
    }

    public function awards(Request $request, Lottery $lottery)
    {
        $awards = $lottery->awards()->paginate();
        return $this->success('ok', $awards);
    }

    public function storeAwards(Request $request, Lottery $lottery)
    {
        $data = $request->all();
        if ($data['each_num'] > $data['num']) return $this->failure("每次中奖人数不能超过总中奖人数");
        $lottery->awards()->create($data);
        return $this->success('ok');
    }

    public function award(Request $request, LotteryAward $award)
    {
        return $this->success('ok', $award);
    }

    public function deleteAward(Request $request, LotteryAward $award)
    {
        if ($award->members()->count()) return $this->failure("该奖品已有用户中奖");
        $award->delete();
        return $this->success('ok');
    }

    public function updateAward(Request $request, LotteryAward $award)
    {
        $data = $request->all();
        if ($data['each_num'] > $data['num']) return $this->failure("每次中奖人数不能超过总中奖人数");
        $award->update($data);
        return $this->success('ok');
    }

    public function deleteWinningMembers(Request $request, Lottery $lottery)
    {
        $lottery->winMembers()->delete();
        return $this->success('ok');
    }
}
