<?php

namespace app\api\controller\company;

use app\common\controller\Apic;
use app\common\service\UserService;
use think\Db;
use think\Log;

/**
 * 预约管理
 */
class Preorder extends Apic
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';


    //列表
    public function lists(){
        $pre_order_status = input('pre_order_status',1);
        $where = [
            'po.company_id' => $this->auth->company_id,
            'po.pre_order_status' => $pre_order_status,
        ];
        $list = Db::name('pre_order')->alias('po')->field('po.*,servicetype.title,order.server_time')
            ->join('servicetype','po.servicetype_id = servicetype.id','LEFT')
            ->join('order','po.order_id = order.id','LEFT')
            ->where($where)->order('po.id desc')->autopage()->select();
        $this->success(1,$list);
    }

    //取消
    public function cancel(){
        $id = input('pre_order_id',0);
        $cancel_reason = input('cancel_reason','');
        $update = [
            'cancel_reason' => $cancel_reason,
            'cancel_time'   => time(),
            'updatetime'    => time(),
            'pre_order_status' => 0,
        ];
        Db::name('pre_order')->where('id',$id)->where('company_id',$this->auth->company_id)->update($update);
        $userService = new UserService();
        $params['pre_order_id'] = $id;
        $userService->msgPreOrder($params);
        $this->success('取消成功');
    }

    //详情
    public function info(){
        $id = input('pre_order_id',0);
        $info = Db::name('pre_order')->alias('po')
            ->field('po.*,servicetype.title as servicetype_title')
            ->join('servicetype','po.servicetype_id = servicetype.id','LEFT')
            ->where('po.id',$id)->find();
        $this->success(1,$info);
    }

    //车牌号或手机号搜用户
    public function searchuser(){
        $keyword = input('keyword','');

        //先搜手机
        $where = [
            'mobile' => ['LIKE','%'.$keyword.'%']
        ];
        $user_ids = Db::name('user')->where($where)->column('id');

        //再搜车牌
        $where = [
            'car_number' => ['LIKE','%'.$keyword.'%']
        ];
        $car_user_ids = Db::name('user_car')->where($where)->column('user_id');

        //合并用户ids
        $new_user_ids = array_merge($user_ids,$car_user_ids);
        $new_user_ids = array_flip(array_flip($new_user_ids));

        //搜用户
        $user_list = Db::name('user')->field('id,mobile,nickname')->where('id','IN',$new_user_ids)->autopage()->select();
        foreach($user_list as $key => &$val){
            $user_car = Db::name('user_car')->where('user_id',$val['id'])->select();
            $val['car_list'] = $user_car;
            $val['address'] = Db::name('user_wallet')->where('user_id',$val['id'])->where('company_id',$this->auth->company_id)->value('address');
        }

        $this->success(1,$user_list);
    }

    //预约开单 在线开单
    public function submit_order(){
        $pre_order_id = input('pre_order_id',0); //预约单id

        $data = request_post_hub([
            'user_name','user_car_number','user_mobile','user_address',
            'servicetype_id','server_time','server_info','server_images','pay_fee'
        ]);

        Db::startTrans();

        //预约单
        if($pre_order_id > 0){
            //check
            $map = ['id'=>$pre_order_id,'company_id'=>$this->auth->company_id,'pre_order_status'=>1];
            $pre_order = Db::name('pre_order')->where($map)->lock(true)->find();
            if(empty($pre_order)){
                Db::rollback();
                $this->error('不存在的预约单');
            }

            $data['pre_order_id'] = $pre_order_id;
            $data['user_id'] = $pre_order['user_id'];


            //修改状态
            $update = [
                'order_time'   => time(),
                'updatetime'    => time(),
                'pre_order_status' => 2,
            ];
            $update_rs = Db::name('pre_order')->where('id',$pre_order_id)->update($update);
            if($update_rs === false){
                Db::rollback();
                $this->error('下单失败');
            }
        }else{
            $data['pre_order_id'] = 0;

            //检查用户
            $user_info = Db::name('user')->where('mobile',$data['user_mobile'])->find();
            if(empty($user_info)){
                Db::rollback();
                $this->error('不存在的用户,请先让客户扫店铺码注册');
            }
            $data['user_id'] = $user_info['id'];
        }

        //检索car_id，没必要了

        //准备数据
        $data['orderno']    = createUniqueNo('O',$this->auth->id);
        $data['ordertype']    = ($pre_order_id > 0) ? 1 : 2;  //类型:1=预约下单,2=在线下单,3=套餐订单
        $data['company_id'] = $this->auth->company_id;
        $data['staff_id'] = $this->auth->id;
        $data['total_fee'] = $data['pay_fee'];

        $data['status'] = 2; //2=已支付,待处理
        $data['createtime'] = time();

        $order_id = Db::name('order')->insertGetId($data);
        if(!$order_id){
            Db::rollback();
            $this->error('下单失败');
        }

        if($pre_order_id > 0){
            $update = [
                'order_id'   => $order_id,
            ];
            $update_rs = Db::name('pre_order')->where('id',$pre_order_id)->update($update);
            if($update_rs === false){
                Db::rollback();
                $this->error('下单失败');
            }
        }
        Db::commit();
        $this->success('下单完成',$order_id);
    }

    //查看预约单下的订单




}
