<?php

namespace App\Http\Controllers\Admin\Settings;

use App\Http\Controllers\Admin\Controller;
use App\Models\BaseSettingsModel;
use Illuminate\Http\Request;
use App\Repositories\Settings\Banner\Criteria\MultiWhere;
use App\Repositories\Settings\SettingsRepository;
use App\Repositories\Base\Criteria\OrderBy;
class SettingsController extends Controller
{
    private $repository;

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

    public function banner(Request $request)
    {
        $search['keyword'] = $request->input('keyword');
        $query = $this->repository->pushCriteria(new MultiWhere($search));
        $request = $request->all();
        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();
        return view('admin.settings.banner.index',compact('list'));
    }

    public function bannerCreate(Request $reqeust)
    {
        if ($reqeust->method() == 'POST') {
            return $this->_createSave();
        }
        return view('admin.settings.banner.edit');
    }

    /**
     * 保存修改
     */
    private function _createSave(){
        $data = (array) request('data');
        $pic = (array) request('value');
        $data['category'] = 'banner';
        $data['key'] = '0';
        $data['pid'] = '0';
        $data['value'] = $pic[0];
        $id = $this->repository->create($data);
        if($id) {
            $url[] = array('url'=>U( 'setting/banner/index'),'title'=>'返回列表');
            $url[] = array('url'=>U( 'setting/banner/create'),'title'=>'继续添加');
            $this->showMessage('添加成功',$url);
        }else{
            $url[] = array('url'=>U( 'setting/banner/index'),'title'=>'返回列表');
            return $this->showWarning('添加失败',$url);
        }
    }

    /**
     *
     * 修改
     *
     *
     */
    public function bannerUpdate(Request $reqeust) {
        if($reqeust->method() == 'POST') {
            return $this->_updateSave();
        }
        $data = $this->repository->find($reqeust->get('id'));
        return view('admin.settings.banner.edit',compact('data'));
    }

    /**
     * 保存修改
     */
    private function _updateSave() {
        $data = (array) request('data');
        $data2 =  $this->repository->find(request('id')) ;
        $old_pics =  $data2->value;
        $pic = (array) request('value');
        if (!empty($pic)) {
            $data['value'] = $pic[0];
            if (is_file('.'.$old_pics)) {
                unlink('.'.$old_pics);
            }
        }
        $ok = $this->repository->update(request('id'),$data);
        if($ok) {
            $url[] = array('url'=>U( 'setting/banner/index'),'title'=>'返回列表');
            return $this->showMessage('操作成功',urldecode(request('_referer')));
        }else{
            $url[] = array('url'=>U( 'setting/banner/index'),'title'=>'返回列表');
            return $this->showWarning('操作失败',$url);
        }
    }

    public function bannerDestroy(Request $request)
    {
        $data2 =  $this->repository->find(request('id')) ;
        $old_pics =  $data2->value;
        $bool = $this->repository->destroy($request->id);
        if($bool) {
            if (is_file('.'.$old_pics)) {
                unlink('.'.$old_pics);
            }
            return  $this->showMessage('操作成功');
        }else {
            return $this->showWarning("操作失败");
        }
    }

//    参数设置
    public function paihangEdit(Request $reqeust) {
        if ($reqeust->method() == 'POST') {
            $data = (array) request('data');
            $result = BaseSettingsModel::where('category','paihang')->get();
            if (count($result) == 0) {
                $data['category'] = 'paihang';
                $data['sort'] = 0;
                $data['pid'] = 0;
                $ok = BaseSettingsModel::create($data);
                if ($ok) {
                    return $this->showMessage('操作成功');
                }else{
                    return $this->showWarning('操作失败');
                }
            }else {
                $ok = BaseSettingsModel::where('category', 'paihang')->update($data);
                if ($ok) {
                    return $this->showMessage('操作成功');
                } else {
                    return $this->showWarning('操作失败');
                }
            }
        }
        $data = BaseSettingsModel::where('category','paihang')->first();
        return view('admin.settings.paihang.edit',compact('data'));
    }

    public function scoreEdit(Request $reqeust) {
        if ($reqeust->method() == 'POST') {
            $data = (array) request('data');
            $result = BaseSettingsModel::where('category','score')->get();
            if (count($result) == 0) {
                $data['category'] = 'score';
                $data['sort'] = 0;
                $data['pid'] = 0;
                $ok = BaseSettingsModel::create($data);
                if ($ok) {
                    return $this->showMessage('操作成功');
                }else{
                    return $this->showWarning('操作失败');
                }
            }else {
                $ok = BaseSettingsModel::where('category', 'score')->update($data);
                if ($ok) {
                    return $this->showMessage('操作成功');
                } else {
                    return $this->showWarning('操作失败');
                }
            }
        }
        $data = BaseSettingsModel::where('category','score')->first();
        return view('admin.settings.score.edit',compact('data'));
    }

    public function sign(Request $request)
    {
        $search['keyword'] = $request->input('keyword');
        $query = $this->repository->pushCriteria(new MultiWhere($search));
        $request = $request->all();
        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();
        return view('admin.settings.sign.index',compact('list'));
    }

    public function signCreate(Request $reqeust)
    {
        if ($reqeust->method() == 'POST') {
            return $this->sign_createSave();
        }
        return view('admin.settings.sign.edit');
    }

    /**
     * 保存修改
     */
    private function sign_createSave(){
        $data = (array) request('data');
        $data['category'] = 'sign';
        $data['key'] = '0';
        $data['pid'] = '0';
        $data['sort'] = 0;
        $id = $this->repository->create($data);
        if($id) {
            $url[] = array('url'=>U( 'setting/sign/index'),'title'=>'返回列表');
            $url[] = array('url'=>U( 'setting/sign/create'),'title'=>'继续添加');
            $this->showMessage('添加成功',$url);
        }else{
            $url[] = array('url'=>U( 'setting/sign/index'),'title'=>'返回列表');
            return $this->showWarning('添加失败',$url);
        }
    }

    /**
     *
     * 修改
     *
     *
     */
    public function signUpdate(Request $reqeust) {
        if($reqeust->method() == 'POST') {
            return $this->sign_updateSave();
        }
        $data = $this->repository->find($reqeust->get('id'));
        return view('admin.settings.sign.edit',compact('data'));
    }

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

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