<?php
!defined('IN_ONEZ') && exit('Access Denied');
include_once(dirname(__FILE__).'/init.php');

$groupid=(int)onez()->gp('groupid');
$gid=(int)onez()->gp('gid');

$fee1=(float)onez('chat.group.plugin.credit')->option('turn_fee1');
$fee2=(float)onez('chat.group.plugin.credit')->option('turn_fee2');
$format=(int)onez('chat.group.plugin.credit')->option('format');


$action=onez()->gp('action');
if($action=='search-ajax'){
  
  $record=array();
  $kw=onez()->gp('kw');
  $uid=(int)$kw;
  $xxx.=" and (`username` like '%$kw%' or key8 like '%$kw%' or id='$uid')";
  $T=$G['this']->data()->open('member')->record("1$xxx order by id desc");
  foreach($T as $rs){
    $record[]=array(
      'type'=>'item',
      'value'=>$rs['id'],
      'title'=>$rs['nickname'].'('.$rs['id'].')',
      'token'=>'seluser',
    );
  }
  
  $A['record']=$record;
  onez()->output($A);
}

$G['title']='转账';

#初始化表单
$form=onez('admin')->widget('form')
  ->set('title',$G['title'])
  ->set('values',$item)
;
#创建表单项
$form->add(array('type'=>'hidden','key'=>'action','value'=>'save'));
$form->add(array('type'=>'hidden','key'=>'userid','value'=>'','save'=>'1'));
$form->add(array('label'=>'目标用户','type'=>'text','key'=>'uname','hint'=>'','notempty'=>'目标用户不能为空','group'=>'','data-ajax'=>'self'));
$form->add(array('label'=>'转账金额','type'=>'text','key'=>'key1','hint'=>'','notempty'=>'转账金额不能为空','group'=>''));
$form->add(array('label'=>'手续费','type'=>'html','html'=>'<pre id="fee_label">0.00</pre>'));
$form->add(array('label'=>'支付总额','type'=>'html','html'=>'<pre id="total_label">0.00</pre>'));
$form->add(array('label'=>'我的'.onez('chat.group.plugin.credit')->name(),'type'=>'html','html'=>'<code>'.onez('chat.group.plugin.credit')->value($G['userid']).'</code>','hint'=>''));

$form->add(array('label'=>'备注信息','type'=>'textarea','key'=>'summary','hint'=>'','notempty'=>'','group'=>''));

#$charge_intro=onez('chat.group.plugin.credit')->option('charge_intro');
#$charge_intro && $form->add(array('label'=>'充值说明','type'=>'html','html'=>$charge_intro));

#处理提交
if($onez=$form->submit()){
  ob_clean();
  
  $money=onez('chat.group.plugin.credit')->format($onez['key1']);
  $money<=0 && onez()->error('转账金额有误');
  
  $total=$money;
  $fee=0;
  if($fee1>0){
    $total+=$money*$fee1/100;
    $fee+=$money*$fee1/100;
  }
  if($fee2>0){
    $total+=$fee2;
    $fee+=$fee2;
  }
  $fee=onez('chat.group.plugin.credit')->format($fee);
  
  $total=onez('chat.group.plugin.credit')->format($total);
  $total<=0 && onez()->error('转账金额有误');
  
  $total>onez('chat.group.plugin.credit')->value($G['userid']) && onez()->error('账户余额不足，请充值！');
  
  $u=$G['this']->data()->open('member')->one("id='$onez[userid]'");
  !$u && onez()->error('目标用户不存在');
  
  
  onez('chat.group.plugin.credit')->add($G['userid'],'money',-$money,'turn',"转账给".$u['nickname']);
  if($fee>0){
    onez('chat.group.plugin.credit')->add($G['userid'],'money',-$fee,'turn',"转账手续费");
  }
  onez('chat.group.plugin.credit')->add($u['id'],'money',$money,'turn',"来自".$G['user']['nickname']."的转账");
  
  onez()->ok('操作成功',onez('chat.group.plugin.credit')->href('/group/money.log.php'));
}
onez('admin')->header();
?>
<section class="content-header">
  <h1>
    <?=$G['title']?>
  </h1>
  <ol class="breadcrumb">
    <li>
      <a href="<?php echo onez()->href('/')?>">
        <i class="fa fa-dashboard">
        </i>
        管理首页
      </a>
    </li>
    <li class="active">
      <?=$G['title']?>
    </li>
  </ol>
</section>
<section class="content">
  <form id="form-common" method="post">
    <div class="box box-info">
      <div class="box-header with-border">
        <h3 class="box-title">
          <?=$G['title']?>
        </h3>
        <div class="box-tools pull-right">
        </div>
      </div>
      <div class="box-body">
        <?=$form->code()?>
      </div>
      <div class="box-footer clearfix">
        <button type="submit" class="btn btn-primary">
          立即转账
        </button>
      </div>
    </div>
    <input type="hidden" name="action" value="save" />
  </form>
</section>
<script type="text/javascript">
function seluser(obj){
  var data=$(obj).data();
  $('#input-userid').val(data.value);
  $('#input-uname').val(data.title);
}
$(function(){
  $('#input-key1').bind('input keyup',function(){
    var value=$(this).val();
    if(value.length<1 || isNaN(value)){
      value=0;
    }
    value=parseFloat(value+'');
    var total=value;
    var label='';
    var fee=0;
    <?if($fee1>0){?>
      label+=' * <?=$fee1?>%';
      fee+=value*<?=$fee1?>/100;
      total+=value*<?=$fee1?>/100;
    <?}?>
    <?if($fee2>0){?>
      label+=' + <?=$fee2?>';
      fee+=<?=$fee2?>;
      total+=<?=$fee2?>;
    <?}?>
    if(label==''){
      label=fee.toFixed(<?=$format?>);
    }else{
      label=value+''+label;
      label+=' = '+fee.toFixed(<?=$format?>);
    }
    $('#fee_label').html(label);
    $('#total_label').html(total.toFixed(<?=$format?>));
  });
  $('#form-common').bind('submit',function(){
    $.post(window.location.href,$(this).serialize()+'&ajax=1',function(data){
      onez.doit(data);
    },'json');
    return false;
  });
});
</script>
<?php
onez('admin')->footer();
$data=ob_get_contents();
ob_clean();
$data=str_replace(' id="input-uname"',' id="input-uname" autocomplete="off"',$data);
echo $data;
?>