 <?php

 Yii::app()->setLanguage(isset(Yii::app()->session['lang']) ? Yii::app()->session['lang'] : 'ar'); 
 $lang=Yii::app()->getLanguage();
 

?>
 
 
 <?php 
//we should make this as widget

$checkresult=PrayTime::model()->findByAttributes(array('date'=>date('Y-m-d')));
if($checkresult==NULL)
{
        $curl = curl_init();
        // Set some options - we are passing in a useragent too here
        curl_setopt_array($curl, array(
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_URL =>  'http://api.aladhan.com/v1/calendarByCity?city=Damascus&country=Syria&method=1&month='.date('m').'&year='.date('Y'),
            CURLOPT_USERAGENT => 'Codular Sample cURL Request',
        // CURLOPT_POST => 1,
        
        ));
        // Send the request & save response to $resp
        $result=  json_decode(curl_exec($curl));
        // Close request to clear up some resources
        curl_close($curl);
        $i=1;
        foreach($result->data as $date)
        {
        $prayer=new PrayTime;
            $prayer->date=date('y-m').'-'.sprintf("%02d", $i);
            $prayer->fajr=trim(substr($date->timings->Fajr,0,6)).":00";
            $prayer->dhuhr=trim(substr($date->timings->Dhuhr,0,6)).":00";
            $prayer->asr=trim(substr($date->timings->Asr,0,6)).":00";
            $prayer->maghrib=trim(substr($date->timings->Maghrib,0,6)).":00";
            $prayer->isha=trim(substr($date->timings->Isha,0,6)).":00";
            $prayer->higry_date=trim($date->date->hijri->date);
           try{
               $prayer->save();
               }
           catch(Exception $e)
            {
                var_dump($prayer->getErrors());
             }
            $i++;
        }

        $checkresult=PrayTime::model()->findByAttributes(array('date'=>date('Y-m-d')));

        

}


