<?php

namespace app\api\controller\school;

use app\common\model\manystore\UserAuth as UserAuthModel;

/**
 * 用户机构授权接口
 */
class UserAuth extends Base
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    protected $model = null;

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

        $this->model = new UserAuthModel;
        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->error(__('缺少必要参数'));
        }

        try {
            $res =  $this->model->detail($id);
        } catch (\Exception $e){
//            Log::log($e->getMessage());
            $this->error($e->getMessage(),['errcode'=>$e->getCode()]);
        }
        $this->success('获取成功', ['detail' => $res]);
    }







    /**
     * @ApiTitle( 我的授权信息列表（取决于搜索条件）)
     * @ApiSummary(我的授权信息列表（取决于搜索条件）)
     * @ApiMethod(GET)
     * @ApiParams(name = "page", type = "string",required=true,description = "页数")
     * @ApiParams(name = "limit", type = "string",required=true,description = "条数")
     * @ApiParams(name = "status", type = "string",required=false,description = "授权状态:1=通过,2=拒绝")
     * @ApiParams(name = "shop_id", type = "int",required=false,description = "机构店铺id")
     * @ApiReturn({
     *
     *})
     */
    public function auth_list()
    {
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];
        $params=[];
        $page      =  $this->request->get('page/d', 0); //页数
        $limit   =  $this->request->get('limit/d', 0); //条数
        $params['status']  =  $this->request->get('status/s', ''); //搜索关键字
        $params['shop_id']  =  $this->request->get('shop_id/d', ''); //搜索关键字
        $params['user_id']  =  $user_id; //搜索关键字


//        $type =  $this->request->get('type/s', ''); //筛选学员和教练单

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

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


         /**
     * @ApiTitle( 用户机构授权)
     * @ApiSummary(支持用户主动授权和确认表单两种形式)
     * @ApiMethod(POST)
     * @ApiParams(name = "id", type = "int",required=false,description = "非必填，确认表单才需要填")
     * @ApiParams(name = "shop_id", type = "int",required=true,description = "机构店铺id")
     * @ApiParams(name = "status", type = "int",required=true,description = "授权操作：1=通过,2=拒绝")
     * @ApiReturn({
     *
     *})
     */
    public function authorization(){
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];

        $id =  $this->request->post('id/d', 0); //搜索关键字
        $shop_id =  $this->request->post('shop_id/d', 0); //搜索关键字
        $status =  $this->request->post('status/d', 0); //搜索关键字

        try{
            $res = $this->model::auth($id,$shop_id,$user_id,$status,'user',$user_id,true);
        }catch (\Throwable $e){
            $this->error($e->getMessage());
        }
        $this->success('操作成功', $res);
    }

}

