<?php

namespace App\Http\Controllers;

use App\OrderIssueProcessLog;
use App\Services\LogService;
use App\Services\OrderIssueProcessLogService;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;

class OrderIssueProcessLogController extends Controller
{


    public function apiStore(Request $request)
    {
        if (!Gate::allows('订单管理-问题件-编辑')) {
            return ['success' => false, 'fail_info' => '没有对应的权限'];
        }
        if ($request->filled(['id', 'content'])) {
            try {
                $log = OrderIssueProcessLog::query()->create([
                    'order_issue_id' => $request->input('id'),
                    'content' => $request->input('content'),
                    'user_id' => Auth::user()['id'],
                    'type' => '处理',
                ]);
                if ($log) {
                    $log->user = $log->hasOne(User::class, 'id', 'user_id')->first();
                }
                app('LogService')->log(__METHOD__, __FUNCTION__, json_encode($request->getContent()));
                return ['success' =>true,'data' =>$log ];
            } catch (\Exception $e) {
                app('LogService')->log(__METHOD__, __FUNCTION__, json_encode($request->getContent()) .'||'.$e->getMessage().'||'.$e->getTraceAsString());
                return ['success' => false, 'fail_info' => $e->getMessage()];
            }
        } else {
            return ['success' => false, 'fail_info' => '没有传入对应参数'];
        }
    }

    public function apiDestroy(Request $request){
        if(!$request->filled('id')){
            return ['success'=>false,'fail_info'=>'没有传入对应参数'];
        }
        OrderIssueProcessLog::query()->where('id',$request->input('id'))->delete();
        app('LogService')->log(__METHOD__, __FUNCTION__, json_encode($request->getContent()));
        return ['success'=>true];
    }

    public function batchStoreApi(Request $request){
        if (!Gate::allows('订单管理-问题件-编辑')) {
            return ['success' => false, 'fail_info' => '没有对应的权限'];
        }
        /** @var OrderIssueProcessLogService $service */
        $service = app(OrderIssueProcessLogService::class);

        try {
            $data = $service->batchStoreAndReturnLogs($request->all());
            $logs = OrderIssueProcessLog::query()
                ->with('user')
                ->whereIn('id',data_get($data,'*.id'))
                ->get();
            $result_data = [];
            foreach ($logs as $log) {
                $result_data[$log->order_issue_id] =$log;
            }
            app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->getContent()));
            return ['success' => true,'logs' => $result_data];
        } catch (\Exception $e) {
            app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->getContent()).'||'.$e->getMessage()."||".$e->getTraceAsString());
            return ['success' => false,$e->getMessage()];
        }
    }

}
