<?php

namespace App\Http\Controllers\H5;

use App\Http\Controllers\Controller;
use App\Http\Response\ResponseJson;
use App\Models\Group;
use App\Models\Order;
use App\Models\ServiceUser;
use App\Models\WorkOrder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class WorkOrderController extends Controller
{
    use ResponseJson;

    /**
     * 添加工单
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse
     */
    public function addWorkOrder(Request $request)
    {
        try {
//            $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;
            $user_id = $service_user->user_id;
            $group_id = $request->input('chat_id');
            $service_user_id = $request->input('service_user_id');
            if(empty($group_id)){
                return $this->failure('群组id不为空');
            }
            $desc = $request->input('desc','');
            $images = $request->input('images');
            if(is_array($images)){
                $images = json_encode($images);
            }
            if($service_user_id){
                $group = Group::where('im_chat_id',$group_id)->first();
            }else{
                $group = Group::where('chat_id',$group_id)->first();
            }
            if($group->order_id){
                $order = Order::where('id',$group->order_id)->first();
            }
            $map = [];
            $map['work_order'] = $user_id.'_'.time();
            $map['project_name'] = '友福健康';
            $map['group_id'] = $group_id;
            $map['group_name'] = $group->name??'';
            $map['user_id'] = $user_id;
            $map['order_id'] = $order->trade_no??0;
            $map['desc'] = $desc;
            $map['images'] = $images;

            WorkOrder::create($map);

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

    /**
     * 获取某个群组工单列表数据
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|void
     */
    public function getWorkOrderList(Request $request){
        try{
//            $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;
            $group_id = $request->get('chat_id');
            $list = WorkOrder::where('user_id',$service_user->user_id)->where('group_id',$group_id)->select()->paginate();
            foreach ($list as $item){
                $item->images = json_decode($item->images,true);
                $item->deal_images = json_decode($item->deal_images,true);
            }
            return $this->success('ok',$list);
        }catch (\Exception $e){
            return $this->jsonResponse(1,$e->getMessage());
        }
    }

    /**
     * 撤销订单
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse
     */
    public function updateWorkOrder(Request $request){
        try{
            $id = $request->get('id');
            $info = WorkOrder::where('id',$id)->first();
            if(!$info){
                return $this->failure('工单不存在');
            }
            $info->status = 3;
            $info->save();
            return $this->success('ok',$info);
        }catch (\Exception $e){
            return $this->jsonResponse(1,$e->getMessage());
        }
    }

    /**
     * 获取工单详情
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse
     */
    public function getWorkOrderDetail(Request $request){
        try{
            $id = $request->get('id');
            $info = WorkOrder::where('id',$id)->first();
            if(!$info){
                return $this->failure('工单不存在');
            }
            return $this->success('ok',$info);
        }catch (\Exception $e){
            return $this->jsonResponse(1,$e->getMessage());
        }
    }

    public function deleteWorkOrder(){

    }
}
