<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use JiGuang\JSMS as JSMS;
use Redis;
use App\Http\Controllers\Transformers\TeacherTransformer;
use Dingo\Api\Http\Response;
use Validator;
use App\Models\Notice;
use App\Models\Banners;
use App\Models\Goods;
use DB;
use App\Models\Activity;

class IndexController extends ApiController {
	/**
	 *
	 * @api {post} index/detail 首页
	 * @apiName index/detail
	 * @apiGroup index
	 * @apiParamExample {curl} Example usage:
	 * http://59.110.137.245:8081/api/index/detail?json=ewogICJib2R5IiA6IHsKICB9LAogICJoZWFkZXIiIDogewogICAgInNpZ24iIDogIjBhMTMyM2UyNWRiZTBmYjI3MjQwYzE4YTZhODYyZjBiIiwKICAgICJ2ZXJzaW9uIiA6ICI1LjEuMCIsCiAgICAiY2hhbm5lbCIgOiAiMiIsCiAgICAiaWRmYSIgOiAiMDMzRUFFNjQtMUFERi00NkJELThFNUQtOTVGODcyNEJBMEFBIiwKICAgICJ0ZXJtaW5hbCIgOiAxLAogICAgInRyYW5zYWN0aW9uX3R5cGUiIDogImluaXRpYWxpemUiLAogICAgImNpdHlfaWQiIDogIjEyMDEwMCIsCiAgICAidG9rZW4iIDogImViNTdjNjc3YTI2MjA1ODU4MmJkMGY0MzUxYTQ4OGI5IiwKICAgICJpbWVpIiA6ICI3MDAzMUU5MC1CM0E4LTRDQ0ItOENGMC02MDZCRUI1RjQ0Q0EiLAogICAgInRpbWVfc3RhbXAiIDogIjE1NjEzNjE2ODciLAogICAgImlwIiA6ICIxMDAtMTA3LTQtMTUyIiwKICAgICJ1YSIgOiAiaVBob25lIDZzIFBsdXMiLAogICAgIm5ldHdvcmtfb3BlciIgOiAiMSIsCiAgICAibmV0d29ya19zdGF0dXMiIDogIjEiLAogICAgImJsYWNrX2JveCIgOiAiZXlKMGIydGxia2xrSWpvaWVYTndjalYwUm5GaU4wUk5RVmhTVm5aRVpuRktSVU5KTUc5TVFtMUhTbHBSZG1wTWNrUnZielp3TTNaTVNrRlBWamcyZFdsb1VFOVlkWEJvZG10cFF6VjBXbGh2VFVsWGFrZGFVSE5EVFZCU1VXUnFWbWM5UFNJc0ltOXpJam9pYVU5VElpd2ljMlZ4U1dRaU9pSXhOVFUzT1RJM016VTNNemt5TVRBM05ESXhNU0lzSW5CeWIyWnBiR1ZVYVcxbElqbzFNallzSW5abGNuTnBiMjRpT2lJekxqSXVNaUo5IiwKICAgICJtZXNzYWdlX2lkIiA6ICIyMDE5MDYyNDE1MzQ0NzE2MzAxNTYxMzYxNjg3IgogIH0KfQ==
	 * @apiParam {int} page 页码
	 * @apiSuccess {List[Map]} banners 顶部轮播
	 * @apiSuccess (banners){String} image 图片
	 * @apiSuccess (banners){String} url 链接
	 * @apiSuccess {String} distance_end_time 结束时间(时间戳)
	 * @apiSuccess {list[Map]} buyer_messages     购买人轮播列表
	 * @apiSuccess (buyer_messages) {String}   avatar  购买人头像
	 * @apiSuccess (buyer_messages) {String}   text  购买人文案
	 * @apiSuccess {Map} rule     砍价规则
	 * @apiSuccess (rule) {String} title 砍价规则标题
	 * @apiSuccess (rule) {String} content 砍价规则内容
	 * @apiSuccess {Map}  goods_list 商品集合
	 * @apiSuccess (goods_list) {Int} current_page 当前页
	 * @apiSuccess (goods_list) {Int} total 总页数
	 * @apiSuccess (goods_list) {List[Map]} data 分页数据
	 * @apiSuccess (data) {string} goods_id 管理系统生成的砍价唯一id 与乐友无关
	 * @apiSuccess (data) {string} sku sku
	 * @apiSuccess (data) {string} name 商品名称
	 * @apiSuccess (data) {string} price 开砍价
	 * @apiSuccess (data) {string} base_price 底价
	 * @apiSuccess (data) {string} img_url 商品主图
	 * @apiSuccess (data) {string} cut_type 1:去砍价 2:继续砍
	 * @apiSuccess (data) {string} already_cut_price 已砍金额
	 * @apiSuccess (data) {string} xxxx 单个商品的结束时间 需与甲方确认
	 * @apiPermission 若有token 则在header中传入
	 */
	function detail(Request $request) {
		// 接口入参验证
		// 轮播
		$lastActivity = Activity::where('starttime','<=',getDateTime())->where('endtime','>',getDateTime())->where('status',1)->orderBy('updated_at','desc')->first();
		if(!$lastActivity){
            $product_ids = [];
        }else {
		    $product_ids = Goods::query()->where('activity_id', $lastActivity->id)->select(['product_id'])->groupBy(['product_id'])->paginate(4)->toArray();
		    if($product_ids['data']){
                $goodsInfo = [];
		        foreach ($product_ids['data'] as $v){
		            $goodsInfo[] = Goods::query()->where('activity_id', $lastActivity->id)->where('product_id', $v['product_id'])->first(['id as goods_id','sku', 'name', 'price', 'base_price', 'img_url', 'product_id'])->toArray();
                }
                $product_ids['data'] = $goodsInfo;
            }else{
                $product_ids = [];
            }
        }
		$banners = Banners::select ( [
				'image',
				'url'
		] )->where ( 'status', 1 )->where ( 'start_at', '<=', getDateTime () )->where ( 'end_at', '>=', getDateTime () )->get ();
		// ad
		$distanceEndTime = isset($lastActivity->endtime) ? strtotime($lastActivity->endtime) : time();
		$resData ['banners'] = $banners;
		$resData ['distance_end_time'] = $distanceEndTime;
		$resData ['buyer_messages'] = [
            [
                'avatar' => 'http://img.mp.sohu.com/upload/20170708/947467c71e444ef8b24d95eeba2fe54b.png',
                'text' => '张三疯用最低价购买了xxx'
            ],
            [
                'avatar' => 'http://img.mp.sohu.com/upload/20170708/947467c71e444ef8b24d95eeba2fe54b.png',
                'text' => '李狗蛋用最低价购买了xxx'
            ],
            [
                'avatar' => 'http://img.mp.sohu.com/upload/20170708/947467c71e444ef8b24d95eeba2fe54b.png',
                'text' => '王二麻子用最低价购买了xxx'
            ],
            [
                'avatar' => 'http://img.mp.sohu.com/upload/20170708/947467c71e444ef8b24d95eeba2fe54b.png',
                'text' => '永强用最低价购买了xxx'
            ],
            [
                'avatar' => 'http://img.mp.sohu.com/upload/20170708/947467c71e444ef8b24d95eeba2fe54b.png',
                'text' => '铁柱用最低价购买了xxx'
            ]

		];
		$resData ['rule'] = Notice::select ( [ 
				'title',
				'content'
		] )->first ();
		$resData ['goods_list'] = $product_ids;
		return $this->success ( $resData );
	}
}
