<?php

namespace App\Http\Controllers;

use App\BlockUnitLog;
use App\CustomerProperty;
use App\CustomerStatement;
use Illuminate\Http\Request;
use App\UnitBlockageCriteria;
use Illuminate\Validation\Rule;
use DB;

class UnitBlockageController extends Controller
{
    //
    public function index(){
        
 

      
        
        

             

      $unit_criteria=  UnitBlockageCriteria::all();
        return view('Setting.unitblockage.index',compact('unit_criteria'));
    }
    public function create(){
        
        $result = UnitBlockageCriteria::where([['criteria_no', '!=', null]])
        ->orderby('criteria_no', 'desc')->first();

    if ($result)
        $criteria_no = ++$result->criteria_no;
    else
        $criteria_no = '1';
   

   
       


        return view('Setting.unitblockage.create',compact('criteria_no'));
    }
    public function store(Request $request){
        $this->validate($request, [

         
            'criteria' =>    'required|unique:unit_blockage_criterias,criteria_no',
            'number_of_days' => 'required',
            'due_amount' => 'required',
            'description'=>'nullable|string|max:255',
           
        ]);
      
      $unit_blockage_criteria= new UnitBlockageCriteria;
      $unit_blockage_criteria->criteria_no = $request->criteria;
      $unit_blockage_criteria->total_days = $request->number_of_days;
      $unit_blockage_criteria->total_amount = $request->due_amount;
      $unit_blockage_criteria->description = $request->description;
      $unit_blockage_criteria->save();
    //   $unit_blockage_criteria->status =
    return redirect()->route('unitblockage.index')->with([
        'message' => 'Unit Criteria Added Successfully!',
        'alert-type' => 'success',
    ]);
    }

    public function activeCriteria(Request $request){

        
        $unit_blockage_criteria=  UnitBlockageCriteria::where('id',$request->id)->first();
        $unit_blockage_criteria->status = 1; 
        $unit_blockage_criteria->save();
        UnitBlockageCriteria::whereNotIn('id', [$unit_blockage_criteria->id])->update(['status' => 0]);       
        return response(['msg'=>"Active"]);
    }
    public function edit($id){

     $unit_blockage=   UnitBlockageCriteria::where('id',$id)->first();
     if(!empty( $unit_blockage)){
        return view('Setting.unitblockage.edit',compact('unit_blockage'));

     }
else{
    return redirect()->back()->with([
        'message' => 'SomeThing went wrong!',
        'alert-type' => 'error',
    ]);

}
     
    }

    public function update(Request $request ){
        
       
        $this->validate($request, [
            'id' => 'required',
            'criteria' => [
                'required',
                Rule::unique('unit_blockage_criterias', 'criteria_no')->ignore($request->id),
            ],
            // 'criteria' =>    'required|unique:unit_blockage_criterias,criteria_no',
            'number_of_days' => 'required',
            'due_amount' => 'required',
            'description'=>'nullable|string|max:255',
           
        ]);
      
        $unit_blockage_criteria=   UnitBlockageCriteria::where('id',$request->id)->first(); 
        $unit_blockage_criteria->criteria_no = $request->criteria;
        $unit_blockage_criteria->total_days = $request->number_of_days;
        $unit_blockage_criteria->total_amount = $request->due_amount;
        $unit_blockage_criteria->description = $request->description;
        if($request->status == 1){
            $unit_blockage_criteria->status = $request->status;
            UnitBlockageCriteria::whereNotIn('id', [$unit_blockage_criteria->id])->update(['status' => 0]);       
        }
        else{
            $unit_blockage_criteria->status = $request->status;
        }
        $unit_blockage_criteria->save();
        return redirect()->route('unitblockage.index')->with([
            'message' => 'Unit Criteria Updated Successfully!',
            'alert-type' => 'success',
        ]);
    }


 public function blockUnitHistory(){

   $block_unit_log= BlockUnitLog::orderBy('created_at', 'desc')->paginate(25); 
   return view('statement.block.blocklistlog',compact('block_unit_log'));
 }
}
