<?php
/**
 *  基础设置
 *  @author  system
 *  @version    1.0
 *  @date 2018-07-22 14:05:49
 *
 */
namespace App\Http\Controllers\Admin\Setting;
use App\Http\Controllers\Admin\Controller;
use App\Models\SettingInfoModel;
use Illuminate\Http\Request;
use App\Repositories\Base\Criteria\OrderBy;
use App\Repositories\Setting\Criteria\MultiWhere;
use App\Repositories\Setting\InfoRepository;

class InfoController extends Controller
{
    private $repository;

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

    function index(Request $request) {
        if($request->method() == 'POST') {
            $iscreate = SettingInfoModel::all()->count();
            if($iscreate){
                return $this->_updateSave();
            }else{
                return $this->_createSave();
            }

        }
        $data = SettingInfoModel::first();

        return view('admin.setting.info.edit',compact('data'));
    }



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

    /**
     * 保存修改
     */
    private function _createSave(){
        $data = (array) request('data');
        $id = $this->repository->create($data);
        if($id) {
            $url[] = array('url'=>U( 'Setting/Info/index'),'title'=>'返回列表');
            $this->showMessage('添加成功',$url);
        }else{
            $url[] = array('url'=>U( 'Setting/Info/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.setting.info.edit',compact('data'));
    }

    /**
     * 保存修改
     */
    private function _updateSave() {
        $data = (array) request('data');
        $data['cert_path'] = $_SERVER['DOCUMENT_ROOT'].$data['cert_path'];
        $data['key_path'] = $_SERVER['DOCUMENT_ROOT'].$data['key_path'];

        $settinig = SettingInfoModel::first();
        $ok = $this->repository->update($settinig->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.setting.info.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("操作失败");
        }
    }
}