<?php
// Include main file
include_once('../libs/main.php');
// Check admin Login
include_once('checkLogin.php');
// Logical section of page
$practiceObj =  new  practice();
$adminlogObj = new admin_log();
$modulepagesObj = new module_pages();
$filename=basename($_SERVER['SCRIPT_FILENAME']);

if($filename == 'practice_edit.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['practice_id'])){
        $practiceDetail = $practiceObj->fromId($_REQUEST['practice_id']);
        //print_r($practiceDetail);exit;

        if($practiceDetail){
            $practiceXcareId = $practiceDetail->practice_xcare_id;
            $clinicObj = new clinic();

            $data = array(
                'fields' => array('clinic_id','clinic_name','pmsOnboardDate'),
                'where'  => "clinic_id = '".$practiceXcareId."'",
                'limit'  => array('start'=>0,'record'=>1)
            );

            $result = $clinicObj->getAllRecords($data);

            if ($result) {
                $clinicDetails = $result[0];
                $practiceDetail->clinic_name = $clinicDetails->clinic_name;
                $practiceDetail->pmsOnboardDate = $clinicDetails->pmsOnboardDate;
            }
        }

    }

if(isset($_REQUEST['act']) && $_REQUEST['act']=='getdata' && isset($_REQUEST['practice_id'])){
    $practiceDetail = $practiceObj->fromId($_REQUEST['practice_id']);
    echo json_encode($practiceDetail);
    exit;
    
}

if(isset($_REQUEST['act']) && $_REQUEST['act']=='update_sync_fields' && isset($_REQUEST['practice_id'])){
    $id = $_REQUEST['practice_id'];
    $fields = isset($_REQUEST['practice_uptodatefields']) ? implode(',', $_REQUEST['practice_uptodatefields']) : '';
    
    $data = array(
        'practice_uptodatefields' => $fields,
        'practice_uptodateflag' => 1
    );

    $rs = $practiceObj->updateRecords($data, $id);
    if($rs){
        echo 'success';
    } else {
        echo 'failed';
    }
    exit;
}

//if(isset($_POST['submit'])){
if(isset($_REQUEST['act']) && $_REQUEST['act']=='load'){    

    $data=array(
        'practice_name'=>trim($_POST['practice_name']),
        'practice_xcare_id'=>trim($_POST['xcare_id']),
        'practice_pullGuarantors'=>trim($_POST['pullguarantors_name']),
        'practice_pullPatients'=>trim($_POST['PullPatients_name']),
        'practice_pullAppointments'=>trim($_POST['PullAppointments_name']),
        'practice_interval'=>trim($_POST['practice_interval']),
    );
    
    if (isset($_POST['practice_id'])){
        $id=$_POST['practice_id'];

        $practice_detail = 'Edit Practice Old';
        $practiceDetailLog = $practiceObj->fromId($_POST['practice_id']);
        if($practiceDetailLog->practice_name != $_POST['practice_name'])
        {
            $practice_detail .= " <b>Practice name<b/>: "." ".$practiceDetailLog->practice_name." <b>To</b> ".$_POST['practice_name']." and ";
            
            $chages_data = true;
        }
        if($practiceDetailLog->practice_xcare_id != $_POST['xcare_id'])
        {
            $clinicObj = new clinic();
            $oldclinicDetailLog = $clinicObj->fromId($practiceDetailLog->practice_xcare_id);
            $newclinicDetailLog = $clinicObj->fromId($_POST['xcare_id']);
            $practice_detail .= " <b>Practice xcare_id</b>: "." ".$oldclinicDetailLog->clinic_name." <b>To</b> ".$newclinicDetailLog->clinic_name." and ";
            
            $chages_data = true;
        }
        if($practiceDetailLog->practice_pullGuarantors != $_POST['pullguarantors_name'])
        {
            $practice_detail .= " <b>Practice pullguarantors name</b>: "." ".$practiceDetailLog->practice_pullGuarantors." <b>To</b> ".$_POST['pullguarantors_name']." and ";
            
            $chages_data = true;
        }
        if($practiceDetailLog->practice_pullPatients != $_POST['PullPatients_name'])
        {
            $practice_detail .= " <b>Practice PullPatients name</b>: "." ".$practiceDetailLog->practice_pullPatients." <b>To</b> ".$_POST['PullPatients_name']." and ";
            
            $chages_data = true;
        }
        if($practiceDetailLog->practice_pullAppointments != $_POST['PullAppointments_name'])
        {
            $practice_detail .= " <b>Practice pullAppointments name</b>: "." ".$practiceDetailLog->practice_pullAppointments." <b>To</b> ".$_POST['PullAppointments_name']." and ";
            
            $chages_data = true;
        }
        if(isset($_POST['xcare_id']) && $_POST['xcare_id'] != '')
        {
            $clinicObj = new clinic();
            $clinic_data=array(
                'pmsOnboardDate'=>trim($_POST['pms_date']),
            );
            
            $rs_clinic=$clinicObj->updateRecords($clinic_data,$_POST['xcare_id']);
        }

        $rs=$practiceObj->updateRecords($data,$id);
        if ($rs){

            if($chages_data == true)
            {
                $rpractice_detail = substr($practice_detail, 0, -4);
                $admin_log = $adminlogObj->addAdminLog($_SESSION['admin_id'],$module_id,$page_id,$pagename,$rpractice_detail);
            }
            echo 'success';
            $successMsg = RECORD_UPDATE;			
            //header('Location:practicelist.php?successMsg='.$successMsg);
            //header('Location:practicelist.php?act=edit&practice_id='.$id);
            exit;
        }
    }
}

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