<?php

namespace app\api\controller\company;

use app\common\controller\Apic;
use think\Db;

use alipaysdkphpallmaster\aop\AopClient;
use alipaysdkphpallmaster\aop\request\AlipayTradePayRequest;
/**
 * 订单管理
 */
class Order extends Apic
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';


    //
    public function lists(){
        $keyword = input('keyword','');
        $status  = input('status','all');

        $where = [
            'company_id' => $this->auth->id,
        ];
        if($status !== 'all'){
            $where['status'] = $status;
        }
        if($status == 20){ //待还车
            $where['status'] = 20;
            $where['endtime'] = ['gt',time()];
        }
        if($status == 200){ //逾期
            $where['status'] = 20;
            $where['endtime'] = ['elt',time()];
        }
        if($status == 30){
            $where['status'] = ['IN',[-1,-2,30]];
        }
        if(!empty($keyword))
        {
            $where['user_truename|user_mobile|orderno'] = ['LIKE','%'.$keyword.'%'];
        }
        $list = Db::name('order')->where($where)->order('id desc')->autopage()->select();

        $list = list_domain_image($list,['car_image','idcard_images','driver_images','get_yibiao_images','get_carvideo','back_yibiao_images','back_carvideo']);

        foreach($list as $key => &$val){
            $val['status_text'] = $this->status_text($val['status'],$val['endtime']);

            if($val['status'] == 20 && time() >= $val['endtime']){
                $val['status'] = 200; //強制改掉
            }

            //追加取消
            $val['cancel_info'] = (object)[];
            if($val['status'] == -1 || $val['status'] == -2){
                //取消订单追加取消原因
                $val['cancel_info'] = Db::name('order_cancel')->where('order_id',$val['id'])->find();
            }

            //追加评价
            $val['comment_info'] = (object)[];
            if($val['status'] == 30){
                //完成订单追加评价
                $comment_info = Db::name('order_comment')->where('order_id',$val['id'])->find();
                if($comment_info){
                    $val['comment_info'] = $comment_info;
                }
            }
        }
        $this->success(1,$list);
    }



    //详情
    public function info(){
        $id = input('id',0);
        $info = Db::name('order')->where('id',$id)->find();
        $info = info_domain_image($info,['car_image','idcard_images','driver_images','get_yibiao_images','get_carvideo','back_yibiao_images','back_carvideo']);

        $info['status_text'] = $this->status_text($info['status'],$info['endtime']);
        if($info['status'] == 20 && time() >= $info['endtime']){
            $info['status'] = 200; //強制改掉
        }

        //完成订单追加评价
        $comment_info = Db::name('order_comment')->alias('c')
            ->field('c.*,user.avatar,user.mobile')
            ->join('user','c.user_id = user.id','LEFT')
            ->where('c.order_id',$id)->find();
        $comment_info = info_domain_image($comment_info,['avatar']);
        if(!empty($comment_info)){
            $comment_info['mobile'] = str_replace(substr($comment_info['mobile'],3,5),'****',$comment_info['mobile']);
        }
        $info['comment_info'] = $comment_info;

        //取消订单追加取消原因
        $info['cancel_info'] = Db::name('order_cancel')->where('order_id',$id)->find();

        $this->success(1,$info);
    }
    //取消理由
    public function cancel_config(){
        $list = Db::name('company_cancel_config')->order('id asc')->select();
        $this->success(1,$list);
    }
    //取消
    public function cancel(){
        $id = input('id',0);
        $reason = input('reason','');

        Db::startTrans();
        $info = Db::name('order')->where('id',$id)->where('company_id',$this->auth->id)->lock(true)->find();
        if($info['status'] == -2){
            Db::rollback();
            $this->error('当前订单已经申请取消,已付款订单审核后可退款');
        }
        if($info['status'] != 0 && $info['status'] != 10){
            Db::rollback();
            $this->error('当前订单状态不能取消');
        }

        //未付款取消
        if($info['status'] == 0){
            $rs = Db::name('order')->where('id',$id)->update(['status'=>-1]);
        }

        //已付款取消
        if($info['status'] == 10){
            $rs = Db::name('order')->where('id',$id)->update(['status'=>-2]);
            //扣除车行收益
            //从计划任务里走
        }

        if($rs === false){
            Db::rollback();
            $this->error('申请取消失败');
        }

        $data = [
            'user_id'  => $info['user_id'],
            'company_id'  => $info['company_id'],
            'order_id' => $info['id'],
            'reason'   => '商家取消:'.$reason,
            'refund_price' => 0,
            'type' => $info['status'],               //分类:0=未付款,10=已付款
            'createtime' => time(),
            'status' => $info['status'] == 0 ? 2 : 0, //状态:0=未处理,1=已处理,2=无需处理
            'from' => 2,  //取消方:1=用户取消,2=商户取消
        ];

        $rs_cancle = Db::name('order_cancel')->insertGetId($data);
        if(!$rs_cancle){
            Db::rollback();
            $this->error('申请取消失败');
        }

        //解除汽车占用
        $rs_car = Db::name('car')->where('id',$info['car_id'])->update(['status'=>1]);
        if(!$rs_car){
            Db::rollback();
            $this->error('申请取消失败');
        }

        Db::commit();
        $this->success('取消成功');

    }


}
