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

$filename=basename($_SERVER['SCRIPT_FILENAME']);

if($filename == 'clinic_setting.php')
{
    $module_id = $adminlogObj->getModuleId($filename);
    $page_id = $adminlogObj->getPageId($filename);
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='setting' && isset($_REQUEST['clinic_id'])){
    $clinicDetail = $clinicObj->fromId($_REQUEST['clinic_id']);
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='communicationprovider' && isset($_REQUEST['clinic_id'])){
    $clinicproviderDetail = $clinicObj->fromId($_REQUEST['clinic_id']);
    if(!empty($clinicproviderDetail)){
        $clinicsettingData = json_decode($clinicproviderDetail->clinic_setting,1);
        
        if(isset($_REQUEST['communicationprovider']) && $_REQUEST['communicationprovider'] != '')
        {
            $clinicsettingData['communicationprovider']=$_REQUEST['communicationprovider'];    
        }else{
            unset($clinicsettingData['communicationprovider']);
        }
        
        $settingData['clinic_setting'] = json_encode($clinicsettingData);
        
        $clinicseting = $clinicObj->updateRecords($settingData,$_REQUEST['clinic_id']);
        if($clinicseting)
        {
            header('Location:cliniclist.php');
            exit;
        }
    }
}

if(isset($_POST['submit'])){
    $field_settingvalues1 = $_POST['field_setting1'];
    $field_settingvalues2 = $_POST['field_setting2'];
    if(!empty($field_settingvalues1) && !empty($field_settingvalues2))
    {
    $clinicsetting_details = array();
        foreach ($field_settingvalues1 as $skey => $settingkey) 
        {        
            if(isset($settingkey) && $settingkey != '' && $field_settingvalues2[$skey] != '')
            {
                $clinicsetting_details[$settingkey] = $field_settingvalues2[$skey];
            }
        }
    
        $all_setting = json_encode($clinicsetting_details);
        $data=array(
            'clinic_setting'=>trim($all_setting),
        );
    }    
    $oldclinicDetail = $clinicObj->fromId($_POST['clinic_id']);
    //print_r($oldclinicDetail->clinic_setting);exit;
    $clinic_detail = " <b>Setting data</b>: ".$oldclinicDetail->clinic_setting." to ".$all_setting;

    //echo $_POST['clinic_id'];exit;
    if (isset($_POST['clinic_id'])){    
        $id=$_POST['clinic_id'];
        //print_r($data);exit;
        $rs=$clinicObj->updateRecords($data,$id);
        if ($rs){

             $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$clinic_detail);
            $successMsg = RECORD_UPDATE;			
            //header('Location:practicelist.php?successMsg='.$successMsg);
            header('Location:cliniclist.php');
            exit;
        }
    }
}


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