<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request as Req;
use Request;

/**
 *
 */
class MoneyListController extends Controller {
	/**
	 * @return 资金流水管理
	 */
	function index(Req $req) {
		$request=array();
		$request['account_name']=isset($req->account_name)?trim($req->account_name):null;
		$request['order_id'] = isset($req->order_id) ? trim($req->order_id) : null;
		$request['trade_type'] = isset($req->trade_type) ? trim($req->trade_type) : null;
		$request['money_small'] = isset($req->money_small) ? trim($req->money_small) : null;
		$request['money_big'] = isset($req->money_big) ? trim($req->money_big) : null;
		$request['star_time'] = isset($req->star_time) ? trim($req->star_time) : null;
		$request['end_time'] = isset($req->end_time) ? trim($req->end_time) : null;
        $request['status']=isset($req->status) ? trim($req->status) : '';
		$dt = \App\Lib\DataTable\DataTable::init();
		$isuser = !empty(Request::has('isuser')) ? Request::get('isuser') : 2;
		$dt->setDataSource('/admin/money/moneyWaterList?account_name=' . $request['account_name'] . '&isuser=' . $isuser);
		$dt->setLang('money');
		$dt->setPriKey('trade_id');
		$dt->addColsFields('id', array('width' => 80));
		$dt->addColsFields('account_name', array('templet' => '#usermoneyRecord', 'width' => 110));
		$dt->addColsFields('money_time', array('templet' => '#orderid_time','width' => 240));
		$dt->addColsFields('change_cash', array('templet' => '#money_aftercash','width' => 290));
		$dt->addColsFields('systemuser_ordertype', array('templet' => '#systemuser_ordertype','sort'=>false,'width' => 160));
		$dt->addColsFields('trade_desc', array('templet' => '#ordertype','sort'=>false));
		$dt->addColsFields('game_view', array('templet' => '#game','width' => 100,'sort'=>false,'align' => 'left'));

		return view('admin.moneylist/index', $dt->render($request));
	}

	/**
	 * 单用户资金流水列表
	 * [detail description]
	 * @return [type] [description]
	 */
	function usermoneyRecord() {
		$account_name = Request::has('account_name') ? Request::get('account_name') : '';
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/money/moneyWaterList?account_name=' . $account_name . '&sureblur=精确');
		$dt->setLang('money');
		$dt->setPriKey('trade_id');
		$dt->addColsFields('id', array('width' => 100));
		$dt->addColsFields('account_name', array('width' => 140));
		$dt->addColsFields('money_time', array('width' => 200));
		$dt->addColsFields('systemuser_ordertype', array('sort' => false, 'width' => 160));
		$dt->addColsFields('money', array('width' => 120));
		$dt->addColsFields('reason', array('sort' => false));
		$dt->setToolBar(array('view'), array('width' => 80));
		return view('admin.view', $dt->render());
	}

