<?php
// file: public/class-cemetery-sexton-interment.php
/**
 * The public-facing functionality of the plugin.
 *
 * @link       https://orbicular.media
 * @since      1.1.1
 *
 * @package    Cemetery_Sexton
 * @subpackage Cemetery_Sexton/public
 */

/**
 * The public-facing functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the public-facing stylesheet and JavaScript.
 *
 * @package    Cemetery_Sexton
 * @subpackage Cemetery_Sexton/public
 * @author     Bryan Meeks <bryan@orbicular.media>
 */
class Cemetery_Sexton_Interment {
    // Get the interment record from the database and return row as array
    public function interment_getInterment($interid) {
        //Get sql query
        $sql = 'SELECT *'
            . ' FROM dbo.vw_ReportFlatView WHERE InterID ='. $interid;
        //Connect to database
        $db = New Cemetery_Sexton_SQLconnect();
        $conn = $db->cemetery_sexton_connect();
        //Execute SQL statement
        $query = sqlsrv_query($conn,$sql);
        //Get the rows of records with associated column name
        //return $query;
        $rsInter = sqlsrv_fetch_array($query,SQLSRV_FETCH_ASSOC);
        return $rsInter;
    }
    // Get the records from the database
    public function interment_getObit($interid) {
        //Connect to database
        $sql = 'SELECT *'
            . ' FROM dbo.Obit WHERE InterID ='. $interid;
        //Connect to database
        $db = New Cemetery_Sexton_SQLconnect();
        $conn = $db->cemetery_sexton_connect();
        //Execute SQL statement
        $query = sqlsrv_query($conn,$sql);
        //Build results multidimensional array
        $obits=array();
        $i=0;
        while($rsObit = sqlsrv_fetch_array($query,SQLSRV_FETCH_ASSOC)){
            $obits[$i]=$rsObit;
            $i++;
        }
        return $obits;
    }
    // Build visual output for obits
    public function interment_displayObits($interid){
        $i = 0;
        $obits = $this->interment_getObit($interid);
        $options = get_option('cemetery-sexton');
        $images = New Cemetery_Sexton_images();
        $output = '';
        if (empty($obits)) {
            $output = 'No obituary available.<br>';
        }else {  
            $num = count($obits);
            if ($images->images_checkImageServer() == True) {
                foreach ($obits as $rsObit){
                    $pic = $options['photoserv_obit'].$rsObit['PicFile'];
                    $name = substr($rsObit['PicFile'],0,-4);

                    $image = $images->images_publishImages($options['photoserv_obit'].$rsObit['PicFile'],$name,'obit');
                    $size = $images->images_getJpegSize($pic);
                    
                    // Determine the size of the modal popup window
                    $maxheight = 700;
                    $maxwidth = 700;
                    $picheight = $size[1];
                    $picwidth = $size[0];
                    $picheight = (int)$picheight;
                    $picwidth = (int)$picwidth;
                    // Height
                    if ($picheight > $maxheight or $picheight == 0 or $picheight == NULL){
                        $modheight = 700;
                        $divheight = 700;
                    }else {
                        $modheight = $picheight+50;
                        $divheight = $picheight+10;
                    }
                    // Width
                    if ($picwidth > $maxwidth or $picwidth == 0 or $picwidth == NULL){
                        $modwidth = 700;
                        $divwidth = 700;
                    }else {
                        $modwidth = $picwidth+60;
                        $divwidth = $picwidth+30;
                    }
                    //give obit photo a number
                    $n = $i+1;
                    if($n == 1){
                        $no = '';
                    }else {
                        $no = $n;
                    }
                    
                    include( plugin_dir_path( __FILE__ ) . 'partials/cemetery-sexton-photo-obit.php' );
                    //$output = '<p><i class="far fa-newspaper"></i> <a class="modal" href="index.php?option=com_cemeterysexton&view=obitmodal&format=raw&img='.$rsObit['PicFile'].'&width='.$divwidth.'&height='.$divheight
                    //.'" rel="{handler: '."'iframe'".', size: {x: '.$modwidth.', y: '.$modheight.'}}">View Obituary'.$no.'</a></p>';
                    $i++;
                }
                //$output .= $output;
    
            }else {
                $output= '<p><i class="far fa-newspaper"></i> There';
                if ($num == 1){
                    $output .= ' is '.$num.' obituary ';
                }else {
                    $output .= ' are '.$num.' obituaries ';
                }
                $output .= 'available for this interment.<br><i class="fas fa-exclamation-circle"></i> Please check back when our Picture Server is available.</p>';
            }
        }
        return $output;
    }
    // Build visual output for FamMon
    public function interment_linkLot($LotID){
        $lotData = new Cemetery_Sexton_Lot();
        $fammons = $lotData->lot_getFammon($LotID);
        
        //print_r($fammons);
        //echo $fammons[0]['LotID'];
        //die;
        
        $options = get_option('cemetery-sexton');
        $images = New Cemetery_Sexton_images();
        $output = '';
        $redirect = esc_html( add_query_arg( 'LotID', $LotID, get_permalink(get_option('cemetery-sexton')['page_lot']) ) );
        if(empty($fammons)){
            $output = '<p>No family memorial photo available for this lot.<br>'
                        .'<a href="'.$redirect.'"><i class="fas fa-vector-square"></i> View Lot Page</a></p>';
        }else {
            $num = count($fammons);
            if ($images->images_checkImageServer() == True){
                if ($num == 0) {
                    $output= '<p><a href="'.$redirect.'">'
                            .'<i class="fas fa-vector-square"></i> View Lot Page</a></p>';
                }else if($num == 1) {
                    $output= '<p><a href="'.$redirect.'">'
                            .'<i class="fas fa-image"></i> View Family Memorial Photo on Lot Page</a></p>';
                }else {
                    $output= '<p><a href="'.$redirect.'">'
                        .'<i class="fas fa-image"></i> View Family Memorial Photos on Lot Page</a></p>';
                }
            }else {
                $output= '<p><i class="fas fa-image"></i> There';
                if ($num == 1){
                    $output .= ' is '.$num.' family memorial photo ';
                }else {
                    $output .= ' are '.$num.' family memorial photos ';
                }
                $output .= 'available for this lot.<br><i class="fas fa-exclamation-circle"></i> Please check back when our Picture Server is available.<br>'
                            .'<a href="'.$redirect.'"><i class="fas fa-vector-square"></i> View Lot Page</a></p>';
            }
        }
        return $output;
    }
    //Build visual output for interment photo
    public function interment_displayInterPhoto($interid){
        // Interment picture
        $options = get_option('cemetery-sexton');
        $images = New Cemetery_Sexton_images();
        $output = '';
        // Check that server is up.
        if ($images->images_checkImageServer() == True) {
            // Get and make images.
            $images->images_publishImages($options['photoserv_inter'].$interid.'.jpg',$interid,'interment');
            // Load Interment picture if it exists.
            if ($images->images_checkRemoteFile($options['photoserv_inter'].$interid.'.jpg')){
                
                include( plugin_dir_path( __FILE__ ) . 'partials/cemetery-sexton-photo-inter.php' );
                /*$output= '<div id="inter-photo">'
                    .'<a class="modal" href="index.php?option=com_cemeterysexton&view=intermodal&format=raw&img='.$interid.'&width=600px&height=500px'
                    .'" rel="{handler: '."'iframe'".', size: {x: 620, y: 520}}">'
                        .'<img src="'.plugin_dir_url( __FILE__ ).'/cempics/interment/t_'.$interid.'.jpg" alt="Interment Photo" width="240" height="200">'
                        .'</a></div>';*/
            }
        }else {
            $output= '<p><i class="fas fa-exclamation-circle"></i> Our Picture Server is down for maintenance. Please check back soon.</p>';
        }
        return $output;
    }
}