<?php
/**
 *  地标
 *  @author  system
 *  @version    1.0
 *  @date 2018-10-26 08:38:42
 *
 */
namespace App\Http\Controllers\Admin\Map;
use App\Http\Controllers\Admin\Controller;
use App\Models\MapBannerModel;
use App\Models\MapMessageModel;
use App\Repositories\Map\Criteria\MarkWhere;
use Illuminate\Http\Request;
use App\Repositories\Base\Criteria\OrderBy;
use App\Repositories\Map\Criteria\MultiWhere;
use App\Repositories\Map\LandmarkRepository;

class LandmarkController extends Controller
{
    private $repository;

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

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

        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.map.landmark.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.map.landmark.check',compact('list'));
    }


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

    /**
     * 保存修改
     */
    private function _createSave(){
        $data = (array) request('data');
        if(!empty($data['banner']['url'])){
            $banner = $data['banner']['url'];
            unset($data['banner']);
        }

//        $data['cover'] = $this->formatImgUrl($data['cover']);
        $id = $this->repository->create($data);
        if(!empty($banner)){
            foreach($banner as $key=>$val){
                $add['image'] = $this->formatImgUrl($val);
                $add['mark_id'] = $id;
                $add['sort'] = 0;
                MapBannerModel::create($add);
            }
        }

        if($id) {
            $url[] = array('url'=>U( 'Map/Landmark/index'),'title'=>'返回列表');
            $url[] = array('url'=>U( 'Map/Landmark/create'),'title'=>'继续添加');
            $this->showMessage('添加成功',$url);
        }else{
            $url[] = array('url'=>U( 'Map/Landmark/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'));
        $banner = MapBannerModel::where('mark_id',$data['id'])->get();
        $imgs = array();
        foreach($banner as $key=>$val){
            $imgs[] = $val['image'];
        }
        $data->banner = $imgs;
        return view('admin.map.landmark.edit',compact('data'));
    }

    /**
     * 保存修改
     */
    private function _updateSave() {
        $data = (array) request('data');
        $id = request('id');
        MapBannerModel::where('mark_id',$id)->delete();
        if(!empty($data['banner'])){
            foreach($data['banner']['url'] as $key=>$val){
                $add['image'] = $this->formatImgUrl($val);
                $add['mark_id'] = $id;
                $add['sort'] = 0;
                MapBannerModel::create($add);
            }

        }
        unset($data['banner']);
//        $data['cover'] = $this->formatImgUrl($data['cover']);
       // dd($data);
        $ok = $this->repository->update(request('id'),$data);
        if($ok) {
            $url[] = array('url'=>U( 'Map/Landmark/index'),'title'=>'返回列表');
            return $this->showMessage('操作成功',urldecode(request('_referer')));
        }else{
            $url[] = array('url'=>U( 'Map/Landmark/index'),'title'=>'返回列表');
            return $this->showWarning('操作失败',$url);
        }
    }

    public function view(Request $request) {
        $data = $this->repository->find(request('id'));
        return view('admin.map.landmark.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'));
        MapMessageModel::where('mark_id',$request->get('id'))->delete();
        if($bool) {
            return  $this->showMessage('操作成功');
        }else{
            return  $this->showWarning("操作失败");
        }
    }


}