<?php 
include('dbcon.php');
session_start();
use SimpleExcel\SimpleExcel;

if(isset($_GET['table_name'])){
    
    $id = $_GET['id'];
    $table_name = $_GET['table_name'];
    $file_name = $_GET['file_name'];
    
    $query = $database->getReference($table_name.'/'.$id.'/')->remove();
    if($query){
        $_SESSION['status'] = "Deleted Successfully";
        header("Location: $file_name");
        exit();
    }else{
        $_SESSION['status'] = "Not Deleted";
        header("Location: $file_name");
        exit();
    }
    
}

if(isset($_POST['send_notification_btn'])){
    $title = $_POST['title'];
    $body = $_POST['desc'];
    $topic = "AllUsers";
    $serverKey = "AAAAlpa2RIQ:APA91bGFHWlDEFAkZqqkpfrm4wZdrdZHsQ96DCkus6x8jSbdogiSJmDnGMkaagJMnlVoqJSP4FxrH_3N8dLY1xt_QYu1zaoJhQ8zJKonvBjPno8hGhuxbvRqLzh_jWuHglbLl4hMM7_F";

        ini_set("allow_url_fopen", "On");
        $data = 
        [
            "to" => '/topics/'.$topic,
            "notification" => [
                "body" => $body,
                "title" => $title,
            ],
        ];
     

        $options = array(
            'http' => array(
                'method'  => 'POST',
                'content' => json_encode( $data ),
                'header'=>  "Content-Type: application/json\r\n" .
                            "Accept: application/json\r\n" . 
                            "Authorization:key=".$serverKey
            )
        );
        

        $context  = stream_context_create( $options );
        $result = file_get_contents( "https://fcm.googleapis.com/fcm/send", false, $context );
        $query = $database->getReference("Notifications/")->push()->set([
            'title' => $title,
            'description' => $body,
            'created_at' => ['.sv' => 'timestamp'],
            ]);
        if($query){
                $_SESSION['status'] = "Notification Send Successfully";
                header('Location: notification.php');
                exit();
            }else{
                $_SESSION['status'] = "Notification Not Send";
                header('Location: notification.php');
                exit();
            }
    
}

if(isset($_POST['add_country_code']))
{
    $country_code = $_POST['country_code'];
    $query = $database->getReference('CountryCode')->push()->set([
        'country_code' => $country_code,
        'created_at' => ['.sv' => 'timestamp'],
    ]);
    if($query){
        $_SESSION['status'] = "Country Code Added Successfully";
        header('Location: spam-country-code.php');
        exit();
    }else{
        $_SESSION['status'] = "Country Code Not Added";
        header('Location: spam-country-code.php');
        exit();
    }
}
if(isset($_POST['code_delete_btn']))
{
    $id = $_POST['code_delete_btn'];
    $query = $database->getReference('CountryCode/'.$id)->remove();
    if($query){
        $_SESSION['status'] = "Country Code Deleted Successfully";
        header('Location: spam-country-code.php');
        exit();
    }else{
        $_SESSION['status'] = "Country Code Not Deleted";
        header('Location: spam-country-code.php');
        exit();
    }
}
if(isset($_POST['add_phone_number'])){
    $phone_number = $_POST['phone_number'];
    $query = $database->getReference('SpamPhoneNumber')->push()->set([
        'phone_number' => $phone_number,
        'created_at' => ['.sv' => 'timestamp'],
    ]);
    if($query){
        $_SESSION['status'] = "Spam Phone Number Added Successfully";
        header('Location: spam-country-number.php');
        exit();
    }else{
        $_SESSION['status'] = "Spam Phone Number Not Added";
        header('Location: spam-country-number.php');
        exit();
    }
}
if(isset($_POST['number_delete_btn']))
{
    $id = $_POST['number_delete_btn'];
    $query = $database->getReference('SpamPhoneNumber/'.$id)->remove();
    if($query){
        $_SESSION['status'] = "Spam Phone Number Deleted Successfully";
        header('Location: spam-country-number.php');
        exit();
    }else{
        $_SESSION['status'] = "Spam Phone Number Not Deleted";
        header('Location: spam-country-number.php');
        exit();
    }
}

if(isset($_POST['report_delete_btn']))
{
    $id = $_POST['report_delete_btn'];
    $query = $database->getReference('Spam User/'.$id)->remove();
    if($query){
        $_SESSION['status'] = "Report Deleted Successfully";
        header('Location: report.php');
        exit();
    }else{
        $_SESSION['status'] = "Report Not Deleted";
        header('Location: report.php');
        exit();
    }
}


