<?php
// assignPointToClinic.php

// Include main file
include_once('../libs/main.php');
include_once('checkLogin.php');

$agencyClinicPointObj = new agencyclinic_point(); 
$clinicObj = new clinic();

// Handle form submission
if (isset($_POST['submit']) && $_POST['submit'] == 'Submit') {
    $resStatus = ['error' => false, 'msg' => "Points assigned successfully."];
    
    // Validate required fields
    if (empty($_POST['clinic_id']) || empty($_POST['plan_id'])) {
        $resStatus['error'] = true;
        $resStatus['msg'] = "Please fill all required fields.";
    } else {
        // Get plan details from clinicloyaltyPlans array
        $selectedPlan = null;
        foreach ($clinicloyaltyPlans as $plan) {
            if ($plan['id'] == $_POST['plan_id']) {
                $selectedPlan = $plan;
                break;
            }
        }
        
        if (!$selectedPlan) {
            $resStatus['error'] = true;
            $resStatus['msg'] = "Invalid plan selected.";
        } else {
            // Prepare data for insertion
            $pointData = array(
                "clinic_id" => $_POST['clinic_id'],
                "points" => $selectedPlan['totalPoints'],
                "amount" => $selectedPlan['amount'],
                "type" => "Credited",
                "admin_id" => $_SESSION['admin_id'],
                "description" => $_POST['description'],
                "created_at" => date("Y-m-d H:i:s")
            );
            
            // Insert the record
            $rs = $agencyClinicPointObj->addRecords($pointData);
            if ($rs) {
                $clinicObj =new clinic();
                $clinicDetails =$clinicObj->fromId($_POST['clinic_id']);
                $userObj=  new user();
                $userDetails =$userObj->fromId($clinicDetails->clinic_user_id);
                
                // Send email notification to clinic
                if (isset($userDetails->user_email_address) && $userDetails->user_email_address != '') {
                    // Prepare email data
                    $emailData = array(
                        "name" => $userDetails->user_fname,
                        "desc" => $pointData['pointData'],
                        "points" => $selectedPlan['totalPoints']
                    );
                    
                    // Get email template
                    $emailContent = emailTemplates::directReferralPoint($emailData);
                    $subject = emailTemplates::subject('directReferralPoint');
                    $fromEmail = "no-reply@xcarepro.com";
                    $fromName = "Xcare Team";
                    
                    sendMail($fromEmail, $fromName, $userDetails->user_email_address, $userDetails->user_fname, $subject, $emailContent);
                }
                
                header('Location:assignPointToClinic.php');
                exit;
            } else {
                header('Location:assignPointToClinic.php');
                exit;
            }
        }
    }
}

// Get all clinics for dropdown
$clinicFields = array('clinic_id', 'clinic_name');
$clinicArg = array("fields" => $clinicFields);
$clinicData = $clinicObj->getAllRecords($clinicArg);

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