<?php
/**
 *  用户表
 *  @author  system
 *  @version    1.0
 *  @date 2017-05-30 12:16:56
 *
 */
namespace App\Http\Controllers\Admin\User;
use App\Http\Controllers\Admin\Controller;
use App\Models\BaseAttachmentModel;
use App\Models\SupportDreamModel;
use Illuminate\Http\Request;
use App\Repositories\Base\Criteria\OrderBy;
use App\Repositories\User\Criteria\MultiWhere;
use App\Repositories\User\InfoRepository;

class InfoController extends Controller
{
    private $repository;

    public function __construct(InfoRepository $repository) {
        if(!$this->repository) $this->repository = $repository;
    }

    function index(Request $reqeust) {
        $search['keyword'] = $reqeust->input('keyword');
        $query = $this->repository->pushCriteria(new MultiWhere($search));
        $request = $reqeust->all();
        if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
        $query = $query->pushCriteria(new OrderBy($request['sort_field'],$request['sort_field_by']));
        }
        $list = $query->paginate();
        foreach ($list as $item) {
            $dream_number = count($item->dreams);
            $score_array = SupportDreamModel::where('user_id',$item->id)->select('score')->get()->toArray();
            $coin_array = $item->dreams->toArray();
            if (count($coin_array) > 0) {
                $coin_list = array_column($coin_array,'get_coin');
                $get_coin = array_sum($coin_list);
            }else{
                $get_coin = 0;
            }
            if (count($score_array) > 0) {
              $score_list = array_column($score_array,'score');
                $sup_score = array_sum($score_list);
            }else{
                $sup_score = 0;
            }
            $item->dream_number = $dream_number;        //梦想数量
            $item->sup_score = $sup_score;              //捐赠分数
            $item->get_coin = $get_coin;                //收到的梦想币
        }
        return view('admin.user.info.index',compact('list'));
    }

    function check(Request $reqeust) {
        $request = $reqeust->all();
        $search['keyword'] = $reqeust->input('keyword');
        $orderby = array();
        if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
            $orderby[$request['sort_field']] = $request['sort_field_by'];
        }
        $list = $this->repository->search($search,$orderby);
        return view('admin.user.info.check',compact('list'));
    }


    /**
     * 添加
     * 
     */
    public function create(Request $reqeust)
    {
        if($reqeust->method() == 'POST') {

            return $this->_createSave();
        }
        return view('admin.user.info.edit');
    }

    /**
     * 保存修改
     */
    private function _createSave(){
        $avatar =  request('avatar');
        $data = (array) request('data');
        $data['avatar'] = $avatar;
        $id = $this->repository->create($data);
        if($id) {
            $url[] = array('url'=>U( 'User/Info/index'),'title'=>'返回列表');
            $url[] = array('url'=>U( 'User/Info/create'),'title'=>'继续添加');
            $this->showMessage('添加成功',$url);
        }else{
            $url[] = array('url'=>U( 'User/Info/index'),'title'=>'返回列表');
            return $this->showWarning('添加失败',$url);
        }
    }
    
    /**
     * 
     * 修改
     * 
     * 
     */
    public function update(Request $reqeust) {
        if($reqeust->method() == 'POST') {
            return $this->_updateSave();
        }
        $data = $this->repository->find($reqeust->get('id'));
        return view('admin.user.info.edit',compact('data'));
    }

    /**
     * 保存修改
     */
    private function _updateSave() {
        $old_avatar = $this->repository->find(request('id'))->avatar;
        $data = (array) request('data');
        if (!empty(request('avatar'))) {
            if (is_file('.'.$old_avatar)) {
                unlink('.'.$old_avatar);
                BaseAttachmentModel::where('url',$old_avatar)->delete();
            }
            $data['avatar'] = request('avatar');
        }
        $ok = $this->repository->update(request('id'),$data);
        if($ok) {
            $url[] = array('url'=>U( 'User/Info/index'),'title'=>'返回列表');
            return $this->showMessage('操作成功',urldecode(request('_referer')));
        }else{
            $url[] = array('url'=>U( 'User/Info/index'),'title'=>'返回列表');
            return $this->showWarning('操作失败',$url);
        }
    }

    public function view(Request $reqeust) {
        $data = $this->repository->find(request('id'));
        return view('admin.user.info.view',compact('data'));
    }


    /**
     *
     * 状态改变
     *
     */
    public function status(Request $reqeust) {
        $ok = $this->repository->updateStatus(request('id'),request('status'));
        if($ok) {
            return $this->showMessage('操作成功');
        }else{
            return $this->showWarning('操作失败');
        }
    }
    
    /**
     * 删除
     */
    public function destroy(Request $reqeust) {
        $bool = $this->repository->destroy($reqeust->get('id'));
        if($bool) {
            return  $this->showMessage('操作成功');
        }else{
            return  $this->showWarning("操作失败");
        }
    }
}