<?php

namespace App\Http\Controllers\H5;

use App\Http\Controllers\Controller;
use App\Http\Response\ResponseJson;
use App\Models\DmaProcessLog;
use App\Models\Group;
use App\Models\Order;
use App\Models\ServiceRoleOrder;
use App\Models\ServiceUser;
use App\Services\CommonService;
use Illuminate\Http\Request;

class ReappraiseController extends Controller
{
    use ResponseJson;

    public function otherRoles(Request $request)
    {
        //群信息
        $group = $request->group;
        $order = $group->order;
        //        //用户企业微信信息
//        $work_wechat_user = session('work_wechat_user');
//        $work_user_id = $work_wechat_user['raw']['userid'];
//        //服务人员信息
//        $service_user = ServiceUser::where('work_user_id', $work_user_id)->first();
        $service_user = $request->service_user;
        if (empty($service_user))
            return $this->failure("企业微信未绑定服务人员");
        //用户是否有角色
        $res = $order->serviceRoleOrders()->where('user_id', $service_user->user_id)->exists();
        if (empty($res))
            return $this->success("ok", []);
        $roles = $order->serviceRoleOrders()->where('role_id', '<=', 3)->with('role:id,name', 'serviceUser:id,user_id,name')->withCount([
            'reappraises' => function ($sql) use ($service_user) {
                $sql->where('operate_user_id', $service_user->user_id);
            }
        ])->where('user_id', '<>', $service_user->user_id)->get();
        return $this->success('ok', $roles);
    }

    // 教练复盘评估
    public function reappraiseRole(Request $request, ServiceRoleOrder $role_order)
    {
        $work_attitude = $request->input('work_attitude');
        if (empty($work_attitude))
            return $this->failure("请选择工作态度");
        $service_attitude = $request->input('service_attitude');
        if (empty($service_attitude))
            return $this->failure("请选择服务态度");
        $is_recommend = $request->input('is_recommend');
        if (empty($is_recommend))
            return $this->failure("请选择是否推荐给下一位用户");
        $recommend_reason = $request->input('recommend_reason');
        //        $study = $request->input('study');
//        if (empty($study)) return $this->failure("请选择建议学习升级");
//        $study_reason = $request->input('study_reason');
        $score = $request->input('score');
        if (!is_numeric($score))
            return $this->failure("请对此次评价打分");
        $suggest = $request->input('suggest');
        //        //用户企业微信信息
//        $work_wechat_user = session('work_wechat_user');
//        $work_user_id = $work_wechat_user['raw']['userid'];
//        //服务人员信息
//        $service_user = ServiceUser::where('work_user_id', $work_user_id)->first();
        $service_user = $request->service_user;
        $owner_role_order = ServiceRoleOrder::where("order_id", $role_order->order_id)->where("user_id", $service_user->user_id)->first();
        $order = Order::find($role_order->order_id);
        $res = $role_order->reappraises()->where('operate_user_id', $service_user->user_id)->exists();
        if ($res)
            return $this->failure("该订单服务人员已被复评");
        $reappraise = $role_order->reappraises()->create([
            'operate_user_id' => $service_user->user_id,
            'order_id' => $role_order->order_id,
            'user_id' => $role_order->user_id,
            'role_id' => $role_order->role_id,
            'work_attitude' => $work_attitude,
            'service_attitude' => $service_attitude,
            'is_recommend' => $is_recommend,
            'recommend_reason' => $recommend_reason,
            //            'study'=>$study,
//            'study_reason'=>$study_reason,
            'score' => $score,
            'suggest' => $suggest,
        ]);

        // 流程记录
        DmaProcessLog::addUserProcessLog($order->user_id ?? 0, $role_order->order_id ?? 0, 1, "submit_server_question_role", "填写了对教练的复盘评估", $service_user->user_id, $owner_role_order ? $owner_role_order->role_id : 0);
        return $this->success('ok', $reappraise);
    }

    public function reappraiseRoleV2(Request $request, ServiceRoleOrder $role_order)
    {
        //        //用户企业微信信息
//        $work_wechat_user = session('work_wechat_user');
//        $work_user_id = $work_wechat_user['raw']['userid'];
//        //服务人员信息
//        $service_user = ServiceUser::where('work_user_id', $work_user_id)->first();
        $service_user = $request->service_user;
        $owner_role_order = ServiceRoleOrder::where("order_id", $role_order->order_id)->where("user_id", $service_user->user_id)->first();
        $order = Order::find($role_order->order_id);
        $res = $role_order->reappraises()->where('operate_user_id', $service_user->user_id)->exists();
        if ($res)
            return $this->failure("该订单服务人员已被复评");
        $content = $request->input('content');
        if (empty($content) || !is_array($content) || !count($content))
            return $this->failure("请完善表单");
        $reappraise = $role_order->reappraises()->create([
            'operate_user_id' => $service_user->user_id,
            'order_id' => $role_order->order_id,
            'user_id' => $role_order->user_id,
            'role_id' => $role_order->role_id,
            'content' => json_encode($content)
        ]);

        DmaProcessLog::addUserProcessLog($order->user_id ?? 0, $role_order->order_id ?? 0, 1, "submit_server_question_role", "填写了对教练的复盘评估", $service_user->user_id, $owner_role_order ? $owner_role_order->role_id : 0);
        return $this->success('ok', $reappraise);
    }

    public function roleReappraise(Request $request, ServiceRoleOrder $role_order)
    {
        //用户企业微信信息
//        $work_wechat_user = session('work_wechat_user');
//        $work_user_id = $work_wechat_user['raw']['userid'];
//        $service_user = ServiceUser::where('work_user_id', $work_user_id)->first();
        $service_user = $request->service_user;
        $reappraise = $role_order->reappraises()->where('operate_user_id', $service_user->user_id)->first();
        return $this->success('ok', $reappraise);
    }
}
