<?php
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// | Date: 2020-11-08 12:56
// +----------------------------------------------------------------------

namespace app\system\controller;

use app\BaseController;
use app\model\api\User as UserModel;
use app\model\api\ServiceType as ServiceTypeModel;
use app\model\api\UserShowTemplate;
use app\model\api\InfoAudit;
use app\model\api\TypeAudit;
use app\model\api\UserAudit;
use app\model\api\ServiceType;
use app\model\api\ServiceTimeType;
use app\model\api\City as CityModel;
use app\model\api\Sys as SysModel;


use think\db\exception\DbException;
use think\facade\Db;
use app\Request;
use library\services\UtilService;
use library\utils\UserUtilsTool;

class Audit extends BaseController
{
    
    /***********************************审核******************************/
    /**
     * 第一步审核初始化
     * @return type
     */
    public function infoAuditInit()
    {
        return app('json')->success([
            'all'  => (new InfoAudit)->count(),
            'wait' => (new InfoAudit)->where('status', 0)->count(),
            'ok'   => (new InfoAudit)->where('status', 1)->count(),
            'no'   => (new InfoAudit)->where('status', -1)->count(),
        ]);
    }

    /**
     * 获取第一步审核列表
     * @param Request $request
     */
    public function infoAuditList(Request $request)
    {
        $post     = UtilService::getMore(
            [
                ['page', 1],
                ['pageSize', 50],
                ['keyword',''],
                ['mobile',''],
                ['user_mobile',''],
                ['status', ''],
                ['uid', ''],
                ['time',[]],
            ], $request
        );
        
        $data = (new InfoAudit)->getList($post,1);
        return app('json')->success([
            'list'      => $data["list"],
            'pageCount' => $data["totalCount"],
            'pageSize'  => $data["pageSize"],
            'page'      => $data["page"],
        ]);
    }
    /**
     * 信息认证详情
     * @param Request $request
     */
    public function infoAuditInfo(Request $request){
        $post = UtilService::getMore([
            ['id', '', 'empty', '参数错误'],
        ], $request);
        $data = (new InfoAudit)->getItem($post,1);
        if(empty($data)){
            return app('json')->fail('数据不存在');
        }
        return app('json')->success($data);
    }
    /**
     * 第一步审核处理
     * @param Request $request
     */
    public function infoAuditSub(Request $request)
    {
        $post   = UtilService::getMore(
            [
                ['id', '', 'empty', '参数错误'],
                ['mono'],
                ['type', 0],
            ], $request);
        $certData = (new InfoAudit)->where('id', $post['id'])->find();
        if (empty($certData)) {
            return app('json')->fail('数据不存在');
        }
        if ($certData['status']!=0) {
            return app('json')->fail( '当前审核已处理');
        }
        //审核通过
        if ($post['type'] == 1) {
            (new InfoAudit)
                ->where('id', $post['id'])
                ->save([
                    'status'            => 1,
                    'admin_mono'        => $post['mono'],
                    'admin_time'        => time(),
                    'admin_id'          => $request->adminInfo['id'],
                ]);
            (new UserModel)->where("uid",$certData["uid"])->update(["work_type_id"=>$certData["user_work_type_id"]]);
            //绑定初始化模板
            (new UserShowTemplate)->userBindTempInit($certData["uid"]);
            return app('json')->success('操作成功');
        }
        //审核驳回
        if ($post['type'] == -1) {
            if(empty($post['mono'])){
                return app('json')->fail( '请输入驳回理由');
            }
            (new InfoAudit)
                ->where('id', $post['id'])
                ->save([
                    'status'     => -1,
                    'admin_mono' => $post['mono'],
                    'admin_time' => time(),
                    'admin_id'   => $request->adminInfo['id'],
                ]);
            return app('json')->success('已驳回');
        }
    }
    
    /**
     * 平台认证初始化
     * @return type
     */
    public function typeAuditInit()
    {
        return app('json')->success([
            'all'  => (new TypeAudit)->count(),
            'wait' => (new TypeAudit)->where('status', 0)->count(),
            'ok'   => (new TypeAudit)->where('status', 1)->count(),
            'no'   => (new TypeAudit)->where('status', -1)->count(),
        ]);
    }
    
    /**
     * 平台认证详情
     * @param Request $request
     */
    public function typeAuditInfo(Request $request){
        $post = UtilService::getMore([
            ['id', '', 'empty', '参数错误'],
        ], $request);
        $data = (new TypeAudit)->getItem($post,1);
        if(empty($data)){
            return app('json')->fail('数据不存在');
        }
        return app('json')->success($data);
    }

    /**
     * 获取平台认证审核列表
     * @param Request $request
     */
    public function typeAuditList(Request $request)
    {
        $post     = UtilService::getMore(
            [
                ['page', 1],
                ['pageSize', 50],
                ['keyword',''],
                ['mobile',''],
                ['user_mobile',''],
                ['status', ''],
                ['uid', ''],
                ['time',[]],
            ], $request
        );
        
        $data = (new TypeAudit)->getList($post,1);
        return app('json')->success([
            'list'      => $data["list"],
            'pageCount' => $data["totalCount"],
            'pageSize'  => $data["pageSize"],
            'page'      => $data["page"],
        ]);
    }
    /**
     * 第一步审核处理
     * @param Request $request
     */
    public function typeAuditSub(Request $request)
    {
        $post   = UtilService::getMore(
            [
                ['id', '', 'empty', '参数错误'],
                ['mono'],
                ['type', 0],
            ], $request);
        $certData = (new TypeAudit)->where('id', $post['id'])->find();
        if (empty($certData)) {
            return app('json')->fail('数据不存在');
        }
        if ($certData['status']!=0) {
            return app('json')->fail( '当前审核已处理');
        }
        //审核通过
        if ($post['type'] == 1) {
            (new TypeAudit)
                ->where('id', $post['id'])
                ->save([
                    'status'     => 1,
                    'admin_mono' => $post['mono'],
                    'admin_time' => time(),
                    'admin_id'   => $request->adminInfo['id'],
                ]);
            return app('json')->success('操作成功');
        }
        //审核驳回
        if ($post['type'] == -1) {
            if(empty($post['mono'])){
                return app('json')->fail( '请输入驳回理由');
            }
            (new TypeAudit)
                ->where('id', $post['id'])
                ->save([
                    'status'     => -1,
                    'admin_mono' => $post['mono'],
                    'admin_time' => time(),
                    'admin_id'   => $request->adminInfo['id'],
                ]);
            return app('json')->success('已驳回');
        }
    }
    
    
}
