<?php 
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$admin_notificationtemplateObj =  new  admin_notificationtemplate();
$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();
$filename=basename($_SERVER['SCRIPT_FILENAME']);
if($filename == 'templateadd.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['template_id'])){
    $templateDetail = $admin_notificationtemplateObj->fromId($_REQUEST['template_id']);
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='load' && isset($_REQUEST['template_name']) && $_REQUEST['template_name'] != "")
{

    if(isset($_POST['template_sms_content_data']) && $_POST['template_sms_content_data'] != '')
    {
        $sms_content = $_POST['template_sms_content_data'];
    }else{
        $sms_content = null;
    }
    
    $data=array(
        'template_type' => trim($_POST['template_name']),
        'subject'  => trim($_POST['template_subject_line']),
        'body'  => $_POST['template_content_data'],
        'isactive'  => '1',
        'smstext' => $sms_content,
    );

    
    if (isset($_REQUEST['template_id']) && $_REQUEST['template_id'] != '')
    {
    
        $id=$_REQUEST['template_id'];
        $temp_detail = $admin_notificationtemplateObj->fromId($_REQUEST['template_id']);

        if($temp_detail->template_type != $_POST['template_name'])
        {
            $tempdetail .= " <b>Template name</b>: "." ".$temp_detail->template_name." <b>To</b> ".$_POST['template_name']." and ";
            
            $chages_data = true;
        }

        if($temp_detail->subject != $_POST['template_subject_line'])
        {
            $tempdetail .= " <b>Template Subject</b>: "." ".$temp_detail->subject." <b>To</b> ".$_POST['template_subject_line']." and ";
            
            $chages_data = true;
        }

        if($temp_detail->body != $_POST['template_content_data'])
        {
            $tempdetail .= " <b>Template Body</b>: "." ".$temp_detail->body." <b>To</b> ".$_POST['template_content_data']." and ";
            
            $chages_data = true;
        }
        
        $rs=$admin_notificationtemplateObj->updateRecords($data,$id);
        if ($rs){
            
            if($chages_data == true)
            {
                $rtempdetail = substr($tempdetail, 0, -4);
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$rtempdetail);
            }
            header('Location:templatelist.php');
            exit;
        }
        
        
    }else{
        
        $template_add  = $admin_notificationtemplateObj->addRecords($data);    
        
        if($template_add){

            $clinic_detail = " <b>Template Name</b>: ".$_POST['template_name']." <b>and subject</b>: ".$_POST['template_subject_line']." <b>and body</b>: ".$_POST['template_content_data'];
            $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$clinic_detail);
            header('Location:templatelist.php');
            exit;
        }
    }
}


$fields = array('template_type');
$where = "isactive='1' AND isdeleted='0' ";
$tempArg    = array('fields' => $fields,'where' => $where);
$tempDetails = $admin_notificationtemplateObj->getAllRecords($tempArg);

$tempdata = array();
foreach($tempDetails as $key => $templatedata)
{
    array_push($tempdata,$templatedata->template_type);
}

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

?>