<?php
/**
 *  导航栏设置
 *  @author  system
 *  @version    1.0
 *  @date 2018-05-16 17:42:12
 *
 */
namespace App\Http\Controllers\Admin\Album;
use App\Http\Controllers\Admin\Controller;
use App\Models\AlbumInfoCatModel;
use App\Models\AlbumNavModel;
use App\Models\AlbumNewsModel;
use App\Repositories\Album\Criteria\NavWhere;
use Illuminate\Http\Request;
use App\Repositories\Base\Criteria\OrderBy;
use App\Repositories\Album\Criteria\MultiWhere;
use App\Repositories\Album\NavRepository;

class NavController extends Controller
{
    private $repository;
    private $url;

    public function __construct(NavRepository $repository) {

        if(!$this->repository) $this->repository = $repository;

    }

    function index(Request $request) {
        $search['keyword'] = $request->input('keyword');
        $query = $this->repository->pushCriteria(new NavWhere($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']));
        }else{
            $query = $query->pushCriteria(new OrderBy('id','DESC'));
        }
        $list = $query->paginate();
        return view('admin.album.nav.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.nav.check',compact('list'));
    }

    private function getPageData(){
        $url = [
            ['name'=>'主页','value'=>'/page/homePage/homePage'],
            ['name'=>'个人中心','value'=>'/page/user/user'],
            ['name'=>'产品列表','value'=>'/pages/goods/list'],
            ['name'=>'新品发布','value'=>'/page/newpro/newpro']
        ];

        $this->url = $url;
    }

    /**
     * 添加
     * 
     */
    public function create(Request $request)
    {
        if($request->method() == 'POST') {
            return $this->_createSave();
        }
        $this->getPageData();
        $page = $this->url;

        return view('admin.album.nav.edit',compact('page'));
    }

    /**
     * 保存修改
     */
    private function _createSave(){
        $data = (array) request('data');
        $data['store_id'] = $this->getStoreId();
        $zhi = request('zhi');
        //dd($zhi);die;
        if($zhi!=null)
            $data['url'].=$zhi;
        $data['pic_url'] = isset($data['pic_url'])?$this->formatImgUrl($data['pic_url']):null;
        $data['pic_url_active'] = isset($data['pic_url_active'])?$this->formatImgUrl($data['pic_url_active']):null;
        $id = $this->repository->create($data);
        if($id) {
            $url[] = array('url'=>U( 'Album/Nav/index'),'title'=>'返回列表');
            $url[] = array('url'=>U( 'Album/Nav/create'),'title'=>'继续添加');
            $this->showMessage('添加成功',$url);
        }else{
            $url[] = array('url'=>U( 'Album/Nav/index'),'title'=>'返回列表');
            return $this->showWarning('添加失败',$url);
        }
    }
    
    /**
     * 
     * 修改
     * 
     * 
     */
    public function update(Request $request) {
        if($request->method() == 'POST') {
            \Log::info($request->all());
            return $this->_updateSave();
        }
        $data = $this->repository->find($request->get('id'));
        if($data['open_type'] == 4){
            $url = $data['url'];
            if(strpos($url,'=')){
                $urls = explode('=',$url);
                $data['zhi'] = $urls[1];
                $data['url'] = $urls[0].'=';
            }else{
                $data['zhi'] = '';
            }
        }
        //dd($data);
        $this->getPageData();
        $page = $this->url;
        return view('admin.album.nav.edit',compact('data','page'));
    }

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

    public function view(Request $request) {
        $data = $this->repository->find(request('id'));
        return view('admin.album.nav.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) {
        $nav = AlbumNavModel::find($request->get('id'));
        $ok = $nav->delete();

        if($ok) {
            return  $this->showMessage('操作成功');
        }else{
            return  $this->showWarning("操作失败");
        }
    }
}