<script type="text/javascript">
	function timeRowShowHide(aval) 
	{
	    if (aval == "minutewise")
	    {
	    	document.getElementById('set_minute').style.display='table-row';
			document.getElementById('set_hours').style.display='none';
	    }   
		else if (aval == "hourwise")
		{
			document.getElementById('set_hours').style.display='table-row';
			document.getElementById('set_minute').style.display='none';
		}
		else
		{
			document.getElementById('set_hours').style.display='none';
			document.getElementById('set_minute').style.display='none';
		}
	   
    }
</script>


<div class="wrap">
    
    <h1> Auto Syncing <?php WOO_VTIGER_INCLUDES_DIR . 'woo-vtiger-login-access.php'; ?> </h1>

    <?php
		if(isset($_POST['start_auto_syncing']) && $_POST['set_time'] != "") {
			
			if($_POST['set_minute'] != "")
			{
				// start auto synching
				$timeInterval = $_POST['set_minute'];
				start_auto_syncing($timeInterval);
				$notification = "Auto syncing has been started and it will schedule after every ".$timeInterval;
				
				// Update last date when auto syncing start
				global $wpdb;
				$cur_date = current_time( 'mysql' );
				$wpdb->query("UPDATE ".$wpdb->prefix."WVS_last_sync_time_details SET set_time = '".$timeInterval."', last_sync_time = '".$cur_date."' WHERE id = 1 ");
			}
			
			if($_POST['set_hours'] != "")
			{
				// start auto synching
				$timeInterval = $_POST['set_hours'];
				start_auto_syncing($timeInterval);
				$notification = "Auto syncing has been started and it will schedule after every ".$timeInterval;
				
				// Update last date when auto syncing start
				global $wpdb;
				$cur_date = current_time( 'mysql' );
				$wpdb->query("UPDATE ".$wpdb->prefix."WVS_last_sync_time_details SET set_time = '".$timeInterval."', last_sync_time = '".$cur_date."' WHERE id = 1 ");
			}
			
			if($_POST['set_time'] == "twicedaily" OR $_POST['set_time'] == "daily")
			{
				// start auto synching
				$timeInterval = $_POST['set_time'];
				start_auto_syncing($timeInterval);
				$notification = "Auto syncing has been started and it will schedule after every ".$timeInterval;
				
				// Update last date when auto syncing start
				global $wpdb;
				$cur_date = current_time( 'mysql' );
				$wpdb->query("UPDATE ".$wpdb->prefix."WVS_last_sync_time_details SET set_time = '".$timeInterval."', last_sync_time = '".$cur_date."' WHERE id = 1 ");
			}
			
    ?>
        <div id="setting-error-settings_updated" class="updated settings-error notice is-dismissible">
            <p><strong> <?php echo $notification; ?></strong></p>
            <button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>
        </div>
        
    <?php } ?>
    
    <?php
		if(isset($_POST['stop_auto_syncing'])) {
			// unschedual cron
			stop_auto_syncing();
			
			// Update last date when auto syncing start
			global $wpdb;
			$cur_date = current_time( 'mysql' );
			$wpdb->query("UPDATE ".$wpdb->prefix."WVS_last_sync_time_details SET last_sync_time = '".$cur_date."' WHERE id = 2 ");
    ?>
        <div id="setting-error-settings_updated" class="updated settings-error notice is-dismissible">
            <p><strong> Auto syncing has been stopped. To restart, choose a time interval and click 'Start Syncing'.</strong></p>
            <button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>
        </div>
        
    <?php } ?>


    <form name="sync_form" method="post" action="">
		<table class="form-table">
	            <tbody>
	                <tr id="set_time">
	                    <th scope="row"><label for="set_time"><?php _e("Select Time Module " ); ?></label></th>
	                    <td>
	                    	<select name="set_time" onchange="java_script_:timeRowShowHide(this.options[this.selectedIndex].value)">
								<option value="">-- Select Option --</option>
								<option value="minutewise"> Minute wise </option>
								<option value="hourwise"> Hour wise </option>
								<option value="twicedaily"> Twice Daily </option>
								<option value="daily"> Daily </option>
							</select>
	                    	<p class="description" id="tagline-description">Select a time module how to auto syncing will perform.</p>
	                    </td>
	                </tr>

	                <tr id="set_minute" style="display:none;">
	                    <th scope="row"><label for="set_minute"><?php _e("Choose Minutes" ); ?></label></th>
	                    <td>
	                    	<select name="set_minute">
								<option value="">-- Choose Option --</option>
								<option value="1minutes"> 1 Minute </option>
								<option value="5minutes"> 5 Minute </option>
								<option value="10minutes"> 10 Minute </option>
								<option value="15minutes"> 15 Minute </option>
								<option value="30minutes"> 30 Minute </option>
							</select>
	                    	<p class="description" id="tagline-description">Set time interval when auto syncing will perform.</p>
	                    </td>
	                </tr>

	                <tr id="set_hours" style="display:none;">
	                    <th scope="row"><label for="set_hours"><?php _e("Choose Hours" ); ?></label></th>
	                    <td>
	                    	<select name="set_hours">
								<option value="">-- Choose Option --</option>
								<option value="hourly"> 1 Hour </option>
								<option value="2hours"> 2 Hours </option>
								<option value="3hours"> 3 Hours </option>
								<option value="6hours"> 6 Hours </option>
								<option value="9hours"> 9 Hours </option>
							</select>
	                    	<p class="description" id="tagline-description">Set time interval when auto syncing will perform.</p>
	                    </td>
	                </tr>

	            </tbody>
	    </table>
        
    
        
		<p class="submit">
		    <input type="submit" name="start_auto_syncing" id="start_auto_syncing" class="button button-primary" value="Start Syncing">
            <input type="submit" name="stop_auto_syncing" id="stop_auto_syncing" class="button button-primary" value="Stop Syncing" style="margin-left:30px">
		</p>

    </form>
    
    
	
	<?php
		// get auto syncing report
		$get_schedule = wp_get_schedule( 'MyCronAction' );
		
		if($get_schedule)
		{
			$get_schedule_time = get_schedule_report($get_schedule);	
	?> 
		<p> <b> Auto Syncing Status : </b> Running  </p>
		<p> <b> Syncing Time Set : </b> Every <?php echo $get_schedule_time; ?>.  </p>
	<?php
		} else {
	?>
		<p> <b> Auto Syncing Status : </b> Not running. </p>
	<?php } ?>

</div>





