<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 产品管理
 */
namespace app\bestbao\controller;
use app\common\facade\Inform;
use app\bestbao\model\BestbaoEngineer;
use app\bestbao\model\BestbaoOrder;
use app\bestbao\model\BestbaoProduct;
use think\facade\Request;
use think\helper\Time;

class Order extends Common{


    public function initialize(){
        parent::initialize();
        $this->assign('pathMaps',[['name'=>'订单管理','url'=>'javascript:;']]);
    }


    /**
     * 列表
     */
    public function index(){
        $condition   = [];
        $keyword     = Request::param('keyword');
        $state     = Request::param('state/s');
        $condition[] = ['member_miniapp_id', '=', $this->member_miniapp_id];
        if (!empty($keyword)) {
            $condition[] = ['title', 'like', '%' . $keyword . '%'];
        }
        if($state != ''){
            $condition[] = ['state', '=', $state];
        }
        $time = Request::param('time/d',0);
        if($time){
            switch ($time) {
                case 2:
                    list($start, $end) = Time::yesterday();
                    break;
                case 30:
                    list($start, $end) = Time::month();
                    break;
                case 60:
                    list($start, $end) = Time::lastMonth();
                    break;
                default:
                    list($start, $end) = Time::today();
                    break;
            }
            $condition[] = ['create_time','>=',$start];
            $condition[] = ['create_time','<=',$end];
        }
        $starttime = Request::param('starttime/s');
        $endtime   = Request::param('endtime/s');
        if($starttime){
            $condition[] = ['create_time','>=',strtotime($starttime)];
        }
        if($endtime){
            $condition[] = ['create_time','<=',strtotime($endtime)];
        }
        $view['lists']     = BestbaoOrder::where($condition)->order('create_time desc')->paginate(15, false, ['query' => ['keyword' => $keyword, 'time' => $time, 'starttime' => $starttime, 'endtime' => $endtime]]);
        $view['time']      = $time;
        $view['starttime'] = $starttime;
        $view['endtime']   = $endtime;
        $view['state']     = $state;
        $view['keyword']   = $keyword;
        return view()->assign($view);
    }

    /**
     * @param int $id
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 订单详情
     */
    public function detail(int $id = 0){
        $info         = BestbaoOrder::where(['id' => $id])->find();
        $view['imgs'] = empty($info->imgs) ? '' : json_decode($info->imgs);
        $view['info'] = $info;
        return view()->assign($view);
    }

    /**
     * @return \think\response\Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 添加订单
     */
    public function add(){
        if(request()->isAjax()){
            $data = [
                'member_miniapp_id' => $this->member_miniapp_id,
                'title'             => input('post.title/s'),
                'question'          => input('post.question/s'),
                'realname'          => input('post.realname/s'),
                'phone'             => input('post.phone/s'),
                'address'           => input('post.address/s'),
                'urgent'            => input('post.urgent/d'),
                'uid'               => input('post.uid/d'),
                'state'             => 0,
                'product_id'        => input('post.product_id/d'),
                'imgs'              => json_encode(input('post.imgs/a')),
                'create_time'       => time(),
                'update_time'       => time(),
            ];
            $validate = $this->validate($data,'Order.add');
            if(true !== $validate){
                return enjson(0,$validate);
            }
            $result = BestbaoOrder::create($data);
            if($result){
                //通知到工程师
                $list = BestbaoEngineer::where(['member_miniapp_id' => $this->member_miniapp_id])->select()->toArray();
                foreach ($list as $info){
                    Inform::sms($info['uid'],$this->member_miniapp_id,['title' =>'业务进展通知','type' => '新订单申请','content' =>'您有新的订单待待处理','state' => '待处理']);
                }
                return enjson(200,'操作成功',['url'=>url('order/index')]);
            }else{
                return enjson(0,'操作失败');
            }
        }else{
            return view();
        }
    }

    /**
     * @return \think\response\View
     * @throws \think\exception\DbException
     * 查找产品
     */
    public function selectProduct(){
        $keyword     = Request::param('keyword/s');
        $input       = Request::param('input/s');
        $view['list']    = BestbaoProduct::where(['member_miniapp_id' => $this->member_miniapp_id])->where('title', 'like', '%' . $keyword . '%')->order('create_time desc')->paginate(20);
        $view['keyword'] = $keyword;
        $view['input']   = $input;
        $view['id']      = $this->member_miniapp_id;
        return view()->assign($view);
    }

    /**
     * @return \think\response\Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 修改订单
     */
    public function edit(){
        if(request()->isAjax()){
            $data = [
                'id'                => input('post.id/d'),
                'member_miniapp_id' => $this->member_miniapp_id,
                'title'             => input('post.title/s'),
                'question'          => input('post.question/s'),
                'realname'          => input('post.realname/s'),
                'phone'             => input('post.phone/s'),
                'address'           => input('post.address/s'),
                'urgent'            => input('post.urgent/d'),
                'uid'               => input('post.uid/d'),
                'state'             => 0,
                'product_id'        => input('post.product_id/d'),
                'imgs'              => json_encode(input('post.imgs/a')),
                'update_time'       => time(),
            ];
            $validate = $this->validate($data,'Order.edit');
            if(true !== $validate){
                return enjson(0,$validate);
            }
            $result = BestbaoOrder::update($data);
            if($result){
                return enjson(200,'操作成功',['url'=>url('order/index')]);
            }else{
                return enjson(0,'操作失败');
            }
        }else{
            $id = input('id/d');
            $info = BestbaoOrder::where(['id' => $id])->find();
            $view['imgs'] = json_decode($info['imgs'],true);
            $view['info'] = $info;
            return view()->assign($view);
        }
    }

    /**
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * 修改订单
     */
    public function delete(){
        $id = input('id/d');
        $result  = BestbaoOrder::where(['id' => $id])->delete();
        if($result){
            return enjson(200,'操作成功');
        }else{
            return enjson(0,'操作失败');
        }
    }

    /**
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * 修改订单
     */
    public function end(){
        $id = input('id/d');
        $result  = BestbaoOrder::where(['id' => $id])->update(['state' => 2]);
        if($result){
            return enjson(200,'操作成功');
        }else{
            return enjson(0,'操作失败');
        }
    }
}