<?php

namespace app\manystoreapi\controller;

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

/**
 * 机构API后台：课程订单
 */
class ClassesOrder extends ManystoreApiBase
{
    protected $model = null;

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

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

        //判断登录用户是否是员工
    }


    /**
     * @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->apierror(__('缺少必要参数'));
        }

        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=待支付,3=使用中,4=售后中,6=已退款,9=已完成")
     * @ApiParams(name = "classes_lib_id", type = "int",required=false,description = "课程id")
     * @ApiParams(name = "has_evaluate", type = "int",required=false,description = "是否评价:默认0全部 ，1查已评价 2查未评价")
     * @ApiReturn({
     *
     *})
     */
    public function order_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_lib_id =  $this->request->get('classes_lib_id/s', ''); //搜索关键字

        $has_evaluate =  $this->request->get('has_evaluate/d', 0); //搜索关键字
//        $type =  $this->request->get('type/s', ''); //筛选学员和教练单
        $params["keywords"] = $keywords;
        $params["status"] = $status;
        $params["classes_lib_id"] = $classes_lib_id;
        $params["has_evaluate"] = $has_evaluate;

        try{
            //当前申请状态
            $res = $this->model::allShopList($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);
    }



    /**
     * @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=待支付,3=使用中,4=售后中,6=已退款,9=已完成")
     * @ApiParams(name = "classes_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_order_id =  $this->request->get('classes_order_id/s', ''); //搜索关键字

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

        try{
            //当前申请状态
            $res = \app\common\model\school\classes\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);
    }




    /**
     * @ApiTitle( 我的订单列表数量统计)
     * @ApiSummary(我的订单列表数量统计)
     * @ApiMethod(GET)
     * @ApiParams(name = "keywords", type = "string",required=false,description = "搜索关键字")
     * @ApiParams(name = "classes_lib_id", type = "int",required=false,description = "课程id")
     * @ApiParams(name = "has_evaluate", type = "int",required=false,description = "是否评价:默认0全部 ，1查已评价 2查未评价")
     * @ApiReturn({
     *
     *})
     */
    public function order_count()
    {
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];
        $params=[
            "shop_id"=>$this->auth->shop_id,
        ];
        $keywords =  $this->request->get('keywords/s', ''); //搜索关键字
//        $status =  $this->request->get('status/s', ''); //搜索关键字
        $classes_lib_id =  $this->request->get('classes_lib_id/s', ''); //搜索关键字

        $has_evaluate =  $this->request->get('has_evaluate/d', 0); //搜索关键字
//        $type =  $this->request->get('type/s', ''); //筛选学员和教练单
        $params["keywords"] = $keywords;
//        $params["status"] = $status;
        $params["classes_lib_id"] = $classes_lib_id;
        $params["has_evaluate"] = $has_evaluate;

        try{
            //当前申请状态
            $res = $this->model::baseCount($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(POST)
     * @ApiParams(name = "ids", type = "int",required=true,description = "课程订单id")
     * @ApiReturn({
     *
     *})
     */
    public function cancel($ids = ''){
        $param = $this->request->param();
        if($this->request->isPost()){
            try{
                if(isset($param['ids']))$ids = $param['ids'];
                //设置模拟资格
                $model = (new \app\common\model\school\classes\order\Order);
                $model->cancel($ids,0,true,'shop',$this->auth->id,true);


            }catch (\Exception $e){
                $this->apierror($e->getMessage());
            }
            $this->apisuccess('取消成功!');
        }

    }



    /**
     * @ApiTitle( 课程订单发起售后)
     * @ApiSummary(课程订单发起售后)
     * @ApiMethod(POST)
     * @ApiParams(name = "id", type = "int",required=true,description = "课程订单id")
     * @ApiParams(name = "reason", type = "int",required=true,description = "申请售后原因")
     * @ApiParams(name = "price", type = "int",required=true,description = "退款金额")
     * @ApiReturn({
     *
     *})
     */
    public function after_sales($ids = ""){

        if($this->request->isPost())
        {
            try{
                $params = $this->request->post();
                $classes_order = $params["id"];
                $reason = $params["reason"];

                $model = (new \app\common\model\school\classes\order\ServiceOrder());
                $remark = "机构端管理员帮忙下售后单";
                $order = $model->afterSales($classes_order,$reason,$remark,'shop',$this->auth->id,true);



                $price = $params["price"];
                $status = "yes";
                $reject_reason = "";
                $reject_images = "";
                $model = (new \app\common\model\school\classes\order\ServiceOrder());
                $model->shopConfirmation($order["order_no"],$status,$price,$reject_reason,$reject_images,0,true,'shop',$this->auth->id,true);



            }catch (\Exception $e){
                $this->apierror($e->getMessage());
            }
            $this->apisuccess("执行成功");
        }

    }


}