<?php

namespace app\manystoreapi\controller;

use app\common\controller\ManystoreApiBase;

use app\common\model\school\classes\hourorder\Order as OrderModel;
use app\common\model\school\classes\hourorder\OrderLog;
use app\common\model\school\classes\order\ServiceOrderLog;
use think\exception\PDOException;

/**
 * 机构API后台：课程预约订单管理
 */
class HourOrder 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(POST)
     * @ApiParams(name = "classes_order_id", type = "int",required=true,description = "课程订单id")
     * @ApiParams(name = "classes_lib_spec_id", type = "int",required=false,description = "课时规格id")
     * @ApiReturn({
     *
     *})
     */
    public function add(){
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];
        $classes_order_id =  $this->request->post('classes_order_id/d', 0); //课程id
        $classes_lib_spec_id =  $this->request->post('classes_lib_spec_id/d', 0); //课程id

//        $order_no  =  $this->request->post('order_no/s', ''); //订单号
//        $is_compute =  $this->request->post('is_compute/d', 1); //是否重新计算并更新缓存
        try{


            //记录代下单人信息
            $param  =  [
                "type" =>'2',
                "help_user_id" =>$this->auth->id,
                "help_type" => 'shop',
            ];
            //确认订单
            $res = $this->model->confirm($this->auth->id,$classes_order_id,null, $classes_lib_spec_id,$param, true);
            $remark = "总后台管理员帮忙下课时预约";
            //创建订单
            $result = $this->model->cacheCreateOrder($res['order_no'], $this->auth->id,$remark, true);


        }catch (\Exception $e){
//            Log::log($e->getMessage());
            $this->apierror($e->getMessage(),['errcode'=>$e->getCode()]);
        }
        $this->apisuccess('预约成功', $result);
    }






    /**
     * @ApiTitle( 机构代修改预约单预约课时)
     * @ApiSummary(机构代修改预约单预约课时)
     * @ApiMethod(POST)
     * @ApiParams(name = "id", type = "int",required=true,description = "预约id")
     * @ApiParams(name = "classes_lib_spec_id", type = "int",required=false,description = "课时规格id")
     * @ApiReturn({
     *
     *})
     */
    public function edit(){
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];
        $id =  $this->request->post('id/d', 0); //课程id
        $classes_lib_spec_id =  $this->request->post('classes_lib_spec_id/d', 0); //课程id

