<?php

namespace app\api\controller\school\worker\activity;


use app\api\controller\school\worker\Base;
use app\common\model\school\classes\activity\order\Order as OrderModel;
use app\common\model\school\classes\Teacher as Teachermodel;

/**
 * 员工端：课程活动订单管理接口
 */
class Order extends Base
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    protected $model = null;

    /**
     * 初始化操作
     * @access protected
     */
    protected function _initialize()
    {

        $this->model = new OrderModel;
        parent::_initialize();

        //判断登录用户是否是员工
        $this->setUrlLock();
    }


    /**
     * @ApiTitle( 订单详情)
     * @ApiSummary(订单详情)
     * @ApiMethod(GET)
     * @ApiParams(name = "id", type = "int",required=true,description = "订单id或订单号")
     * @ApiReturn({
     *
     *})
     */
    public function detail(){
        $id = $this->request->get('id/s','');

        if(empty($id)){
            $this->error(__('缺少必要参数'));
        }

        try {
            $res =  OrderModel::getDetail($id,$this->classes_activity_ids);
            if(!$res)throw new \Exception('您无操作权限！',40003);
        } catch (\Exception $e){
//            Log::log($e->getMessage());
            $this->error($e->getMessage(),['errcode'=>$e->getCode()]);
        }
        $this->success('获取成功', ['detail' => $res]);
    }




    /**
     * @ApiTitle( 我的订单列表接口)
     * @ApiSummary(我的订单列表接口)
     * @ApiMethod(GET)
     * @ApiParams(name = "keywords", type = "string",required=false,description = "搜索关键字")
     * @ApiParams(name = "page", type = "string",required=true,description = "页数")
     * @ApiParams(name = "limit", type = "string",required=true,description = "条数")
     * @ApiParams(name = "status", type = "string",required=false,description = "状态搜索条件：-3=已取消,0=待支付,2=已报名待审核,3=已预约,4=售后中,6=已退款,9=已完成")
     * @ApiParams(name = "auth_status", type = "string",required=false,description = "审核状态:0=待审核,1=审核通过,2=审核失败")
     * @ApiParams(name = "classes_activity_id", type = "int",required=false,description = "课程活动id")
     * @ApiReturn({
     *
     *})
     */
    public function order_list()
    {
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];
        $page  =  $this->request->get('page/d', 0); //页数
        $limit =  $this->request->get('limit/d', 0); //条数
        $keywords =  $this->request->get('keywords/s', ''); //搜索关键字
        $status =  $this->request->get('status/s', ''); //搜索关键字
        $auth_status =  $this->request->get('auth_status/s', ''); //搜索关键字
        $classes_activity_id =  $this->request->get('classes_activity_id/s', ''); //搜索关键字
//        $type =  $this->request->get('type/s', ''); //筛选学员和教练单


        try{
            //当前申请状态
            $res = $this->model::workList($user_id,$page, $limit,$keywords,$status,$classes_activity_id,$this->classes_activity_ids,0,$auth_status);
//             var_dump($this->model->getLastSql());
        }catch (\Exception $e){

            $this->error($e->getMessage());
        }
        $this->success('查询成功', $res);
    }


    /**
     * @ApiTitle( 我的订单数量接口)
     * @ApiSummary(返回订单各个数量)
     * @ApiMethod(GET)
     * @ApiParams(name = "classes_activity_id", type = "int",required=false,description = "课程活动id")
     * @ApiReturn({
     *
     *})
     */
    public function order_count(){
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];

        $classes_activity_id =  $this->request->get('classes_activity_id/s', ''); //搜索关键字
        try{
            $res = $this->model::workCount($classes_activity_id,$this->classes_activity_ids);
        }catch (\Throwable $e){
            $this->error($e->getMessage());
        }
        $this->success('查询成功', $res);
    }





    /**
     * @ApiTitle( 员工代操作：活动订单取消接口)
     * @ApiSummary(活动订单取消接口（已支付单无法取消，要走售后）)
     * @ApiMethod(POST)
     * @ApiParams(name = "order_no", type = "string",required=true,description = "订单号")
     * @ApiReturn({
     *
     *})
     */
    public function cancel(){
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];
        $order_no =  $this->request->post('order_no/s', ''); //订单号
        try{
            //当前申请状态
            $res = $this->model->cancel($order_no,0,true,'user',$user_id,true);
        }catch (\Throwable $e){
            $this->error($e->getMessage());
        }
        $this->success('预约课时取消成功', $res);
    }


    /**
     * @ApiTitle( 员工代操作：预约报名审核接口)
     * @ApiSummary(预约报名审核接口)
     * @ApiMethod(POST)
     * @ApiParams(name = "order_no", type = "string",required=true,description = "活动订单号")
     * @ApiParams(name = "auth_status", type = "int",required=true,description = "审核状态:1=审核通过,2=审核失败")
     * @ApiParams(name = "reason", type = "string",required=false,description = "审核失败理由")
     * @ApiReturn({
     *
     *})
     */
    public function examine(){
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];
        $order_no =  $this->request->post('order_no/s', ''); //订单号
        $auth_status =  $this->request->post('auth_status/d', ''); //审核状态
        $reason =  $this->request->post('reason/s', ''); //审核失败理由
        try{
            //当前申请状态
            $res = $this->model->examine($order_no,$auth_status,$reason,0,true,'user',$user_id,true);
        }catch (\Throwable $e){
            $this->error($e->getMessage());
        }
        $this->success('预约课时取消成功', $res);
    }


    /**
     * @ApiTitle( 员工代操作：活动核销完成接口)
     * @ApiSummary(活动核销完成接口)
     * @ApiMethod(POST)
     * @ApiParams(name = "order_no", type = "string",required=true,description = "活动订单号")
     * @ApiReturn({
     *
     *})
     */
    public function verification(){
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];
        $order_no =  $this->request->post('order_no/s', ''); //订单号
        try{
            //当前申请状态
            $res = $this->model->verification($order_no,0,true,'user',$user_id,true);
        }catch (\Throwable $e){
            $this->error($e->getMessage());
        }
        $this->success('预约课时核销成功', $res);
    }

}

