<?php

namespace App\Http\Controllers\User;

use Exception;
use App\Models\User;
use App\Models\Order;
use App\Models\Service;
use Illuminate\Http\Request;
use App\Models\UserBalanceLog;
use App\Models\ServiceCategory;
use App\Models\ServiceProvider;
use App\Models\ServiceCustomPrice;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use App\DataTables\User\OrderDataTable;
use App\Models\ServiceFavorite;
use App\Models\ServiceLog;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;

class OrderController extends Controller {
    public function single(PostRequest $request) {
        if ($request->method() ==  'GET') {
            // $check_service = $service = Service::find(1);
            $service_fav = ServiceFavorite::where('user_id', Auth::id())->join('services', 'service_favorites.service_id', '=', 'services.id')->join('service_categories', 'services.category_id', '=', 'service_categories.id')->oldest('category_name')->get(['category_id', 'service_categories.name AS category_name']);
            $components['categories'] = Service::distinct()->join('service_categories', 'services.category_id', '=', 'service_categories.id')->oldest('category_name')->get(['category_id', 'service_categories.name AS category_name']);
            $components['categories_favorite'] = $service_fav->unique('category_id');
            $components['breadcrumb'] = (object) [
                'first' => 'Pesan Baru',
                'second' => 'Pesanan'
            ];
            return view('user.order.single', $components);
        } elseif ($request->method() == 'POST') {
            if ($request->ajax() == false) abort('404');
            $input_data = [
                'user_id'             => Auth::user()->id,
                'service_id'          => escape_input($request->service),
                'target'              => escape_input($request->target),
                'quantity'            => fixed_amount($request->quantity),
                'price'               => 0,
                'profit'              => 0,
                'provider_id'         => 0,
                'provider_order_id'   => 0,
                'status'              => 'Pending',
                'ip_address'          => $request->ip(),
                'custom_comments'     => ($request->custom_comments <> '') ? escape_input(strip_tags($request->custom_comments)) : null,
                'provider_order_log'  => null,
                'provider_status_log' => null,
            ];
            $user = User::find(Auth::id());
            $check_service = Service::find($input_data['service_id']);
            if ($check_service == false) {
                return response()->json([
                    'status'  => false, 
                    'type'    => 'alert',
                    'message' => 'Layanan tidak tersedia.'
                ]);
            } else {
                if ($check_service->type == 'custom_comments') {
                    $data_input['quantity'] = count(explode('\r\n', $input_data['custom_comments']));
                    $validator = Validator::make($request->all(), [
                        'custom_comments' => 'required',
                    ], [], ['custom_comments' => 'Kustom Komentar']);
                    if ($validator->fails()) {
                        return response()->json([
                            'status'  => false, 
                            'type'    => 'validation',
                            'message' => $validator->errors()->toArray()
                        ]);
                    }
                }
                $input_data['price'] = $check_service->price/1000 * $input_data['quantity'];
                $input_data['profit'] = $check_service->profit/1000 * $input_data['quantity'];
                $custom_price = ServiceCustomPrice::where([
                    ['user_id', Auth::user()->id],
                    ['service_id', $input_data['service_id']]
                ])->first();
                if ($custom_price == true) {
                    $input_data['price'] = $input_data['quantity'] * ($custom_price->price / 1000);
                    $input_data['profit'] = $input_data['quantity'] * ($custom_price->profit / 1000);
                }
                $check_provider = ServiceProvider::where([
                    ['id', $check_service->provider_id],
                    ['status', '1']
                ])->first();
                if ($check_provider == false) {
                    return response()->json([
                        'status'  => false, 
                        'type'    => 'alert',
                        'message' => 'Layanan tidak tersedia.'
                    ]);
                } elseif ($input_data['quantity'] < $check_service->min) {
                    return response()->json([
                        'status'  => false, 
                        'type'    => 'alert',
                        'message' => 'Jumlah minimal adalah '.number_format($check_service->min,0,',','.').'.'
                    ]);
                } elseif ($input_data['quantity'] > $check_service->max) {
                    return response()->json([
                        'status'  => false, 
                        'type'    => 'alert',
                        'message' => 'Jumlah maksimal adalah '.number_format($check_service->max,0,',','.').'.'
                    ]);
                } elseif (Auth::user()->balance < $input_data['price']) {
                    return response()->json([
                        'status'  => false, 
                        'type'    => 'alert',
                        'message' => 'Saldo anda tidak mencukupi.'
                    ]);
                } else {
                    $input_data['provider_id'] = $check_service->provider_id;
                    $order_response = false;
                    if ($check_provider->name == 'MANUAL') {
                        $order_response = true;
                        $input_data['provider_id'] = $check_provider->id;
                    } else {
                        $provider_config = json_decode($check_provider->order_config, true);
                        $parameter_values = [
                            'service'         => $check_service->provider_service_id,
                            'target'          => $input_data['target'],
                            'quantity'        => $input_data['quantity'],
                            'provider_id'     => $check_provider->provider_id,
                            'provider_key'    => $check_provider->provider_key,
                            'provider_secret' => $check_provider->provider_secret,
                            'custom_comments' => $input_data['custom_comments'],
                        ];
                        foreach ($provider_config['request'] as $key => $value) {
                            if ($key == 'action') {
                                $post_api['action'] = $value;
                            } else {
                                $post_api[$value] = $parameter_values[$key]; 
                            }
                        }
                        try {
                            $curl = post_curl($provider_config['endpoint'], $post_api);
                            $curl_response = json_decode($curl, true);
                            if ($curl <> false) {
                                $provider_order_id = deltonsz($curl_response, to_array(json_decode($provider_config['response']['order_id'])));
                                if (!is_array($provider_order_id)) {
                                    $order_response = true;
                                    $input_data['provider_order_id'] = $provider_order_id;
                                    $input_data['provider_order_log'] = $curl;
                                }
                            } else {
                                return response()->json([
                                    'status'  => false, 
                                    'type'    => 'alert',
                                    'message' => 'Layanan tidak tersedia.'
                                ]);
                            }
                        } catch (Exception $error) {
                            return response()->json([
                                'status'  => false, 
                                'type'    => 'alert',
                                'message' => 'Layanan tidak tersedia.'
                            ]);
                        }
                    }
                    if ($order_response == false) {
                        if (isset($curl)) {
                            if (ServiceLog::where([
                                ['service_id', $check_service->id],
                                ['logs', $curl]
                            ])->count() == 0) {
                                $insert_log = ServiceLog::create([
                                    'user_id'     => Auth::user()->id,
                                    'service_id'  => $check_service->id,
                                    'provider_id' => $check_service->provider_id,
                                    'logs'        => $curl
                                ]);
                            }
                            $check_service->update([
                                'status' => '0'
                            ]);
                        }
                        return response()->json([
                            'status'  => false, 
                            'type'    => 'alert',
                            'message' => 'Layanan tidak tersedia.'
                        ]);
                    } else {
                        $input_data['service_name'] = $check_service->name;
                        $check_user = User::find(Auth::user()->id);
                        $insert_data = Order::create($input_data);
                        if ($insert_data == true) {
                            $cut_balance = $check_user->update([
                                'balance' => $check_user->balance - $input_data['price'],
                            ]);
                            $balance_logs = UserBalanceLog::create([
                                'user_id'     => $input_data['user_id'],
                                'type'        => 'Minus',
                                'action'      => 'Order',
                                'amount'      => $input_data['price'],
                                'description' => 'Membuat Pesanan #'.$insert_data->id.'.',
                            ]);
                            if (json_decode(Auth::user()->notification)->order == '1') {
                                $details = [
                                    'name'       => Auth::user()->full_name,
                                    'id'         => $insert_data->id,
                                    'service'    => $check_service->name,
                                    'target'     => $input_data['target'],
                                    'quantity'   => $input_data['quantity'],
                                    'price'      => $input_data['price'],
                                    'status'     => 'Pending',
                                    'source'     => 'WEB',
                                    'ip_address' => $request->ip(),
                                ];
                                $this->send_email_user($details, Auth::user()->email);
                            }
                            if (website_config('notification')->email <> '' AND website_config('notification')->order == '1') {
                                $details = [
                                    'username'   => Auth::user()->username,
                                    'full_name'  => Auth::user()->full_name,
                                    'id'         => $insert_data->id,
                                    'service'    => $check_service->name,
                                    'target'     => $input_data['target'],
                                    'quantity'   => $input_data['quantity'],
                                    'price'      => $input_data['price'],
                                    'status'     => 'Pending',
                                    'source'     => 'WEB',
                                    'ip_address' => $request->ip(),
                                ];
                                $this->send_email_admin($details, website_config('notification')->email);
                            }
                            return response()->json([
                                'status'  => true, 
                                'last_balance' => number_format($check_user->balance,0,',','.'),
                                'message' => '
                                    Pesanan anda akan segera diproses, berikut adalah data pesanan Anda.
                                    <br /><b>ID Pesanan:</b> '.$insert_data->provider_order_id.'
                                    <br /><b>Layanan:</b> '.$check_service->name.'
                                    <br /><b>Target:</b> '.$input_data['target'].'
                                    <br /><b>Jumlah:</b> '.number_format($input_data['quantity'],0,',','.').'
                                '
                            ]);
                        } else {
                            return response()->json([
                                'status'  => false, 
                                'type'    => 'alert',
                                'message' => 'Terjadi Kesalahan'
                            ]);
                        }
                    }
                }
            }
        }
    }
    public function getNew() {
        $components = [
            'categories' => ServiceCategory::where('status', '1')->oldest('name')->get(),
        ];
        $components['breadcrumb'] = (object) [
			'first' => 'Pesan Baru',
			'second' => 'Pesanan'
		];
        return view('user.order.new', $components);
    }
    public function postNew(PostRequest $request) {
		if ($request->ajax() == false) abort('404');
		$input_data = [
            'user_id'             => Auth::user()->id,
            'service_id'          => escape_input($request->service_id),
            'target'              => escape_input($request->target),
            'quantity'            => fixed_amount($request->quantity),
            'price'               => 0,
            'profit'              => 0,
            'provider_id'         => 0,
            'provider_order_id'   => 0,
            'status'              => 'Pending',
            'ip_address'          => $request->ip(),
            'custom_comments'     => ($request->custom_comments <> '') ? escape_input(strip_tags($request->custom_comments)) : null,
            'provider_order_log'  => null,
            'provider_status_log' => null,
        ];
        $check_service = Service::find($input_data['service_id']);
		if ($check_service == false) {
            return response()->json([
				'status'  => false, 
				'type'    => 'alert',
                'message' => 'Layanan tidak tersedia.'
			]);
        } else {
            if ($check_service->type == 'custom_comments') {
                $data_input['quantity'] = count(explode('\r\n', $input_data['custom_comments']));
                $validator = Validator::make($request->all(), [
					'custom_comments' => 'required',
				], [], ['custom_comments' => 'Kustom Komentar']);
				if ($validator->fails()) {
					return response()->json([
						'status'  => false, 
						'type'    => 'validation',
						'message' => $validator->errors()->toArray()
					]);
				}
            }
            $input_data['price'] = $check_service->price/1000 * $input_data['quantity'];
            $input_data['profit'] = $check_service->profit/1000 * $input_data['quantity'];
            $custom_price = ServiceCustomPrice::where([
                ['user_id', Auth::user()->id],
                ['service_id', $input_data['service_id']]
            ])->first();
            if ($custom_price == true) {
                $input_data['price'] = $input_data['quantity'] * ($custom_price->price / 1000);
                $input_data['profit'] = $input_data['quantity'] * ($custom_price->profit / 1000);
            }
            $check_provider = ServiceProvider::where([
                ['id', $check_service->provider_id],
                ['status', '1']
            ])->first();
            if ($check_provider == false) {
                return response()->json([
                    'status'  => false, 
                    'type'    => 'alert',
                    'message' => 'Layanan tidak tersedia.'
                ]);
            } elseif ($input_data['quantity'] < $check_service->min) {
                return response()->json([
                    'status'  => false, 
                    'type'    => 'alert',
                    'message' => 'Jumlah minimal adalah '.number_format($check_service->min,0,',','.').'.'
                ]);
            } elseif ($input_data['quantity'] > $check_service->max) {
                return response()->json([
                    'status'  => false, 
                    'type'    => 'alert',
                    'message' => 'Jumlah maksimal adalah '.number_format($check_service->max,0,',','.').'.'
                ]);
            } elseif (Auth::user()->balance < $input_data['price']) {
                return response()->json([
                    'status'  => false, 
                    'type'    => 'alert',
                    'message' => 'Saldo anda tidak mencukupi.'
                ]);
            } else {
                $input_data['provider_id'] = $check_service->provider_id;
                $order_response = false;
                if ($check_provider->name == 'MANUAL') {
                    $order_response = true;
                    $input_data['provider_id'] = $check_provider->id;
                } else {
                    $provider_config = json_decode($check_provider->order_config, true);
                    $parameter_values = [
                        'service'         => $check_service->provider_service_id,
                        'target'          => $input_data['target'],
                        'quantity'        => $input_data['quantity'],
                        'provider_id'     => $check_provider->provider_id,
                        'provider_key'    => $check_provider->provider_key,
                        'provider_secret' => $check_provider->provider_secret,
                        'custom_comments' => $input_data['custom_comments'],
                    ];
                    foreach ($provider_config['request'] as $key => $value) {
                        if ($key == 'action') {
                            $post_api['action'] = $value;
                        } else {
                            $post_api[$value] = $parameter_values[$key]; 
                        }
                    }
                    try {
                        $curl = post_curl($provider_config['endpoint'], $post_api);
                        $curl_response = json_decode($curl, true);
                        if ($curl <> false) {
                            $provider_order_id = deltonsz($curl_response, to_array(json_decode($provider_config['response']['order_id'])));
                            if (!is_array($provider_order_id)) {
                                $order_response = true;
                                $input_data['provider_order_id'] = $provider_order_id;
                                $input_data['provider_order_log'] = $curl;
                            }
                        } else {
                            return response()->json([
                                'status'  => false, 
                                'type'    => 'alert',
                                'message' => 'Layanan tidak tersedia.'
                            ]);
                        }
                    } catch (Exception $error) {
                        return response()->json([
                            'status'  => false, 
                            'type'    => 'alert',
                            'message' => 'Layanan tidak tersedia.'
                        ]);
                    }
                }
                if ($order_response == false) {
                    if (isset($curl)) {
                        if (ServiceLog::where([
                            ['service_id', $check_service->id],
                            ['logs', $curl]
                        ])->count() == 0) {
                            $insert_log = ServiceLog::create([
                                'user_id'     => Auth::user()->id,
                                'service_id'  => $check_service->id,
                                'provider_id' => $check_service->provider_id,
                                'logs'        => $curl
                            ]);
                        }
                        $check_service->update([
                            'status' => '0'
                        ]);
                    }
                    return response()->json([
                        'status'  => false, 
                        'type'    => 'alert',
                        'message' => 'Layanan tidak tersedia.'
                    ]);
                } else {
                    $input_data['service_name'] = $check_service->name;
                    $check_user = User::find(Auth::user()->id);
                    $insert_data = Order::create($input_data);
                    if ($insert_data == true) {
                        $cut_balance = $check_user->update([
                            'balance' => Auth::user()->balance - $input_data['price'],
                        ]);
                        $balance_logs = UserBalanceLog::create([
                            'user_id'     => $input_data['user_id'],
                            'type'        => 'Minus',
                            'action'      => 'Order',
                            'amount'      => $input_data['price'],
                            'description' => 'Membuat Pesanan #'.$insert_data->id.'.',
                        ]);
                        if (json_decode(Auth::user()->notification)->order == '1') {
                            $details = [
                                'name'       => Auth::user()->full_name,
                                'id'         => $insert_data->id,
                                'service'    => $check_service->name,
                                'target'     => $input_data['target'],
                                'quantity'   => $input_data['quantity'],
                                'price'      => $input_data['price'],
                                'status'     => 'Pending',
                                'source'     => 'WEB',
                                'ip_address' => $request->ip(),
                            ];
                            $this->send_email_user($details, Auth::user()->email);
                        }
                        if (website_config('notification')->email <> '' AND website_config('notification')->order == '1') {
                            $details = [
                                'username'   => Auth::user()->username,
                                'full_name'  => Auth::user()->full_name,
                                'id'         => $insert_data->id,
                                'service'    => $check_service->name,
                                'target'     => $input_data['target'],
                                'quantity'   => $input_data['quantity'],
                                'price'      => $input_data['price'],
                                'status'     => 'Pending',
                                'source'     => 'WEB',
                                'ip_address' => $request->ip(),
                            ];
                            $this->send_email_admin($details, website_config('notification')->email);
                        }
                        return response()->json([
                            'status'  => true, 
                            'message' => '
                                Pesanan anda akan segera diproses, berikut adalah data pesanan Anda.
                                <br /><b>ID Pesanan:</b> '.$insert_data->id.'
                                <br /><b>Layanan:</b> '.$check_service->name.'
                                <br /><b>Target:</b> '.$input_data['target'].'
                                <br /><b>Jumlah:</b> '.number_format($input_data['quantity'],0,',','.').'
                            '
                        ]);
                    } else {
                        return response()->json([
                            'status'  => false, 
                            'type'    => 'alert',
                            'message' => 'Terjadi Kesalahan'
                        ]);
                    }
                }
            }
		}
    }
    public function history(OrderDataTable $dataTable) {
        $components['breadcrumb'] = (object) [
			'first' => 'Riwayat',
			'second' => 'Pesanan'
		];
        $components['services'] = Order::where('user_id', Auth::user()->id)->distinct()->latest('service_id')->get(['service_id', 'service_name']);
        $components['statuses'] = ['Pending', 'Processing', 'Success', 'Error', 'Partial'];
        $components['created_at'] = Order::where('user_id', Auth::user()->id)->selectRaw('DATE(created_at) AS created_at')->distinct()->latest('created_at')->get();
        $components['total'] = [
            'total' => Order::where('user_id', Auth::user()->id)->sum('price'),
            'count' => Order::where('user_id', Auth::user()->id)->count('*')
        ];
        $components['pending'] = [
            'total' => Order::where('user_id', Auth::user()->id)->where('status', 'Pending')->sum('price'),
            'count' => Order::where('user_id', Auth::user()->id)->where('status', 'Pending')->count('*')
        ];
        $components['processing'] = [
            'total' => Order::where('user_id', Auth::user()->id)->where('status', 'Processing')->sum('price'),
            'count' => Order::where('user_id', Auth::user()->id)->where('status', 'Processing')->count('*')
        ];
        $components['failed'] = [
            'total' => Order::where('user_id', Auth::user()->id)->whereIn('status', ['Error', 'Partial'])->sum('price'),
            'count' => Order::where('user_id', Auth::user()->id)->whereIn('status', ['Error', 'Partial'])->count('*')
        ];
        $components['success'] = [
            'total' => Order::where('user_id', Auth::user()->id)->where('status', 'Success')->sum('price'),
            'count' => Order::where('user_id', Auth::user()->id)->where('status', 'Success')->count('*')
        ];
        return $dataTable->render('user.order.history', $components);
    }
    public function detail(Order $target, Request $request) {
		if ($request->ajax() == false) abort('404');
        return view('user.order.detail', compact('target'));
    }
    public function report(Request $request) {
        if ($request->ajax() == false) abort('404');
        $check_data = Order::where('user_id', Auth::user()->id)->with('service')->select('orders.*')->when(request(), function ($query) use ($request) {
            if ($request->has('start_date') AND $request->has('end_date') AND $request->start_date <> '' AND $request->end_date <> '') {
                $request->start_date = month_format($request->start_date);
                $request->end_date = month_format($request->end_date);
                $query->whereRaw('DATE(orders.created_at) BETWEEN "'.date('Y-m-d', strtotime($request->start_date)).'" AND "'.date('Y-m-d', strtotime($request->end_date)).'"');
            }
            if ($request->has('service') <> '' AND $request->service <> '') {
                $query->where(function($query) {
                    $query
                    ->whereHas('service', function($query) {
                        $query->where('name', 'like', "%".escape_input(request('service'))."%");
                    })
                    ->orWhere('orders.service_name', 'like', "%".escape_input(request('service'))."%");
                });
            }
            if ($request->has('status') <> '' AND $request->status <> '') {
                $query->where('orders.status', '=', escape_input(request('status')));
            }
            if ($request->has('is_api') <> '' AND $request->is_api <> '') {
                $query->where('orders.is_api', '=', escape_input(request('is_api')));
            }
            if ($request->has('search') <> '' AND $request->search <> '') {
                $query->where(function($query) use ($request) {
                    $query
                    ->whereHas('service', function($query) use ($request) {
                        $query->where('name', 'like', '%'.escape_input($request->search).'%');
                    })
                    ->orWhere('orders.id', 'like', '%'.escape_input($request->search).'%')
                    ->orWhere('orders.service_name', 'like', '%'.escape_input($request->search).'%')
                    ->orWhere('orders.target', 'like', '%'.escape_input($request->search).'%')
                    ->orWhere('orders.quantity', 'like', '%'.escape_input($request->search).'%')
                    ->orWhere('orders.price', 'like', '%'.escape_input($request->search).'%')
                    ->orWhere('orders.status', 'like', '%'.escape_input($request->search).'%');
                });
            }
        })->get();
        return response()->json([
            'status'  => true, 
            'total' => 'Rp '.number_format($check_data->sum('price'),0,',','.').' ('.number_format($check_data->count('*'),0,',','.').')',
            'pending' => 'Rp '.number_format($check_data->where('status', 'Pending')->sum('price'),0,',','.').' ('.number_format($check_data->where('status', 'Pending')->count('*'),0,',','.').')',
            'failed' => 'Rp '.number_format($check_data->whereIn('status', ['Error', 'Partial'])->sum('price'),0,',','.').' ('.number_format($check_data->whereIn('status', ['Error', 'Partial'])->count('*'),0,',','.').')',
            'success' => 'Rp '.number_format($check_data->where('status', 'Success')->sum('price'),0,',','.').' ('.number_format($check_data->where('status', 'Success')->count('*'),0,',','.').')',
            
        ]);
    }
    public function send_email_user($details = [], $to = '') {
		config(['mail.mailers.smtp.username' => website_config('smtp')->username]);
		config(['mail.mailers.smtp.password' => website_config('smtp')->password]);
		config(['mail.mailers.smtp.encryption' => website_config('smtp')->encryption]);
		config(['mail.mailers.smtp.port' => website_config('smtp')->port]);
		config(['mail.mailers.smtp.host' => website_config('smtp')->host]);
		config(['mail.from.address' => website_config('smtp')->from]);
		config(['mail.from.name' => website_config('main')->website_name]);
		try {
            Mail::send('user.mail.notification.order', $details, function($message) use ($details, $to) {
                $message
                 ->to($to, $details['name'])
                 ->from(config('mail.from.address'), config('mail.from.name'))
                 ->subject('Informasi Pesanan - '.website_config('main')->website_name.'');
             });
			return true;
		} catch (Exception $message) {
			return true;
		}
    }
    public function send_email_admin($details = [], $to = '') {
		config(['mail.mailers.smtp.username' => website_config('smtp')->username]);
		config(['mail.mailers.smtp.password' => website_config('smtp')->password]);
		config(['mail.mailers.smtp.encryption' => website_config('smtp')->encryption]);
		config(['mail.mailers.smtp.port' => website_config('smtp')->port]);
		config(['mail.mailers.smtp.host' => website_config('smtp')->host]);
		config(['mail.from.address' => website_config('smtp')->from]);
		config(['mail.from.name' => website_config('main')->website_name]);
		try {
            Mail::send('admin.mail.notification.order', $details, function($message) use ($details, $to) {
                $message
                 ->to($to, 'Admin')
                 ->from(config('mail.from.address'), config('mail.from.name'))
                 ->subject('Informasi Pesanan - '.website_config('main')->website_name.'');
             });
			return true;
		} catch (Exception $message) {
			return true;
		}
    }
}

class PostRequest extends FormRequest {
	protected function prepareForValidation() {
		$this->merge([
			'quantity' => $this->quantity <> '' ? fixed_amount($this->quantity) : '',
		]);
	}
    protected function getValidatorInstance() {
        $instance = parent::getValidatorInstance();
        if ($instance->fails() == true) {
            throw new HttpResponseException(response()->json([
                'status'  => false, 
                'type'    => 'validation',
                'message' => parent::getValidatorInstance()->errors()
            ]));
        }
        return parent::getValidatorInstance();
    }
    public function rules(Request $request) {
        if (request()->method() == 'GET') return [];
        return [
            'category'    => 'required|numeric|exists:service_categories,id',
            'service'     => 'required|numeric|exists:services,id',
            'target'      => 'required',
            'quantity'    => 'required|numeric|integer|min:0',
            'approval'    => 'required|in:1',
        ];
        
    }
    public function attributes() {
        return [
            'category'    => 'Kategori',
            'service'     => 'Layanan',
            'target'      => 'Target',
            'quantity'    => 'Jumlah',
            'approval'    => 'Persetujuan',
        ];
    }
}