<?php
if (!defined('ABSPATH')) {
    exit('Direct\'s not allowed');
}
add_action('admin_menu', 'easyiit_cf7_admin_add_init');

function easyiit_cf7_admin_add_init() {
    add_menu_page('询盘添加', '询盘添加', 7, 'cf7_admin_add');
    add_submenu_page('cf7_admin_add', '询盘添加', '询盘添加', 7, 'cf7_admin_add', 'cf7_admin_add');
	
}
 
function cf7_admin_add() {
	global $wpdb;
	if(isset($_POST['dosubmit'])){
		$cf7_id = 19;
		$time = date('Y-m-d H:i:s');
		$_POST['data']['submit_time'] = $time;
		$posted_data = $_POST['data'];
		
		$wpdb->query($wpdb->prepare('INSERT INTO '.$wpdb->prefix.'cf7_data(`created`) VALUES (%s)', $time));
		$data_id = $wpdb->insert_id;
		foreach ($posted_data as $k => $v) {
			 $wpdb->query($wpdb->prepare('INSERT INTO '.$wpdb->prefix.'cf7_data_entry(`cf7_id`, `data_id`, `name`, `value`) VALUES (%d,%d,%s,%s)', $cf7_id, $data_id, $k, $v));		
		}
		$wpdb->query($wpdb->prepare('INSERT INTO '.$wpdb->prefix.'cf7_data_entry(`cf7_id`, `data_id`, `name`, `value`) VALUES (%d,%d,%s,%s)', $cf7_id, $data_id, "readed", 0));

		
		$ip=$posted_data['submit_ip'];
		$ip_area = file_get_contents("http://ip.globalso.com/?ip=".$ip);
		$siteurl = get_option('siteurl',true);
		file_get_contents($siteurl."/smsapi?country=".$ip_area);
		$wpcf7 = wpcf7_contact_form(19);
		$to = $wpcf7->mail['recipient'];	
		$subject = str_replace("[Name]",$posted_data['Name'],$wpcf7->mail['subject']);
		$message = str_replace("[Name]",$posted_data['Name'],$wpcf7->mail['body']);
		$message = str_replace("[Email]",$posted_data['Email'],$message);
		$message = str_replace("[Phone]",$posted_data['Phone'],$message);
		$message = str_replace("[Message]",str_replace("<br />","\n",$posted_data['Message']),$message);
		$message = str_replace("[_url]",$posted_data['refer'],$message);
		$message = str_replace("[_remote_ip] ",$ip,$message);
		$message = str_replace("[_remote_ip_area]",$ip_area,$message);
		ob_start();
		$result = wp_mail($to,$subject,$message);	
		$smtp_debug = ob_get_clean();		
		echo '<script language="javascript">alert("添加成功。")</script>';
	}
	?>
	<div class="wrap">
	<form id="posts-filter" method="post">
	<h2 class="title">询盘添加</h2>
	<table class="form-table permalink-structure">
		<tbody>	
		 
		<tr>
			<th>
				<label>姓名:</label>
			</th>
			<td>
			   <input type="text" name="data[Name]" value="" placeholder="* Your Name..."/>
			</td>
		</tr>

		<tr>
			<th>
				<label>邮箱:</label>
			</th>
			<td>
			   <input type="text" name="data[Email]" value="" placeholder="* Your Email..."/>
			</td>
		</tr>

		<tr>
			<th>
				<label>电话:</label>
			</th>
			<td><input type="text" name="data[Phone]" value="" placeholder="* Your Phone..."/></td>
		</tr>

		<tr>
			<th>
				<label>内容:</label>
			</th>
			<td>
			  <textarea name="data[Message]" placeholder="* Your Message" style="width:400px;height:100px"></textarea>
			</td>
		</tr>

		<tr>
			<th>
				<label>发送页面:</label>
			</th>
			<td>
			   <input type="text" name="data[refer]" value=""/>
			</td>
		</tr>

		<tr>
			<th>
				<label>IP:</label>
			</th>
			<td>
			   <input type="text" name="data[submit_ip]" value=""/>
			</td>
		</tr>
		
	</tbody>
	</table>

	<p class="submit"><input type="submit" name="dosubmit" id="submit" class="button button-primary" value="添加"></p>
	</form>
	</div>
	<div class="clear"></div>
	<?php
}