<?php
/**
 *  系统设置
 *  @author  system
 *  @version    1.0
 *  @date 2018-05-14 13:30:36
 *
 */
namespace App\Http\Controllers\Admin\Album;
use App\Http\Controllers\Admin\Controller;
use App\Models\AlbumManufacturerModel;
use Illuminate\Http\Request;
use App\Repositories\Base\Criteria\OrderBy;
use App\Repositories\Album\Criteria\MultiWhere;
use App\Repositories\Album\ManufacturerRepository;

class ManufacturerController extends Controller
{
    private $repository;

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

    function list(Request $request){
        $search['keyword'] = $request->input('keyword');
        $isalbum = request('isalbum')?request('isalbum'):0;
        $query = $this->repository->pushCriteria(new MultiWhere($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']));
        }
        $list = $query->paginate(10);
        return view('admin.album.manufacturer.index',compact('list','isalbum'));
    }

    function index(Request $request) {
        if($request->method() == 'POST') {
            $iscreate = AlbumManufacturerModel::where('store_id',$this->getStoreId())->count();
            if($iscreate){
                return $this->_updateSave();
            }else{
                return $this->_createSave();
            }

        }
        $data = AlbumManufacturerModel::where('store_id',$this->getStoreId())->first();

        return view('admin.album.manufacturer.edit',compact('data'));
    }



    /**
     * 保存修改
     */
    private function _createSave(){
        $data = (array) request('data');
        $data['store_id'] =$this->getStoreId();
        if(!empty($data['avatar']))
            $data['avatar'] = $this->formatImgUrl($data['avatar']);
        if(!empty($data['background_pic']))
            $data['background_pic'] = $this->formatImgUrl($data['background_pic']);
        if(!empty($data['advertising_pic']))
            $data['advertising_pic'] = $this->formatImgUrl($data['advertising_pic']);
        if(!empty($data['notice_icon']))
            $data['notice_icon'] = $this->formatImgUrl($data['notice_icon']);
        if(!empty($data['furniture_ads_pic']))
            $data['furniture_ads_pic'] = $this->formatImgUrl($data['furniture_ads_pic']);

        $id = $this->repository->create($data);
        if($id) {
            $this->showMessage('添加成功');
        }else{
            return $this->showWarning('添加失败');
        }
    }

    /**
     * 保存修改
     */
    private function _updateSave() {
        $data = (array) request('data');
        if(!empty($data['avatar']))
            $data['avatar'] = $this->formatImgUrl($data['avatar']);
        if(!empty($data['background_pic']))
            $data['background_pic'] = $this->formatImgUrl($data['background_pic']);
        if(!empty($data['advertising_pic']))
            $data['advertising_pic'] = $this->formatImgUrl($data['advertising_pic']);
        if(!empty($data['notice_icon']))
            $data['notice_icon'] = $this->formatImgUrl($data['notice_icon']);
        if(!empty($data['furniture_ads_pic']))
            $data['furniture_ads_pic'] = $this->formatImgUrl($data['furniture_ads_pic']);

        $id = AlbumManufacturerModel::where('store_id',$this->getStoreId())->first()->id;
        $ok = $this->repository->update($id,$data);
        if($ok) {
            return $this->showMessage('操作成功');
        }else{
            return $this->showWarning('操作失败');
        }
    }

    public function view(Request $request) {
        $data = $this->repository->find(request('id'));
        return view('admin.album.manufacturer.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("操作失败");
        }
    }
}