if(!empty($checkresult))
        {
?>

<?php if($lang=='ar'){ 
    ?>

    <div class="col-sm-6 col-xs-12 flex-item">
        <div class="pray-card">
        
            <div class="time-container">
                <div class="icon">
                    <img class="img-responsive pray-icon center-block" src="images/icontime1.png" alt="first icon">
                </div>
                <div class="pray-time">
                    <p class="time-name">
                    <?php //echo Yii::t('prayTime','fajr');?>
                    <?php echo($lang=='en'?'Dawn':'الفجر') ?></p>
                    <p class="pray-time-con" id="prayTime1"><?php echo substr($checkresult->fajr,0,5)?></p>
                </div>
            </div>
            <div class="time-container">
                <div class="icon">
                    <img class="img-responsive pray-icon center-block" src="images/icontime2.png" alt="first icon">
                </div>
                <div class="pray-time">
                    <p class="time-name">
                    <?php //echo Yii::t('prayTime','dhuhr');?>
                    <?php echo($lang=='en'?'Noon':'الظهر') ?></p>
                    <p class="pray-time-con" id="prayTime2"><?php echo substr($checkresult->dhuhr,0,5)?></p>
                </div>
            </div>
            <div class="time-container">
                <div class="icon">
                    <img class="img-responsive pray-icon center-block" src="images/icontime3.png" alt="first icon">
                </div>
                <div class="pray-time">
                    <p class="time-name">
                    <?php //echo Yii::t('prayTime','Asr');?>
                    <?php echo($lang=='en'?'Afternoon':'العصر') ?></p>
                    <p class="pray-time-con" id="prayTime3"><?php echo substr($checkresult->asr,0,5)?></p>
                </div>
            </div>
            <div class="time-container">
                <div class="icon">
                    <img class="img-responsive pray-icon center-block" src="images/icontime4.png" alt="first icon">
                </div>
                <div class="pray-time">

                    <p class="time-name">
                    <?php //echo Yii::t('prayTime','Maghrib');?>
                    <?php echo($lang=='en'?'Sunset':'المغرب') ?></p>
                    <p class="pray-time-con" id="prayTime4"><?php echo substr($checkresult->maghrib,0,5)?></p>
                </div>
            </div>
            <div class="time-container">
                <div class="icon">
                    <img class="img-responsive pray-icon center-block" src="images/icontime5.png" alt="first icon">
                </div>
                <div class="pray-time">
                    <p class="time-name">
                    <?php //echo Yii::t('prayTime','Isha');?>
                    <?php echo($lang=='en'?'Evening':'العشاء') ?></p>
                    <p class="pray-time-con" id="prayTime5"><?php echo substr($checkresult->isha,0,5)?></p>
                </div>
            </div>
            <div class="text-center head-text">
            <?php //echo Yii::t('prayTime','prayerTimings');?>
            <?php echo($lang=='en'?'Prayer Timings':'مواقيت الصلاة') ?>
               </div>
        </div>
    </div>

<?php }else{
    ?>
     <div class="col-sm-6 col-xs-12 flex-item">
        <div class="pray-card">



 <div class="time-container">
                <div class="icon">
                    <img class="img-responsive pray-icon center-block" src="images/icontime5.png" alt="first icon">
                </div>
                <div class="pray-time">
                    <p class="time-name">
                    <?php //echo Yii::t('prayTime','Isha');?>
                    <?php echo($lang=='en'?'Evening':'العشاء') ?></p>
                    <p class="pray-time-con" id="prayTime5"><?php echo substr($checkresult->isha,0,5)?></p>
                </div>
            </div>

            <div class="time-container">
                <div class="icon">
                    <img class="img-responsive pray-icon center-block" src="images/icontime4.png" alt="first icon">
                </div>
                <div class="pray-time">

                    <p class="time-name">
                    <?php //echo Yii::t('prayTime','Maghrib');?>
                    <?php echo($lang=='en'?'Sunset':'المغرب') ?></p>
                    <p class="pray-time-con" id="prayTime4"><?php echo substr($checkresult->maghrib,0,5)?></p>
                </div>
            </div>
         
        
            <div class="time-container">
                <div class="icon">
                    <img class="img-responsive pray-icon center-block" src="images/icontime3.png" alt="first icon">
                </div>
                <div class="pray-time">
                    <p class="time-name">
                    <?php //echo Yii::t('prayTime','Asr');?>
                    <?php echo($lang=='en'?'Afternoon':'العصر') ?></p>
                    <p class="pray-time-con" id="prayTime3"><?php echo substr($checkresult->asr,0,5)?></p>
                </div>
            </div>

    <div class="time-container">
                <div class="icon">
                    <img class="img-responsive pray-icon center-block" src="images/icontime2.png" alt="first icon">
                </div>
                <div class="pray-time">
                    <p class="time-name">
                    <?php //echo Yii::t('prayTime','dhuhr');?>
                    <?php echo($lang=='en'?'Noon':'الظهر') ?></p>
                    <p class="pray-time-con" id="prayTime2"><?php echo substr($checkresult->dhuhr,0,5)?></p>
                </div>
            </div>

               <div class="time-container">
                <div class="icon">
                    <img class="img-responsive pray-icon center-block" src="images/icontime1.png" alt="first icon">
                </div>
                <div class="pray-time">
                    <p class="time-name">
                    <?php //echo Yii::t('prayTime','fajr');?>
                    <?php echo($lang=='en'?'Dawn':'الفجر') ?></p>
                    <p class="pray-time-con" id="prayTime1"><?php echo substr($checkresult->fajr,0,5)?></p>
                </div>
            </div>
           
            <div class="text-center head-text">
            <?php //echo Yii::t('prayTime','prayerTimings');?>
            <?php echo($lang=='en'?'Prayer Timings':'مواقيت الصلاة') ?>
               </div>
        </div>
    </div>
<?php } ?>

    
<?php }?>