<?php
/**
 *  新品列表
 * @author  system
 * @version    1.0
 * @date 2018-12-20 10:09:42
 *
 */

namespace App\Http\Controllers\Admin\Furniture\Newgoods;

use App\Http\Controllers\Admin\Controller;
use App\Models\FurnitureNewgoodsBookingModel;
use App\Models\FurnitureNewgoodsCommentModel;
use App\Models\FurnitureNewgoodsInfoModel;
use Illuminate\Http\Request;
use App\Repositories\Base\Criteria\OrderBy;
use App\Repositories\Furniture\Newgoods\Criteria\MultiWhere;
use App\Repositories\Furniture\Newgoods\InfoRepository;

class InfoController extends Controller
{
    private $repository;

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

    function index(Request $request)
    {
        $search['keyword'] = $request->input('keyword');
        $search['store_id'] = $this->getStoreId();

        $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(20);
        return view('admin.furniture.newgoods.info.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.furniture.newgoods.info.check', compact('list'));
    }


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

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

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

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

    /***
     * 评价统计
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function commentschart(Request $request)
    {
        $newgoods_id = $request->get('id');
        $newgoods = FurnitureNewgoodsInfoModel::find($newgoods_id);
        $comments_field_1 = FurnitureNewgoodsCommentModel::where('newgoods_id', $newgoods_id)->select(\DB::raw('AVG(comments_field_1) as field'))->first();
        $comments_field_2 = FurnitureNewgoodsCommentModel::where('newgoods_id', $newgoods_id)->select(\DB::raw('AVG(comments_field_2) as field'))->first();
        $comments_field_3 = FurnitureNewgoodsCommentModel::where('newgoods_id', $newgoods_id)->select(\DB::raw('AVG(comments_field_3) as field'))->first();
        $comments_field_4 = FurnitureNewgoodsCommentModel::where('newgoods_id', $newgoods_id)->select(\DB::raw('AVG(comments_field_4) as field'))->first();

        return view('admin.furniture.newgoods.info.commentschart', compact('comments_field_1', 'comments_field_2', 'comments_field_3', 'comments_field_4', 'newgoods'));
    }

    public function booking(Request $request)
    {
        $newgoods_id = $request->get('id');
        $list = FurnitureNewgoodsBookingModel::where('newgoods_id', $newgoods_id)->orderBy('id', 'desc')->paginate(10);
        return view('admin.furniture.newgoods.info.booking', compact('list'));
    }

    public function bookingremark(Request $request){
        $booking = FurnitureNewgoodsBookingModel::find($request->get('id'));
        $booking->remark = $request->get('remark');
        $bool = $booking->save();
          if ($bool) {
              return $this->showMessage('操作成功');
          } else {
              return $this->showWarning("操作失败");
          }
    }

}