<?php
    session_start();
    if (!isset($_SESSION['username'])) {
        $_SESSION['login_msg'] = "Please login first to continue.";
        header("Location: login.php");
        exit();
    }
    
    require_once('include/config.php');
    require_once('include/startup.php');                           
    $alltables = new AllTables();
    
    include('include/header.php');  
    
    // Add Correspondence Address
    if ($_POST['btn-submit']) 
    {
        
        $add_srevice                = $_POST['txt_service'];
        $add_conn_type              = $_POST['txt_conn_type'];
        $add_quantity               = $_POST['txt_quantity'];
        $add_amount                 = $_POST['txt_amount'];
        
        
        $array['cmp_id']            = $G_companyid;     
        $array['service_id']        = $add_srevice;     
        $array['request']           = $add_conn_type;     
        $array['quantity']          = $add_quantity;
        $array['amount']            = $add_amount;
        $array['status']            = 1;
                                
            
        $service = $alltables -> AddInfo($array,'service_requests'); 
        // echo "Last insetr id:".$address;
        // exit();   
        
        if($service){
            $redirect   = $_POST['redirect_url'];
            echo "<script>alert('Service Request sent successfully!');window.location.href = '$redirect';</script>";
            exit();
        }
        else{
            $redirect   = $_POST['redirect_url'];
            echo "<script>alert('Service Request not sent, please try again. ');window.location.href = '$redirect';</script>";
            exit();
        }
    }  
    else{
                                
    }
    
    // Add Correspondence Address
    if ($_POST['btn-edit']) 
    {
      
        $edit_id                    = $_POST['txt_id'];
        $edit_srevice               = $_POST['txt_service'];
        $edit_conn_type             = $_POST['txt_conn_type'];
        $edit_quantity              = $_POST['txt_quantity'];
        $edit_amount                = $_POST['txt_amount'];
        
        
        $array['cmp_id']            = $G_companyid;     
        $array['service_id']        = $edit_srevice;     
        $array['request']           = $edit_conn_type;     
        $array['quantity']          = $edit_quantity;
        $array['amount']            = $edit_amount;

        $wherecondition = "id = $edit_id";
        
        $result = $alltables -> EditInfo($array,$wherecondition,'service_requests');   
        // $result = $alltables -> EditInfo($array,$wherecondition,'service_requests',1);    // To print query
        if($result){
            echo '<script>alert("Service Request Edited successfully!");window.location.href = "air-connection.php";</script>';
            exit();
        }
        else{
            echo '<script>alert("Service Request not Edited, please try again. ");window.location.href = "air-connection.php";</script>';
            exit();
        }
    }  
    else{
                                
    }
    
    include('include/footer.php');         
?>    