<?php

namespace App\Http\Controllers\Admin;

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

/**
 *
 */
class PaymentController extends Controller {

	function index(Req $req) {
		$request['show_name'] = isset($req->show_name) ? trim($req->show_name) : null;
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/Payment/info');
		$dt->setLang('payment');
		$dt->addColsFields('infoname', array('templet' => '#infoname', 'sort' => false, 'width' => 160));
		$dt->addColsFields('bank_num', array('sort' => false));
		$dt->addColsFields('bank');
        $dt->addColsFields('groups_names');
		$dt->addColsFields('remark');
		$dt->addColsFields('sort');
		$dt->addColsFields('time');
        $dt->addColsFields('type', array('templet' => '#type', 'sort' => false, 'width' => 150,'align' => 'center'));
		$dt->addColsFields('status', array('templet' => '#status', 'sort' => false, 'width' => 150,'align' => 'left'));
		$dt->enableCheckBox();
		if (checkRriv('/admin/Payment/edit')) {
			$dt->setToolBar(array('edit'));
		}
		return view('admin.Payment/index', $dt->render($request));
	}

	function info() {
		$db = new \App\Models\Payment();
		$data = $db->getInfos();
		return \App\Lib\DataTable\DataTable::init()->toJson($data);
	}


    //启用/禁用会员信息
    function UserStart(Req $req) {
        $id = $req->id;
        $status = is_numeric($req->status) ? intval($req->status) : '';
        if (empty($id) || !is_numeric($status)) {
            return responseToJson(-2020100102);
        }
        $u_db = new \App\Models\Payment;
        $data = array(
            'status' => $status,
        );
        $res = $u_db->updateInfos($data, $id);
        $dd = $u_db->upInfos($id);
        if ($status == 1) {
            $log = array(
                session('adminInfo.admin_name'), 
                $dd['bank'],
            );
            $res = OperationLog(session('adminInfo.admin_id'), 'upUserStart', $log);
        }else {
            $log = array(
                session('adminInfo.admin_name'), 
                $dd['bank'],
            );
            $res = OperationLog(session('adminInfo.admin_id'), 'deUserStart', $log);
        }
        return responseToJson($res);
    }

    function edit(Req $req) {
        $id = $req->id;
        if (intval($id) < 1) {
            return -1;
        }
        /***获取会员等级信息***/
        $labelInfo=\App\Models\Account_group::getAllInfo();
        /**********/
        if (!$req->isMethod('post')) {
            $data = \App\Models\Payment::where('id', $id)->first();
            if (!$data) {
                return -2;
            }
            $data = $data->toArray();
            return view('admin.Payment/edit', array('data' => $data,'group'=>$labelInfo));
        } else {
            $groups=$req->groups?$req->groups:'';
            //选择的组
            $group_codes=array_keys($groups);
            //获取组名的
            $groups_names='| ';
            foreach ($labelInfo as $k=>$v){
                if(in_array($v['group_code'],$group_codes))
                    $groups_names.=$v['group_name'].'| ';
            }

            $model = \App\Models\Payment::where('id', $id)->first();
            $model->infoname = trim($req->input('infoname'));
            $model->bank_num = trim($req->input('bank_num'));
            $model->bank = trim($req->input('bank'));
            $model->remark = trim($req->input('remark'));
            $model->groups='0,'.implode(',',$group_codes).',';
            $model->groups_names=$groups_names;
            $model->sort = trim($req->input('sort'));
            $model->time = date('Y-m-d H:i:s');
            $model->status = trim($req->input('status'));
            $type = trim($req->input('type'));
            if(!empty($type)){
                $model->type = $type;
            }

            $model->save();
            $log = array(
                session('adminInfo.admin_name'),
                $model->infoname,
                $model->bank_num,
            );
            OperationLog(session('adminInfo.admin_id'), 'UserStart', $log);
            return responseToJson(1);
        }

    }

    function add(Req $req) {
        /***获取会员等级信息***/
        $labelInfo=\App\Models\Account_group::getAllInfo();
        /***获取会员等级信息**/
        if (!$req->isMethod('post')) {
            $data['status'] = 1;
            $data['type'] = 0;
            return view('admin.Payment/edit', ['data' => $data,'group'=>$labelInfo]);
        } else {
            $groups=$req->groups?$req->groups:'';
            //选择的组
            $group_codes=array_keys($groups);
            //获取组名的
            $groups_names='| ';
            foreach ($labelInfo as $k=>$v){
                if(in_array($v['group_code'],$group_codes))
                    $groups_names.=$v['group_name'].'| ';
            }
            $model = new \App\Models\Payment();
            $model->infoname = trim($req->input('infoname'));
            $model->bank_num = trim($req->input('bank_num'));
            $model->bank = trim($req->input('bank'));
            $model->groups='0,'.implode(',',$group_codes).',';
            $model->groups_names=$groups_names;
            $model->remark = trim($req->input('remark'));
            $model->sort = trim($req->input('sort'));
            $model->time = date('Y-m-d H:i:s');
            $model->status = 1;
            $type = trim($req->input('type'));
            if(!empty($type)){
                $model->type = $type;
            }
            $model->save();
            $log = array(
                session('adminInfo.admin_name'),
                $model->infoname,
                $model->bank_num,
            );
            OperationLog(session('adminInfo.admin_id'), 'addStart', $log);
            return responseToJson(1);
        }
    }

    /**
     * 批量删除
     * [delete description]
     * @return [type] [description]
     */
    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\Payment::whereIn('id', $ids)->delete();
        if (!$rows) {
            return responseToJson(-2003); //id����
        }
        $log = array(
           session('adminInfo.admin_name'), 
        );
        $res = OperationLog(session('adminInfo.admin_id'), 'deleteUserStart', $log);
        return responseToJson(1); //id����
    }
}