<?php
/**
 *  小游戏用户
 *  @author  system
 *  @version    1.0
 *  @date 2018-05-25 10:59:46
 *
 */
namespace App\Http\Controllers\Admin\Album\Xyx;
use App\Http\Controllers\Admin\Controller;
use App\Models\AlbumAgentModel;
use Illuminate\Http\Request;
use App\Repositories\Base\Criteria\OrderBy;
use App\Repositories\Album\Xyx\Criteria\MultiWhere;
use App\Repositories\Album\Xyx\UserRepository;
use Illuminate\Support\Facades\DB;

class UserController extends Controller
{
    private $repository;

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

    function index(Request $request) {
        $search['keyword'] = $request->input('keyword');
        $query = $this->repository->pushCriteria(new MultiWhere($search,$this->getStoreId()));

        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();
        /*$obj1s = DB::table('album_cat')->where([['store_id',1],['parent_id',0]])->get();
        $cat_arr = array();
        if(!empty($obj1s)){
            foreach ($obj1s as $obj1){
                $obj1 = get_object_vars($obj1);
                $obj1['store_id'] = 2;
                $old_cat_id = $obj1['id'];
                unset($obj1['id']);
                $new_cat_id = DB::table('album_cat')->insertGetId($obj1);
                $cat_arr[$old_cat_id] = $new_cat_id;
            }
        }
        $obj2s = DB::table('album_product_style')->where([['store_id',1]])->get();
        $style_arr = array();
        if(!empty($obj2s)){
            foreach ($obj2s as $obj2){
                $obj2 = get_object_vars($obj2);
                $obj2['store_id'] = 2;
                $old_style_id = $obj2['id'];
                unset($obj2['id']);
                $new_style_id = DB::table('album_product_style')->insertGetId($obj2);
                $style_arr[$old_style_id] = $new_style_id;
            }
        }
        $obj3s = DB::table('album_product')->where([['store_id',1]])->get();
        if(!empty($obj3s)){
            foreach ($obj3s as $obj3){
                $obj3 = get_object_vars($obj3);
                if(!isset($style_arr[$obj3['style']])||!isset($cat_arr[$obj3['cat_id']])){
                    continue;
                }
                $obj3['store_id'] = 2;
                $obj3['style'] = $style_arr[$obj3['style']];
                $obj3['cat_id'] = $cat_arr[$obj3['cat_id']];
                unset($obj3['id']);
                DB::table('album_product')->insert($obj3);
            }
        }
        $obj4s = DB::table('album_banner')->where([['store_id',1]])->get();
        if(!empty($obj4s)){
            foreach ($obj4s as $obj4){
                $obj4 = get_object_vars($obj4);
                $obj4['store_id'] = 2;
                unset($obj4['id']);
                DB::table('album_banner')->insert($obj4);
            }
        }
        $obj5s = DB::table('album_manufacturer')->where([['store_id',1]])->get();
        if(!empty($obj5s)){
            foreach ($obj5s as $obj5){
                $obj5 = get_object_vars($obj5);
                $obj5['store_id'] = 2;
                unset($obj5['id']);
                DB::table('album_manufacturer')->insert($obj5);
            }
        }
        $obj6s = DB::table('album_information')->where([['store_id',1]])->get();
        if(!empty($obj6s)){
            foreach ($obj6s as $obj6){
                $obj6 = get_object_vars($obj6);
                $obj6['store_id'] = 2;
                unset($obj6['id']);
                DB::table('album_information')->insert($obj6);
            }
        }
        $obj7s = DB::table('album_news')->where([['store_id',1]])->get();
        if(!empty($obj7s)){
            foreach ($obj7s as $obj7){
                $obj7 = get_object_vars($obj7);
                $obj7['store_id'] = 2;
                unset($obj7['id']);
                DB::table('album_news')->insert($obj7);
            }
        }
        $obj8s = DB::table('album_nav')->where([['store_id',1]])->get();
        if(!empty($obj8s)){
            foreach ($obj8s as $obj8){
                $obj8 = get_object_vars($obj8);
                $obj8['store_id'] = 2;
                unset($obj8['id']);
                DB::table('album_nav')->insert($obj8);
            }
        }
        $obj8s = DB::table('album_info_cat')->where([['store_id',1]])->get();
        $info_cat_arr = array();
        if(!empty($obj8s)){
            foreach ($obj8s as $obj8){
                $obj8 = get_object_vars($obj8);
                $obj8['store_id'] = 2;
                $old_info_cat_id = $obj8['id'];
                unset($obj8['id']);
                $new_info_cat_id = DB::table('album_info_cat')->insertGetId($obj8);
                $info_cat_arr[$old_info_cat_id] = $new_info_cat_id;
            }
        }
        $obj6s = DB::table('album_information')->where([['store_id',1]])->get();
        if(!empty($obj6s)){
            foreach ($obj6s as $obj6){
                $obj6 = get_object_vars($obj6);
                if(!isset($info_cat_arr[$obj6['cat_id']])){
                    continue;
                }
                $obj6['store_id'] = 2;
                $obj6['cat_id'] = $info_cat_arr[$obj6['cat_id']];
                unset($obj6['id']);
                DB::table('album_information')->insert($obj6);
            }
        }*/
        return view('admin.album.xyx.user.index',compact('list'));
    }


    function check(Request $request) {
        $request = $request->all();
        $search['keyword'] = $request->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.album.xyx.user.check',compact('list'));
    }


    /**
     * 添加
     * 
     */
    public function create(Request $request)
    {
        if($request->method() == 'POST') {
            return $this->_createSave();
        }
        return view('admin.album.xyx.user.edit');
    }

    /**
     * 保存修改
     */
    private function _createSave(){
        $data = (array) request('data');
        $id = $this->repository->create($data);
        if($id) {
            $url[] = array('url'=>U( 'Album/Xyx/User/index'),'title'=>'返回列表');
            $url[] = array('url'=>U( 'Album/Xyx/User/create'),'title'=>'继续添加');
            $this->showMessage('添加成功',$url);
        }else{
            $url[] = array('url'=>U( 'Album/Xyx/User/index'),'title'=>'返回列表');
            return $this->showWarning('添加失败',$url);
        }
    }
    
    /**
     * 
     * 修改
     * 
     * 
     */
    public function update(Request $request) {
        /*if($request->method() == 'POST') {
            return $this->_updateSave();
        }
        $data = $this->repository->find($request->get('id'));
        return view('admin.album.xyx.user.edit',compact('data'));*/
        $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;
            $res = AlbumAgentModel::create($add);
            if($res) {
                return $this->showMessage('操作成功');
            }else{
                return $this->showWarning('操作失败');
            }
        }
        return view('admin.album.user.edit',compact('data'));
    }

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

    public function view(Request $request) {
        $data = $this->repository->find(request('id'));
        return view('admin.album.xyx.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 destroy(Request $request) {
        $bool = $this->repository->destroy($request->get('id'));
        if($bool) {
            return  $this->showMessage('操作成功');
        }else{
            return  $this->showWarning("操作失败");
        }
    }
}