//        $order_no  =  $this->request->post('order_no/s', ''); //订单号
//        $is_compute =  $this->request->post('is_compute/d', 1); //是否重新计算并更新缓存
        try{
            $result = $this->model->updateClassesSpec($id,$classes_lib_spec_id,0,true,'shop',$this->auth->id,true);
        }catch (\Exception $e){
//            Log::log($e->getMessage());
            $this->apierror($e->getMessage(),['errcode'=>$e->getCode()]);
        }
        $this->apisuccess('预约修改成功', $result);
    }





    /**
     * @ApiTitle( 机构代取消预约单)
     * @ApiSummary(机构代取消预约单)
     * @ApiMethod(POST)
     * @ApiParams(name = "ids", type = "string",required=true,description = "预约id,多值逗号拼接")
     * @ApiReturn({
     *
     *})
     */
    public function cancel($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->apierror(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids/s","");
        if ($ids) {
            $pk = $this->model->getPk();
            if($this->shopIdAutoCondition){
                $this->model->where(array('shop_id'=>SHOP_ID));
            }
            $list = $this->model->where($pk, 'in', $ids)->select();

            $count = 0;

            try {
                foreach ($list as $k => $v) {
                    $res = $this->model->cancel($v["id"],0,true,'shop',$this->auth->id,true);

                    $count ++;
                }

            } catch (PDOException $e) {

                $this->apierror($e->getMessage());
            } catch (\Exception $e) {

                $this->apierror($e->getMessage());
            }
            if ($count) {
                $this->apisuccess();
            } else {
                $this->apierror(__('No rows were deleted'));
            }
        }
        $this->apierror(__('Parameter %s can not be empty', 'ids'));
    }





    /**
     * @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,'shop',$user_id,true);
        }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 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,'shop',$user_id,true);
        }catch (\Throwable $e){
            $this->apierror($e->getMessage());
        }
        $this->apisuccess('预约课时核销成功', $res);
    }









    /**
     * @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,$this->classes_lib_ids);
        } 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=已取消,-1=已报名待审核,0=已预约,3=已完成")
     * @ApiParams(name = "classes_order_id", type = "string",required=false,description = "课程订单id")
     * @ApiParams(name = "classes_lib_id", type = "int",required=false,description = "课程id")
     * @ApiParams(name = "shop_id", type = "int",required=false,description = "机构shopid")
     * @ApiParams(name = "start_time", type = "string",required=false,description = "按开始时间区间查（传值格式：Y/m/d H:M:S-Y/m/d H:M:S）")
     * @ApiParams(name = "createtime", type = "string",required=false,description = "按创建时间区间查（传值格式：Y/m/d H:M:S-Y/m/d H:M:S）")
     * @ApiParams(name = "time", type = "string",required=false,description = "按开始和结束时间区间查（传值格式：Y/m/d H:M:S-Y/m/d H:M:S）")
     * @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', ''); //搜索关键字
        $classes_order_id =  $this->request->get('classes_order_id/d', 0); //搜索关键字
        $classes_lib_ids =  $this->request->get('classes_lib_id/s', 0); //搜索关键字
        $shop_id =  $this->request->get('shop_id/d', 0); //搜索关键字

        $start_time =  $this->request->get('start_time/s', ''); //搜索关键字
        $createtime =  $this->request->get('createtime/s', ''); //搜索关键字
        $time =  $this->request->get('time/s', ''); //搜索关键字

//        $type =  $this->request->get('type/s', ''); //筛选学员和教练单
//        var_dump($this->classes_lib_ids);die;
        try{
            //当前申请状态
            $res = $this->model::workList($page, $limit,$keywords,$status,$classes_order_id,0,$classes_lib_ids,$this->classes_lib_ids,$start_time,$createtime,$time,$shop_id);
//            var_dump($this->model->getLastSql());die;
//            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_order_id", type = "int",required=true,description = "课程订单id")
     * @ApiParams(name = "classes_lib_id", type = "int",required=false,description = "课程id")
     * @ApiParams(name = "shop_id", type = "int",required=false,description = "机构shopid")
     * @ApiReturn({
     *
     *})
     */
    public function order_count(){


        $classes_order_id =  $this->request->get('classes_order_id/d', 0); //搜索关键字
        $classes_lib_ids =  $this->request->get('classes_lib_id/s', 0); //搜索关键字
        $shop_id =  $this->request->get('shop_id/d', 0); //搜索关键字

        try{
            $res = $this->model::workCount($classes_lib_ids,$this->classes_lib_ids,$classes_order_id,$shop_id);
        }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=已取消,-1=已报名待审核,0=已预约,3=已完成")
     * @ApiParams(name = "auth_status", type = "string",required=false,description = "审核状态:0=待审核,1=审核通过,2=审核失败")
     * @ApiParams(name = "classes_hour_order_id", type = "int",required=false,description = "课时预约订单id")
     * @ApiReturn({
     *
     *})
     */
    public function order_log_list()
    {
        $user_id = 0;
//        $user = $this->auth->getUser();//登录用户
//        if($user)$user_id = $user['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_hour_order_id =  $this->request->get('classes_hour_order_id/s', ''); //搜索关键字
        if(!$classes_hour_order_id) $this->apierror('缺少课时预约订单id');
//        $type =  $this->request->get('type/s', ''); //筛选学员和教练单

        try{
            //当前申请状态
            $res = OrderLog::allList($user_id,$page, $limit,$keywords,$status,$auth_status,$classes_hour_order_id);
//            if($user_id =='670153'){
//               file_put_contents("ceshi66.txt",(new AppointmentOrder())->getLastSql());
//            }
        }catch (\Exception $e){
            $this->apierror($e->getMessage());
        }
        $this->apisuccess('查询成功', $res);
    }














}