<?php

if(!defined('IN_DISCUZ')) {
    exit('Access Denied');
}

if(!$_G['uid']){
    odz_error('token_expire', -1000);
}

$tid = isset($_GET['tid'])?intval($_GET['tid']):null;
$score   = isset($_GET['score'])?intval($_GET['score']):null;

if( $tid === null || empty($score)){
    //参数传递错误提示
    odz_error('reward_failed', -112);
}

//根据贴子获取发帖人的ID
$sql = "SELECT * FROM ".DB::table('forum_thread')." WHERE `tid` = '{$tid}'";
$thread = DB::fetch_first($sql);
empty($thread) && odz_error('reward_failed',-1);
$authorid = $thread['authorid'];

//不能给自己打赏
// if($authorid == $_G['uid'])
// {
//     odz_error('reward_failed',-1);
// }
$type = get_mocuz_setting('reward_score_type');
$extcredits = 'extcredits'.$type['reward_score_type'];


$score_name = $_G['setting']['extcredits'][$type['reward_score_type']]['title'];


//打赏人积分

$sql = "SELECT * FROM ".DB::table('common_member_count')." WHERE `uid` = '{$_G['uid']}'";
$member = DB::fetch_first($sql);
$user_credits = $member [$extcredits];

//积分不足不能打赏
if($user_credits<$score)
{
    odz_error('overscore',-1);
}

//积分日志变动   打赏用户
updatemembercount($_G['uid'], array($extcredits => '-'.$score), true, '', $tid, '');
credit_log($_G['uid'],'reward',$tid,array($extcredits => '-'.$score), 'reward');
//积分日志变动 文章作者
updatemembercount($authorid, array($extcredits => '+'.$score), true, '', $tid, '');
credit_log($authorid,'reward',$tid,array($extcredits => '+'.$score), 'reward');

DB::insert('mocuz_reward_list',array('ruid'=>$_G['uid'],'username'=>$_G['username'],'auid'=>$authorid,'tid'=>$tid,'score'=>$score,'score_name'=>$score_name,'date'=>time()));


odz_success('reward_success','0');