<?php
/**
 *  预约管理
 *  @author  system
 *  @version    1.0
 *  @date 2018-07-18 01:53:05
 *
 */
namespace App\Http\Controllers\Admin\User;
use App\Http\Controllers\Admin\Controller;
use App\Models\ProductScheduleModel;
use App\Models\StoreInfoModel;
use App\Models\UserScheduleModel;
use Illuminate\Http\Request;
use App\Repositories\Base\Criteria\OrderBy;
use App\Repositories\User\Criteria\MultiWhere;
use App\Repositories\User\ScheduleRepository;

class ScheduleController extends Controller
{
    private $repository;

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

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

        $order = array();
        if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
            $order[$request['sort_field']] = $request['sort_field_by'];
        }else{
            if(isset($search['expire'])){
                $order['time']='DESC';
                $order['user_schedule.id']='ASC';
            }else{
                $order['time']='ASC';
                $order['user_schedule.id']='ASC';
            }


        }

        $list = $this->repository->searchSchedule($search,$order);

        return view('admin.user.schedule.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.user.schedule.check',compact('list'));
    }


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

        $stores = StoreInfoModel::all();
        return view('admin.user.schedule.edit',compact('stores'));
    }

    /**
     * 保存修改
     */
    private function _createSave(){
        $data = (array) request('data');
        if(!$this->canOrder($data['time'],$data['store_id'])){
            $url[] = array('url'=>U( 'User/Schedule/index'),'title'=>'返回列表');
            return $this->showWarning('改时间段预约人数已满',$url);
        }
        $id = $this->repository->create($data);
        if($id) {
            $url[] = array('url'=>U( 'User/Schedule/index'),'title'=>'返回列表');
            $url[] = array('url'=>U( 'User/Schedule/create'),'title'=>'继续添加');
            $this->showMessage('添加成功',$url);
        }else{
            $url[] = array('url'=>U( 'User/Schedule/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'));
        $stores = StoreInfoModel::all();

        return view('admin.user.schedule.edit',compact('data','stores'));
    }

    /**
     * 保存修改
     */
    private function _updateSave() {
        $data = (array) request('data');
        if(!$this->canOrder($data['time'],$data['store_id'])){
            $url[] = array('url'=>U( 'User/Schedule/index'),'title'=>'返回列表');
            return $this->showWarning('改时间段预约人数已满',$url);
        }
        $ok = $this->repository->update(request('id'),$data);
        if($ok) {
            $url[] = array('url'=>U( 'User/Schedule/index'),'title'=>'返回列表');
            return $this->showMessage('操作成功',urldecode(request('_referer')));
        }else{
            $url[] = array('url'=>U( 'User/Schedule/index'),'title'=>'返回列表');
            return $this->showWarning('操作失败',$url);
        }
    }

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

    public function canOrder($time,$storeid){
        $max = ProductScheduleModel::first()->max;
        $count = UserScheduleModel::where('time',$time)->where('store_id',$storeid)->count();

        if($count >= $max){
            return false;
        }
        else{
            return true;
        }

    }
}