<script type="text/javascript">

    function ShowHideSyncSummary(id) 
	{
       var e = document.getElementById(id);
	   var f = document.getElementById('view_sync_summary');
       if(e.style.display == 'block')
	   {
		  f.value = 'View Synchronization Summary';
		  e.style.display = 'none';
	   }
	   else
	   {
		  f.value = 'Hide Synchronization Summary';
		  e.style.display = 'block';
       }
	}

</script>

<div class="wrap">
    
    <h1> Manual Syncing </h1>

    <?php
		if(isset($_POST['start_manual_syncing']) && $_POST['module_type'] == 'import_products' && !is_wp_error($product_id) ) {
    
			// Update last date when manually products import
			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 = 3 ");
			
			// include login access file 
		    include_once WOO_VTIGER_INCLUDES_DIR .'woo-vtiger-login-access.php';

		    // include file to import products 
			include_once WOO_VTIGER_INCLUDES_DIR .'woo-vtiger-import-products.php';
    ?>
        <div id="setting-error-settings_updated" class="updated settings-error notice is-dismissible">
            <p><strong>Synchronization Successful !!!</strong></p> 
            <p><strong><?php echo $count_created_products; ?> products has been added into WooCommerce. <br>
                       <?php echo $count_modified_products; ?> products has been modified into WooCommerce. <br>
                       <?php echo $count_deleted_products; ?> products has been deleted from WooCommerce. 
            </strong></p>
            <button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>
        </div>
    <?php } ?>



    <?php
		if(isset($_POST['start_manual_syncing']) && $_POST['module_type'] == 'export_products' ) {

			// Update last date when manually products export
			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 = 4 ");
    
			// include login access file 
            require_once WOO_VTIGER_INCLUDES_DIR .'woo-vtiger-login-access.php';

		    // include file to import products 
            require_once WOO_VTIGER_INCLUDES_DIR .'woo-vtiger-export-products.php';
    ?>
        <div id="setting-error-settings_updated" class="updated settings-error notice is-dismissible">
            <p><strong>Synchronization Successful !!!</strong></p> 
            <p><strong><?php echo $count_created_products; ?> products has been added into Vtiger crm. <br>
                       <?php echo $count_modified_products; ?> products has been modified into Vtiger crm. <br>
                       <?php echo $count_deleted_products; ?> products has been deleted from Vtiger crm. 
            </strong></p>
            <button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>
        </div>
    <?php } ?>


    <?php
		if(isset($_POST['start_manual_syncing']) && $_POST['module_type'] == 'import_contacts' ) {
			
			// Update last date when manually customers import
			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 = 5 ");
    
			// include login access file 
		    include_once WOO_VTIGER_INCLUDES_DIR .'woo-vtiger-login-access.php';

		    // include file to import customers 
			include_once WOO_VTIGER_INCLUDES_DIR .'woo-vtiger-import-contacts.php';
    ?>
        <div id="setting-error-settings_updated" class="updated settings-error notice is-dismissible">
            <p><strong>Synchronization Successful !!!</strong></p> 
            <p><strong><?php echo $count_created_contacts; ?> contacts has been added into WooCommerce. <br>
                       <?php echo $count_modified_contacts; ?> contacts has been modified into WooCommerce. <br>
                       <?php echo $count_deleted_contacts; ?> contacts has been deleted from WooCommerce. 
            </strong></p>
            <button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>
        </div>
    <?php } ?>

    <?php
        if(isset($_POST['start_manual_syncing']) && $_POST['module_type'] == 'export_contacts' ) {
			
			// Update last date when manually customers export
			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 = 6 ");
    
            // include login access file 
            include_once WOO_VTIGER_INCLUDES_DIR .'woo-vtiger-login-access.php';

            // include file to import customers 
            include_once WOO_VTIGER_INCLUDES_DIR .'woo-vtiger-export-contacts.php';
    ?>
        <div id="setting-error-settings_updated" class="updated settings-error notice is-dismissible">
            <p><strong>Synchronization Successful !!!</strong></p> 
            <p><strong><?php echo $count_created_contacts; ?> contacts has been added into Vtiger crm. <br>
                       <?php echo $count_modified_contacts; ?> contacts has been modified into Vtiger crm. <br>
                       <?php echo $count_deleted_contacts; ?> contacts has been deleted from Vtiger crm. 
            </strong></p>
            <button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>
        </div>
    <?php } ?>

    <?php
        if(isset($_POST['start_manual_syncing']) && $_POST['module_type'] == 'import_orders' ) {
			
			// Update last date when manually orders import
			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 = 7 ");
    
            // include login access file 
            include_once WOO_VTIGER_INCLUDES_DIR .'woo-vtiger-login-access.php';

            // include file to import orders 
            include_once WOO_VTIGER_INCLUDES_DIR .'woo-vtiger-import-orders.php';
    ?>
        <div id="setting-error-settings_updated" class="updated settings-error notice is-dismissible">
            <p><strong>Synchronization Successful !!!</strong></p> 
            <p><strong><?php echo $count_created_orders; ?> orders has been added into WooCommerce. <br>
                       <?php echo $count_modified_orders; ?> orders has been modified into WooCommerce. <br>
                       <?php echo $count_deleted_orders; ?> orders has been deleted from WooCommerce. 
            </strong></p>
            <button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>
        </div>
    <?php } ?>


    <?php
        if(isset($_POST['start_manual_syncing']) && $_POST['module_type'] == 'export_orders' ) {
			
			// Update last date when manually orders export
			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 = 8 ");
    
            // include login access file 
            include_once WOO_VTIGER_INCLUDES_DIR .'woo-vtiger-login-access.php';

            // include file to import orders 
            include_once WOO_VTIGER_INCLUDES_DIR .'woo-vtiger-export-orders.php';
    ?>
        <div id="setting-error-settings_updated" class="updated settings-error notice is-dismissible">
            <p><strong>Synchronization Successful !!!</strong></p> 
            <p><strong><?php echo $count_created_orders; ?> orders has been added into Vtiger crm. <br>
                       <?php echo $count_modified_orders; ?> orders has been modified into Vtiger crm. <br>
                       <?php echo $count_deleted_orders; ?> orders has been deleted from Vtiger crm. 
            </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="module_type">
	                    <th scope="row"><label for="module_type"><?php _e("Select Module" ); ?></label></th>
	                    <td>
	                    	<select name="module_type">
								<option value="">-- Select Option --</option>
								<option value="import_products">Import Products</option>
								<option value="export_products">Export Products</option>
								<option value="import_contacts">Import Customers</option>
								<option value="export_contacts">Export Customers</option>
                                <option value="import_orders">Import Orders</option>
                                <option value="export_orders">Export Orders</option>
							</select>
	                    	<p class="description" id="tagline-description">Choose an option that you want to synchronize.</p>
	                    </td>
	                </tr>

	            </tbody>
	    </table>

		<p class="submit">
		    <input type="submit" name="start_manual_syncing" id="start_manual_syncing" class="button button-primary" value="Start Syncing">
            <input type="button" name="view_sync_summary" id="view_sync_summary" onclick="ShowHideSyncSummary('sync_summary_table')" class="button button-primary" value="View Synchronization Summary" style="margin-left:30px">
		</p>
    </form>
	
    
    <div id='sync_summary_table' style="display:none;">
    <table border="1" style="border-collapse:collapse;">
        <tbody>
            <tr>
                <th style="padding:10px;">Synchronization Summary</th>
                <th style="padding:10px;">Last Sync Time</th>
            </tr>
            
            <?php 	
				$get_last_sync_time = get_last_sync_time();

				foreach($get_last_sync_time as $last_sync_time)
				{
					$id = $last_sync_time->id;
					$last_time = $last_sync_time->last_sync_time;
                    $description = $last_sync_time->description; 
	
                    if($last_time == '') {
						$display = 'Not Scheduled';
					} else {
                        $display = date("F j, Y, g:i A", strtotime($last_time));
                    }
					
					if($id != '1' && $id != '2') {
			?>
                    <tr>
                        <td style="padding:10px;"> <?php echo $description; ?></td>
                        <td style="padding:10px;"> <?php echo $display; ?></td>
                    </tr>
                    
            <?php   } 
                } 
            ?>
        </tbody>
    </table>
    </div>
    
</div>





