<?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_name                   = $_POST['txt_cp_name'];
        $add_type                   = $_POST['txt_cp_type'];
        $add_email                  = $_POST['txt_cp_email'];
        $add_contactno              = $_POST['txt_cp_contactno'];
        $add_whatsapp               = $_POST['txt_cp_whatsapp'];
        
        
        $array['cmp_id']            = $G_companyid;     
        $array['name']              = $add_name;     
        $array['designation']       = $add_type;
        $array['email']             = $add_email;
        $array['mobile_no']         = $add_contactno;
        $array['whatsapp_no']           = $add_whatsapp;
                                
            
        $address = $alltables -> AddInfo($array,'contact_person'); 
        // echo "Last insetr id:".$address;
        // exit();   
        
        if($address){
            echo '<script>alert("Contact Person added successfully!");window.location.href = "contact-person.php";</script>';
            exit();
        }
        else{
            echo '<script>alert("Contact Person not added, please try again. ");window.location.href = "contact-person.php";</script>';
            exit();
        }
    }  
    else{
                                
    }
    
    // Add Correspondence Address
    if ($_POST['btn-edit']) 
    {
      
        $edit_id                     = $_POST['txt_id'];
        $edit_name                   = $_POST['txt_cp_name'];
        $edit_type                   = $_POST['txt_cp_type'];
        $edit_email                  = $_POST['txt_cp_email'];
        $edit_contactno              = $_POST['txt_cp_contactno'];
        $edit_whatsapp               = $_POST['txt_cp_whatsapp'];
        
        
        $array['cmp_id']            = $G_companyid;     
        $array['name']              = $edit_name;     
        $array['designation']       = $edit_type;
        $array['email']             = $edit_email;
        $array['mobile_no']         = $edit_contactno;
        $array['whatsapp_no']       = $edit_whatsapp;

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