<?php
namespace App\Http\Controllers\Admin;

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

class SystemRedController extends Controller {

    public function Index(Req $req)
    {
        $request=array();
        $request['name']=isset($req->name)?trim($req->name):null;

        $dt = \App\Lib\DataTable\DataTable::init();
        $dt->setDataSource('/admin/SystemRed/redList');
        $dt->setLang('systemred');
        $dt->enableCheckBox();
        $dt->addColsFields('id');
        $dt->addColsFields('red_num');
        $dt->addColsFields('red_money');
        $dt->addColsFields('room_name');
        $dt->addColsFields('username');
        $dt->addColsFields('status');
        $dt->addColsFields('tool', array('templet' => '#option','sort'=>false));
        return view('admin.SystemRed/index',$dt->render($request));
    }

    public function redList(Req $req)
    {
        $list = $req->limit;
        $db = new \App\Models\SystemRed();
        $data = $db->getAll($list);
        $status = trans ('systemred.stat');
        foreach ($data['data'] as $key => $val)
        {
            $data['data'][$key]->status =  $status[$val->status];
        }
        return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);
    }



    public function add(Req $req)
    {
        if(!$req->isMethod('post')){
            $chatRoom = new \App\Models\ChatRooms();
            $chatRoom = $chatRoom->getAll();
            return view('admin.SystemRed/add',['chatRoom' => $chatRoom]);
        }else{
            $chatRoom = $req->chatRoom?trim($req->chatRoom):'';
            $red_num = trim($req->red_num);
            $red_money = trim($req->red_money);
            $red_mark = trim($req->red_mark);
            $status = trim($req->status);
            $username = trim($req->username);
            $account = new \App\Models\Account();
            $res = $account->checkUserByName($username);
            if($res<0){
                return responseToJson(-1,'账户不存在');
            }
            iF($red_money > $res['cash'])
            {
                return responseToJson(-1,'账户余额不足');
            }
            if(empty($chatRoom)){
                return responseToJson(-10086);
            }
            if(empty($red_num)){
                return responseToJson(-10086);
            }
            if(empty($red_money)){
                return responseToJson(-10088);
            }
            $red = new \App\Models\SystemRed();
            $room = $red->checkRoom($chatRoom);
            if($room){
                return responseToJson(-1,'房间红包已存在');
            }
            $update_data=array(
                'red_num'=>$red_num,
                'red_money' =>$red_money,
                'red_mark' => $red_mark,
                'room_id' =>$chatRoom,
                'status' => $status,
                'username'=>$username
            );
            $res=$red->addSystemRed($update_data);
            $log = array(
                session('adminInfo.admin_name'),
                $chatRoom
            );
            OperationLog(session('adminInfo.admin_id'), 'edit_info', $log);
            return responseToJson($res);
        }
    }

    public function edit(Req $req)
    {
        $id=$req->id;
        $chatRoom = new \App\Models\ChatRooms();
        $red = new \App\Models\SystemRed();
        if(!$req->isMethod('post')){
            $data=$red->getSystemRedOne(['id'=>$id]);
            $room = $chatRoom->getAll();
            return view('admin.SystemRed/edit',['chatRoom'=>$room,'data'=>$data]);
        }else{
            $red_num = trim($req->red_num);
            $red_money = trim($req->red_money);
            $red_mark = trim($req->red_mark);
            $status = trim($req->status);
            $username = trim($req->username);
            $account = new \App\Models\Account();
            $res = $account->checkUserByName($username);
            if($res<0){
                return responseToJson(-1,'账户不存在');
            }
            iF($red_money > $res['cash'])
            {
                return responseToJson(-1,'账户余额不足');
            }
            if(empty($chatRoom)){
                return responseToJson(-10086);
            }
            if(empty($red_num)){
                return responseToJson(-10086);
            }
            if(empty($red_money)){
                return responseToJson(-10088);
            }
            $update_data=array(
                'red_num'=>$red_num,
                'red_money' =>$red_money,
                'red_mark' => $red_mark,
                'status' => $status,
                'username'=>$username
            );
            $res=$red->updateSystemRed(['id'=>$id],$update_data);
            $log = array(
                session('adminInfo.admin_name'),
                $chatRoom
            );
            OperationLog(session('adminInfo.admin_id'), 'edit_info', $log);
            return responseToJson($res);
        }
    }

    public function delete(Req $req)
    {
        $id = $req->input('id');
        if (empty($id)) {
            return responseToJson(-2001);
        }
        $ids = explode(',', $id);
        if (!is_array($ids) && intval($ids) < 0) {
            return responseToJson(-2002);
        }
        if (is_array($ids) && count($ids) > 0) {
            foreach ($ids as $k => $v) {
                if (intval($v) < 1) {
                    unset($ids[$k]);
                }
            }
        }
        $rows = \App\Models\SystemRed::whereIn('id', $ids)->delete();
        if (!$rows) {
            return responseToJson(-2003);
        }
        return responseToJson(1);
    }

}
