<?php

namespace App\Http\Controllers;
use App\Models\modelSuppliers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;

class SuppliersController extends Controller
{
    public function index()
    {
        $accounts = modelSuppliers::selectAccounts(Session::get('PropertyCode'));
        if($accounts==null)
        {
            $account="";
        }
        else
        {
            $account=$accounts[0]->AccCode;
        }
        $suppliers = modelSuppliers::selectsuppliers(Session::get('PropertyCode'));

        return view('/Suppliers', compact('suppliers','account'));

    }

    public function addsuppliers(Request $request)
    {
       
            $request->validate([
                'AccCode' => 'required|string',
                'code' => 'required|string',
                'name' => 'required|string',
                'contactperson' => 'required|string',
                'contact1' => 'required|string',
                'contact2' => 'required|string',
                'email' => 'required|string',
                'Address' => 'required|string'
                
            ]);
    
            $AccCode = $request->input('AccCode');
            $code = $request->input('code');
            $name = $request->input('name');
            $contactperson = $request->input('contactperson');
            $contact1 = $request->input('contact1');
            $contact2 = $request->input('contact2');
            $email = $request->input('email');
            $Address = $request->input('Address');
         
    

            try
            {


            $exists = DB::table('suppliers')->where('SupplierCode', $code)  ->where('Code', Session::get('PropertyCode'))->exists();

            if ($exists) {
             
                return redirect()->route('suppliers')->withErrors(['error' => 'Supplier with this code:'.$code.' already exists.']);
            }

            $result = modelSuppliers::savesuppliers(Session::get('RegNo'),Session::get('PropertyCode'),$AccCode, $code,$name,$contactperson, $contact1, $contact2,$email, $Address);
            if ($result) 
            {
                 return redirect()->route('suppliers')->with('success', 'Supplier Saved Successfully.');
           } 
            else 
            {
                return redirect()->route('suppliers')->withErrors(['error' => 'Failed to Save Supplier Details.']);
            }

        } catch (\Exception $e) {
            
            return redirect()->route('suppliers')->withErrors(['error' => 'An error occurred!!' . $e->getMessage()]);
        }

        }

        public function editsuppliers(Request $request)
        {
            $request->validate([
                'AccCode' => 'required|string',
                'code' => 'required|string',
                'name' => 'required|string',
                'contactperson' => 'required|string',
                'contact1' => 'required|string',
                'contact2' => 'required|string',
                'email' => 'required|string',
                'Address' => 'required|string',
                'id' => 'required|string'
                
            ]);
    
            $AccCode = $request->input('AccCode');
            $code = $request->input('code');
            $name = $request->input('name');
            $contactperson = $request->input('contactperson');
            $contact1 = $request->input('contact1');
            $contact2 = $request->input('contact2');
            $email = $request->input('email');
            $Address = $request->input('Address');
         
    
                $id = $request->input('id');
        
    
                try
                {
    
    
                $exists = DB::table('suppliers')->where('ID', $id)->exists();
    
                if ($exists) 
                {


                $result =  modelSuppliers::updatesuppliers($id,Session::get('RegNo'),Session::get('PropertyCode'),$AccCode, $code,$name,$contactperson, $contact1, $contact2,$email, $Address);
                
                if ($result) 
                {
                     return redirect()->route('suppliers')->with('success', 'Supplier Updated Successfully.');
               } 
                else 
                {
                    return redirect()->route('suppliers')->withErrors(['error' => 'Failed to Update Supplier.']);
                }
                 
                 
                }
                else
                {

                    return redirect()->route('suppliers')->withErrors(['error' => 'Supplier Not Found.']);
                

                }
    
                
    
            } catch (\Exception $e) {
                
                return redirect()->route('suppliers')->withErrors(['error' => 'An error occurred!!' . $e->getMessage()]);
            }
    
            }
            public function deletesuppliers( $id)
        {
          
    
                try
                {
    
    
                $exists = DB::table('suppliers')->where('ID', $id)->exists();
    
                if ($exists) 
                {


                $result =modelSuppliers::Deletesuppliers($id,Session::get('PropertyCode'));
                
                if ($result) 
                {
                     return redirect()->route('suppliers')->with('success', 'Supplier Removed Successfully.');
               } 
                else 
                {
                    return redirect()->route('suppliers')->withErrors(['error' => 'Failed to Remove Supplier.']);
                }
                 
                 
                }
                else
                {

                    return redirect()->route('suppliers')->withErrors(['error' => 'Supplier Not Found.']);
                

                }
    
                
    
            } catch (\Exception $e) {
                
                return redirect()->route('suppliers')->withErrors(['error' => 'An error occurred!!' . $e->getMessage()]);
            }
    
            }
}
