<?php

namespace App\Http\Controllers;

use App\OrderIssue;
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
{

    /**
     * 处理日志创建
     * @param Request $request
     * @return array
     */
    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' => '没有传入对应参数'];
        }
    }

    /**
     * 删除处理日志
     * @param Request $request
     * @return array|bool[]
     * @throws \Exception
     */
    public function destroyApi(Request $request): array
    {
        if(!$request->filled('id')){
            return ['success'=>false,'fail_info'=>'没有传入对应参数'];
        }
        /** @var OrderIssueProcessLog $orderIssueProcessLog */
        $orderIssueProcessLog = OrderIssueProcessLog::query()->where('id',$request->input('id'))->first();
        // 删除的如果是完结条目，将处理状态改为空
        if($orderIssueProcessLog['type'] === '结束') OrderIssue::query()->where('id',$orderIssueProcessLog['order_issue_id'])->update(['final_status'=>null]);
        $orderIssueProcessLog->delete();
        app('LogService')->log(__METHOD__, __FUNCTION__, json_encode($request->getContent()));
        return ['success'=>true];
    }

    /**
     * 批量完结订单
     * @param Request $request
     * @return array
     */
    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()];
        }
    }

}
