<?php
/**
 * Created by PhpStorm.
 * User: lfq
 * Date: 2018/12/1
 * Time: 10:55
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request as Req;
use Illuminate\Support\Facades\App;
use Request;
use DB;

class RedpackController extends Controller{
    //页面
    function Index(Req $req){
        $request['type']=isset($req->type)?trim($req->type):'-1';
        $request['lm_order_id']=$req->lm_order_id ? $req->lm_order_id : '';
        $request['user_name']=$req->user_name ? $req->user_name : '';
        $request['start_time'] = isset($req->start_time) ? trim($req->start_time) : null;
        $request['end_time'] = isset($req->end_time) ? trim($req->end_time) : null;
       
        $dt = \App\Lib\DataTable\DataTable::init();
        $dt->setDataSource('/admin/Redpack/getList?user_name='.$request['user_name']);
        $dt->setLang('lucky_money');
        $dt->addColsFields('lm_order_id', array('templet' => '#lm_order_id','sort' => false));
        $dt->addColsFields('account', array('sort' => false));
        $dt->addColsFields('room_name', array('sort' => false));
        $dt->addColsFields('money', array('sort' => false));
        $dt->addColsFields('num', array('sort' => false));
        $dt->addColsFields('take_num', array('sort' => false));
        $dt->addColsFields('take_money', array('sort' => false));
        $dt->addColsFields('created_at', array('sort' => false,'width'=>160));
        // $dt->addColsFields('lm_return_money', array('sort' => false));
        $dt->addColsFields('type_name', array('sort' => false));
        $dt->addColsFields('action_d', array('templet' => '#waterTool', 'sort' => false));
        return view('admin.redpack/index', $dt->render($request));
    }
    //数据源
    function getList(Req $req){
        $list=isset($req->limit)?trim($req->limit):50;
        $request['type']=isset($req->type)?trim($req->type):'-1';
        $lm_order_id = $req->lm_order_id ? $req->lm_order_id : '';
        // $start_time = Request::get('start_time') ? Request::get('start_time') . ' 00:00:00' : '';
        // $end_time = Request::get('end_time') ? Request::get('end_time') . ' 23:59:59' : '';
        $start_time = Request::has ('start_time') ? Request::get ('start_time') : '';
        $end_time = Request::has ('end_time') ? Request::get ('end_time') : '';
        $user_name = $req->user_name ? $req->user_name : '';
        $where=array();
        if($request['type'] > '-1'){
            $where['type'] = $request['type'];
        }
        if(!empty($lm_order_id)){
            $where[]=array('lm_order_id',$lm_order_id);
        }
        if (!empty($user_name)) {
            $where[] = array('account', '=', $user_name);
        }
        if (!empty($start_time)) {
            $where[] = array('created_at', '>=', $start_time);
        }
        if (!empty($end_time)) {
            $where[] = array('created_at', '<=', $end_time);
        }
        $langinfo = trans('lucky_money');
        $db=new \App\Models\LuckyMoney;
        $data=$db->getList($list,$where);

        foreach ($data['data'] as $k=>$v){
            // $user_info=\App\Model\Account::where('identity',$v['account_identity'])->first()->toArray();
            // $data['data'][$k]['user_name']=$user_info['account'];
            $data['data'][$k]['type_name']=$langinfo['type'][$v['type']];
            // $data['data'][$k]['room_name']='';
            $room_info=DB::table('chat_rooms')->where('id',$v['room_id'])->first();
            if($room_info){
                $data['data'][$k]['room_name']=$room_info->room_name;
            }
        }
        return \App\Lib\DataTable\DataTable::init()->toJson($data['data'],$data['total']);
    }
    function redinfo(Req $req){
        $request = array();
        $request['lm_id'] = isset($req->lm_id) ? trim($req->lm_id) : null;
        $dt = \App\Lib\DataTable\DataTable::init();
        $dt->setDataSource('/admin/Redpack/getRecordList?lm_id=' . $request['lm_id']);
        $dt->setLang('lucky_money_record');
        $dt->addColsFields('account', array('sort' => false));
        $dt->addColsFields('time', array('sort' => false));
        $dt->addColsFields('money', array('sort' => false));
        $dt->addColsFields('room_name', array('sort' => false));
        return view('admin.redpack/redinfo', $dt->render($request));
    }
    function getRecordList(Req $req){
        $list=isset($req->limit)?trim($req->limit):50;
        $lm_id = Request::has('lm_id') ? Request::get('lm_id') : '';
        $where=array();
         if (!empty($lm_id)) {
            $where[] = array('lm_id', '=', $lm_id);
        }
        $db=new \App\Models\LuckyMoneyTake;
        $data=$db->getList($list,$where);
        foreach ($data['data'] as $k=>$v){
            $user_info=\App\Models\Account::where('identity',$v['account_identity'])->first()->toArray();
            $data['data'][$k]['account']=$user_info['account'];
            $data['data'][$k]['time']=date('Y-m-d H:i:s',$v['time']);
            $data['data'][$k]['room_name']='';
            $room_info=DB::table('chat_rooms')->where('id',$v['room_id'])->first();
            if($room_info){
                $data['data'][$k]['room_name']=$room_info->room_name;
            }
            
        }
        return \App\Lib\DataTable\DataTable::init()->toJson($data['data'],$data['total']);
    }
    

}