<?php
/**
 * Created by PhpStorm.
 * User: Jonlin
 * Date: 2019/4/9
 * Time: 9:11
 */
namespace App\Http\Controllers\Admin;

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

/**
 *
 */
class TennisNoteListController extends Controller
{

    public function notelist(Req $req)
    {
        $request['name_chinese'] = isset($req->name_chinese) ? trim($req->name_chinese) : null;
        $request['sureblurs'] = isset($req->sureblurs) ? $req->sureblurs : 'on';
        $request['star_time'] = isset($req->star_time) ? trim($req->star_time) :null ;
        $request['end_time'] = isset($req->end_time) ? trim($req->end_time) :null ;
        $dt = \App\Lib\DataTable\DataTable::init();
        $dt->setDataSource('/admin/TennisNoteList/info');
        $dt->setLang('sportsnotelist');
        $dt->addColsFields('id', array('templet' => '#userdetail', 'sort' => true, 'width' => 80));
        $dt->addColsFields('order_time', array('templet' => '#userdetail', 'sort' => true, 'width' => 200));
        $dt->addColsFields('game', array('templet' => '#userdetail', 'sort' => false, 'width' => 80));
        $dt->addColsFields('game_no', array('templet' => '#userdetail', 'sort' => false, 'width' => 80));
        $dt->addColsFields('prize_note', array('templet' => '#userdetail', 'sort' => false, 'width' => 80));
        $dt->addColsFields('money', array('templet' => '#userdetail', 'sort' => false, 'width' => 80));
        $dt->addColsFields('prize_money', array('templet' => '#userdetail', 'sort' => false, 'width' => 80));
        $dt->addColsFields('status', array('templet' => '#userdetail', 'sort' => false, 'width' => 70));
        $dt->addColsFields('content', array('templet' => '#userdetail', 'sort' => false, 'width' => 200));
        $dt->addColsFields('water_status', array('templet' => '#userdetail', 'sort' => false, 'width' => 70));
        //$dt->addColsFields('prize', array('templet' => '#userdetail', 'sort' => false, 'width' => 80));
        $dt->addColsFields('settle_status', array('templet' => '#userdetail', 'sort' => false, 'width' => 70));
        $dt->addColsFields('game_status', array('templet' => '#userdetail', 'sort' => false, 'width' => 70));
        $dt->addColsFields('account_name', array('templet' => '#userdetail', 'sort' => false, 'width' => 120));
        $dt->addColsFields('member_type', array('templet' => '#userdetail', 'sort' => false, 'width' => 80));
        //$arr[] = 'view';
        if (checkRriv('/admin/TennisNoteList/edit')) {
            $arr[] = 'edit';
        }
        $dt->setToolBar($arr, array('width' => 70));
        $dt->enableCheckBox();
        return view('sports/sports_notelist', $dt->render($request));
    }

    function info()
    {
        $type = 'wq';
        $page = Request::has('page') ? Request::get('page') : '';
        $list = Request::has('limit') ? Request::get('limit') : 10;
        $star_time = Request::get('star_time') ? Request::get('star_time').' 00:00:00' : '';
        $end_time = Request::get('end_time') ? Request::get('end_time').' 23:59:59' : '';
//        $sureblur = Request::has('sureblurs') ? Request::get('sureblurs') : 'off';
        $where = array();
        if (!empty($star_time)) {
            if (!empty($end_time)){
                $where[] = array('money_buy.money_time', '>', $star_time);
                $where[] = array('money_buy.money_time', '<', $end_time);
            }else{
                $where[] = array('money_buy.money_time', '>', $star_time);
            }
        }else if(empty($star_time)){
            if (!empty($end_time)){

                $where[] = array('money_buy.money_time', '<', $end_time);
            }
        }
        $newapp = new \App\Models\SportsNoteList();
        $data = $newapp->getinfo($list, $page, $where,$type);
        return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);
    }

    /**
     *修改订单
     */
    function edit(Req $req) {
        $id = $req->id;
        if (intval($id) < 1) {
            return -1;
        }
        $lange = trans('sportssoccer');
        $data = \App\Models\SportsNoteList::where('id',$id)->first();
        $data->order_time = $data->order_id.'     '.$data->money_time;
        //for($i=0;$i<count($data);$i++){
        $codes = json_decode($data->codes, true);
        $data->content = '';
        for($j=0;$j<count($codes);$j++){
            $odds_only = $data->odds_only;
            $res = DB::table('st_wq_odds_record')->where('odds_only',$odds_only)->first();
            $result = DB::table('st_odds_code')->where('odds_code',$res['odds_code'])->first();
            //$result = \App\Model\Matchcode::where('odds_code',$res->odds_code)->first();
            $game = DB::table('st_game_type')->where('game_code',$data->game_name)->first();
            $data->content = $data->content.$result['odds_name'].  '￥'.$codes[$j]['bet_amount'];
            $data->game = $game->game_name;
        }
        //}

        if (!$req->isMethod('post')) {


            $data = $data->toArray();

            return view('sports/sports_notelist_form', $data);
        } else {
            $model = new \App\Models\SportsNoteList();
            $model->id = $req->input('id');
            $model = $model::find($model->id);
            $model->water_status = $req->input('water_status');
            $model->settle_status = $req->input('settle_status');
            $model->game_status = $req->input('game_status');
            //$model->utime = date('Y-m-d H:i:s',time());

            $model->save();
            return responseToJson(1);
        }

    }

    /**
     *订单审核
     */
    public function orderstatus(Req $req) {
        $id = $req->input('id');
        if (empty($id)) {
            return responseToJson(-2001); //id����Ϊ��
        }
        $ids = explode(',', $id);
        if (!is_array($ids) && intval($ids) < 0) {
            return responseToJson(-2002); //id����
        }
        if (is_array($ids) && count($ids) > 0) {
            foreach ($ids as $k => $v) {
                if (intval($v) < 1) {
                    unset($ids[$k]);
                }
            }
        }

        $rows = \App\Models\SportsNoteList::whereIn('id', $ids)->delete();
        if (!$rows) {
            return responseToJson(-2003); //id����
        }
        return responseToJson(1, trans('menu.delete_success')); //id����

    }


    /**
     *删除订单
     */
    public function delete(Req $req) {
        $id = $req->input('id');
        if (empty($id)) {
            return responseToJson(-2001); //id����Ϊ��
        }
        $ids = explode(',', $id);
        if (!is_array($ids) && intval($ids) < 0) {
            return responseToJson(-2002); //id����
        }
        if (is_array($ids) && count($ids) > 0) {
            foreach ($ids as $k => $v) {
                if (intval($v) < 1) {
                    unset($ids[$k]);
                }
            }
        }

        $rows = \App\Models\SportsNoteList::whereIn('id', $ids)->delete();
        if (!$rows) {
            return responseToJson(-2003); //id����
        }
        return responseToJson(1, trans('menu.delete_success')); //id����

    }
}