<?php

namespace App\Http\Controllers\Admin;

use App\Models\Lang;
use App\Models\ServiceRecord;
use Illuminate\Http\Request;

class ServiceRecordController extends AdminController
{
    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where    = $request->except('page');
        $records = ServiceRecord::with('langs');
        $records = $request->has('lang') ? $records->where('lang', $where['lang']) : $records;
        $records = $request->has('type') ? $records->where('type', $where['type']) : $records;

        $records = $records->latest()->paginate(20);
        $langs = Lang::where('buy', 1)->where('enabled', 1)->oldest('orders')->lists('name', 'slug')->toArray();

        return view('admin.service_record.index', compact('records', 'where', 'langs'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $ids    = array_str($id);
        $result = ServiceRecord::whereIn('id', $ids)->delete();

        if ($result) {
            flash('success', '删除成功！');
            return response()->json(['code' => 200]);
        }

        return response()->json(['code' => 500]);
    }

}
