<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\CreditreportProvider;
use App\Utility\ActionLogCreate;
use App\Http\Requests\CreditReportProviderRequest;

class CreditReportProviderController extends Controller
{
    private $message;
    private $code;
    private $data;
    private $action;
    private $type;
    public function __construct()
    {
        $this->action='';
        $this->type='error';
        $this->message = 'Fail';
        $this->code = config('constant.API_FAILED_CODE');
        $this->data=[];
    }

    public function index(Request $request)
    {
        $providers=(new CreditreportProvider())->getReportProviders();

        return view('report_provider.provider_add_edit_delete',compact('providers'));
    }
    public function createReportProvider(CreditReportProviderRequest $request)
    {

        $this->action='INSERT_CREDIT_REPORT_PROVIDER';
        $this->message='can not save data.';
        $inputData = $request->only(['source_type','link']);
        $inputData['company_type'] = $inputData['source_type'];
        $returnData = (new CreditreportProvider())->insertReportProvider($inputData);

        if (!empty($returnData)) {
            $this->message='Successfully Saved';
            $this->code=config('constant.API_SUCCESS_CODE');
            $this->type='success';
        }
        ActionLogCreate::logDataCreate( $this->action,$request->all(),$this->message,$returnData,$this->code);
        return back()->with($this->type, $this->message);
    }
    public function editReportProvider(Request $request)
    {
        $validated = $request->validate([
            'edit_source_type' => 'required',
            'edit_link' => 'required|regex:'.config('constant.REPORT_PROVIDER_LINK_VALIDATION'),
        ]);

        $this->action='EDIT_CREDIT_REPORT_PROVIDER';
        $this->message='can not save data.';
        $inputData = $request->only(['edit_source_type','edit_link']);
        $inputData['company_type'] = $inputData['edit_source_type'];
        $returnData = (new CreditreportProvider())->updateReportProvider($inputData);

        if (!empty($returnData)) {
            $this->message='Successfully Updated';
            $this->code=config('constant.API_SUCCESS_CODE');
            $this->type='success';
        }
        ActionLogCreate::logDataCreate( $this->action,$request->all(),$this->message,$returnData,$this->code);
        return back()->with($this->type, $this->message);
    }
    public function deleteReportProvider(Request $request)
    {
        $request->validate([
            'delete_source_type' => 'required|gt:0',
        ]);
        $this->action='DELETE_TRAINING_VIDEO';
        $this->message='can not delete data.';

        $inputData = $request->only(['delete_source_type']);

        if($inputData['delete_source_type']>0) {
            $returnData = (new CreditreportProvider())->deleteReportProvider($inputData);
            if (!empty($returnData)) {
                $this->message='Successfully Deleted';
                $this->code=config('constant.API_SUCCESS_CODE');
                $this->type='success';
            }
            ActionLogCreate::logDataCreate($this->action,$request->all(),$this->message,$returnData,$this->code);
        }
        return back()->with($this->type, $this->message);

    }
}