if(isset($_POST['spam_reported_number']))
{
    $phone_number = $_POST['number'];
    $id = $_POST['id'];
    $query = $database->getReference('SpamPhoneNumber')->push()->set([
        'phone_number' => $phone_number,
        'created_at' => ['.sv' => 'timestamp'],
    ]);
    $query1 = $database->getReference('Spam User/'.$id)->remove();

    if($query){
        $_SESSION['status'] = "Spam Phone Number Added Successfully";
        header('Location: spam-country-number.php');
        exit();
    }else{
        $_SESSION['status'] = "Spam Phone Number Not Added";
        header('Location: spam-country-number.php');
        exit();
    }

}


if(isset($_POST['dealers_banners_btn'])){
    
    $banner1 = $$_FILES['banner1']['name'];
    $banner2 = $_FILES['banner2']['name'];;
    $banner3 = $_FILES['banner3']['name'];;

    if($_FILES['banner1']['name'] != null){
        $query = $database->getReference('DealerBanners/')->update([
            'banner1' => $_FILES['banner1']['name'],
            ]);
        $bucket->upload(
            file_get_contents($_FILES['banner1']['tmp_name'],'dealersbanners/banner1/'),
            [
                'name' => 'dealersbanners/banner1/'.$_FILES['banner1']['name'],
            ]
        );
    }

    if($_FILES['banner2']['name'] != null){
        $query = $database->getReference('DealerBanners/')->update([
            'banner2' => $_FILES['banner2']['name'],
            ]);
        $bucket->upload(
            file_get_contents($_FILES['banner2']['tmp_name'],'dealersbanners/banner2/'),
            [
                'name' => 'dealersbanners/banner2/'.$_FILES['banner2']['name'],
            ]
        );
    }

    if($_FILES['banner3']['name'] != null){
        $query = $database->getReference('DealerBanners/')->update([
            'banner3' => $_FILES['banner3']['name'],
            ]);
        $bucket->upload(
            file_get_contents($_FILES['banner3']['tmp_name'],'dealersbanners/banner3/'),
            [
                'name' => 'dealersbanners/banner3/'.$_FILES['banner3']['name'],
            ]
        );
    }


    $_SESSION['status'] = "Banners Updated Successfully";
        header('Location: dealer_banner.php');
        exit();


}


if(isset($_POST['banners_btn'])){
    $banner1 = $$_FILES['banner1']['name'];
    $banner2 = $_FILES['banner2']['name'];;
    $banner3 = $_FILES['banner3']['name'];;

    if($_FILES['banner1']['name'] != null){
        $query = $database->getReference('Banners/')->update([
            'banner1' => $_FILES['banner1']['name'],
            ]);
        $bucket->upload(
            file_get_contents($_FILES['banner1']['tmp_name'],'banners/banner1/'),
            [
                'name' => 'banners/banner1/'.$_FILES['banner1']['name'],
            ]
        );
    }

    if($_FILES['banner2']['name'] != null){
        $query = $database->getReference('Banners/')->update([
            'banner2' => $_FILES['banner2']['name'],
            ]);
        $bucket->upload(
            file_get_contents($_FILES['banner2']['tmp_name'],'banners/banner2/'),
            [
                'name' => 'banners/banner2/'.$_FILES['banner2']['name'],
            ]
        );
    }

    if($_FILES['banner3']['name'] != null){
        $query = $database->getReference('Banners/')->update([
            'banner3' => $_FILES['banner3']['name'],
            ]);
        $bucket->upload(
            file_get_contents($_FILES['banner3']['tmp_name'],'banners/banner3/'),
            [
                'name' => 'banners/banner3/'.$_FILES['banner3']['name'],
            ]
        );
    }


    $_SESSION['status'] = "Banners Updated Successfully";
        header('Location: banners.php');
        exit();


}

