<?php

    namespace App\Http\Controllers;
    use App\Models\Bank;
    use App\Models\ReportControl;
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\DB;
    class ReportControllers extends Controller{

    /**
     * 
     * 
     * 
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     * 
     * 
     */
     
    public function index(){
        
        $dataemp  = DB::table("employee")->where("job_roll",'<>','cs')->get();
        $data = bank::get();
        $dsaMaster = DB::table("daschannel")->get();
        $ChannelSourcerdata  = DB::table("employee")->where("job_roll",'cs')->where("status",'active')->get();
        return view("layouts.Payout",["bankData"=>$data,'employee'=>$dataemp,"dsaMaster"=>$dsaMaster ,"channel_sourcer"=>$ChannelSourcerdata]);
        
    }

    /**
     * 
     * Show the form for creating a new resource.
     *
     * 
     * 
     * 
     * 
     * @return \Illuminate\Http\Response
     */
     
    public function create()
    {
        //echo "h";
        $data["GoogleAdsLeads"] = DB::table('employee')
        ->select("*")
        ->where('job_roll', 'cs')
        ->where('status', 'active')
        ->get();
        
        $data4 = $data["GoogleAdsLeads"];
        
        //echo "<pre>";
        //print_r($data4);

        $array = json_decode(json_encode($data4), true);
        
        $dataw["GoogleAdsLeads"] = DB::table('loanstatus')
        ->select("*")
        ->where('status', 'active')
        ->get();
        
        $data42 = $dataw["GoogleAdsLeads"];
        
        $dsa["daschannel"] = DB::table('daschannel')
        ->select("*")
        ->where('status', 'active')
        ->get();
        
        $dataDSAChannel = $dsa["daschannel"];
        $arrayw         = json_decode(json_encode($data42), true);
        $dataDSAChannel2 = json_decode(json_encode($dataDSAChannel), true);
        
        //print_r($dataDSAChannel);
        //die();
        
        $loanproduct["loanProduct"] = DB::table('loanproduct')
        ->select("*")
        ->where('status', 'active')
        ->get();
        
        $loanproductData = $loanproduct["loanProduct"];
        $loanProDataArray         = json_decode(json_encode($loanproductData), true);

        $employee["employee"] = DB::table('employee')
        ->select("*")
        ->where('status', 'active')
        ->where('job_roll', '<>','cs')
        ->get();
        
        $employeeData = $employee["employee"];
        $employeeDataArrY         = json_decode(json_encode($employeeData), true);

        $industrytype["industrytype"] = DB::table('industrytype')
        ->select("*")
        ->where('status', 'active')
        ->get();
        
        $industrytypeData = $industrytype["industrytype"];
        $industrytypeArray         = json_decode(json_encode($industrytypeData), true);

        $promoCode["promoCode"] = DB::table('promoCode')
        ->select("*")
        ->where('status', 'active')
        ->get();
        
        $promoCode = $promoCode["promoCode"];
        $promoCodeArr         = json_decode(json_encode($promoCode), true);

        $datasm["sm"] = DB::table('sm')
        ->select("*")
        ->where('status', 'active')
        ->get();
        $SM             = $datasm["sm"];
        $SMData         = json_decode(json_encode($SM), true);

        $loanPurposedata["loan_purpose"] = DB::table('loan_purpose')
        ->select("*")
        ->where('status', 'active')
        ->get();
        $loanpur         = $loanPurposedata["loan_purpose"];
        $loanpurposeData = json_decode(json_encode($loanpur), true);

        
        $AppliedBankData["AppliedBank"] = DB::table('bank')
        ->select("*")
        ->where('status', 'active')
        ->get();
        $applyBank         = $AppliedBankData["AppliedBank"];
        $AppliedBankArray = json_decode(json_encode($applyBank), true);
        
          $state["stateName"] = DB::table('state')
        ->select("*")
        ->where('status', 'active')
        ->get();
        $stateName         = $state["stateName"];
        $stateNameArr = json_decode(json_encode($stateName), true);
       // echo "<pre>";   print_r($stateNameArr); die(); 

        return view("layouts.addNewLeads",["stateNameArr"=>$stateNameArr,"allSM"=>$SMData,"AppliedBank"=>$AppliedBankArray,"loanpurposeData"=>$loanpurposeData,"alldataSource"=>$array,"promoCodeArr"=>$promoCodeArr,"HandledBy"=>$employeeDataArrY,"industrytypeArray"=>$industrytypeArray],["loanstatus"=>$arrayw,"DSAchannel"=>$dataDSAChannel2,"loanProDataArray"=>$loanProDataArray]);
    }

    /**
     * 
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     * 
     */

    public function store(Request $request){
       
        $newLeads = new AddNewLead;
        $start_date = strtoupper($request->get("case_start_date"));
        $newLeads->start_date =date('Y-m-d',strtotime($start_date));
        //die();
        $newLeads->type =  strtoupper($request->get("type"));
        $newLeads->OrganizationName= strtoupper($request->get("OrganizationName"));
        $newLeads->ChannelSource = strtoupper($request->get("ChannelSource"));
        $newLeads->status = strtoupper($request->get("status"));
        $newLeads->CustomerName = strtoupper($request->get("CustomerName"));
        $newLeads->EMailId = strtoupper($request->get("EMailId"));
        $newLeads->PhoneNumber = strtoupper($request->get("PhoneNumber"));
        //$newLeads->RequiredLoanAmount = $request->get("RequiredLoanAmount");
        $newLeads->BankName = strtoupper($request->get("BankName"));
        $newLeads->home_Location = strtoupper($request->get("home_Location"));
        $newLeads->off_Location = strtoupper($request->get("off_Location"));
        $newLeads->industry_type = strtoupper($request->get("industry_type"));
        $newLeads->home_address = strtoupper($request->get("home_address"));
        $newLeads->home_state = strtoupper($request->get("home_state"));
        $newLeads->off_state = strtoupper($request->get("off_state"));
        // die();
        $newLeads->office_address = strtoupper($request->get("office_address"));
        $newLeads->dob = strtoupper($request->get("dob"));
        $newLeads->net_salary	 = strtoupper($request->get("net_salary"));
        $newLeads->salary_bank = strtoupper($request->get("salary_bank"));
        $newLeads->createdBy =  session('loggedUser');
        //$newLeads->loan_approval_Bank = $request->get("loan_approval_Bank");
        $newLeads->los_no = strtoupper($request->get("los_no"));
        $newLeads->emi_cc = strtoupper($request->get("emi_cc"));
        $newLeads->ref_name = strtoupper($request->get("ref_name"));
        $newLeads->ref_mob = strtoupper($request->get("ref_mob"));
        $newLeads->ref_add = strtoupper($request->get("ref_add"));
        $newLeads->sales_manager_name = strtoupper($request->get("sales_manager_name"));
        $newLeads->promoCode = strtoupper($request->get("promoCode"));
        $newLeads->apply_loanAmt = strtoupper($request->get("apply_loanAmt"));
        $newLeads->tenureMonth = strtoupper($request->get("tenureMonth"));
        $newLeads->login_date = strtoupper($request->get("login_date"));
        $newLeads->loanPurpose = strtoupper($request->get("loanPurpose"));
        $newLeads->mother_name = strtoupper($request->get("mother_name"));
        $newLeads->nextFollowupDate = strtoupper($request->get("nextFollowupDate"));
       
        $newLeads->dsa_name = strtoupper($request->get("dsa_name"));
        //$newLeads->case_start_date = $request->get("case_start_date");
        $newLeads->panCard = strtoupper($request->get("panCard"));
        $newLeads->whNumber = strtoupper($request->get("whNumber"));
        $newLeads->home_pin = strtoupper($request->get("home_pin"));
        $newLeads->office_pin = strtoupper($request->get("office_pin"));
        $newLeads->office_no = strtoupper($request->get("office_no"));
        $newLeads->cibilScore = strtoupper($request->get("cibilScore"));
        $newLeads->save();
        return redirect('/AddNewLead')->with('status', 'New Lead Saved Successfully');

    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\AddNewLead  $addNewLead
     * @return \Illuminate\Http\Response
     */

    public function show(AddNewLead $addNewLead)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\AddNewLead  $addNewLead
     * @return \Illuminate\Http\Response
     */
    
    public function edit(AddNewLead $addNewLead ,$id){
             function amount2Rs($amount)
{
    $amount = explode("-", $amount);
    $l = COUNT($amount);
    if ($l > 1) {
        $minus = '-';
        $amount = $amount[1];
    } else {
        $amount = $amount[0];
    }
    $amount = explode(".", $amount);
    $l = COUNT($amount);
    if ($l > 1) {
        $after_point = $amount[1];
    }
    $amount = $amount[0];
    $len = strlen($amount);
    switch ($len) {
        case 1:
        case 2:
        case 3:
            $str = $amount;
            break;
        case 4:
        case 5:
            $j = $amount / 1000;
            settype($j, "int");
            $sub = substr($amount, strlen($j), $len);
            $amount = $j . ',' . $sub;
            $str = $amount;
            break;
        case 6:
        case 7:
            $j = $amount / 100000;
            settype($j, "int");
            //echo $amount."->$j";
            $sub = substr($amount, (strlen($j) + 2), $len);
            $amount = $j . ',' . substr($amount, strlen($j), 2) . ',' . $sub;
            $str = $amount;
            break;
        case 8:
        case 9:
        case 10:
            $j = $amount / 10000000;
            settype($j, "int");
            $sub = substr($amount, (strlen($j) + 4), $len);
            $amount = $j . ',' . substr($amount, strlen($j), 2) . ',' . substr($amount, (strlen($j) + 2), 2) . ',' . $sub;
            $str = $amount;

            break;
    }
    if (empty($str)) {
        $str = '0';
    }
    if (empty($after_point)) {
        $after_point = '00';
    }

    $str .= "." . $after_point;

    if (!empty($minus)) {
        $str = $minus . $str;
    }
    return $str;
}   
         
         //echo "h";
         $data["GoogleAdsLeads"] = DB::table('employee')
         ->select("*")
         ->where('job_roll', 'cs')
         ->get();
         
         $data4 = $data["GoogleAdsLeads"];
         //echo "<pre>";
         //print_r($data4);gfxz
         $array = json_decode(json_encode($data4), true);
         $dataw["GoogleAdsLeads"] = DB::table('loanstatus')
         ->SELECT("*")
         ->WHERE('status', 'active')
         ->get();
         
         $data42 = $dataw["GoogleAdsLeads"];
         
         $dsa["daschannel"] = DB::table('daschannel')
         ->select("*")
         ->where('status', 'active')
         ->get();
         
         $dataDSAChannel = $dsa["daschannel"];
  $dataDSAChannel2 = json_decode(json_encode($dataDSAChannel), true);
         $arrayw         = json_decode(json_encode($data42), true);
         
        
         
         //print_r($dataDSAChannel);
         //die();
         
         $loanproduct["loanProduct"] = DB::table('loanproduct')
         ->select("*")
         ->where('status', 'active')
         ->get();
         
         $loanproductData = $loanproduct["loanProduct"];
         $loanProDataArray         = json_decode(json_encode($loanproductData), true);
 
         $employee["employee"] = DB::table('employee')
         
         ->select("*")
         ->where('status', 'active')
         ->where('job_roll','<>', 'cs')
         ->get();
         
         $employeeData = $employee["employee"];
         $employeeDataArrY         = json_decode(json_encode($employeeData), true);
 
         $industrytype["industrytype"] = DB::table('industrytype')
         
         ->select("*")
         ->where('status', 'active')
         ->get();

        $industrytypeData  =   $industrytype["industrytype"];
        $industrytypeArray = json_decode(json_encode($industrytypeData), true);
 
        $promoCode["promoCode"] = DB::table('promoCode')
         
         ->select("*")
         ->where('status', 'active')
         ->get();
         
        $promoCode = $promoCode["promoCode"];
        $promoCodeArr         = json_decode(json_encode($promoCode), true);
 
        $datasm["sm"] = DB::table('sm')
         ->select("*")
         ->where('status', 'active')
         ->get();
    
        $SM             = $datasm["sm"];
        $SMData         = json_decode(json_encode($SM), true);
 
        $loanPurposedata["loan_purpose"] = DB::table('loan_purpose')
        ->select("*")
        ->where('status', 'active')
        ->get();
        
        $loanpur         = $loanPurposedata["loan_purpose"];
        $loanpurposeData = json_decode(json_encode($loanpur), true);

        $AppliedBankData["AppliedBank"] = DB::table('bank')
        ->select("*")
        ->where('status', 'active')
        ->get();
        
        $applyBank         = $AppliedBankData["AppliedBank"];
        $AppliedBankArray  = json_decode(json_encode($applyBank), true);
 
        $EditData          = DB::table("digitaladsleads")->where("id",$id)->get();
         $state["stateName"] = DB::table('state')
        ->select("*")
        ->where('status', 'active')
        ->get();
        $stateName         = $state["stateName"];
        
        $EditData      = json_decode(json_encode($EditData), true);
        
        $stateNameArr      = json_decode(json_encode($stateName), true);
         
        $apply_loanAmt   = ($EditData[0]['apply_loanAmt']);
        $amount2Rs        = amount2Rs($apply_loanAmt);
        //echo "<pre>;"print_r($amount2Rs);die();
        return view("layouts.EditNewLeads",["amount2Rs"=>$amount2Rs,"stateNameArr"=>$stateNameArr,"allSM"=>$SMData,"AppliedBank"=>$AppliedBankArray,"loanpurposeData"=>$loanpurposeData,"alldataSource"=>$array,"promoCodeArr"=>$promoCodeArr,"HandledBy"=>$employeeDataArrY,"industrytypeArray"=>$industrytypeArray],["loanstatus"=>$arrayw,"DSAchannel"=>$dataDSAChannel2,"loanProDataArray"=>$loanProDataArray,"EditData"=>$EditData]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\AddNewLead  $addNewLead
     * @return \Illuminate\Http\Response
     */

    public function update(Request $request, AddNewLead $addNewLead)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\AddNewLead  $addNewLead
     * @return \Illuminate\Http\Response
     */
    public function destroy(AddNewLead $addNewLead ,$id)
    {
        //$id;
        DB::table("digitaladsleads")->where("id",$id)->delete();
        return redirect("/AddNewLead")->with('message',"deletion successfully");

    }
}
