<?php

namespace App\Http\Controllers;

use App\OrderIssueProcessLog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;

class OrderIssueProcessLogController extends Controller
{

    public function index()
    {
        //
    }

    public function create()
    {
        //
    }

    public function store(Request $request)
    {
        //
    }

    public function show(OrderIssueProcessLog $orderIssueProcessLog)
    {
        //
    }

    public function edit(OrderIssueProcessLog $orderIssueProcessLog)
    {
        //
    }

    public function update(Request $request, OrderIssueProcessLog $orderIssueProcessLog)
    {
        //
    }

    public function destroy(OrderIssueProcessLog $orderIssueProcessLog)
    {
        //
    }

    public function apiStore(Request $request)
    {
        if (!Gate::allows('订单管理-问题件-编辑')) {
            return ['success' => false, 'fail_info' => '没有对应的权限'];
        }
        if ($request->filled(['id', 'content'])) {
            $arr = ['order_issue_id' => $request->input('id'),
                'content' => $request->input('content'),
                'user_id' => Auth::user()['id'],
                'type' => '处理',
            ];
            $orderIssueProcessLog = OrderIssueProcessLog::create($arr);
            if ($orderIssueProcessLog) {
                $data = OrderIssueProcessLog::with(['user'])->where('id',$orderIssueProcessLog['id'])->first();
                $this->log(__METHOD__, __FUNCTION__, json_encode($request->toArray()), Auth::user()['id']);
                return ['success' => true, 'data' => $data];
            } else {
                return ['success' => false, 'fail_info' => '录入失败'];
            }
        } else {
            return ['success' => false, 'fail_info' => '没有传入对应参数'];
        }
    }

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