if(isset($_POST['user_status_update_btn'])){
    $id = $_POST['id'];
    $points = $_POST['points'];
    $old_points = $_POST['old_points'];
    $roles = $_POST['roles'];
    $select_enable_disable = $_POST['select_enable_disable'];
    if($select_enable_disable == 'review'){
        $status = 0;
    }else if($select_enable_disable == 'accepted'){
        $status = 1;
    }else if($select_enable_disable == 'reject'){
        $status = 2;
    }
    $query = $database->getReference('Users/'.$id)->update([
        'status' => $status,
        'points' => $points,
        'roles' => $roles
    ]);
    $s = $points - $old_points;
    $point_name = $_POST['point_name'];
    if(empty($point_name)){
        $point_name = "Manual Add";
    }
    $query1 = $database->getReference('Redeem History/'.$id)->push()->set([
        'point' => $s,
        'add' => 1,
        'qr_code' => $point_name,
        'created_at' => ['.sv' => 'timestamp'],
        ]);
    if($query1){
        $_SESSION['status'] = "User Status Updated Successfully";
        header('Location: users.php');
        exit();
    }else{
        $_SESSION['status'] = "User Status Not Updated";
        header('Location: users.php');
        exit();
    }

}

if(isset($_POST['add_offer_btn'])){
    $name = $_POST['name'];
    $point = $_POST['point'];
    $price = $_POST['price'];
    $cash = $_POST['cash'];
    $photo_name = $_FILES['image1']['name'];
    $select_enable_disable = $_POST['select_enable_disable'];
    if($select_enable_disable == 'disable'){
        $status = 0;
    }else if($select_enable_disable == 'enable'){
        $status = 1;
    }
    $query = $database->getReference('Offers/')->push()->set([
        'name' => $name,
        'point' => $point,
        'price' => $price,
        'photo_name' => $photo_name,
        'status' => $status,
        'cash' => $cash,
    ]);
    $bucket->upload(
        file_get_contents($_FILES['image1']['tmp_name'],'offer/'),
        [
            'name' => 'offer/'.$_FILES['image1']['name'],
        ]
    );
    if($query){
        $_SESSION['status'] = "Offer Added Successfully";
        header('Location: offers.php');
        exit();
    }else{
        $_SESSION['status'] = "Offer Not Added";
        header('Location: offers.php');
        exit();
    }


}

if(isset($_POST['update_offer_btn'])){
    $id = $_POST['id'];
    $name = $_POST['name'];
    $point = $_POST['point'];
    $price = $_POST['price'];
    $cash = $_POST['cash'];
    $photo_name = $_FILES['image1']['name'];
    
    $select_enable_disable = $_POST['select_enable_disable'];
    if($select_enable_disable == 'rejected'){
        $status = 0;
    }else if($select_enable_disable == 'accepted'){
        $status = 1;
    }
    if($photo_name != null){
        $query = $database->getReference('Offers/'.$id)->update([
        'name' => $name,
        'point' => $point,
        'price' => $price,
        'cash' => $cash,
        'photo_name' => $photo_name,
        'status' => $status,
        ]);
    
        $bucket->upload(
            file_get_contents($_FILES['image1']['tmp_name'],'offer/'),
            [
                'name' => 'offer/'.$_FILES['image1']['name'],
            ]
        );
    }else {
         $query = $database->getReference('Offers/'.$id)->update([
        'name' => $name,
        'point' => $point,
        'price' => $price,
        'cash' => $cash,
        'status' => $status,
        ]);
    }
     
    if($query){
        $_SESSION['status'] = "Offer Updated Successfully";
        header('Location: offers.php');
        exit();
    }else{
        $_SESSION['status'] = "Offer Not Updated";
        header('Location: offers.php');
        exit();
    }
}

if(isset($_POST['change_qr_btn'])){
     $allowed_ext = ['xls','csv','xlsx'];
    $fileName = $_FILES['import_file']['name'];
    $checking = explode(".",$fileName);

    $file_ext = end($checking);
    

    if(in_array($file_ext,$allowed_ext)){


        $targetPath = $_FILES['import_file']['tmp_name'];

        /** Load $inputFileName to a Spreadsheet object **/
        $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($targetPath);
        $data  = $spreadsheet->getActiveSheet()->toArray();

        foreach($data as $row){
            $unique_code = $row['0'];

             $ref = "QRSheet/";
            try {
                $fetchdata = $database->getReference($ref)->orderByChild('unique_code')->equalTo($unique_code)->getSnapshot();
                }
                        
                        //catch exception
            catch(Exception $e) {
                 echo 'Message: ' .$e->getMessage();
            }
                  
                   $fetchdata = $fetchdata->getValue();
                        if($fetchdata>0){
                            foreach ($fetchdata as $key => $row) {
                                
                                $query = $database->getReference('QRSheet/'.$key.'/status')->set('0');
                                
                            }   
                        }


        }

        $_SESSION['status'] = "QRCode List Updated Successfully";
        header('Location: qrcode.php');
        exit();


    }else{


    }
}

