<?php

namespace app\api\controller;

use app\api\controller\v1\Base;
use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Car extends Base
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 查询常用路线
     * @return void
     */
    public function selectroute(){
        $res=Db::name("route")->field("id,name,start_address,end_address")->limit(2)->select();
        return $this->success($res);
    }

    /**
     * 通过线路查询车辆
     * @return void
     */
    public function selectcarbyroute(){
        $route_id = $this->request->param("route_id");
        $time = $this->request->param("time");
        $res=Db::name("car")
            ->alias("a")
            ->join("route b","a.route_id=b.id")
            ->field("a.id,b.id as route_id,DATE_FORMAT(FROM_UNIXTIME(a.reservation_time), '%H:%i') AS reservation_time ,DATE_FORMAT(FROM_UNIXTIME(a.start_time), '%H:%i') AS start_time ,b.price")
            ->where("route_id",$route_id)
            ->group("start_time")
            ->select();

        $start_time=strtotime(date("Y-m-d",$time));
        $end_time=$start_time+86400;
        $newtime=time();
        foreach ($res as $k=>$v){
            $res[$k]['order']=Db::name("order")
                ->where("car_id",$res[$k]['id'])
                ->where("is_pay",1)
                ->where("reservation_time",">",$start_time)
                ->where("reservation_time","<",$end_time)
                ->count();

        }
        return $this->success($res);
    }


    /**
     * 创建订单
     * @return void
     */
    public function createorder(){
        $number=$this->request->param("number");//乘车人数
        $phone = $this->request->param("phone");//联系手机号码
        $user_name = $this->request->param("user_name");//联系人
        $position = $this->request->param("position");//上车地址
        $lat = $this->request->param("lat");//经度
        $lng = $this->request->param("lng");//维度
        $remarks = $this->request->param("remarks");//备注
        $intended_driver_id = $this->request->param("intended_driver_id");//意向司机
        $route_id = $this->request->param("route_id");//线路
        $car_id = $this->request->param("car_id");//车辆
        if($number<=0){
            $this->error("人数错误");
        }
        //查询线路
        $route=Db::name("route")->where("id",$route_id)->find();
        //查询车辆
        $car=Db::name("car")->where("id",$car_id)->find();
        //查询司机
        $driver=Db::name("driver")->where("id",$car['driver_id'])->find();
        $data=[
            "order_no"=>getOrderSn(),
            "price"=>bcmul($route['price'],$number,2),
            "is_pay"=>"2",//未支付
            "car_id"=>$car_id,
            "user_id"=>$this->auth->id,
            "phone"=>$phone,
            "user_name"=>$user_name,
            "driver_id"=>$driver['id'],
            "driver_name"=>$driver['name'],
            "create_time"=>time(),
            "reservation_time"=>getOrderSn(),
            "position"=>$position,
            "lat"=>$lat,
            "lng"=>$lng,
            "number"=>$number,
            "remarks"=>$remarks,
            "intended_driver_id"=>$intended_driver_id,
        ];
        $res=Db::name("order")->insertGetId($data);
        $userinfo = Db::name('user')
            ->where(['id' => $this->auth->id])
            ->field('id,wx_xcx_openid')
            ->find();
        $notifyURI = $this->doman . '/addons/epay/api/OrderPayNtf';
        $params = [
            'amount' => $data['price'],
            'orderid' => $data['order_no'],
            'type' => 'wechat',
            'notifyurl' => $notifyURI,
            'method' => 'miniapp',
            'openid' => $userinfo['wx_xcx_openid'],
        ];
        $f = \addons\epay\library\Service::submitOrder($params);
        $this->success("请求成功",$f);


    }

    /**
     * 查询乘车人信息
     * @return void
     */
    public function passengerlist(){
        $res=Db::name("passenger")->where("user_id",$this->auth->id)->select();
        return $this->success("请求成功",$res);
    }
    /**
     * 增加乘车人信息
     * @return void
     */
    public function addpassenger(){
        $name=$this->request->param("name");
        $IDcard=$this->request->param("IDcard");
        $phone=$this->request->param("phone");
        $is_adult=$this->request->param("is_adult");
        $res=Db::name("passenger")->insert([
            "name"=>$name,
            "IDcard"=>$IDcard,
            "phone"=>$phone,
            "user_id"=>$this->auth->id,
            "is_adult"=>$is_adult
        ]);
        return $this->success("添加成功",$res);
    }

    /**
     * 修改乘车人信息
     * @return void
     */
    public function updatepassenger(){
        $name=$this->request->param("name");
        $IDcard=$this->request->param("IDcard");
        $phone=$this->request->param("phone");
        $is_adult=$this->request->param("is_adult");
        $id=$this->request->param("id");
        $res=Db::name("passenger")->where("id",$id)->update([
            "name"=>$name,
            "IDcard"=>$IDcard,
            "phone"=>$phone,
            "is_adult"=>$is_adult
        ]);
        return $this->success("修改成功",$res);
    }

    /**
     * 查询订单
     * @return void
     */
    public function selectorder(){
        $is_pay=$this->request->param("is_pay"); //是否支付:1=已支付,2=未支付,3=已退款,4=已取消
        $w['user_id'] = $this->auth->id;
        if ($is_pay){
            $w['is_pay'] = $is_pay;
        }
        $res=Db::name("order")
            ->where($w)
            ->select();
        return $this->success("请求成功",$res);
    }


    /**
     * 切换司机
     * @return void
     */
    public function switching_driver(){
        $res=Db::name("driver")->where("user_id",$this->auth->id)->find();
        if($res){
            return $this->success("请求成功",$res);
        }else{
            return $this->error("请求失败");
        }
    }
    /**
     * 切换司机
     * @return void
     */
    public function switching_user(){
        $res=Db::name("user")->where("id",$this->auth->id)->find();
        if($res){
            return $this->success("请求成功",$res);
        }else{
            return $this->error("请求失败");
        }
    }
    /**
     *
     * @return void
     */
    public function selectbydriver(){
        //$driver
        $res=Db::name("order")
            ->where("user_id",$this->auth->id)
            ->select();
    }

    /**
     *认证声明
     * @return void
     */
    public function authentication_statement(){
        $content = config("site.content");//微信小程序AppID
        return $this->success($content);
    }


}