<?php

namespace App\Http\Controllers\Admin;

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

/**
 *
 */
class FeedbackController extends Controller {

	function index() {
		# code...
	}

	/**
	 *用户反馈信息
	 * [Colse description]
	 */
	function feedbacks(Req $req) {
		$request=array();
		$request['account']=isset($req->account)?trim($req->account):null;
		$request['type'] = isset($req->type) ? trim($req->type) : null;
		$request['content'] = isset($req->content) ? trim($req->content) : null;
		$request['reply'] = isset($req->reply) ? trim($req->reply) : null;
		$request['status'] = isset($req->status) ? trim($req->status) : null;
		$request['startime'] = isset($req->startime) ? trim($req->startime) : null;
		$request['endtime'] = isset($req->endtime) ? trim($req->endtime) : null;
		
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/Feedback/feedback');
		$dt->setLang('user');
		$dt->addColsFields('account_name', array('templet' => '#userdetail', 'width' => 130, 'sort' => false));
		$dt->addColsFields('type', array('width' => 110, 'sort' => false));
		$dt->addColsFields('content', array('sort' => false));
		$dt->addColsFields('submit_time', array('width' => 180));
		$dt->addColsFields('status', array('width' => 110, 'sort' => false));
		$dt->addColsFields('reply', array('sort' => false));
		$dt->enableCheckBox();
		if (checkRriv('/admin/Feedback/Edit')) {
			$dt->setToolBar(array('edit'),array('width' => 100));
		}
		return view('admin.feedback/feedbacks', $dt->render($request));
	}

	/**
	 * 用户反馈内容
	 * 'feedback description'
	 * @return 'type' 'description'
	 */
	function feedback() {
		$page = Request::has('page') ? Request::get('page') : '';
		$list = Request::has('limit') ? Request::get('limit') : '';

		$account = Request::has('account') ? Request::get('account') : '';
		$type = Request::has('type') ? Request::get('type') : '';
		$content = Request::has('content') ? Request::get('content') : '';
		$reply = Request::has('reply') ? Request::get('reply') : '';
		$status = Request::has('status') ? Request::get('status') : '';
		$startime = Request::get('startime') ? Request::get('startime').' 00:00:00' : '';
		$endtime = Request::get('endtime') ? Request::get('endtime').' 23:59:59' : '';
		$sureblur = Request::has('sureblur') ? Request::get('sureblur') : '';
		$where = array();
		if (!empty($account)) {
			if (empty($sureblur) || $sureblur == '模糊') {
				$where[] = array('account_name', 'like', '%' . $account . '%');
			} else {
				$where[] = array('account_name', '=', $account);
			}
		}
		if (!empty($type)) {
			$where[] = array('type', '=', $type);
		}
		if (!empty($content)) {
			$where[] = array('content', 'like', '%' . $content . '%');
		}
		if (!empty($reply)) {
			$where[] = array('reply', 'like', '%' . $reply . '%');
		}
		if (!empty($status)) {
			if ($status == 2) {
				$where[] = array('status', '=', '0');
			} else {
				$where[] = array('status', '=', $status);
			}
		}
		if (!empty($startime)) {
			$startime = date('Y-m-d H:i:s', strtotime($startime));
			$where[] = array('submit_time', '>=', $startime);
		}
		if (!empty($endtime)) {
			$endtime = date('Y-m-d H:i:s', strtotime($endtime));
			$where[] = array('submit_time', '<=', $endtime);
		}

		$db = new \App\Models\Feedback;
		$data = $db->feedback($list, $page, $where);
		if (!($data < 0)) {
			$langinfo = trans('status.feedback.status');
			foreach ($data['data'] as $k => $v) {
				$data['data'][$k]['status'] = ($v['status'] == 0) ? $langinfo[2] : $langinfo[$v['status']];
			}
		}
		return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);
	}

	//显示用户详情
	function View(Req $req) {
		$account = !empty($req->account) ? $req->account : '';
		if (empty($account)) {
			abort(404);
		}
		$u_db = new \App\Models\Account();
		$u_details = new \App\Models\Account_detailed();
		$bank = new \App\Models\Account_bank();
		$data = $u_db::where('account', $account)->first();
		if (!$data) {
			abort(404);
		}
		$data = $data->toArray();
		$data_details = $u_details->getInfoBy($data['identity']);
		$data_details['account'] = $data['account'];
		$bank_data = $bank->getInfoByUid($data['identity']);
		$data_details['bank_name'] = isset($bank_data['bank_name']) ? $bank_data['bank_name'] : '';
		$data_details['bank_address'] = isset($bank_data['bank_address']) ? $bank_data['bank_address'] : '';
		$data_details['bank_number'] = isset($bank_data['bank_number']) ? $bank_data['bank_number'] : '';
		$data_details['register_time'] = isset($data_details['register_time']) ? $data_details['register_time'] : '';
		$data_details['register_ip'] = isset($data_details['register_ip']) ? $data_details['register_ip'] : '';
		$data_details['last_ip'] = isset($data_details['last_ip']) ? $data_details['last_ip'] : '';
		$data_details['cash'] = isset($data_details['cash']) ? $data_details['cash'] : '';
		$data_details['email'] = isset($data_details['email']) ? $data_details['email'] : '';
		$data_details['name'] = isset($data_details['name']) ? $data_details['name'] : '';
		unset($data_details['password']);
		return view('admin.feedback/view', ['data' => $data_details]);
	}

	//修改会员信息
	function Edit(Req $req) {
		$id = $req->id;
		if (intval($id) < 1) {
			abort(404);
		}
		$db = new \App\Models\Feedback();
		if (!$req->isMethod('post')) {
			$data = $db->getCloseOne($id);
			if (!$data) {
				return responseToJson($data);
			}
			return view('admin.feedback/edit', ['data' => $data]);
		} else {
			$data['status'] = '1';
			$data['reply'] = $req->input('reply');
			$res = $db->updateOne($data, $id);
			return responseToJson($res);
		}
	}

	/**
	 * 批量删除
	 * [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\Feedback::whereIn('id', $ids)->delete();
		if (!$rows) {
			return responseToJson(-2003); //id����
		}
		return responseToJson(1, trans('menu.delete_success')); //id����
	}

}