if(isset($_POST['add_qr_btn'])){
    
    
    $allowed_ext = ['xls','csv','xlsx'];
    $fileName = $_FILES['import_file']['name'];
    $checking = explode(".",$fileName);

    $file_ext = end($checking);
    

    if(in_array($file_ext,$allowed_ext)){


        $targetPath = $_FILES['import_file']['tmp_name'];

        /** Load $inputFileName to a Spreadsheet object **/
        $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($targetPath);
        $data  = $spreadsheet->getActiveSheet()->toArray();

        foreach($data as $row){
            $date = $row['0'];
            
            $dealer_name = $row['1'];
            $location = $row['2'];
            $product_brand = $row['3'];
            $product_type = $row['4'];
            $density = $row['5'];
            $thickness = $row['6'];
            $size = $row['7'];
            $array = explode("*",$size);
            $length = $array[0];
            $breath = $array[1];
            $color = $row['8'];
            $unique_code = $row['9'];

            $query = $database->getReference('QRSheet/')->push()->set([
                'date' => $date,
                'dealer_name' => $dealer_name,
                'location' => $location,
                'product_brand' => $product_brand,
                'product_type' => $product_type,
                'density' => $density,
                'thickness' => $thickness,
                'length' => $length,
                'breath' => $breath,
                'color' => $color,
                'unique_code' => $unique_code,
                'status' => 1,
            ]);
            


        }

        $_SESSION['status'] = "QRCode List Added Successfully";
        header('Location: qrcode.php');
        exit();


    }else{


    }





}

if(isset($_POST['add_ledger_btn'])){
       $allowed_ext = ['xls','csv','xlsx'];
    $fileName = $_FILES['import_file']['name'];
    $checking = explode(".",$fileName);
    $query = $database->getReference('Dealers_Ledger/')->remove();
    $file_ext = end($checking);
    

    if(in_array($file_ext,$allowed_ext)){


        $targetPath = $_FILES['import_file']['tmp_name'];

        /** Load $inputFileName to a Spreadsheet object **/
        $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($targetPath);
        $data  = $spreadsheet->getActiveSheet()->toArray();

        foreach($data as $row){
            
            
            $number = "+91".$row['0'];
            $date = $row['1'];
            $type = $row['2'];
            $invo = $row['3'];
            $nar = $row['4'];
            $amtdr = $row['5'];
            $amtcr = $row['6'];
            $bal =  $row['7'];

            $query = $database->getReference('Dealers_Ledger/')->push()->set([
                'number' => $number,
                'date' => $date,
                'type' => $type,
                'invo' => $invo,
                'nar' => $nar,
                'amtdr' => $amtdr,
                'amtcr' => $amtcr,
                'bal' => $bal,
            ]);
            


        }

        $_SESSION['status'] = "Dealers Ledger Added Successfully";
        header('Location: dealer_ledger.php');
        exit();


    }else{


    }
}



if(isset($_POST['add_bill_btn'])){
    $allowed_ext = ['xls','csv','xlsx'];
    $fileName = $_FILES['import_file']['name'];
    $checking = explode(".",$fileName);

    $file_ext = end($checking);
    

    if(in_array($file_ext,$allowed_ext)){


        $targetPath = $_FILES['import_file']['tmp_name'];

        /** Load $inputFileName to a Spreadsheet object **/
        $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($targetPath);
        $data  = $spreadsheet->getActiveSheet()->toArray();

        foreach($data as $row){
            
            
            $number = "+91".$row['0'];
            $plno = $row['1'];
            $date = $row['2'];
            $itmnm = $row['3'];
            $bund = $row['4'];
            $den = $row['5'];
            $stmp = $row['6'];
            $size = $row['7'];
            $thk =  $row['8'];
            $sht =  $row['9'];
            $rmrk =  $row['10'];
            $colr =  $row['11'];
            $mm =  $row['12'];
            $mtr =  $row['13'];
            $wgt =  $row['14'];

            $query = $database->getReference('Dealers_Packing/')->push()->set([
                'number' => $number,
                'pl_no' => $plno,
                'date' => $date,
                'itmnm' => $itmnm,
                'bund' => $bund,
                'den' => $den,
                'stmp' => $stmp,
                'size' => $size,
                'thk' => $thk,
                'sht' => $sht,
                'rmrk' => $rmrk,
                'colr' => $colr,
                'mm' => $mm,
                'mtr' => $mtr,
                'wgt' => $wgt,
            ]);
            


        }

        $_SESSION['status'] = "Dealers Packing Added Successfully";
        header('Location: dealer_bill.php');
        exit();


    }else{


    }
}

