<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/7/26
 * Time: 上午11:00
 */

class rank_listControl extends mobileControl
{
    public function __construct() {
        parent::__construct();
    }

    public function indexOp()
    {
        $list_sn = $_GET['list_sn'];
        if(!isset($list_sn) || empty($list_sn)) {
            return self::outerr(errcode::ErrParamter,'请输入排行榜序列号.');
        }

        $items = ranklist_helper::gen_ranklist($list_sn,$userid,$err);
        if($items == false) {
            return self::outerr($err['code'],$err['msg']);
        }

        $mids = array();
        $list_infos = array();
        $rank_num = 1;
        $mine = null;

        if(!empty($items)) {
            $list_date = $items[0]['list_date'];
        } else {
            $list_date = '';
        }

        foreach ($items as $item) {
            $rank = ranklist::create_by_store($item);

            $item = array();
            $item['supported'] = $this->supported($rank->list_sn());
            $item['list_sn'] = $rank->list_sn();
            $item['supports'] = $rank->supports();
            $item['list_value'] = $rank->money();
            $item['member_id'] = $rank->member_id();
            $item['rank'] = $rank_num;

            array_push($list_infos,$item);
            array_push($mids,$rank->member_id());
            if($userid == $rank->member_id()) {
                $mine = $item;
            }
            ++$rank_num;
        }

        $mine_desc = null;
        $mem_desc = array();
        $members = Model('member')->getMemberList(array('member_id' => array('in',$mids)));
        foreach ($members as $val)
        {
            $info = new member_info($val);
            array_push($mem_desc,$info->filter());
            if($val['member_id'] == $mine['member_id']) {
                $mine_desc = $info;
            }
        }

        $title = $mine_desc->nickname() . "的" . strftime("熊猫美妆%m月%d日红包风云榜",intval($list_date));
        return self::outsuccess(array('mine' => $mine,'list_infos' => $list_infos,'mem_desc' => $mem_desc,'rank_title' => $title),"bonus/ranklist");
    }

    public function supportOp()
    {
        $list_sn = $_GET['list_sn'];
        if(!isset($list_sn) || empty($list_sn)) {
            return self::outerr(errcode::ErrParamter,'请输入排行榜序列号.');
        }

        $rank = ranklist::create_by_sn($list_sn);
        if($rank == false) {
            return self::outerr(errcode::ErrParamter,'该排行榜不存在或者已经过期~');
        }


        if($this->supported($list_sn)) {
            $rank->unsupport();
            $_SESSION['rank_list'][$list_sn]['supported'] = false;
        } else {
            $rank->support();
            $_SESSION['rank_list'][$list_sn]['supported'] = true;
        }

        return self::outsuccess(array('supports' => $rank->supports(),'list_sn' => $rank->list_sn()));
    }

    private function supported($list_sn)
    {
        if(!isset($_SESSION['rank_list'])) {
            $_SESSION['rank_list'] = array();
        }

        if(array_key_exists($list_sn,$_SESSION['rank_list']) == false) {
            $_SESSION['rank_list'][$list_sn] = array();
            $_SESSION['rank_list'][$list_sn]['supported'] = false;
        }

        return boolval($_SESSION['rank_list'][$list_sn]['supported']);
    }
}

function bonus_output_meminfo($mid,$mdescs)
{
    foreach ($mdescs as $info) {
        if($info['member_id'] == $mid) {
            return $info;
        }
    }

    return false;
}

function bonus_output_rankitem($item,$mdescs,$mine_id)
{
    $mid = $item['member_id'];
    $minfo = bonus_output_meminfo($mid,$mdescs);

    echo '<div class="list_li overflow">';
    echo    '<div class="index float_l w-8">' .$item['rank']. '</div>';
    echo    '<div class="icon_header float_l w-12 margin-r-1">';
    echo        '<img src="' . $minfo['avatar'] .'" alt=""/>';
    echo    '</div>';
    echo    '<div class="msg float_l w-80 relative_li">';
    echo        '<div class="name text_l w-45 float_l overflow">' . $minfo['nickname'] . '</div>';
    echo        '<div class="number text_r w-35 float_l">' . $item['list_value'] .'元</div>';
    if($item['supported'] == true)
    {
        if($mid == $mine_id) {
            echo '<div class="float_l w-20 concern_box concern_box_active mine">';
        } else {
            echo '<div class="float_l w-20 concern_box concern_box_active">';
        }
    }
    else
    {
        if($mid == $mine_id) {
            echo    '<div class="float_l w-20 concern_box mine">';
        } else {
            echo    '<div class="float_l w-20 concern_box">';
        }
    }
    echo                '<div class="concern">' . $item['supports'] .'</div>';
    echo            '</div>';
    echo            '<input type="hidden" class="list_sn" value="' . $item['list_sn'] .'"/>';
    echo        '</div>';
    echo '</div>';
}