<?php
global $wpdb;
$tbname = "qg_orders";
$out2='';
$td_st = ' class="order2"';
$td_st2 = ' class="order3"';
$td_st20 = ' class="order4"';
$td_st3 = ' class="order5"';
$th_st = ' class="order6"';
$showid = 1;
$order_num='';
$link = 'options-general.php?page=quotegenerator&links=orders&rowid=';
$link2 = 'options-general.php?page=quotegenerator&links=orders&deleteid=';
$imageqg = sprintf("%s/quotegenerator/assets/img/no.png", plugins_url());
$imagfrm = sprintf("%s/quotegenerator/assets/img/print.png", plugins_url());
$imageml= sprintf("%s/quotegenerator/assets/img/email.png", plugins_url());
$emailto="";
$div_st='';
$div_st2='';
$clientn ='';

function set_html_content_type() {
	return 'text/html';
}


if(isset($_REQUEST["rowid"])){
    if(intval($_REQUEST["rowid"])>0){
	$div_st=' class="order7"';
	$div_st2=' class="order8"';
	$showid = $_REQUEST["rowid"];
	
	   $sql2 = "SELECT * FROM ".$wpdb->prefix.$tbname." WHERE id=".$showid;
           $moderation2 = $wpdb->get_results($sql2, ARRAY_A);
       
       foreach ( (array) $moderation2 as $row ) {

          $out2.='<tr><th colspan="2" class="order1">'.get_option("blogname").'</th></tr>';
	  $order_num = $row["field_7"];
	  
	  $out2.='<tr><td'.$td_st2.'>'.get_option("setting_qgname1").'</td><td'.$td_st3.'>'.$row["field_1"].'</td></tr>';
	  $clientn = $row["field_1"];
	  $out2.='<tr><td'.$td_st2.'>'.get_option("setting_qgname2").'</td><td'.$td_st3.'>'.$row["field_2"].'</td></tr>';
	  $out2.='<tr><td'.$td_st2.'>'.get_option("setting_qgname3").'</td><td'.$td_st3.'>'.$row["field_3"].'</td></tr>';
	  $emailto = $row["field_2"];
	  $out2.='<tr><td'.$td_st2.'>'.get_option("setting_qgname4").'</td><td'.$td_st3.'>'.$row["field_4"].'</td></tr>';
	  $out2.='<tr><td'.$td_st2.'>IP</td><td'.$td_st3.'>'.$row["ip"].'</td></tr>';
	  
	  $out2.='<tr><td'.$td_st2.'>Online Order #</td><td'.$td_st3.'>'.$row["field_7"].'</td></tr>';
	  $out2.='<tr><td'.$td_st2.'>Order placed</td><td'.$td_st3.'>'.$row["order_date"].'</td></tr>';
	  
	  if($row["field_5"]!=""){$out2.='<tr><td'.$td_st2.'>'.get_option("setting_qgname5").'</td><td'.$td_st3.'>'.$row["field_5"].'</td></tr>';}
	  if($row["field_6"]!=""){ $out2.='<tr><td'.$td_st2.'>'.get_option("setting_qgname6").'</td><td'.$td_st3.'>'.$row["field_6"].'</td></tr>';}
	  
	  if($row["field_8"]!=""){ $out2.='<tr><td'.$td_st2.'></td><td'.$td_st3.'>'.$row["field_8"].'</td></tr>';}
	  if($row["field_9"]!=""){ $out2.='<tr><td'.$td_st2.'></td><td'.$td_st3.'>'.$row["field_9"].'</td></tr>';}
	  if($row["field_10"]!=""){ $out2.='<tr><td'.$td_st2.'></td><td'.$td_st3.'>'.$row["field_10"].'</td></tr>';}
	  if($row["field_11"]!=""){ $out2.='<tr><td'.$td_st2.'></td><td'.$td_st3.'>'.$row["field_11"].'</td></tr>';} 
	  if($row["field_12"]!=""){$out2.='<tr><td'.$td_st2.'>Memo</td><td'.$td_st3.'>'.$row["field_12"].'</td></tr>';}
	  
	  $out2.='<tr><td colspan="2"><hr></td></tr>';
	  $out2.='<tr><td colspan="2"><table class="tabs2ord">'.$row["data_field"].'</table><hr></td></tr>';

	  
	  if($row["subtotal"]!=0){$out2.='<tr><td'.$td_st20.'>Subtotal</td><td'.$td_st3.'>'.$row["subtotal"].'</td></tr>';}
	  if($row["discount"]!=0){$out2.='<tr><td'.$td_st20.'>Discount</td><td'.$td_st3.'>'.$row["discount"].'</td></tr>';}
	  if($row["total_before_tax"]!=0){$out2.='<tr><td'.$td_st20.'>Total before Tax</td><td'.$td_st3.'>'.$row["total_before_tax"].'</td></tr>';}
	  $out2.='<tr><td'.$td_st20.'>Tax</td><td'.$td_st3.'>'.$row["tax_1"].'</td></tr>';
	  if($row["tax_2"]!=0){$out2.='<tr><td'.$td_st20.'>Tax</td><td'.$td_st3.'>'.$row["tax_2"].'</td></tr>';}
	  if($row["tax_3"]!=0){$out2.='<tr><td'.$td_st20.'>Tax</td><td'.$td_st3.'>'.$row["tax_3"].'</td></tr>';}
	  
	  $out2.='<tr><td'.$td_st20.'>Total</td><td'.$td_st3.'>'.$row["total"].'</td></tr>';
       }
    }
}else if(isset($_REQUEST["deleteid"])){ // delete order
    if(intval($_REQUEST["deleteid"])>0){
	$deletes = intval($_REQUEST["deleteid"]);
	$sql3 = "UPDATE ".$wpdb->prefix.$tbname." SET visible='no' WHERE id='$deletes'";
        $wpdb->query($sql3) or die (mysql_error());
    }
}