	//查看资金流水
	function View(Req $req) {
		$order_id = $req->order_id;
		if (empty($order_id)) {
			abort(404);
		}
		$money_db = new \App\Models\Money_details;
		$bank_db = new \App\Models\Account_bank;
		$data = $money_db->getOrderDetails($order_id, 3);
		$bank_data = $bank_db->getInfoByUid($data['account_identity']);

		$data['bank_name'] = isset($bank_data['bank_name']) ? $bank_data['bank_name'] : '';
		$data['bank_address'] = isset($bank_data['bank_address']) ? $bank_data['bank_address'] : '';
		$data['bank_number'] = isset($bank_data['bank_number']) ? $bank_data['bank_number'] : '';
		$data['money_cash'] = round($data['money_cash'],2);
		$data['name'] = isset($bank_data['account_name']) ? $bank_data['account_name'] : '';
		return view('admin.moneylist/view', ['data' => $data]);
	}
	//获取按照类型统计
	function getCount(Req $req) {
		$startTime = $req->startTime;
		$endTime = $req->endTime;
		$type = $req->type;
		$res = $this->getSumMoney($startTime, $endTime, $type);
		return responseToJson($res);
	}
    //按照类型统计总金额
    function getSumMoney($where, $type = '') {
        $db = new \App\Models\Money_details;
        $where[]=array('status','=',1);
        return $db->totalTypeMoney($where);
    }
    function getActive($where, $type) {
        $db = new \App\Models\Money_details;
        return $db->getActiveMoney($where, $type);
    }
    //统计总数
    //统计总数
    function allTotal(Req $req) {
        $where = $req->where;
        if (count($where) == 0) {
        return    $where=array();
        }
        $data_odc=$this->getSumMoney($where);
        $data = array();
        //赋初值
        $data['totalMoney']=0;
        $data['onlineRecharge']=0;
        $data['backstageRecharge']=0;
        $data['remit']=0;
        $data['rechargePrize']=0;
        $data['active_add_money']=0;
        $data['active_cut_money']=0;
        $data['active_money']=0;
        $data['back_water']=0;
        $data['return_water']=0;
        $data['take_money']=0;
        $data['cut_money']=0;
        $data['commission_addmoney']=0;
        $data['nagentin']=0; //代理充值
        $data['nagentout']=0; //代理代充

        foreach ($data_odc as $k=>$v){
            if($v['trade_type']==6){
                $data['onlineRecharge']=round($v['money'],2);
            }
            if($v['trade_type']==11){
                $data['backstageRecharge']=round($v['money'],2);//后台充值
            }
            if($v['trade_type']==14){
                $data['remit']=round($v['money'],2);
            }
            if($v['trade_type']==13){
                $data['rechargePrize']=round($v['money'],2);
            }
            if($v['trade_type']==12&&$v['money_type']==1){
                $data['active_add_money']=round($v['money'],2);
                $data['active_money']+= $data['active_add_money'];
            }
            if($v['trade_type']==12&&$v['money_type']==2){
                $data['active_cut_money']=round($v['money'],2);
                $data['active_money']+=$data['active_cut_money'];
            }
            if($v['trade_type']==7){
                $data['back_water']=round($v['money'],2);
            }
            if($v['trade_type']==8){
                $data['return_water']=round($v['money'],2);
            }
            if($v['trade_type']==5){
                $data['take_money'] =round($v['money'],2); //5前台提现
            }
            if($v['trade_type']==9){
                $data['cut_money'] = round($v['money'],2); //管理员扣款
            }
            if($v['trade_type']==16&&$v['money_type']==1){
                $data['reward_addmoney']=round($v['money'],2);
            }
            if($v['trade_type']==16&&$v['money_type']==2){
                $data['reward_reducemoney']=round($v['money'],2);
            }
            if($v['trade_type']==19&&$v['money_type']==1){
                $data['conversion_addmoney']=round($v['money'],2);
            }
            if($v['trade_type']==19&&$v['money_type']==2){
                $data['conversion_reducemoney']=round($v['money'],2);
            }
            if($v['trade_type']==10&&$v['money_type']==1){
                $data['commission_addmoney']=round($v['money'],2);
            }
            if($v['trade_type']==22&&$v['money_type']==2){
                $data['nagentout']=round($v['money'],2);
            }
            if($v['trade_type']==22&&$v['money_type']==1){
                $data['nagentin']=round($v['money'],2);
            }

            $data['totalMoney']+=$v['money'];
        }
        if(empty($data['reward_addmoney'])){
            $data['reward_addmoney'] =0;
        }
        if(empty($data['reward_reducemoney'])){
            $data['reward_reducemoney'] =0;
        }
        if(empty($data['conversion_addmoney'])){
            $data['conversion_addmoney'] =0;
        }
        if(empty($data['conversion_reducemoney'])){
            $data['conversion_reducemoney'] =0;
        }
//		$data['totalMoney'] = $this->getSumMoney($where);
//		$data['onlineRecharge'] = $this->getSumMoney($where, 6); //在线充值
//		$data['backstageRecharge'] = $this->getSumMoney($where, 11); //后台充值
//		$data['remit'] = $this->getSumMoney($where, 14); //14汇款
//		$data['rechargePrize'] = $this->getSumMoney($where, 13); //13充值赠送
        //活动金额
//		$data['active_cut_money'] = $this->getActive($where, 2); //活动扣除
//        $data['active_cut_money']=round($data['active_cut_money'],2);
//        $data['totalMoney']+=$data['active_cut_money'];
//		$data['active_add_money'] = $this->getActive($where, 1); //活动加款
//        $data['active_add_money']=round($data['active_add_money'],2);
//		$data['active_money'] = $this->getSumMoney($where, 12); //总活动

//		$data['back_water'] = $this->getSumMoney($where, 7); //反水
//		$data['return_water'] = $this->getSumMoney($where, 8); //回水
//		$data['take_money'] = $this->getSumMoney($where, 5); //5前台提现
//		$data['cut_money'] = $this->getSumMoney($where, 9); //管理员扣款
        $data['intoTotalMoney'] = $data['onlineRecharge'] + $data['backstageRecharge'] + $data['remit'] + $data['nagentin']; //总入款扣除活动充值赠送
        $data['cut_total_money'] = $data['cut_money'] + $data['take_money'] + $data['nagentout']; //扣款总额扣除活动充值赠送
        $data['profit'] = $data['intoTotalMoney'] - $data['cut_total_money'];
        $data['all_save_money'] = $data['intoTotalMoney'] + $data['active_add_money'] + $data['rechargePrize'] + $data['back_water'] + $data['return_water']; //总存款
        $data['all_take_money'] = $data['take_money'] + $data['cut_money'] + $data['active_cut_money'] + $data['nagentout']; //总扣款
//        $type= ['totalMoney', 'onlineRecharge', 'backstageRecharge','remit','rechargePrize','active_cut_money','active_add_money','active_money','back_water',
//        'return_water','take_money','cut_money','intoTotalMoney','cut_total_money','profit','all_save_money','all_take_money'];
//        $arr = arrayToNum($data,$type);
        return responseToJson($data);
    }

}

?>