<?php
/**
 *  用户管理
 *  @author  system
 *  @version    1.0
 *  @date 2018-05-14 13:25:12
 *
 */
namespace App\Http\Controllers\Admin\Album;
use App\Http\Controllers\Admin\Controller;
use App\Models\AlbumAgentModel;
use App\Models\AlbumUserModel;
use App\Repositories\Album\Criteria\UserWhere;
use Illuminate\Http\Request;
use App\Repositories\Base\Criteria\OrderBy;
use App\Repositories\Album\Criteria\MultiWhere;
use App\Repositories\Album\UserRepository;

class UserController extends Controller
{
    private $repository;

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

    function furnitureIndex(Request $request) {
        $search['keyword'] = $request->input('keyword');
        $search['storeid'] = $this->getStoreId();
        $search['role'] = $request->input('role') ?? -1;
        $order = array();
        if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
            $order[$request['sort_field']] = $request['sort_field_by'];
        }else{
            $order['id']='DESC';
        }

        $list = $this->repository->searchUser($search,$order);
        //dd($list);
//        dump($list);die;
        return view('admin.album.user.furniture-index',compact('list'));
    }

    function albumIndex(Request $request) {
        $search['keyword'] = $request->input('keyword');
        $search['storeid'] = $this->getStoreId();
        $search['is_boss'] = $request->input('is_boss') ?? -1;
        $order = array();
        if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
            $order[$request['sort_field']] = $request['sort_field_by'];
        }else{
            $order['id']='DESC';
        }

        $list = $this->repository->searchUser($search,$order);
        //dd($list);
//        dump($list);die;
        return view('admin.album.user.album-index',compact('list'));
    }



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


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

    /**
     *
     * 修改
     *
     *
     */
    public function update(Request $request) {
        $data = $this->repository->find($request->get('id'));
        if($data->is_dealer == 1){
            return $this->showWarning('该用户已成为经销商');
        }
        $save['is_dealer'] = 1;
        $ok = $this->repository->update(request('id'),$save);

        if($ok){
            $add['store_id'] = $this->getStoreId();
            $add['user_id'] = $data->id;
            $add['name'] = $data->username;
            $add['status'] = 1;
            $res = AlbumAgentModel::create($add);
            if($res) {
                return $this->showMessage('操作成功');
            }else{
                return $this->showWarning('操作失败');
            }
        }
        
    }

    /**
     * 保存修改
     */
    private function _updateSave() {
        $data = (array) request('data');
        $ok = $this->repository->update(request('id'),$data);
        if($ok) {
            $url[] = array('url'=>U( 'Album/User/index'),'title'=>'返回列表');
            return $this->showMessage('操作成功',urldecode(request('_referer')));
        }else{
            $url[] = array('url'=>U( 'Album/User/index'),'title'=>'返回列表');
            return $this->showWarning('操作失败',$url);
        }
    }


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

    public function role(Request $request){
        $id = request('id');
        $role = request('role');
        $user = AlbumUserModel::find($id);
        if ($role == 4) {
            $user->is_boss = 1;
        } else if ($role == 5) {
            $user->is_boss = 0;
        } else {
            $user->role = $role;
        }
        $ok = $user->save();
        if($ok) {
            return $this->showMessage('操作成功');
        }else{
            return $this->showWarning('操作失败');
        }
    }
}