if(isset($_POST['add_dealer_btn'])){
    
      $name = $_POST['name'];
    $number = $_POST['number'];
    $state = $_POST['state'];
    $city = $_POST['city'];
    $email = $_POST['email'];
    $gst = $_POST['gst'];
    $address = $_POST['address'];
    
    $query = $database->getReference('Dealers/')->push()->set([
        'name' => $name,
        'number' => $number,
        'state' => $state,
        'city' => $city,
        'email' => $email,
        'gst' => $gst,
        'address' => $address,
        'created_at' =>  ['.sv' => 'timestamp'],
    ]);
    if($query){
        $_SESSION['status'] = "Dealer Added Successfully";
        header('Location: dealer_account.php');
        exit();
    }else{
        $_SESSION['status'] = "Dealer Not Added Successfully";
        header('Location: dealer_account.php');
        exit();
    }
    
}

if(isset($_POST['add_mattress_btn'])){
    $name = $_POST['name'];
    $model_no = $_POST['model_no'];
    
    $query = $database->getReference('Mattress/')->push()->set([
        'name' => $name,
        'model_no' => $model_no,
        'created_at' =>  ['.sv' => 'timestamp'],
    ]);
    if($query){
        $_SESSION['status'] = "Mattress Added Successfully";
        header('Location: mattress.php');
        exit();
    }else{
        $_SESSION['status'] = "Mattress Not Added Successfully";
        header('Location: mattress.php');
        exit();
    }
}

if(isset($_POST['add_mattress_pricing_btn'])){
    
    $mattress = $_POST['mattress'];
    $thickness = $_POST['thickness'];
    $size = $_POST['size'];
    $warrenty = $_POST['warrenty'];
    $price = $_POST['price'];
    
    
    $query = $database->getReference('MattressPricing/')->push()->set([
        'mattress' => $mattress,
        'thickness' => $thickness,
        'size' => $size,
        'warrenty' => $warrenty,
        'price' => $price,
        'created_at' =>  ['.sv' => 'timestamp'],
    ]);
    
    if($query){
        $_SESSION['status'] = "Mattress Added Successfully";
        header('Location: mattress_pricing.php');
        exit();
    }else{
        $_SESSION['status'] = "Mattress Not Added Successfully";
        header('Location: mattress_pricing.php');
        exit();
    }
}

if(isset($_POST['update_mattress_pricing_btn'])){
    $id = $_POST['update_mattress_pricing_btn'];
    
    $mattress = $_POST['mattress'];
    $thickness = $_POST['thickness'];
    $size = $_POST['size'];
    $warrenty = $_POST['warrenty'];
    $price = $_POST['price'];
    
    
    $query = $database->getReference("MattressPricing/$id/")->set([
        'mattress' => $mattress,
        'thickness' => $thickness,
        'size' => $size,
        'warrenty' => $warrenty,
        'price' => $price,
        'created_at' =>  ['.sv' => 'timestamp'],
    ]);
    
    if($query){
        $_SESSION['status'] = "Mattress Updated Successfully";
        header('Location: mattress_pricing.php');
        exit();
    }else{
        $_SESSION['status'] = "Mattress Not Updated Successfully";
        header('Location: mattress_pricing.php');
        exit();
    }
}


if(isset($_POST['add_qrcode_answer_btn'])){
    
    $answers = $_POST['answers'];
     $mattress_name = $_POST['mattress_name'];
     $mattress_name_2 = $_POST['mattress_name_2'];
     $mattress_name_3 = $_POST['mattress_name_3'];
    
    
    $query = $database->getReference("QR Code Answers/$answers/")->set([
        'answers' => $answers,
        'mattress_name' => $mattress_name,
        'mattress_name_2' => $mattress_name_2,
        'mattress_name_3' => $mattress_name_3,
    ]);
    
    if($query){
        $_SESSION['status'] = "QR Code Answer Added Successfully";
        header('Location: qrcode_answers.php');
        exit();
    }else{
        $_SESSION['status'] = "QR Code Answer Not Added Successfully";
        header('Location: qrcode_answers.php');
        exit();
    }
}

