<?php

namespace App\Http\Controllers\Admin;

use App\Exports\LotteryWinMembersExport;
use App\Exports\RegularActivityExport;
use App\Exports\RegularActivityMemberExport;
use App\Exports\RegularActivityOrdersExport;
use App\Facades\CommonService;
use App\Http\Controllers\Controller;
use App\Http\Response\ResponseJson;
use App\Models\Lottery;
use App\Models\RegularActivity;
use App\Models\RegularActivityMember;
use App\Models\RegularActivityOrder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\ActivityMemberLogExport;
class RegularActivityController extends Controller
{
    use ResponseJson;

    public function index(Request $request) {
        $activities = RegularActivity::date()->keyword()->orderByDesc("id");
        $is_export = $request->input('is_export');
        if ($is_export) {
            $activities = $activities->get();
            return Excel::download(new RegularActivityExport($activities) , "定期活动.xlsx");
        }
        $activities = $activities->paginate();
        return $this->success("ok", $activities);
    }

    public function show(Request $request, RegularActivity $activity) {

        if (empty($activity->sign_qrcode) || empty($activity->sign_qrcode2) || empty($activity->detail_qrcode)) {
            $sign_url = config("app.url").'/go_html/store_agent#/inquireSignUp?id='.$activity->id;
            $sign_url2 = config("app.url").'/go_html/store_agent#/inquireSignUpV2?id='.$activity->id;
            $detail_url = config("app.url")."/go_html/store_agent#/smallConferenceApply?id=".$activity->id;
            $sign_qrcode = CommonService::makeQrcode($sign_url);
            $sign_qrcode2 = CommonService::makeQrcode($sign_url2);
            $detail_qrcode = CommonService::makeQrcode($detail_url);
            $activity->update(['sign_qrcode'=>$sign_qrcode, 'sign_qrcode2'=>$sign_qrcode2, "detail_qrcode"=>$detail_qrcode]);
        }
        return $this->success('ok', $activity);
    }

    public function activityDate()
    {
        $dates = RegularActivity::distinct("date")->orderBy("date")->pluck("date");
        return $this->success('ok', $dates);
    }

    public function store(Request $request)
    {
        DB::beginTransaction();
        $start_time = $request->input('start_time');
        $end_time = $request->input('end_time');
        if (empty($start_time) || empty($end_time)) return $this->failure("请选择活动时间");
        $pic = $request->input('pic');
//        $date = $request->input("date");
//        if (empty($date)) return $this->failure("请选择活动日期");
        $date = date("Y-m-d", strtotime($start_time));
//        $week = date('w', strtotime($date));
//        $week_array = array("日","一","二","三","四","五","六");
//        $title = "【周{$week_array[$week]}活动 {$date}】";

        //获取自定义标题
        $title = $request->input('title');
//        $start_time = date("Y-m-d 05:30:00", strtotime($date));
//        $end_time = date("Y-m-d 22:30:00", strtotime($date));
        $is_provide_food = $request->input('is_provide_food');
        $is_need_hand = $request->input('is_need_hand');
        $activity = RegularActivity::create(["title"=>$title, 'pic'=>$pic, "date"=>$date, 'start_time'=>$start_time, 'end_time'=>$end_time,'is_provide_food'=>$is_provide_food,'is_need_hand'=>$is_need_hand]);
        //二维码
//        $url = config("app.url")."/h5/#/inquireSignUp?id=".$activity->id;
        $sign_url = config("app.url").'/go_html/store_agent#/inquireSignUp?id='.$activity->id;
        $sign_url2 = config("app.url").'/go_html/store_agent#/inquireSignUpV2?id='.$activity->id;
        $detail_url = config("app.url")."/go_html/store_agent#/smallConferenceApply?id=".$activity->id;
        
        $sign_qrcode = CommonService::makeQrcode($sign_url);
        $sign_qrcode2 = CommonService::makeQrcode($sign_url2);
        $detail_qrcode = CommonService::makeQrcode($detail_url);

        $activity->update(['sign_qrcode'=>$sign_qrcode, 'sign_qrcode2'=>$sign_qrcode2, "detail_qrcode"=>$detail_qrcode]);
        //创建抽奖活动
        $lottery = Lottery::create(['title'=>$title, 'repeat'=>0]);
        //绑定活动和抽奖
        $lottery->addLotteryable($activity);
        //新版抽奖信息
        $lottery->path = config('app.url').'/award/#/newLuckyDrawUpdate?id='.$lottery->id;
        $lottery->new_path = config('app.url').'/award/#/newLuckyDraw?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, 'qrcode'=>$lottery->qrcode, 'apply_qrcode'=>$lottery->apply_qrcode]);
        DB::commit();
        return $this->success("ok",$activity);
    }

