<?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 == 'templatelist.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']=='change_status' && isset($_REQUEST['template_id'])){
    if(isset($_REQUEST['status'])){
        $templateDetail = $admin_notificationtemplateObj->fromId($_REQUEST['template_id']);

        if($_REQUEST['status'] == '1')
        {
            $fields = array('template_type','count(admin_notificationtemplate_id) as templatcountact','isactive');
            $where = array('template_type='=>$templateDetail->template_type,'isactive='=>'1');
            $templatearg = array('fields'=>$fields,'where'=>$where);
            $templatecheck_act = $admin_notificationtemplateObj->fromFields($templatearg);
            $tempactcout = $templatecheck_act->templatcountact;
        }else{
            $tempactcout = '0';
        }

        
        if($tempactcout == '0'|| $_REQUEST['status'] == '0')
        {
            
            if($templateDetail->isactive == '1')
            {
                $old_status = 'Active';
            }else{
                $old_status = 'Inactive';
            }

            if($_REQUEST['status'] == '1')
            {
                $template_status = '1';
                $new_status = 'Active';
            }else{
                $template_status = '0';
                $new_status = 'Inactive';
            }
        
            $id=$_REQUEST['template_id'];
            $data['isactive'] = $template_status;
            $rs=$admin_notificationtemplateObj->updateRecords($data,$id);

            $templateDetail = " <b>Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
            
            $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$templateDetail);
            echo "successfully";
            echo RECORD_STATUS;
        }else{
            echo "already";
        }
    }
    return true;
}

if(isset($_REQUEST['action']) && $_REQUEST['action']!=''){
    if($_REQUEST['action']=="activate_all"){
        $data=array("isactive" => '1');

        $chk_data = $_POST['check'];
        foreach($chk_data as $akey => $val_actchk)
        {
            $old_data = $admin_notificationtemplateObj->fromId($val_actchk);
            if($old_data->isactive == '1')
            {
                $old_status = 'Active';
            }else{
                $old_status = 'Inactive';
            }

            if($old_data->isactive != '1')
            {
                $new_status = 'Active';
                $templatedata = " <b>Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$templatedata);
            }
        }
        $rs = $admin_notificationtemplateObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_ACTIVE);
    }else if($_REQUEST['action']=="inactivate_all"){
        $data=array("isactive" => '0');
        $chk_data = $_POST['check'];
        foreach($chk_data as $akey => $val_actchk)
        {
            $old_data = $admin_notificationtemplateObj->fromId($val_actchk);
            if($old_data->isactive == '1')
            {
                $old_status = 'Active';
            }else{
                $old_status = 'Inactive';
            }

            if($old_data->isactive != '0')
            {
                $new_status = 'Inactive';
                $templatedata = " <b>Status</b>: "." ".$old_status." <b>To</b> ".$new_status." ";
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$templatedata);
            }
        }
        $rs = $admin_notificationtemplateObj->updateManyRecords($data, $_POST['check']);
        $successMsg = base64_encode(RECORD_ALL_INACTIVE);
    }
}