?>
<div id="contanerord"> 
 <div id="mainord" <?php echo $div_st2; ?>>
  <table class="tabs2ord"> 
   <tr><td colspan="9" class="td1ord">Pending Orders</td></tr>
   <tr><th<?php echo $th_st; ?>>#</th>
       <th<?php echo $th_st; ?>>Date</th>
       <th<?php echo $th_st; ?>><?php echo get_option("setting_qgname1"); ?></th>
       <th<?php echo $th_st; ?>><?php echo get_option("setting_qgname2"); ?></th>
       <th<?php echo $th_st; ?>><?php echo get_option("setting_qgname4"); ?></th>
       <th<?php echo $th_st; ?>><?php echo get_option("setting_qgname5"); ?></th>
       <th<?php echo $th_st; ?>>Total Amount</th>
       <th<?php echo $th_st; ?>></th>
   </tr> 
   <?php
       $sql = "SELECT * FROM ".$wpdb->prefix.$tbname." WHERE visible='yes' ORDER BY id DESC";
       $moderation = $wpdb->get_results($sql, ARRAY_A);
       $out='';
       foreach ( (array) $moderation as $row ) {
	  $out.='<tr>';
	  $out.='<td'.$td_st.'><a href="'.$link.$row["id"].'">'.$row["field_7"].'</a></td>';
	  $out.='<td'.$td_st.'><a href="'.$link.$row["id"].'">'.$row["order_date"].'</a></td>';
	  $out.='<td'.$td_st.'>'.$row["field_1"].'</td>';
	  $out.='<td'.$td_st.'>'.$row["field_2"].'</td>';
	  $out.='<td'.$td_st.'>'.$row["field_4"].'</td>';
	  $out.='<td'.$td_st.'>'.$row["field_5"].'</td>';
	  $out.='<td'.$td_st.'>'.$row["total"].'</td>';
	  $out.='<td'.$td_st.'><a href="'.$link2.$row["id"].'"><img src="'. $imageqg.'" width="20" height="20" alt="Delete"></a></td></tr>';
       }
       echo $out;
   ?>
  </table>
 </div>

 <div id="tabs3ord" <?php echo $div_st; ?>>
     <div class="elm1"> <input type="button" class="butord" onClick="printf();" value="Print Order">   </div>
     <div class="elm1"> <input type="button" class="butord" onClick="emailf();" value="Email to Customer">   </div>   
     <div class="elm1"> <input type="button" class="butord" onClick="invoice();" value="Convert to Invoice">   </div>  
     <div class="elm1"> <input type="button" class="butord" onClick="cloz(1);" value="Close">   </div>
 </div>

 <div id="shwordord" <?php echo $div_st; ?>>
   <input name="showid" type="hidden" value="<?php echo $showid; ?>" />
   <input name="ordnum" type="hidden" value="<?php echo $order_num; ?>" />
   <input name="clientn" type="hidden" value="<?php echo $clientn; ?>" />
   <input name="convert" id="convert" type="hidden" value="" />
     
    <div id="emailsord">
     To: <input name="mailto" type="text" value="<?php echo $emailto; ?>" />
     Subject: <input name="subject" type="text" value="Online Order" />
     Message:<br>
     <textarea name="memos0" id="memos0" rows="3"></textarea>
     <div class="order8"><textarea name="memos" id="memos"></textarea></div>
     <input name="sent" id="sent" type="hidden" value="" />
     <input type="button" class="butord" onClick="send_order(1);" value="Submit">
    </div> 
  
    <div class="dv1ord"><a href="javascript:cloz(1);"><img src="<?php echo $imageqg; ?>" width="30" height="30" alt="QuoteGeneratorPlus"></a></div>
    <div id="printable" contenteditable="true"><table class="tabs1ord"><?php echo $out2; ?></table></div>
   
   
    <div class="order9">
     <li class="elm1">You can edit this order.  </li>
     <li class="elm1">You can print the order confirmation for your records. </li>
     <li class="elm1">You can send the order to customer email address.</li>
     <li class="elm1">You can convert an estimate into an invoice.</li>
    </div> 
 </div>
 
</div>