<?php

namespace App\Http\Controllers\H5;

use App\Facades\WechatService;
use App\Http\Controllers\Controller;
use App\Http\Response\ResponseJson;
use App\Models\Collaborator;
use App\Models\Partner;
use App\Services\ImService;
use App\Validators\CollaboratorValidator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class CollaboratorsController extends Controller
{
    use ResponseJson;

    protected CollaboratorValidator $validator;

    /**
     * @param CollaboratorValidator $validator
     */
    public function __construct(CollaboratorValidator $validator)
    {
        $this->validator = $validator;
    }


    /**
     * 合作商信息
     */
    public function detail()
    {
        $user = Auth::user();

        //获取合作商信息
        $collaborator = Collaborator::where('user_id', $user->id)
            ->select(['openid', 'user_id', 'name','mobile','avatar','intro','specialty','bio','protocols','diploma','status','reason','filing_at','audit_at'])
            ->first();

        //合作商不存在创建空对象返回
        if (empty($collaborator)) {
            $collaborator = new Collaborator([
                'openid'=>$user->openid,
                'user_id'=>$user->id,
                'name'=>null,
                'mobile'=>null,
                'avatar'=>null,
                'intro'=>null,
                'specialty'=>null,
                'bio'=>null,
                'protocols'=>null,
                'diploma'=>null,
                'reason'=>null,
                'filing_at'=>null,
                'status' => 3,
            ]);
        }
        return $this->success("ok", $collaborator);
    }

    /**
     * 创建/修改合作商
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse
     */
    public function store(Request $request)
    {
        try {
            $user = Auth::user();
            $data = $request->only(['avatar', 'name', 'mobile', 'intro', 'specialty', 'bio'/*,'protocols'*/, 'diploma']);

            //数据验证
            $this->validator->scene('store')->validate($data);
            $data['openid'] = $user->officialWechat->openid;

            $collaborator = Collaborator::withTrashed()->where('user_id', $user->id)->first();
            DB::beginTransaction();
            Log::info('用户id:' . $user->id);

            if ($collaborator) {
                Log::info("渠道商id:" . $collaborator->id);
                $collaborator->update(array_merge($data, ['deleted_at' => null]));
            } else {
                $data['status'] = Collaborator::STATUS_PREVIEW;
                $collaborator = Collaborator::create(array_merge($data, ['user_id' => $user->id]));
            }

            //查看是否存在合作伙伴信息,如果存在则同步信息
            $partners = Partner::where('user_id', $user->id)->first();
            if ($partners) {
                $partners->name = $data['name'];
                $partners->mobile = $data['mobile'];
                $partners->pic = $data['avatar'];
                $partners->save();
            }

            if (!$collaborator->qrcode) {
                if (config('app.env') == 'production') {
                    //生成二维码
                    $scene = 'collaborator=' . $collaborator->id;
                    //todo::跳转页面待确认
                    $page = 'pages/offlineOrder/bindUser';
                    $qrcode = WechatService::getMPQrcode($scene, $page);
                    //更新合作商信息
                    $collaborator->qrcode = $qrcode;
                } else {
                    //更新合作商信息
                    $collaborator->qrcode = 'test_qrcode.png';
                }
                $collaborator->save();
            }

            DB::commit();

            //更新网易云信数据
            $imService = new ImService();
            $imService->updateImUser($user->id,$collaborator);

            return $this->success('ok');
        }catch (\Exception $e){
            return $this->failure($e->getMessage());
        }
    }


}