if(isset($_POST['update_qrcode_answer_btn'])){
    
    $id = $_POST['update_qrcode_answer_btn'];
    
   $answers = $_POST['answers'];
   $mattress_name = $_POST['mattress_name'];
    $mattress_name_2 = $_POST['mattress_name_2'];
     $mattress_name_3 = $_POST['mattress_name_3'];
    
    
    $query = $database->getReference("QR Code Answers/$id/")->set([
        'answers' => $answers,
        'mattress_name' => $mattress_name,
         'mattress_name_2' => $mattress_name_2,
        'mattress_name_3' => $mattress_name_3,
    ]);
    
     if($query){
        $_SESSION['status'] = "QR Code Answer Added Successfully";
        header('Location: qrcode_answers.php');
        exit();
    }else{
        $_SESSION['status'] = "QR Code Answer Not Added Successfully";
        header('Location: qrcode_answers.php');
        exit();
    }
}

if(isset($_POST['update_mattress_btn'])){
    $id = $_POST['update_mattress_btn'];
     $name = $_POST['name'];
    $model_no = $_POST['model_no'];
    
    $query = $database->getReference('Mattress/'.$id.'/')->update([
        'name' => $name,
        'model_no' => $model_no,
    ]);
    if($query){
        $_SESSION['status'] = "Mattress Updated Successfully";
        header('Location: mattress.php');
        exit();
    }else{
        $_SESSION['status'] = "Mattress Not Updated Successfully";
        header('Location: mattress.php');
        exit();
    }
}

if(isset($_POST['add_sales_man_btn'])){
    $name = $_POST['name'];
    $number = $_POST['number'];
    $state = $_POST['state'];
    $city = $_POST['city'];
    $email = $_POST['email'];
    $dealers = $_POST['dealers'];
    $address = $_POST['address'];
    $dealers = implode(',', $dealers);
    
    $query = $database->getReference('SalesAccount/')->push()->set([
        'name' => $name,
        'number' => $number,
        'state' => $state,
        'city' => $city,
        'email' => $email,
        'dealers' => $dealers,
        'address' => $address,
        'created_at' =>  ['.sv' => 'timestamp'],
    ]);
    if($query){
        $_SESSION['status'] = "SalesMan Added Successfully";
        header('Location: sales_account.php');
        exit();
    }else{
        $_SESSION['status'] = "SalesMan Not Added Successfully";
        header('Location: sales_account.php');
        exit();
    }
}
if(isset($_POST['update_sales_man_btn'])){
    $id = $_POST['update_sales_man_btn'];
    $name = $_POST['name'];
    $number = $_POST['number'];
    $state = $_POST['state'];
    $city = $_POST['city'];
    $email = $_POST['email'];
    $dealers = $_POST['dealers'];
    $address = $_POST['address'];
    $dealers = implode(',', $dealers);
    
    $query = $database->getReference('SalesAccount/'.$id.'/')->update([
        'name' => $name,
        'number' => $number,
        'state' => $state,
        'city' => $city,
        'email' => $email,
        'dealers' => $dealers,
        'address' => $address,
    ]);
    if($query){
        $_SESSION['status'] = "SalesMan Updated Successfully";
        header('Location: sales_account.php');
        exit();
    }else{
        $_SESSION['status'] = "SalesMan Not Updated Successfully";
        header('Location: sales_account.php');
        exit();
    }
}

if(isset($_POST['delet_qr_code'])){
    
    $id = $_POST['id'];
     $query = $database->getReference('QRSheet/'.$id.'/')->remove();
            
        $_SESSION['status'] = "QRCode Deleted Successfully";
        header('Location: qrcode.php');
        exit();
    
}

if(isset($_POST['update_news_btn'])){
    $news  = $_POST['news'];

    $query = $database->getReference('Floating News/')->set($news);
    if($query){
        $_SESSION['status'] = "Floating News Updated Successfully";
        header('Location: floating_news.php');
        exit();
    }else{
        $_SESSION['status'] = "Floating News Not Updated";
        header('Location: floating_news.php');
        exit();
    }

}

