<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 用户推荐
 */
class Userintro extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    //生成海报
    public function createposter() {
//        $image = input('image', '', 'trim');
//        if (!$image) {
//            $this->error('您的网络开小差啦~');
//        }
        $image = config('site.intro_imges');
        $haibao = $this->haibao($this->auth->id,['invite_no'=>$this->auth->invite_no, 'background' => $image]);
        return $haibao;
//        $this->success('success', $haibao);
    }

    //海报
    public function haibao($player_id,$data){

        //下载页二维码,没必要保留
        $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
        $params = [
            'text' => $httpStr.'/index/index/appdownload?code=' . $data['invite_no'],
        ];
        $qrCode = \addons\qrcode\library\Service::qrcode($params);
        $qrcode_path = 'uploads/hbplayer/'.date('Ymd');
        mk_dir($qrcode_path);
        $download_qrcode = $qrcode_path.'/download'.$player_id.'.png';
        $qrCode->writeFile($download_qrcode);

        //海报
        $result['url'] = $this->createhaibao($download_qrcode,$player_id,$data);

        $this->success('获取成功',$result);
    }

    public function createhaibao($download_qrcode,$player_id,$sub_data){
        //二维码
        $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
        $download_qrcode= $httpStr.'/'.$download_qrcode;

        $data = [

            [
                "left" => "18px",
                "top" => "410px",
                "type" => "text",
                "width" => "80px",
                "height" => "24px",
                "size" => "16px",
                "color" => "#123354",
                "content" => 'K歌社交'
            ],
            [
                "left" => "18px",
                "top" => "445px",
                "type" => "text",
                "width" => "80px",
                "height" => "24px",
                "size" => "10px",
                "color" => "#123354",
                "content" => '更多金币奖励等你来拿'
            ],
            [
                "left" => "210px",
                "top" => "385px",
                "type" => "img",
                "width" => "95px",
                "height" => "95px",
                "src" => $download_qrcode//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"
            ]
        ];

        $data = json_encode($data, 320);

        $poster = [
            'id' => $player_id,
            'title' => 'K歌',
            'waittext' => '您的专属海报正在拼命生成中,请等待片刻...',
            'bg_image' => $sub_data['background'] ? cdnurl($sub_data['background']) : '/assets/img/inviteposter.png',
            'data' => $data,
            'status' => 'normal',
            'weigh' => 0,
            'createtime' => 1653993709,
            'updatetime' => 1653994259,
        ];

        $image = new \addons\poster\library\Image();
        $imgurl = $image->createPosterImage($poster, $this->auth->getUser());

        if (!$imgurl) {
            $this->error('生成海报出错');
        }
//        $imgurl = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"] . '/' . $imgurl;
        return $httpStr.'/' . $imgurl;
    }


    //我邀请的人列表
    public function myintro_list(){
        $list = Db::name('user')->field('id,u_id,mobile,username,nickname,avatar,createtime')->where('pre_userid',$this->auth->id)->autopage()->select();
        $list = list_domain_image($list,['avatar']);

        $this->success('success',$list);
    }

    //邀请页多个信息
    public function introsite_info(){
        //本周
        $thisweek_start = strtotime(date('Y-m-d')) - ((date('w')==0?7:date('w'))-1)*86400;
        $thisweek_end   = $thisweek_start + 604799;
        //上周
        $lastweek_start = $thisweek_start - 604800;
        $lastweek_end   = $thisweek_end - 604800;

        //本周邀请人数
        $intro_num_thisweek = Db::name('user')->where('pre_userid',$this->auth->id)->where('createtime','BETWEEN',[$thisweek_start,$thisweek_end])->count();
        //上周邀请人数
        $intro_num_lastweek = Db::name('user')->where('pre_userid',$this->auth->id)->where('createtime','BETWEEN',[$lastweek_start,$lastweek_end])->count();
        //总邀请人数
        $intro_num_all      = Db::name('user')->where('pre_userid',$this->auth->id)->count();
        //本周奖励
        $money_sum_thisweek = Db::name('user_money_log')->where(['user_id'=>$this->auth->id,'type'=>103,'createtime'=>['BETWEEN',[$thisweek_start,$thisweek_end]]])->sum('value');
        //上周奖励
        $money_sum_lastweek = Db::name('user_money_log')->where(['user_id'=>$this->auth->id,'type'=>103,'createtime'=>['BETWEEN',[$lastweek_start,$lastweek_end]]])->sum('value');
        //我邀请的人
        $user_list = Db::name('user')->field('id,u_id,username,nickname,mobile,avatar,createtime')->where('pre_userid',$this->auth->id)->limit(3)->select();

        $rs = [
            'introsite_activeinfo' => config('site.introsite_activeinfo'),
            'introsite_introrule' => config('site.introsite_introrule'),
            'invite_no' => $this->auth->invite_no,
            'user_list' => $user_list,
            'money_sum_thisweek' => $money_sum_thisweek,
            'money_sum_lastweek' => $money_sum_lastweek,
            'intro_num_thisweek' => $intro_num_thisweek,
            'intro_num_lastweek' => $intro_num_lastweek,
            'intro_num_all'      => $intro_num_all,
        ];

        $this->success('success',$rs);
    }






}