    public function update(Request $request, RegularActivity $activity) {
//        $background = $request->input('background');
//        $pic = $request->input('pic');
        $data = $request->all();
        if(isset($data['start_time'])){
            $data['date'] =  date("Y-m-d", strtotime($data['start_time']));
        }
        $activity->update($data);
        return $this->success('ok');
    }

    public function destroy(Request $request, RegularActivity $activity) {
        $activity->delete();
        return $this->success('ok');
    }

    public function members(Request $request, RegularActivity $activity) {
        $members = $activity->members()->with("user:id,name,mobile","agentUser")->keyword()->success()->orderByDesc("id");
        $is_export = $request->get('is_export');
        if ($is_export) {
            $members = $members->get();
            return Excel::download(new RegularActivityMemberExport($members), '签到人员.xlsx');
        }
        $members = $members->paginate();
        foreach ($members as $member) {
            $member->level = $member->agentUser->level??0;
        }
        return $this->success('ok', $members);
    }

    public function membersLogs(Request $request, RegularActivity $activity)
    {   
        $members = $activity->membersLogs()->keyword()->with("user:id,name,mobile,avatar", "user.officialWechat")->orderByDesc("updated_at");
        $export = $request->input('export');
        if ($export) {
            $members = $members->get();
            foreach ($members as $member) {
                $member->level = $member->agentUser?$member->agentUser->level:0;
            }
            return Excel::download(new ActivityMemberLogExport($members), 'members.xlsx');
        }
        $members = $members->paginate();
        foreach ($members as $member) {
            $member->level = $member->agentUser?$member->agentUser->level:0;
        }
        return $this->success('ok', $members);
    }

    public function awardMembers(Request $request, RegularActivity $activity) {
        $lottery = $activity->lottery;
        if (empty($lottery)) return $this->success("ok", []);
        $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 orders(Request $request, RegularActivity $activity)
    {
        $orders = $activity->orders()->keyword()->with("user:id,name,mobile","agentUser")->with( ['activityMember'=>function($sql) use($activity){
            $sql->where('activity_id', $activity->id);
        }])->orderByDesc("id");
        $export = $request->export;
        if ($export) {
            $orders = $orders->get();
            foreach ($orders as $order) {
                $order->name = !empty($order->user->name) ? $order->user->name : $order->name;
                $order->mobile = !empty($order->user->mobile) ? $order->user->mobile : $order->mobile;
                $order->sign_in  = 0;
                if ($order->activityMember && $order->activityMember->status == 1) $order->sign_in = 1;
                unset($order->activityMember);
            }
            return Excel::download(new RegularActivityOrdersExport($orders),'报名用户.xlsx');
        }else {
            $orders = $orders->paginate();
        }
        foreach ($orders as $order) {
            $order->name = !empty($order->user->name) ? $order->user->name : $order->name;
            $order->mobile = !empty($order->user->mobile) ? $order->user->mobile : $order->mobile;
            $order->sign_in  = 0;
            if ($order->activityMember && $order->activityMember->status == 1) $order->sign_in = 1;
            unset($order->activityMember);
        }

        return $this->success('ok', $orders);
    }

}