if(isset($_POST['add_pdf_btn'])){
    $fileName = $_FILES['import_file']['name'];
    $query = $database->getReference('PDF/')->set($fileName);
    $bucket->upload(
        file_get_contents($_FILES['import_file']['tmp_name'],'pdf/'),
        [
            'name' => 'pdf/'.$_FILES['import_file']['name'],
        ]
    );
    $_SESSION['status'] = "PDF Uploaded Successfully";
        header('Location: pdf.php');
        exit();
}

if(isset($_POST['add_packing_slip_btn'])){
     $packing_slip = $_FILES['packing_slip']['name'];
     $bill = $_FILES['bill']['name'];
     $id = $_POST['add_packing_slip_btn'];
      $query = $database->getReference('DealersData/'.$id.'/')->set([ 'packing_slip' => $packing_slip,'bill' => $bill]);
        $bucket->upload(
            file_get_contents($_FILES['bill']['tmp_name'],'dealer_bill/'.$id.'/'),
            [
                'name' => 'dealer_bill/'.$id.'/'.$_FILES['bill']['name'],
            ]
        );
       $bucket->upload(
        file_get_contents($_FILES['packing_slip']['tmp_name'],'dealer_packing_slip/'.$id.'/'),
        [
            'name' => 'dealer_packing_slip/'.$id.'/'.$_FILES['packing_slip']['name'],
        ]
    );
    $_SESSION['status'] = "Packing Slip And Bill Uploaded Successfully";
        header('Location: dealer_account.php');
        exit();
}

if(isset($_POST['update_qr_code'])){
    $select_enable_disable = $_POST['select_enable_disable'];
    $id = $_POST['id'];
     $query = $database->getReference('QRSheet/'.$id.'/')->update([
                'status' => $select_enable_disable,
            ]);
            
        $_SESSION['status'] = "QRCode Updated Successfully";
        header('Location: qrcode.php');
        exit();
}

if(isset($_POST['update_warranty_status'])){
    $id = $_POST['update_warranty_status'];
    $status = $_POST['status'];
    
    $query = $database->getReference('Warranty Register/'.$id.'/')->update([
                'status' => $status,
            ]);
            
    if($status == 'Approved'){
        
         $ref = "Warranty Register/$id/";
        $fetchdata = $database->getReference($ref)->getValue();
        
        if(isset($fetchdata['uid'])){
           
            $uid = $fetchdata['uid'];
            $ref1 = "RSA/$uid/";
            $fetchdata1 = $database->getReference($ref1)->getValue();
           
            $current_point = $fetchdata1['point'];
            
            $serial_no = $fetchdata['serial_no'];
             $ref2 = "MRPSticker/$serial_no/";
            $fetchdata2 = $database->getReference($ref2)->getValue();
            
            $mattress_pricing = $fetchdata2['mattress_pricing'];
            $ref3 = "MattressPricing/$mattress_pricing/";
            $fetchdata3 = $database->getReference($ref3)->getValue();
            
            $mattress_price = $fetchdata3['price'];
            $new_point = ($mattress_price/100);
            $current_point = $current_point+$new_point;
            
             $query = $database->getReference('RSA/'.$uid.'/')->update([
                'point' => $current_point,
            ]);
            
            
        }
                        
    }        
            
        $_SESSION['status'] = "Warrenty Status Updated Successfully";
        header('Location: warranty_register.php');
        exit();
}

if(isset($_POST['add_mrp_btn'])){
    $dealer = $_POST['dealer'];
    $mattress_pricing = $_POST['mattress_pricing'];
    
   
    $quantity = $_POST['quantity'];
    $array = array();
    for ($x = 1; $x <= $quantity; $x++) {
        $random_number = rand(100000000,1000000000);
       
        $query = $database->getReference("MRPSticker/$random_number/")->set([
            'dealer' => $dealer,
            'mattress_pricing' => $mattress_pricing,
            'serial_number' => $random_number,
        ]);
        array_push($array,$query->getKey());
    }
    
    $_SESSION['seesion_ids'] = $array;
    
   
    
    if($query){
        $_SESSION['status'] = "MRP Sticker Added Successfully";
        header('Location: print_mrp.php');
        exit();
    }else{
        $_SESSION['status'] = "MRP Sticker Not Added Successfully";
        header('Location: print_mrp.php');
        exit();
    }
}

if(isset($_POST['warranty_register_btn_by_customer'])){
    echo('123');
}



?>