<?php
// file: public/class-cemetery-sexton-lot.php
/**
 * The public-facing functionality of the plugin.
 *
 * @link       https://orbicular.media
 * @since      1.0.0
 *
 * @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_Lot {
    // Get lot data
    public function lot_getLot($LotID){
        $sql = 'SELECT *'
            . ' FROM dbo.Lot WHERE LotID ='. $LotID;
        //Connect to database
        $db = New Cemetery_Sexton_SQLconnect();
        $conn = $db->cemetery_sexton_connect();
        //Execute SQL statement
        $query = sqlsrv_query($conn,$sql);
        //return $query;
        $rsLot = sqlsrv_fetch_array($query,SQLSRV_FETCH_ASSOC);
        return $rsLot;
    }
    
    // Get the same lot interments from the database
    public function lot_getSameLot($LotID) {
        // Get the InterID for this result
        //$rsInter = $this->interment_getInterment($interid);
        // Get the LotID for this InterID
        //$LotID = $rsInter['LotID'];
        // Columns to get from database
        $columns = 'InterID, LastName, FirstName, MidName, Suffix, DateBirth, DateDeath';
        $table = 'dbo.Inter';
        $orderby = 'LastName, FirstName, MidName, Suffix';
        // Build new query
        $sql = 'SELECT DISTINCT '.$columns
        . ' FROM '.$table.' WHERE LotID ='.$LotID.' ORDER BY '.$orderby;
        //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
        $i=0;
        while($rsLot = sqlsrv_fetch_array($query,SQLSRV_FETCH_ASSOC)){
            $sameLot[$i]=$rsLot;
            $i++;
        }
        return $sameLot;
        //return $query;
    }
    // Get the records from the database
    public function lot_getFammon($LotID) {
        // Get the InterID for this result
        //$rsInter = $this->interment_getInterment($interid);
        // Get the LotID for this InterID
        //$LotID = $rsInter['LotID'];
        //Connect to database
        $sql = 'SELECT *'
            . ' FROM dbo.FamMon WHERE LotID ='. $LotID;
        //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
        $fammon=array();
        $i=0;
        while($rsFammon = sqlsrv_fetch_array($query,SQLSRV_FETCH_ASSOC)){
            $fammon[$i]=$rsFammon;
            $i++;
        }
        return $fammon;
    }
    // Build visual output for FamMon
    public function lot_displayFammon($LotID){
        $i = 0;
        $fammons = $this->lot_getFammon($LotID);
        
        //print_r($fammons);
        //echo $fammons[0]['LotID'];
        //die;
        
        $options = get_option('cemetery-sexton');
        $images = New Cemetery_Sexton_images();
        $output = '';
        if(empty($fammons)){
            $output = 'No family memorial photo available for this lot.<br>';
        }else {
            $num = count($fammons);
            if ($images->images_checkImageServer() == True){
                foreach ($fammons as $rsFammon){
                    $pic = $options['photoserv_fammon'].$rsFammon['PicFile'];
                    $name = substr($rsFammon['PicFile'],0,-4);
                    $image = $images->images_publishImages($options['photoserv_fammon'].$rsFammon['PicFile'],$name,'fammon');
                    
                    //give photo a number
                    $n = $i+1;
                    if($n == 1){
                        $no = '';
                    }else {
                        $no = $n;
                    }
                    include( plugin_dir_path( __FILE__ ) . 'partials/cemetery-sexton-photo-fammon.php' );
                    $i++;
                }
                
            }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.</p>';
            }
        }
        return $output;
    }
}
