<?php

namespace app\manystoreapi\controller;

use app\common\controller\ManystoreApiBase;
use app\common\model\school\classes\activity\order\Order as OrderModel;

/**
 * 机构API后台:机构课程活动订单
 *
 * @icon fa fa-circle-o
 */
class ActivityOrder extends ManystoreApiBase
{
    protected $model = null;

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

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

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



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

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

        try {
            $res =  OrderModel::getDetail($id);
        } catch (\Exception $e){
//            Log::log($e->getMessage());
            $this->apierror($e->getMessage(),['errcode'=>$e->getCode()]);
        }
        $this->apisuccess('获取成功', ['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', ''); //搜索关键字

        $has_evaluate =  $this->request->get('has_evaluate/d', 0); //搜索关键字
//        $type =  $this->request->get('type/s', ''); //筛选学员和教练单
        $params = [
            "shop_id"=>$this->auth->shop_id,
        ];
        try{
            //当前申请状态
            $res = $this->model::allList($user_id,$page, $limit,$keywords,$status,$classes_activity_id,$has_evaluate,$auth_status,$params);
//            if($user_id =='670153'){
//               file_put_contents("ceshi66.txt",(new AppointmentOrder())->getLastSql());
//            }
        }catch (\Exception $e){

            $this->apierror($e->getMessage());
        }
        $this->apisuccess('查询成功', $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', ''); //搜索关键字
        $params = [
            "shop_id"=>$this->auth->shop_id,
        ];

        try{
            $res = $this->model::orderCount($user_id,$classes_activity_id,$params);
        }catch (\Throwable $e){
            $this->apierror($e->getMessage());
        }
        $this->apisuccess('查询成功', $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,'shop',$this->auth->id,true);
        }catch (\Throwable $e){
            $this->apierror($e->getMessage());
        }
        $this->apisuccess('活动取消成功', $res);
    }




    /**
     * @ApiTitle(预约审核)
     * @ApiSummary(预约审核)
     * @ApiMethod(POST)
     * @ApiParams(name = "auth_status", type = "string",required=true,description = "审核状态:0=待审核,1=审核通过,2=审核失败")
     * @ApiParams(name = "reason", type = "string",required=true,description = "审核不通过原因")
     * @ApiParams(name = "id", type = "string",required=true,description = "订单id")
     * @ApiReturn({
     *
     *})
     */
    public function examine(){
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];
        $id =  $this->request->post('id/s', ''); //订单号
        $reason =  $this->request->post('reason/s', ''); //订单号
        $auth_status =  $this->request->post('auth_status/s', ''); //订单号
        try{
            //当前申请状态
            $res = $this->model->examine($id,$auth_status,$reason,0,true,'shop',$this->auth->id,true);
        }catch (\Throwable $e){
            $this->apierror($e->getMessage());
        }
        $this->apisuccess('预约审核完成', $res);
    }



    /**
     * @ApiTitle(活动单后台核销接口)
     * @ApiSummary(活动单后台核销接口)
     * @ApiMethod(POST)
     * @ApiParams(name = "ids", type = "string",required=true,description = "订单id")
     * @ApiReturn({
     *
     *})
     */
    public function verification(){
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];
        $ids =  $this->request->post('ids/s', ''); //订单号
        try{
            //当前申请状态
            $res = $this->model->verification($ids,0,true,'shop',$this->auth->id,true);
        }catch (\Throwable $e){
            $this->apierror($e->getMessage());
        }
        $this->apisuccess('活动核销成功', $res);
    }



    /**
     * @ApiTitle(活动单退款重试)
     * @ApiSummary(活动单退款重试)
     * @ApiMethod(POST)
     * @ApiParams(name = "ids", type = "string",required=true,description = "订单id")
     * @ApiReturn({
     *
     *})
     */
    public function refund(){
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];
        $ids =  $this->request->post('ids/s', ''); //订单号
        try{
            //当前申请状态
            $res = $this->model->orderRefund($ids,null,'shop',$this->auth->id,true);
        }catch (\Throwable $e){
            $this->apierror($e->getMessage());
        }
        $this->apisuccess('已重新发起退款,如果是第三方支付请等待回调!', $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=售后中,5=退款结算中,6=已退款,9=已完成")
     * @ApiParams(name = "classes_activity_order_id", type = "int",required=false,description = "课程活动订单id")
     * @ApiReturn({
     *
     *})
     */
    public function log_list()
    {
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];

        $params=[
//            "shop_id"=>$this->auth->shop_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', ''); //搜索关键字
        $classes_activity_order_id =  $this->request->get('classes_activity_order_id/s', ''); //搜索关键字

        $params["keywords"] = $keywords;
        $params["status"] = $status;
        $params["classes_activity_order_id"] = $classes_activity_order_id;
        if(empty($classes_activity_order_id)) $this->apierror( __('课程活动订单id必传'));

        try{
            //当前申请状态
            $res = \app\common\model\school\classes\activity\order\OrderLog::allList($page, $limit,$params);
//            if($user_id =='670153'){
//               file_put_contents("ceshi66.txt",(new AppointmentOrder())->getLastSql());
//            }
        }catch (\Exception $e){

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



}