if (isset($_REQUEST['act']) && $_REQUEST['act']=='load'){
    $records = $_REQUEST['length'];
    $start=0;
    if(isset($_GET['start']) && $_GET['start']!='0'){
        $page = $_GET['start'];
    }
    $start=($page-1)*$limit;
    $limit = array('start'=>$_GET['start'],'record'=>$records);
    
    $where = " 1=1 ";

    if(isset($_REQUEST['template_name']) && $_REQUEST['template_name'] != '')
    {
        //$where .= " AND template_type LIKE '%".$_REQUEST['template_name']."%'";

        $templatename = preg_replace('/\s\s+/', ' ', $_REQUEST['template_name']);
        $fullname = explode(" ", $templatename);
        if(count($fullname)==2){
            $where .=" AND ((template_type='".$fullname['0']."' AND  template_type='".$fullname['1']."' ) OR (template_type='".$fullname['0']."' AND  template_type='".$fullname['1']."' ) OR (template_type='".ucfirst($fullname['0'])."' AND  template_type='".ucfirst($fullname['1'])."' ) OR (template_type='".ucfirst($fullname['0'])."' AND  template_type='".ucfirst($fullname['1'])."' ) )";
        }else{
            foreach ($fullname as $key => $f) {
                if($key==0){
                    $where .=" AND ( ";
                }
                $where .=" template_type LIKE '%".$f."%' OR template_type LIKE '%".ucfirst($f)."%'  OR template_type LIKE '%".$f."%' OR template_type LIKE '%".ucfirst($f)."%' ";
                if( ($key+1) != count($fullname) ){
                        $where .=" OR ";
                }
                if( ($key+1) == count($fullname) ){
                    $where .=" )";
                }
            }
        }
    }

    if(isset($_REQUEST['template_subject']) && $_REQUEST['template_subject'] != '')
    {
        $where .= " AND subject = '".$_REQUEST['template_subject']."'";
    }

    if(isset($_REQUEST['template_status']) && $_REQUEST['template_status'] != '')
    {
        $where .= " AND isactive = '".$_REQUEST['template_status']."'";
    }

    if (isset($_REQUEST['search']['value']) && $_REQUEST['search']['value']!=""){
        $where = " template_type LIKE '%".$_REQUEST['search']['value']."%' OR subject LIKE '%".$_REQUEST['search']['value']."%' OR admin_notificationtemplate_id LIKE '%".$_REQUEST['search']['value']."%' ";
    } 

    $orderFields = array('','admin_notificationtemplate_id','template_type','subject','','');
    
    // Array of order by 
    $orderby=array();
    if (isset($_REQUEST['order']['0']['column']) && $_REQUEST['order']['0']['column']!=""){
        $orderby = array(" ".$orderFields[$_REQUEST['order']['0']['column']]." ".$_REQUEST['order']['0']['dir']);
    }else{
        $orderby = array(" admin_notificationtemplate_id desc ");
    }
    //print_r($orderby);exit;
    $data    = array('where' => $where,'orderBy'=>$orderby,'limit' => $limit);

    $templateDetails = $admin_notificationtemplateObj->getAllRecords($data);

    $recordsTotal   =   $admin_notificationtemplateObj->getAllCount($data);

        
    if ($templateDetails && count($templateDetails)>0){
        foreach ($templateDetails as $key => $templatedata)
        {

            if($templatedata->isactive == '1'){
              $status = '<a href="javascript:void(0)" onclick="change_status('.$templatedata->admin_notificationtemplate_id.',0)" title="Inactivate"><span class="badge badge-success">Active</span></a>';
            }else{
              $status = '<a href="javascript:void(0)" onclick="change_status('.$templatedata->admin_notificationtemplate_id.',1)" title="Activate"><span class="badge badge-danger">Inactive</span></a>';
            }
            if($templatedata->template_type == 'digitaldoc')
            {
                $templateName = 'Digital Doc Upload From Patient';
            }else if($templatedata->template_type == 'referral_sender'){
                $templateName = 'Referral Sender';
            }else if($templatedata->template_type == 'referral_receiver'){
                $templateName = 'Referral Receiver';
            }else if($templatedata->template_type == 'referral_success_sender'){
                $templateName = 'Referral Success Sender';
            }else if($templatedata->template_type == 'referral_success_receiver'){
                $templateName = 'Referral Success Receiver';
            }else{
                $templateName = $templatedata->template_type;
            }

            $action = '
                 <a href="templateadd.php?act=edit&template_id='.$templatedata->admin_notificationtemplate_id.'" class="btn btn-success white-btn"><i class="nav-icon i-Pen-2 "></i></a>';

            $response[] = array('<input type="checkbox" name="check[]" id="check[]" value="'.$templatedata->admin_notificationtemplate_id.'" class="check_class" />',
                    $templatedata->admin_notificationtemplate_id,
                    $templateName,
                    $templatedata->subject,
                    "<div id='td_status_".$templatedata->admin_notificationtemplate_id."'>".$status."</div>",  
                    $action,
             );
        }
        $result=array('recordsFiltered'=>$recordsTotal,'recordsTotal'=>$recordsTotal,'data'=>$response);
    }else{
        $result=array('recordsFiltered'=>0,'recordsTotal'=>0,'data'=>array());
    }
    echo json_encode($result);
    exit;
}

// Include Template 
include_once('../libs/routs.php');
?>