<?php

namespace App\Http\Controllers\Game;

use App\Game\Services\OuroGameService;
use App\Http\Controllers\Controller;
use App\Http\helper\HttpCurl;
use App\Models\AccountWithDrawInfo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Yansongda\Pay\Log;

class MailController extends Controller
{
    public function index()
    {

    }

    public function delete(Request $request){
        $user = $request->user();
        $MailID=$request->input('MailID',-1);
        if($MailID==-1)return apiReturnFail(['web.mail.wrong_id','You need select mail to delete!']);

        if($MailID==0){
            $allmails=DB::connection('read')->table('QPAccountsDB.dbo.PrivateMail')
                ->where('UserID', $user->UserID)->where('MailStatus','<',4)->get();

            foreach($allmails as $mail){

                if($mail->MailStatus<3){
                    $giftGold=0;
                    if (!empty($mail->BonusString)) {
                        $bonus = $mail->BonusString;
                        $bonus = explode(';', $bonus);
                        $mail->BonusString = '';
                        foreach ($bonus as $k => &$v) {
                            $v = explode(',', $v);
                            if($v=='30000'){
                                $giftGold+=$v[1] / 100;
                                $mail->BonusString .=  '$' . ($v[1] / 100) . ',';
                            }
                        }
                    }
                    if($giftGold){
                        return apiReturnFail(['web.mail.mail_has_money','The mail still has money!'],[$mail]);
                    }
                }

            }
            DB::connection('read')->table('QPAccountsDB.dbo.PrivateMail')
                ->where('UserID', $user->UserID)->update(['MailStatus'=>4]);
            return apiReturnSuc(compact('MailID'),['mail.delete_all_success','All mails have been deleted successfully!']);
        }

        $mail=DB::connection('read')->table('QPAccountsDB.dbo.PrivateMail')
            ->where('UserID', $user->UserID)
            ->where('MailID', $MailID)->first();
        if(!$mail){
            return apiReturnFail(['web.mail.mail_not_existed','The mail could not be found!']);
        }else{
            if($mail->MailStatus==4){
                //delete
                return apiReturnFail(['web.mail.mail_not_existed','The mail could not be found!']);
            }
            if($mail->MailStatus<3){
                $giftGold=0;
                if (!empty($mail->BonusString)) {
                    $bonus = $mail->BonusString;
                    $bonus = explode(';', $bonus);
                    $mail->BonusString = '';
                    foreach ($bonus as $k => &$v) {
                        $v = explode(',', $v);
                        if($v=='30000'){
                            $giftGold+=$v[1] / 100;
                            $mail->BonusString .=  '$' . ($v[1] / 100) . ',';
                        }
                    }
                }
                if($giftGold){
                    return apiReturnFail(['web.mail.mail_has_money','The mail still has money!']);
                }
            }
            DB::connection('read')->table('QPAccountsDB.dbo.PrivateMail')
                ->where('UserID', $user->UserID)
                ->where('MailID', $MailID)->update(['MailStatus'=>4]);
            return apiReturnSuc(compact('MailID'),['mail.delete_success','The mail has been deleted successfully!']);
        }

    }
    public function read(Request $request){
        $user = $request->user();
        $MailID=$request->input('MailID',0);
        if(!$MailID)return apiReturnFail(['web.mail.wrong_id','You need select mail to delete!']);
        $mail=DB::connection('read')->table('QPAccountsDB.dbo.PrivateMail')
            ->where('UserID', $user->UserID)
            ->where('MailID', $MailID)->first();
        if(!$mail){
            return apiReturnFail(['web.mail.mail_not_existed','The mail could not be found!']);
        }else{
            if($mail->MailStatus==4){
                //delete
                return apiReturnFail(['web.mail.mail_not_existed','The mail could not be found!']);
            }
            if($mail->MailStatus==3){
                return apiReturnFail(['web.mail.mail_gift_taken','The attachment has been received']);
            }
            DB::connection('read')->table('QPAccountsDB.dbo.PrivateMail')
                ->where('UserID', $user->UserID)
                ->where('MailID', $MailID)->update(['MailStatus'=>2]);
            return apiReturnSuc(compact('MailID'),['mail.read_success','The mail has been read!']);
        }
    }
    public function take(Request $request){
        $user = $request->user();
        $MailID=$request->input('MailID',0);
        if(!$MailID)return apiReturnFail(['web.mail.wrong_id','You need select mail to delete!']);
        $mail=DB::connection('read')->table('QPAccountsDB.dbo.PrivateMail')
            ->where('UserID', $user->UserID)
            ->where('MailID', $MailID)->first();
        if(!$mail){
            return apiReturnFail(['web.mail.mail_not_existed','The mail could not be found!']);
        }else{
            if($mail->MailStatus==4){
                //delete
                return apiReturnFail(['web.mail.mail_not_existed','The mail could not be found!']);
            }
            if($mail->MailStatus==3){
                return apiReturnFail(['web.mail.mail_gift_taken','The attachment of the email has been received']);
            }
            $GiftScore=0;
            $FreeScore=0;
            if (!empty($mail->BonusString)) {
                $bonus = $mail->BonusString;
                $bonus = explode(';', $bonus);
                $mail->BonusString = '';
                foreach ($bonus as $v) {
                    $v = explode(',', $v);
                    if($v[0]=='30000'){
                        $GiftScore+=$v[1] ;
                        $mail->BonusString .=  '$' . ($v[1] / 100) . ',';
                    }
                    if($v[0]=='31000'){
                        $FreeScore+=$v[1] ;
                        $mail->BonusString .=  '$' . ($v[1] / 100) . ',';
                    }
                }
            }
//            dd($GiftScore);
            if(!$GiftScore && !$FreeScore){
                return ApiReturnFail(['web.mail.mail_has_nomoney','The mail does not have money!']);
            }

            $FromType=$mail->type;
            $Amount=$mail->amount;
            $DrawBase=$mail->DrawBase;
            $Reason=3;
            if($FromType==1)$Reason=43;
            if($FromType==3)$Reason=42;
            if($FromType==2&&$Amount>0){
                $Reason=54;
            }

            DB::connection('read')->table('QPAccountsDB.dbo.PrivateMail')
                ->where('UserID', $user->UserID)
                ->where('MailID', $MailID)->update(['MailStatus'=>3]);

            $UserID=$user->UserID;


            if($GiftScore)
                [$OrgScore,$NowScore]=OuroGameService::AddScore($user->UserID,$GiftScore,$Reason);
            if($FreeScore)
                [$OrgScore,$NowScore]=OuroGameService::AddFreeScore($user->UserID,$FreeScore,$Reason);

            if($FromType==2&&$Amount>0){
                AccountWithDrawInfo::where('UserID', $user->UserID)->update(['Win'=>DB::raw('Win+'.$Amount),'HistoryWithDraw'=>DB::raw('HistoryWithDraw-'.$Amount)]);
            }
            if($DrawBase){
                OuroGameService::AddDrawBase($UserID,$DrawBase,0);
            }
            // 增加用户金币
//            DB::connection('sqlsrv')->select("SET NOCOUNT ON use QPAccountsDB exec GSP_GR_PrivateMail_Update $MailID,$UserID,2");
            //0其它 1充值 2提现 3彩金


            return apiReturnSuc(compact('MailID','OrgScore','GiftScore','NowScore'),['mail.take_success','Attachment received successfully!']);
        }
    }
    public function tst(Request $request){
//        $user=$request->user();
//
//        $GiftScore=100;
//        // 增加用户金币
//        $OrgScore = DB::table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $user->UserID)->value('Score');
//
//        Log::info('邮件领取变化金币' . $GiftScore);
//        DB::table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $user->UserID)->increment('Score',$GiftScore);
//
//        $NowScore=$OrgScore+$GiftScore;
//        // 服务器通知
//
//        $url = config('transfer.stock')['url'] . 'notifyPay';

//        $data = [
//            'userid' =>  $user->UserID,
//            'getScore' => $GiftScore,
//            'score' => $NowScore,
//        ];

//            (new HttpCurl())->service($url, $data);


    }
    public function newMsg(Request $request)
    {
        $user = $request->user();
        $newMails = DB::connection('read')->table('QPAccountsDB.dbo.PrivateMail')
            ->where('UserID', $user->UserID)
            ->where('MailStatus', 1)
            ->count();
        return apiReturnSuc(['new' => $newMails]);

    }

    //邮件列表
    public function mailList(Request $request)
    {
        $user = $request->user();

        $list = DB::connection('read')->table('QPAccountsDB.dbo.PrivateMail')
            ->where('UserID', $user->UserID)
            ->where('MailStatus', '<',4)
            ->orderBy('MailID', 'desc')
            ->paginate(10);
        return apiReturnSuc($list);
    }

}
