<?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_address                = $_POST['txt_address'];
        $add_country                = $_POST['txt_country'];
        $add_state                  = $_POST['txt_state'];
        $add_city                   = $_POST['txt_city'];
        $add_pincode                = $_POST['txt_pincode'];
        
        
        $array['cmp_id']            = $G_companyid;     
        $array['address']           = $add_address;     
        $array['country']           = $add_country;
        $array['state']             = $add_state;
        $array['city']              = $add_city;
        $array['pincode']           = $add_pincode;
                                
            
        $address = $alltables -> AddInfo($array,'correspondence_address'); 
        // echo "Last insetr id:".$address;
        // exit();   
        
        if($address){
            echo '<script>alert("Address added successfully!");window.location.href = "company-address.php";</script>';
            exit();
        }
        else{
            echo '<script>alert("Address not added, please try again. ");window.location.href = "company-address.php";</script>';
            exit();
        }
    }  
    else{
                                
    }
    
    // Add Correspondence Address
    if ($_POST['btn-edit']) 
    {
      
        $edit_id                     = $_POST['txt_id'];
        $edit_address                = $_POST['txt_address'];
        $edit_country                = $_POST['txt_country'];
        $edit_state                  = $_POST['txt_state'];
        $edit_city                   = $_POST['txt_city'];
        $edit_pincode                = $_POST['txt_pincode'];
        
        
        $array['cmp_id']            = $G_companyid;     
        $array['address']           = $edit_address;     
        $array['country']           = $edit_country;
        $array['state']             = $edit_state;
        $array['city']              = $edit_city;
        $array['pincode']           = $edit_pincode;

        $wherecondition = "id = $edit_id";
        $result = $alltables -> EditInfo($array,$wherecondition,'correspondence_address');   
        // $result = $alltables -> EditInfo($array,$wherecondition,'mst_company',1);    // To print query
        if($result){
            echo '<script>alert("Form edited successfully!");window.location.href = "company-address.php";</script>';
            exit();
        }
        else{
            echo '<script>alert("Form not edited, please try again. ");window.location.href = "company-address.php";</script>';
            exit();
        }
    }  
    else{
                                
    }
    
    include('include/footer.php');         
?>    