<?php

namespace App\Http\Controllers;

use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Http\Request;
use App\Http\Requests\VendorPaymentRequest;
use App\HR\Department;
use App\VendorPayment;
use App\Vendor;
use App\Account;




class VendorPaymentController extends Controller
{
    // public function index()
    // {
    // 	$vendor_payment     = new VendorPayment;
    //   $department = new Department;


    // 	return view('VendorPayments.manage_VendorPayments',[
    // 		'VendorPayment_list'     => $vendor_payment->getAllVendorPayments(),
    //     'department_list' => $department->getDepartmentList(),

    // 	]);
    // }

    public function storeVendorPayment(VendorPaymentRequest $request)
    {
   		$vendor_payment = new VendorPayment;

        $form_collect = $request->input();

        try
        {
        	$form_collect['vendor'] = decrypt($form_collect['vendor']);
          $form_collect['debit_account']  = decrypt($form_collect['debit_account'] );

          $form_collect['credit_account'] = decrypt($form_collect['credit_account'] );

        	$curr_vendor_payment = $vendor_payment->storeVendorPayment($form_collect);

        	if(isset($curr_vendor_payment->id))
	   		{
	          	  parent::setLogMessage('Vendor payment Invoice # '.$curr_vendor_payment->invoice_no.' has been created');
	   			

	            parent::successMessage('Vendor payment has been created.',parent::getLogMessage());
	   		}

   		} 
	    catch (DecryptException $e) 
	    {
	            //
	    }

    	return redirect()->back();
    }

    public function updateVendorPayment(VendorPaymentRequest $request)
    {
	      $vendor_payment = new VendorPayment;

	      $form_collect = $request->input();

	      try 
	      {
	          $form_collect['vendor_payment'] = decrypt($form_collect['vendor_payment']);
              $form_collect['debit_account']  = decrypt($form_collect['debit_account'] );

          $form_collect['credit_account'] = decrypt($form_collect['credit_account'] );

	          $curr_vendor_payment = $vendor_payment->updateVendorPayment($form_collect);

	          if(isset($curr_vendor_payment->id))
	          {
	          	  parent::setLogMessage('Vendor payment Invoice # '.$curr_vendor_payment->invoice_no.' has been updated');

	              parent::successMessage('Vendor payment has been updated.',parent::getLogMessage());
	          }
	      } 
	      catch (DecryptException $e) 
	      {
	            //
	      }

      

      return redirect()->back();
    }

    public function removeVendorPayment($id)
    {
      $vendor_payment = new VendorPayment;

      try 
      {
          $id = decrypt($id);

          $curr_vendor_payment = $vendor_payment->removeVendorPayment($id);

          if(isset($curr_vendor_payment->id))
          {
	          	  parent::setLogMessage('Vendor payment Invoice # '.$curr_vendor_payment->invoice_no.' has been removed');
          	  

              parent::successMessage('Vendor payment has been removed.',parent::getLogMessage());
          }
      } 
      catch (DecryptException $e) 
      {
            //
      }

      

      return redirect()->back();
    }

    function showVendorPaymentByVendor($id)
    {
    	$view = 'Record Not Found';

	    try 
	    {
	        if(\Request::ajax())
	        {
	        	$id = decrypt($id);

	        	$vendor = new Vendor;

		        $curr_vendor = $vendor->getVendorById($id);



		        if(isset($curr_vendor->id))
		        {
			        $vendor_payment = new VendorPayment;

			        $view = view('vendor_payments.manage_vendor_payments',[

			        	'vendor_payment_list' => $vendor_payment->getVendorPaymentByVendor($id),

			        	'curr_vendor'		  => $curr_vendor,
			        ]);
	          	}
	        }
        } 
        catch (DecryptException $e) 
        {
            //
        }

        return $view;
    }

    function showVendorPaymentUpdateModel($id)
    {
      $view = 'Record Not Found';

      try 
      {
          if(\Request::ajax())
          {
            $id = decrypt($id);

            $vendor_payment = new VendorPayment;
            $account = new Account;

            $curr_vendor_payment = $vendor_payment->getVendorPaymentById($id);

            if(isset($curr_vendor_payment->id))
            {
               if($curr_vendor_payment->payment_option === "Pay Now")
               {
                  $debit_list   = $account->getAccountListByNature(['Short-term Liability','Expense','Cost of Sales']);
                   
                   $credit_list  = $account->getAccountListByType(['Cash','Bank']);
               }
               else
               {
                   // $debit_list   = $account->getAccountListByType(['Cash','Bank','General']);
                  $debit_list   = $account->getAccountListByNature(['Expense','Cost of Sales']);

                  $credit_list  = $account->getAccountListByType(['Account Payable']);
               }

              $view = view('vendor_payments.modals.update_vendor_payment',[

                  'curr_vendor_payment'     => $curr_vendor_payment,
                  'debit_list'              => $debit_list,
                  'credit_list'             => $credit_list,
              ]);
            }
          }
        } 
        catch (DecryptException $e) 
        {
            //
        }

        return $view;
    }
}
