<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Booking;
use App\Transfer;

use App\Commission;
use EF;
use App\Payment;
use App\Journal;
use App\Plot;
use App\MinuteSheet;
use App\PayrollDetail;
use App\Deduction;
use App\Account;
use App\CancelBooking;
use App\Payroll;

class DataTableController extends Controller
{
    public function showDataTableBranchWise(Request $request)
    {
    	if($request->ajax())
    	{
    		try 
			{
				$key    = decrypt($request->input('key'));

				$branch = decrypt($request->input('branch_object'));

				if($key === "Booking")
				{
					return $this->showBookings($branch);
				}
                else if($key === "Soft Booking")
                {
                    return $this->showSoftBookings($branch);
                }
                else if($key === "Cancel Booking")
                {
                    return $this->showCancelBookings($branch);
                }
                else if($key === "Block Booking")
                {
                    return $this->showBlockBookings($branch);
                }
                else if($key === "Plot")
                {
                    return $this->showPlots($branch);
                }
                else if($key === "Available Plot")
                {
                    return $this->showAvailablePlots($branch);
                }
                else if($key === "Transfer Booking")
                {
                    return $this->showTransferBookings($branch);
                }
                else if($key === "Payments")
                {
                    return $this->showPayments($branch);
                }
                else if($key === "Vouchers")
                {
                    return $this->showVouchers($branch);
                }
                else if($key === "Ledger")
                {

                    if(isset($request->args1))
                    {
                        $account = decrypt($request->input('args1'));

                        return $this->showLedger($account,$branch);
                    }

                }
                else if($key === "Minute Sheet Detail")
                {
                    // BASICALLY ITS NOT A BRANCH ITS A SALARY MODE
                    // ARGS CONTAIN MINUTE SHEET ID
                    if(isset($request->args1))
                    {
                        $minute_sheet_id = decrypt($request->input('args1'));

                        return $this->showMinuteSheetDetail($minute_sheet_id,$branch);
                    }
                }
                else if($key === "BlockWisePlots"){
                    // return block wise plots
                    $block = decrypt($request->input('args1'));
                    return $this->showBlockWisePlots($branch,$block);
                }
               

			}
			catch (DecryptException $e) 
			{

			}

            return '';
    	}

    	return redirect('home');
    }

    public function showBookings($branch_id)
    {
    	$condition = ['Approve','Unapprove'];
              
        $booking     =   new Booking;
        
        $commission  =   new Commission;
         
          $data = [

              'booking_list'           =>  $booking->getBookingByBranch([$branch_id],$condition),
              'commission'             =>  $commission,
              'plot_labels'            =>  EF::plotWhiteLabel(),
            ];

        return view('components.booking.table_booking',$data)->render();
    }

    function showSoftBookings($branch_id)
    {
        $condition = ['Expired','Reserved'];

        $booking = new Booking;
    
        $booking->verifiyExpireBooking();

        return view('components.booking.table_softbooking',[
            'booking_list'           =>  $booking->getBookingByBranch([$branch_id],$condition)
        ])->render();
    }

    function showCancelBookings($branch_id)
    {
        $condition = ['Cancel'];



        $cancel_booking = new CancelBooking;

        return view('components.booking.table_cancelbooking',[

             'cancelbooking_list'  =>  $cancel_booking->getAllCancelBookingByBranch($branch_id),

        ])->render();
    }

    function showBlockBookings($branch_id)
    {
        $status = ['Approve','Cancel','Transfer','Unapprove','Reserve'];
        $booking = new Booking;



        return view('components.booking.table_blockbooking',[
            'block_booking_list' =>  $booking->getBlockBookingByBranch([$branch_id],$status)
        ])->render();
    }

    function showTransferBookings($branch_id)
    {
       
        $transfer = new Transfer;

        return view('components.booking.table_transferbooking',[
          'transferbooking_list' => $transfer->getBranchWiseTransfer([$branch_id])
        ])->render();
    }

    public function showPayments($branch_id)
    {
        $payment = new Payment;

        return view('components.payment.table_payment',[

          'payment_list'  => $payment->getBranchWisePayments([$branch_id],['Unapprove']),
          'unique_id'     =>  uniqid(),

        ])->render();
    }

    public function showVouchers($branch_id)
    {
        $journal = new Journal;

        return view('components.voucher.table_voucher',[

          'journal_list'    => $journal->getBranchWiseTransaction([$branch_id],['Unapprove'])

        ])->render();
    }

    public function showLedger($account_id,$branch_id)
    {
        $accountObj = new AccountController;

        $journal = new Journal;

        $journal_list = $journal->getJournalAccountBranchWise($account_id,[$branch_id]);

      
        return view('components.subhead.table_subhead_ledger',[
            'journal_list' => $accountObj->filterAccounts($journal_list)
        ])->render();
    }

    public function showPlots($branch)
    {

        $inventory = new Plot;

        return view('components.plot.table_plot',
            [
                'inventory_list' => $inventory->getAllPlot(['unsold'],[$branch]),
                'plot_labels'    => EF::plotWhiteLabel(),
                'booking_labels'      => EF::getWhiteLabel(),

            ]);
    }
    public function showAvailablePlots($branch)
    {

        $inventory = new Plot;



        return view('components.plot.table_available_plots',
            [
                'inventory_list' => $inventory->getAllPlot(['unsold'],[$branch],['0']),
                'plot_labels'    => EF::plotWhiteLabel(),
                'booking_labels'      => EF::getWhiteLabel(),

            ]);
    }

    public function showBlockWisePlots($branch,$block)
    {

        $inventory = new Plot;

        if($block != "0")
        {
            $inventory_list = $inventory->getPlotOfBranchByBlock([$branch],[$block]);
        }
        else
        {
            $inventory_list = $inventory->getBranchWiseAllBlocks([$branch]);

        }

        return view('components.plot.table_plot',
            [
                'inventory_list' => $inventory_list,
                'plot_labels'    => EF::plotWhiteLabel(),
                'booking_labels'      => EF::getWhiteLabel(),

            ]);
    }


    public function showMinuteSheetDetail($minute_sheet_id,$salary_mode)
    {
            $minute_sheet      = new MinuteSheet;
            $payroll_detail    = new PayrollDetail;
            $deduction         = new Deduction;
            $account           = new Account;
            $payroll           = new Payroll;

            $curr_minute_sheet = $minute_sheet->getMinuteSheetById($minute_sheet_id);
            
            if(isset($curr_minute_sheet->id))
            {
                $minute_payroll_detail = $payroll_detail->getPayrollDetailByMinuteId($curr_minute_sheet->id,[$salary_mode]);

                   $deduction_titles = $deduction->getDistinctDeduction('title',['title','id']);

                   $curr_accounts = $account->getSubheadByType('Bank');


                  $data = [
                    'minute_payroll_detail'       =>  $minute_payroll_detail,
                'curr_minute_sheet'           =>  $curr_minute_sheet,
                'payroll'                     => $payroll,
                'unique_id'                   => uniqid(),
                


                  ];

                  return  view('hr.components.minute_sheet.table_minute_sheet_details',$data)->render();
            }

            return '';
    }


     


}
