<?php 
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$domainObj =  new  domain();
$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();

$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'domainadd.php')
{
    $module_id = $adminlogObj->getModuleId($filename);
    $page_id = $adminlogObj->getPageId($filename);
    $allpages = $modulepagesObj->fromId($page_id);
    $pagename = $allpages->page;

}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='edit' && isset($_REQUEST['domain_id'])){
    $domainDetail = $domainObj->fromId($_REQUEST['domain_id']);
}
if(isset($_POST['submit'])){
    	$data=array(
          'domain_title'=>trim($_POST['domain_title']),
    );
        if(isset($_POST['domain_status'])){
            $data['domain_status'] 			= '1';
        }else{
            $data['domain_status'] 			= '0';
        }
        $domainDetail = $domainObj->fromId($_REQUEST['domain_id']);
        if(!empty($domainDetail))
        {
            $domaindata = "<b>Edit Record</b> ";
            if($domainDetail->domain_title != trim($_POST['domain_title']))
            {
                $domaindata .= " <b>domain Name</b>: "." ".$domainDetail->domain_title." <b>To</b> ".$_POST['domain_title']." and ";
            
                $chages_data = true;
            }

            if($domainDetail->domain_status != $data['domain_status'])
            {
                if($data['domain_status'] == '1')
                {
                    $new_status = 'Active';
                }else{
                    $new_status = 'Inactive';
                }

                if($domainDetail->domain_status == '1')
                {
                    $old_status = 'Active';
                }else{
                    $old_status = 'Inactive';
                }

                $domaindata .= " <b>Status</b>: "." ".$old_status." <b>To</b> ".$new_status." and ";
            
                $chages_data = true;
            }  
        }
        

	if (isset($_POST['domain_id'])){
        $id=$_POST['domain_id'];
        $rs=$domainObj->updateRecords($data,$id);
        if ($rs){

            if($chages_data == true)
            { 
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,rtrim($domaindata," and"));
            }
            $successMsg = base64_encode(RECORD_UPDATE);			
            header('Location:domainlist.php');
            exit;
        }
	}else{
        $data['domain_order'] = $domainObj->getNextorder('domain_order');
        $domaindata = "<b>Add Record</b> ";
        $domaindata.= " <b>Domain Data</b>: ".json_encode($data);
        $rs = $domainObj->addRecords($data);
        if($rs){

            $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$domaindata);
            $successMsg = base64_encode(RECORD_ADD);			
            header('Location:domainlist.php');
            exit;
        }
	}
}

// Include Template 
include_once('../libs/routs.php')

?>