<?php

namespace Gisplus\Package\EstatechVendor\Controllers;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use Gisplus\Package\EstatechVendor\Models\Vendor;

use Gisplus\Package\EstatechVendor\Requests\VendorRequest;

use EF;

use App\User;

use App\Journal;




class VendorController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $user = new User;

        $vendor_list = $user->getUserListByType(['Vendor']);

        return view('estatechvendor::manage_vendor',[
            'vendor_list' => $vendor_list,
        ]);
    }

    function updateVendorOpeningBalance(Request $request)
    {
        try{

            if($request->isMethod('post'))
            {
                $flag = "false";

                $amount = $request->amount;

                $id     = decrypt($request->journal);

                $journal = new Journal;

                if($journal->updateJournalWithAmount($id,$amount))
                {
                    return "true";
                }

                return $flag;
            }
            else
            {
                $id = decrypt($request->id);

                $journal = new Journal;

                $opening_balance_list = $journal->getOpeningBalanceListByVendor($id);

                return view('estatechvendor::vendor.vendor_opening_balance',[
                'opening_balance_list' => $opening_balance_list,
            ]);
            }

            

        }
        catch (DecryptException $e) {


     
        }

        return redirect()->back();
        
    }

    public function vendorDashboard()
    {
      
        return view('estatechvendor::vendor_dashboard',[
            

        ]);   
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function storeVendor(VendorRequest $request)
    {
        $vendor = new Vendor;
        $collect_form = $request->input();
        
        $collect_form['email']                      = NULL;
        $collect_form['password']                   = NULL;        
        $collect_form['type']                       = 'Vendor';
        $collect_form['user_image']                 = $request->user_image;
 
 
        $collect_form['branch_id'] = 1;

        
        $curr_vendor =  $vendor->storeVendor($collect_form);
     
 
          if(isset($curr_vendor->id))
          {
              parent::setLogMessage('New vendor "'.$curr_vendor->name.'" has been added.');
  
              parent::successMessage('New vendor has been added.',parent::getLogMessage());
           
          }
          else
          {
            parent::errorMessage('Vendor already found.');
          }
  
          return redirect()->back();
    }

  
    public function updateVendor(VendorRequest $request)
    {
        try {
            $collect_form = $request->input();

            $collect_form['user']       = decrypt($collect_form['user']);
 
            $collect_form['type']       =    'Vendor';

            if($request->user_image)
            {
                $collect_form['user_image']  = $request->user_image;
  
            }
            else
            {
               $collect_form['user_image']            = NULL;
  
            }

            $vendor = new Vendor;

            $curr_vendor = $vendor->updateVendor($collect_form);

            if(isset($curr_vendor))
            {
                
                parent::setLogMessage($curr_vendor->name.' profile has been updated.');

                parent::successMessage('Vendor profile has been updated.',parent::getLogMessage());
            }

     
        } catch (DecryptException $e) {
     
        }

        return redirect()->back();
    }


    public function removeVendor($id)
    {
        return redirect('home');

        parent::errorMessage('Vendor not found.');

        try {
            $id = decrypt($id);

            $vendor = new Vendor;

            if($vendor->removeUser($id))
            {
                parent::successMessage('Vendor has been removed.');
            }
        }
         catch (DecryptException $e) {
        }

        return redirect()->back();
    }

    public function getVendorInfo($id)
    {
        try
            {
                $user = new User;

                $vendor_id = decrypt($id);

                $vendor_info = $user->getUserById($vendor_id); 
                
                return $vendor_info;

            }
            catch(DecryptException $e)
            {
    
            }    
        }
    }
